// Cufon
if (typeof(Cufon) !== 'undefined')
{
	Cufon.replace('#header h1, h2.menu, #home-content p strong', {
		//textShadow: '2px 1px 0px #000000'
	});
	Cufon.replace('.post h1, .post h2, .post h3, .post h4, .post h5, .post h6 #sidebar h1, #sidebar h2, #sidebar h3, #sidebar h4, #sidebar h5, #sidebar h6', {
		//textShadow: '1px 1px 0px #ffffff'
	});
	Cufon.replace('h2.small', {
		//textShadow: '1px 1px 0px #ffffff'
	});

	$(function(){
		// rtl cufon hack
		if ($('html').attr('dir') === 'rtl') // todo: select by attr dir value rtl -> find cufon -> foreach results... 
		{
			var cache = {};
			$('cufon').each(function(){
				var self	= $(this),
					container = self.parent(),

					ltr_str	= container.text(),
					rtl_str	= ltr_str.split('').reverse().join(''),

					// http://www.i18nguy.com/temp/rtl.html
					// http://www.regularexpressions.info/unicode.html#block
					// match (non) Arabic, Hebrew
					regex = RegExp(/[^\u0600-\u06FF\u0590-\u05FF]+/g),
					no_rtl_str = ltr_str.match(regex),
					rv_ltr_str = rtl_str.match(regex);

				if (ltr_str in cache) return;

				cache[ltr_str] = rtl_str; // todo: change cache .. container context

				if ((no_rtl_str && rv_ltr_str) && (no_rtl_str.length == rv_ltr_str.length))
				{
					for (i = 0; i < no_rtl_str.length; i++)
					{
						rtl_str = rtl_str.replace(rv_ltr_str[i], '<bdo dir="ltr">' + no_rtl_str[i] + '</bdo>');
					}
				}

				if (ltr_str !== rtl_str)
				{

					container.html(rtl_str);

					Cufon.refresh();

					$('> cufon', container).each(function(){
						var canvas = $('canvas', this);
						canvas.css({
							left	: canvas.css('right'),
							right	: canvas.css('left')
						});
					});					
				}
			});
		}
	});
}


function showCables() {
	if($('#hydro-menu-content').css('display') != "none"){
		$('#hydro-menu').removeClass('hover');
    	$('#hydro-menu-content').fadeOut(0,
	    	function() {
		    	showCables();
	    	}
    	);
	}else if($('#electrical-menu-content').css('display') != "none"){
		$('#electrical-menu').removeClass('hover');
		$('#electrical-menu-content').fadeOut(0,
	    	function() {
		    	showCables();
	    	}
    	);
	}else{
	
		$('#cables-menu-content').css('position','absolute');
		
		var offset = $('#cables-menu').offset();
		$('#cables-menu-content').css('top',(offset.top + 70) +'px');
		$('#cables-menu-content').css('left',(offset.left) +'px');
	
		$('#cables-menu-content').fadeIn(100);
		$('#cables-menu').addClass('hover');
	}
}

function showHydro() {
	if($('#cables-menu-content').css('display') != "none"){
		$('#cables-menu').removeClass('hover');
    	$('#cables-menu-content').fadeOut(0,
	    	function() {
		    	showHydro();
	    	}
    	);
	}else if($('#electrical-menu-content').css('display') != "none"){
		$('#electrical-menu').removeClass('hover');
		$('#electrical-menu-content').fadeOut(0,
	    	function() {
		    	showHydro();
	    	}
    	);
	}else{
	
		$('#hydro-menu-content').css('position','absolute');
		
		var offset = $('#cables-menu').offset();
		$('#hydro-menu-content').css('top',(offset.top + 70) +'px');
		$('#hydro-menu-content').css('left',(offset.left) +'px');
	
		$('#hydro-menu-content').fadeIn(100);
		$('#hydro-menu').addClass('hover');
	}
}

function showElectrical() {
	if($('#hydro-menu-content').css('display') != "none"){
		$('#hydro-menu').removeClass('hover');
		$('#hydro-menu-content').fadeOut(0,
	    	function() {
		    	showElectrical();
	    	}
    	);
	}else if($('#cables-menu-content').css('display') != "none"){
		$('#cables-menu').removeClass('hover');
    	$('#cables-menu-content').fadeOut(0,
	    	function() {
		    	showElectrical();
	    	}
    	);
	}else{
		//$('#electrical-menu-content').fadeIn(100);
		$('#electrical-menu').addClass('hover');
	}
}


