/* Copyright (c) 2009 Brandon Aaron (http://brandonaaron.net). Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php) and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses. Version: 3.0.2.  Requires: 1.2.2+ */
(function(c){var a=["DOMMouseScroll","mousewheel"];c.event.special.mousewheel={setup:function(){if(this.addEventListener){for(var d=a.length;d;){this.addEventListener(a[--d],b,false)}}else{this.onmousewheel=b}},teardown:function(){if(this.removeEventListener){for(var d=a.length;d;){this.removeEventListener(a[--d],b,false)}}else{this.onmousewheel=null}}};c.fn.extend({mousewheel:function(d){return d?this.bind("mousewheel",d):this.trigger("mousewheel")},unmousewheel:function(d){return this.unbind("mousewheel",d)}});function b(f){var d=[].slice.call(arguments,1),g=0,e=true;f=c.event.fix(f||window.event);f.type="mousewheel";if(f.wheelDelta){g=f.wheelDelta/120}if(f.detail){g=-f.detail/3}d.unshift(f,g);return c.event.handle.apply(this,d)}})(jQuery);		

function setUps(){

	//$('#mcs_container .scrollUpBtn').focus();
	
	/*$('div#mcs_container .scrollDownBtn').one('click',function(){
	
							var btnsScrollTo=$dragger_container.height()-$dragger.height();
							//alert(btnsScrollTo);
							var scrollSpeed=Math.abs($dragger.position().top-btnsScrollTo)*(100/scrollBtnsSpeed);
							$dragger.stop().animate({top: btnsScrollTo}, scrollSpeed,"linear");	
		
	});*/
	
	if (!localStorage.getItem("playMovie")){//not true = does not exist/have a value
		localStorage.setItem("playMovie","true");
	}
		
	if (localStorage.getItem("playMovie") == "true"){//play film
		$('body.coming #left header, #left > section').hide();//these stop ugly flash of main content onload before movie plays
		$('body.coming > section').addClass('firstLoad');
	}
		
	if( navigator.userAgent.match( /(iPhone|iPod|iPad)/i ) ) {
		$('body').addClass('iPad');	

		$('<img />').attr('src',ipad_bg_path);/*basic prefetch*/
		$('<img />').attr('src',bg_open_shadow_top_path);
		$('<img />').attr('src',bg_open_shadow_bottom_path);
		$('<img />').attr('src',bg_open_shadow_shim_path);
		$('<img />').attr('src',main_form_bg); 
	}	
	
	//$('body.coming form input[type=text],form input[type=password],form input[type=email],form select').wrap('<span/>');
	//$('section.hide form input[type=email], section.hide form input[type=text], section.hide form input[type=password], section.hide form select').wrap('<span/>');
	
	$('#country').find('li a').click(function(){// Market drop down links
		recordOutboundLink($(this).attr('href'), 'Outbound Links', $(this).attr('href'), true); 
		return false;
	});
	
	$('body > footer menu ul li ul').find('li a').click(function(){// Facebook links
		recordOutboundLink($(this).attr('href'), 'Outbound Links', $(this).attr('href'), true); 
		return false;
	});
	
	$('body > header menu ul').find('li a').click(function(){// Language links
		recordOutboundLink($(this).attr('href'), 'Language Links', $(this).attr('href'), false); 
		return false;
	});
	
	$('body > footer section nav ul').find('li a').click(function(){// Footer links
		recordOutboundLink($(this).attr('href'), 'Outbound Links', $(this).attr('href'), true); 
		return false;
	});
	
	$('#cogentFooterLink').click(function(){// Cogent Elliott link
		recordOutboundLink($(this).attr('href'), 'Outbound Links', $(this).attr('href'), true); 
		return false;
	});
	
	$('#specs').find('ul li a').click(function(){// Test Ride links
		recordOutboundLink($(this).attr('href'), 'Outbound Links', $(this).attr('href'), true); 
		return false;
	});
	
	$('#currentLang').click(function(){
		
		var theHeadMenu = $('body > header menu');
		
		if (theHeadMenu.is(':visible')){
			theHeadMenu.fadeOut('fast');
		}else{
			theHeadMenu.fadeIn('fast');
			theHeadMenu.find('li').click(function(){
				theHeadMenu.fadeOut('fast');
			});

			$('body > footer, body > section').one('click',function(){
				theHeadMenu.fadeOut('fast');
			});			
		}
	});
	
	$('#facebook').click(function(){
		
		var theFooter = $('body > footer');
		
		if ($('#facebook').find('ul').is(':visible')){
     		theFooter.css('zIndex','20');
			$('#facebook').find('ul').fadeOut('fast');
		}else{
			$('#facebook').find('ul').fadeIn('fast');
			theFooter.css('zIndex','60');
			$('#specs').find('ul').fadeOut('fast');
			$('#facebook').find('li').click(function(){
				theFooter.css('zIndex','20');	
				$('#facebook').find('ul').fadeOut('fast');
			});

			$('body > header, body > section').one('click',function(){
				theFooter.css('zIndex','20');	
				$('#facebook').find('ul').fadeOut('fast');
			});			
		}
	});	
	
	$('#specs').find('p').click(function(){
		
		var theFooter = $('body > footer');
		
		if ($('#specs').find('ul').is(':visible')){
     		theFooter.css('zIndex','20');
			$('#specs').find('ul').fadeOut('fast');
		}else{
			$('#specs').find('ul').fadeIn('fast');
			theFooter.css('zIndex','60');
			$('#facebook').find('ul').fadeOut('fast');
			$('#specs').find('ul li').click(function(){
				theFooter.css('zIndex','20');	
				$('#specs').find('ul').fadeOut('fast');
			});

			$('body > header, body > section').one('click',function(){
				theFooter.css('zIndex','20');	
				$('#specs').find('ul').fadeOut('fast');
			});			
		}
	});		

	$('body.coming form#updatesForm input#email').one("focus", function(){
	
		$('#left > section').addClass('open').removeClass('closed');
		$('body > footer').css('zIndex','0');
		$('#updatesForm label, #updatesForm input, #updatesForm select, #updatesForm p, #updatesForm button').fadeIn('fast');//can't use selector * due to IE8
		//applyFormVal();
	});
	
	//if (localStorage.getItem("playMovie") == "true"){
		$('#skip').click(function(){//is a bit different to the code in coming-soon.html script
		
			//$('body > section').removeClass();
			$(this).fadeOut('fast');
			$('body').removeClass('movie');
			$('#mcs_container #wrapper').fadeIn('slow');
			 _gat._getTrackerByName()._trackEvent('Video', 'Skipped');
			$('#mediaplayer_wrapper').fadeOut('fast', function(){
				jwplayer("mediaplayer").remove(); 
				$('#mediaplayer_wrapper').remove();
				
				//if($('body').hasClass('coming')){
					//comingSoonAnimations.init();
				//}
			});
			localStorage.setItem("playMovie", "false");
		});

		/*$('body > footer p#muteBtn').click(function(){
			$(this).toggleClass("mute");
			jwplayer().setMute();
		});
		
	}else{
		$('body > footer p#muteBtn').fadeOut('fast').remove();*/	
	//}

	$('#signUp').click(function() {
		$('#country').fadeOut('fast');
		$('#left').removeClass('makeZIndex0');
	  	$('body > section header nav ul, #left > section.hide, #updatesForm label, #updatesForm input, #updatesForm select, #updatesForm p, #updatesForm button').fadeIn();
	});
	
	/*$('form#updatesForm label,form#updatesForm input,form#updatesForm select,form#updatesForm p,form#updatesForm button').fadeIn('fast');*/

	$('<span />').attr({'id':'closeBtn', 'title':'Close'}).text('Close').click(function(){
	   $('body > div.error, article#left > section.hide, form#updatesForm label,form#updatesForm input,form#updatesForm select,form#updatesForm p,form#updatesForm button').fadeOut('fast');
	}).prependTo('article#left > section.hide form#updatesForm');
	
	$('#countries').click(function(){
		
		var bodySection = $('body > section');
		
		$('#left').toggleClass('makeZIndex0');
		$('#left > section.hide').fadeOut('fast');
    	$('#country').add('body > section header nav ul').fadeToggle('fast', 'linear');
		bodySection.one('click',function(){
        	$('#country').fadeOut('fast');
			bodySection.find('header nav ul').fadeIn('fast');
			$('#left').removeClass('makeZIndex0');
        });
		
    });	

}

