/*************************
 *Javascript file containing functions firing effects on the website. Depends on jQuery 1.2.3, http://jquery.com/ 
 *
 *@author Adrian Silimon (adrian.silimon.eu)
 *@since 1.0
 *@requires jquery-1.4.3.min.js
 *@version 1.0
 *************************/

var ePointer 		  = null;
var lastAnimatedPdBox = "";

function hide_elements(identifier, speed) {
	$(identifier).hide(speed);
}

function show_elements(identifier, speed){
	$(identifier).show(speed);
}

function slide_toggle(identifier, speed){
	$(identifier).slideToggle(speed);
}

function hide_this_show_this(tohide, toshow, speed) {
	$(tohide).hide(speed);
	$(toshow).show(speed);
}

function init_submenu(isIndex){
	//----sub menu effects--------------//
	$('#Menu ul.submenu').css('display', 'none');
	
	var submenuOptions = {
			hoverClass: 'active',
			autoArrows: 'false',
			delay:      400, 
			dropShadows:false,
			animation:   {opacity:'show',height:'show'},
			speed:		300,
			onInit:		function(){
				$('ul.submenu').each(function(){
					var parent 	    = $(this).parent().get(0);
					
					var parentWidth  = $(parent).outerWidth(true);
					var parentHeight = $(parent).outerHeight();
					
					var parentOffs	= $(parent).offset();
					
					if (isIndex)
						var eTop = 60;
					else
						var eTop = parentOffs.top + parentHeight - 10;
					
					var eLeft	    = parentOffs.left - 24;
					
					$(this).css('top', eTop + 'px');
					$(this).css('left', eLeft + 'px');
					$(this).css('position', 'absolute');
				});
			}
	};
	
	$('#Menu ul').superfish(submenuOptions);
	//----sub menu effects--------------//
}

function show_color_guide(){
	$('#ColorGuide').show('fast');
	$('#hidecguide').html('&raquo;');
	
	$('#hidecguide').unbind();
	$('#hidecguide').bind('click', hide_color_guide);
	
	set_cookie('cguide_state', 'block', 365, false, false, false);
}

function hide_color_guide(){
	$('#ColorGuide').hide('fast');
	$('#hidecguide').html('&laquo;');
	
	$('#hidecguide').unbind();
	$('#hidecguide').bind('click', show_color_guide);
	
	set_cookie('cguide_state', 'none', 365, false, false, false);
}

function set_cookie( name, value, expires, path, domain, secure ){
	
	// set time, it's in milliseconds
	var today = new Date();
	today.setTime( today.getTime() );
	
	/*
	if the expires variable is set, make the correct
	expires time, the current script below will set
	it for x number of days, to make it for hours,
	delete * 24, for minutes, delete * 60 * 24
	*/
	if ( expires ){
		expires = expires * 1000 * 60 * 60 * 24;
	}
	
	var expires_date = new Date( today.getTime() + (expires) );
	
	document.cookie = name + "=" +escape( value ) +
	( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) +
	( ( path ) ? ";path=" + path : "" ) +
	( ( domain ) ? ";domain=" + domain : "" ) +
	( ( secure ) ? ";secure" : "" );
}

$(document).ready(function (){
	
	//-------- Main menu hover effect ---------------//
	$('#Menu a').mouseover(function () {
		if('active' != $(this).attr('if')){							 	
			var parent = $(this).parent().get(0);
			$(parent).children('img:first').attr('src', 'images/arrow-down-green.png');
		}
	});
	
	$('#Menu a').mouseout(function () {
		if('active' != $(this).attr('if')){
			var parent = $(this).parent().get(0);
			$(parent).children('img:first').attr('src', 'images/arrow-down-red.png');	
		}
	});
	//-------- Main menu hover effect ---------------//
	
	if(PageName == 'index'){
		
		//----- Main menu slide in animation----------//
		$('#Menu ul').css('position', 'absolute');
		$('#Menu ul').css('margin-left', '-1500px');
		$('#Menu ul').css('display', 'block');
		$('#Menu ul.submenu').css('display', 'none');
		$('body').css('overflow-x', 'hidden');
		
		$('#Menu ul').animate({
					marginLeft:[10],
					opacity:[1, 0]
				}, 
				3500,
				['easeOutSine'],
				function () {
					//restore defaults
					$('#Menu ul').css('position', 'relative');
					$('body').css('overflow-x', 'visible');
					
					//call to initialize sub menu
					init_submenu(true);
				}
		);
		//----- Main slide in animation----------//
		
		//----- Buttons sliding content ---------------//
		$('div.buttons div p a').click(function (){
			ePointer = $(this).attr('href');
			
			//do not animate the same element twice
			if(ePointer == lastAnimatedPdBox) return;
			
			//the div is already showing
			if($(ePointer + '_div').hasClass('showing')) return;
			
			if( '#how-it-works' != ePointer ){
				$('div#video_deswrapper').hide('normal');
			}
			
			//animate current showing div
			$('table#presentationSlider div.showing').css('position', 'absolute');
			$('table#presentationSlider div.showing').animate({
					marginLeft:[200],
					opacity:[0]
				}, 
				900,
				function () {
					//add class
					$(this).removeClass('showing');
					$(this).addClass('hidden');
			});
			
			lastAnimatedPdBox = ePointer;
			ePointer 		  = ePointer + '_div';
			
			//animate div to be showing
			$(ePointer).css('position', 'absolute');
			$(ePointer).css('margin-left', '-200px');
			$(ePointer).css('float', 'left');
			$(ePointer).css('width', '455px');
			$(ePointer).removeClass('hidden');
			$(ePointer).addClass('showing');
			$(ePointer).animate({
					marginLeft:[1],
					opacity:[1, 0]
				}, 
				900,
				function () {
					
					$('div#video_deswrapper div.flash_wrapper').hide();
					
					if( '#how-it-works_div' == ePointer ){
						$('div#video_deswrapper div.flash_wrapper:last').hide();
						$('div#video_deswrapper div.flash_wrapper:first').show();
						
						$('div#video_deswrapper').fadeIn('normal');
						$(this).css('width', '255px');
						
						HIDE_FLVS = true;
					}
					
					if( '#buy-the-book_div' == ePointer ){
						$('div#video_deswrapper div.flash_wrapper:first').hide();
						$('div#video_deswrapper div.flash_wrapper:last').show();
						
						$('div#video_deswrapper').fadeIn('normal');
						
						HIDE_FLVS = true;
					}
			});
		});
		//----- Buttons sliding content ---------------//
		
		//----- Before and after slideshow ------------//
		var slideshowSpeed = 4000; //miliseconds
		
		$('#cycleBefore').cycle({ 
		    fx:    'fade', 
		    speed:  slideshowSpeed 
		});

		$('#cycleAfter').cycle({ 
		    fx:    'fade', 
		    speed:  slideshowSpeed 
		});
		//----- Before and after slideshow ------------//
	}
	else{
		$('#Menu ul').show();
		
		//call to initialize sub menu
		init_submenu(false);
	}
	
	//-----------bind the appropriate function to color guide hide/show trigger----------//
	if ( color_guide_state == 'block' ){
		$('#hidecguide').bind('click', hide_color_guide);
	}
	else{
		$('#hidecguide').bind('click', show_color_guide);
	}
	//-----------bind the appropriate function to color guide hide/show trigger----------//
});