$(function($){


	$('.menu-content').hide()
	
	// CABLES
	
	$("#cables-menu").hover(
	  function () {
	  
    	showCables();
		
	    $.data(this, 'hover', true);
	  },
	  function () {
	  	$.data(this, 'hover', false);
	  }
	).data('hover', false);
	$("#cables-menu-content").hover(
	  function () {
	    $.data(this, 'hover', true);
	  },
	  function () {
	  	$.data(this, 'hover', false);
	  }
	).data('hover', false);
	
	$('#cables-menu-content').mouseleave(
		function(){
			var timeoutID = window.setTimeout(function (){
				if(!$("#cables-menu").data('hover')){
					$('#cables-menu').removeClass('hover');
			    	$('#cables-menu-content').fadeOut('fast');
			    }
			}, 10);
	    }
	);
	$('#cables-menu').mouseleave(
		function(){
			var timeoutID = window.setTimeout(function (){
				if(!$("#cables-menu-content").data('hover')){
					$('#cables-menu').removeClass('hover');
			    	$('#cables-menu-content').fadeOut('fast');
			    }
			}, 10);
	    }
	);
	
	// HYDRO
	
	$("#hydro-menu").hover(
	  function () {
	  
	  	showHydro();
		
	    $.data(this, 'hover', true);
	  },
	  function () {
	  	$.data(this, 'hover', false);
	  }
	).data('hover', false);
	$("#hydro-menu-content").hover(
	  function () {
	    $.data(this, 'hover', true);
	  },
	  function () {
	  	$.data(this, 'hover', false);
	  }
	).data('hover', false);
	
	$('#hydro-menu-content').mouseleave(
		function(){
			var timeoutID = window.setTimeout(function (){
				if(!$("#hydro-menu").data('hover')){
					$('#hydro-menu').removeClass('hover');
			    	$('#hydro-menu-content').fadeOut('fast');
			    }
			}, 10);
	    }
	);
	$('#hydro-menu').mouseleave(
		function(){
			var timeoutID = window.setTimeout(function (){
				if(!$("#hydro-menu-content").data('hover')){
					$('#hydro-menu').removeClass('hover');
			    	$('#hydro-menu-content').fadeOut('fast');
			    }
			}, 10);
	    }
	);
	
	// ELECTRICAL
	
	$("#electrical-menu").hover(
	  function () {
	  
	  	showElectrical();
		
	    $.data(this, 'hover', true);
	  },
	  function () {
	  	$.data(this, 'hover', false);
	  }
	).data('hover', false);
	$("#electrical-menu-content").hover(
	  function () {
	  	$.data(this, 'hover', true);
	  },
	  function () {
	  	$.data(this, 'hover', false);
	  }
	).data('hover', false);
	
	$('#electrical-menu-content').mouseleave(
		function(){
			var timeoutID = window.setTimeout(function (){
				if(!$("#electrical-menu").data('hover')){
					$('#electrical-menu').removeClass('hover');
			    	$('#electrical-menu-content').fadeOut('fast');
			    }
			}, 10);
	    }
	);
	$('#electrical-menu').mouseleave(
		function(){
			var timeoutID = window.setTimeout(function (){
				if(!$("#electrical-menu-content").data('hover')){
					$('#electrical-menu').removeClass('hover');
			    	$('#electrical-menu-content').fadeOut('fast');
			    }
			}, 10);
	    }
	);
	
	

	$('#banner-slideshow').coinslider({ 	
		width: 620, // width of slider panel
		height: 390, // height of slider panel
		effect: 'straight', // random, swirl, rain, straight
		navigation: true, // prev next and buttons
		links : false, // show images as links
		hoverPause: true // pause on hover
	 });


	// Scroll
	$.localScroll();

	// Link Nudge
	$('#sidebar #navigation li a').nudge();
});

jQuery.fn.nudge = function(params) {
	//set default parameters
	params = jQuery.extend({
		amount: 15,				//amount of pixels to pad / marginize
		duration: 300,			//amount of milliseconds to take
		property: 'padding', 	//the property to animate (could also use margin)
		direction: 'left',		//direction to animate (could also use right)
		toCallback: function() {},	//function to execute when MO animation completes
		fromCallback: function() {}	//function to execute when MOut animation completes
	}, params);
	//For every element meant to nudge...
	this.each(function() {
		//variables
		var jQueryt = jQuery(this);
		var jQueryp = params;
		var dir = jQueryp.direction;
		var prop = jQueryp.property + dir.substring(0,1).toUpperCase() + dir.substring(1,dir.length);
		var initialValue = jQueryt.css(prop);
		/* fx */
		var go = {};
		go[prop] = parseInt(jQueryp.amount) + parseInt(initialValue);
		var bk = {};
		bk[prop] = initialValue;
		
		//Proceed to nudge on hover
		jQueryt.hover(function() {
			jQueryt.stop().animate(go, jQueryp.duration, '', jQueryp.toCallback);
		}, function() {
			jQueryt.stop().animate(bk, jQueryp.duration, '', jQueryp.fromCallback);
		});
	});
	return this;
};