var comingSoonAnimations = {
	init:function(){
		$('#explorer1').ceAnnotation({ 
			posX : 475, 
			posY : 305, 
			distance : 300, 
			direction : 2, 
			startDelay: 250,
			lineEffectSpeed: 600,
			onComplete : function(){
				$('#explorer2').ceAnnotation({ 
					posX : 745, 
					posY : 355, 
					distance : 260, 
					direction : 2, 
					startDelay: 250,
					lineEffectSpeed: 600,
					onComplete : function(){
						applyFormVal();
						$('div.comingSoonPopup img').animate({ width: '355px' , height : '255px' },500);
						$('div.comingSoonPopup').delay(1400).animate({ top : '20px' , left : '530px' , width : '375px' , height : '275px' },500,function(){
							$('#left > section, #left header').fadeIn(1000);
						});
					}
				});
			}
		});
	}
}

function alignBottomButtons(){
	var wrapperWidth = $('.bottomButtonsWrapper').width();
	var leftMargin   = (990 - wrapperWidth) / 2;
	$('.bottomButtonsWrapper').css({ left : leftMargin + 'px' });
}

/////////////////////////////////////////////
//
// 		HELPER FUNCTIONS FOR FLYOUTS	
//
/////////////////////////////////////////////

	// ADD CLICK EVENT LISTENERS FOR EACH FLYOUT
	function addFlyoutCloseListeners(flyouts){
		$(flyouts).each(function(i){
			$(flyouts[i] + ' span.close').live('click',function(){
				$(flyouts[i]).ceFlyoutClose({ direction : directions[i] });
				$(this).hide();
			});
		});
	}
	
	// HANDLES CLOSING OF EACH FLYOUT, IGNORING EXCEPTION ID
	function closeFlyouts(flyoutIds,closeDirections,exception){
		for(var i=0 ; i < flyoutIds.length ; i++){
			if(flyoutIds[i] != exception){
				$(flyoutIds[i]).ceFlyoutClose({ direction : closeDirections[i] });
			}
		}
	}
	
	// SAVE FLYOUTS TO AN ARRAY AND REMOVE FROM DOM
	function saveAndRemoveFlyoutMarkup(flyoutIds){
		for(var i=0 ; i < flyoutIds.length ; i++){
			flyoutHTML[i] = $(flyoutIds[i]).html();
			$(flyoutIds[i]).remove();
		}
	}

