$(document).ready(function () {

    $('#which-comparator').click(function() {
        VW.trackData.comparator = 'Which_Model_Link';
        VW.track(document.location.pathname);
    });
    
	function sortAlpha(a, b) {
              return a.innerHTML > b.innerHTML ? 1 : -1;
       }

       jQuery.fn.sort = function() {
              return this.pushStack([].sort.apply(this, arguments), []);
       };

 	$("#sortable_feat_1 span").sort(sortAlpha).appendTo("#sortable_feat_1");
	$("#sortable_feat_2 span").sort(sortAlpha).appendTo("#sortable_feat_2");
	$("#sortable_feat_3 span").sort(sortAlpha).appendTo("#sortable_feat_3");

	$(".configure a").click(function () {
		var strTrack = "";
		var popupMode = ""
		var href = this.href;
		
		var addEvents = function(){
		
			var getModel = location.href.substr(location.href.lastIndexOf('/')+1);
			//tracking

			$("div." + strTrack + " a.appStore").click(function() {
				
				VW.trackEvent(popupMode+"_itunes_configure_"+getModel,null,null,this);
			});
			
			$("div." + strTrack + " a.stay").click(function() {
				$("#overlay-dialog").fadeOut();
				$("#shade").fadeOut();
				$(window).unbind("resize", shadeResize);
				setCookie("flash", "no thanks");
				VW.trackEvent(popupMode+"_stayhere_configure_"+getModel,null,null,this);
			});
			
			$("div." + strTrack + " a.flash").click(function() {
				
				if(popupMode = "basic"){
					var strM = "gotofull";	
				}else{
					var strM = "download";
				}
				VW.trackEvent(popupMode+"_" + strM + "_configure_"+getModel,null,null,this);
				VW.setCookie("flash", "yes please");
				window.location = href; // redirect to flash
			});
	
		};		
		
		if (swfobject.hasFlashPlayerVersion(VW.flashVersion)) { // got correct flash version
			strTrack = "msgContent3";
			popupMode = "basic";
			VW.shadeDialog(
				'<div class="msgContent3">' +
				'<ul>' +
				'<li>' +
				'<a href="#" class="flash"></a>' +
				'</li>' +
				'<li>' +
				'<a href="#" class="stay"></a>' +
				'</li>'+
				'<li>' +
				'<a href="http://itunes.apple.com/gb/app/volkswagen-uk-car-configurator/id410271612?mt=8" class="appStore"></a>' +
				'</li>'+				
				'</ul>'+
				'</div>'			
			);
			$('div#overlay-dialog').css('background','url("/assets/common/img/overlayBox_new.png") no-repeat scroll 0 0 transparent');
			$('div#overlay-dialog').css('height','368px');
			
			addEvents();
		} else if (navigator.userAgent.match(/iPhone/i) || navigator.userAgent.match(/iPod/i) || navigator.userAgent.match(/iPad/i)){
			strTrack = "msgContent1";
			popupMode = "iphone";			
			VW.shadeDialog(
				'<div class="msgContent1">' +
				'<ul>' +
				'<li>' +
				'<a href="http://itunes.apple.com/gb/app/volkswagen-uk-car-configurator/id410271612?mt=8" class="appStore"></a>' +
				'</li>'+	
				'<li>' +
				'<a href="#" class="stay"></a>' +
				'</li>'+
				'</ul>'+
				'</div>'
			);	
			addEvents();			
		}else {
			strTrack = "msgContent2";
			popupMode = "noflash";			
			VW.shadeDialog(
				'<div class="msgContent2">' +
				'<ul>' +
				'<li>' +
				'<a href="http://www.adobe.com/go/getflashplayer" class="flash"></a>' +
				'</li>' +
				'<li>' +
				'<a href="#" class="stay"></a>' +
				'</li>'+
				'<li>' +
				'<a href="http://itunes.apple.com/gb/app/volkswagen-uk-car-configurator/id410271612?mt=8" class="appStore"></a>' +
				'</li>'+				
				'</ul>'+
				'</div>'
			);
			addEvents();

		}

		

		return false;
	});
/*---------- Popup above central car in "Which model carousel" ----------------*/
	function bigPopup() {
		$("div.which-model div#models li.left .popper,div.which-model div#models li.left img.popperImg, div.which-model div#models li.left .expandedPopup").hover(function(){
			$("div.which-model div#models li.left .expandedPopup").show();
			$("div.which-model div#models li.left a.popupSmall").hide();
			$('.which-model #models li.left .tabNew').css('top', $('.which-model #models li.left .expandedPopup').offset().top - 164);
		},function(){
			$("div.which-model div#models li.left .expandedPopup").hide();
			$("div.which-model div#models li.left a.popupSmall").show();
			$("div.which-model div#models li.left .tabNew").css("top", 0);
		});
		
		$("div.which-model div#models li.middle .popper,div.which-model div#models li.middle img.popperImg,div.which-model div#models li.middle .expandedPopup").hover(function(){
			$("div.which-model div#models li.middle .expandedPopup").show();
			$("div.which-model div#models li.middle a.popupSmall").hide();
			$('.which-model #models li.middle .tabNew').css('top', $('.which-model #models li.middle .expandedPopup').offset().top - 164);
		},function(){
			$("div.which-model div#models li.middle .expandedPopup").hide();
			$("div.which-model div#models li.middle a.popupSmall").show();
			$("div.which-model div#models li.middle .tabNew").css("top", 0);
		});
		
		$("div.which-model div#models li.right .popper,div.which-model div#models li.right img.popperImg,div.which-model div#models li.right .expandedPopup").hover(function(){
			$("div.which-model div#models li.right .expandedPopup").show();
			$("div.which-model div#models li.right a.popupSmall").hide();
			$('.which-model #models li.right .tabNew').css('top', $('.which-model #models li.right .expandedPopup').offset().top - 164);
		},function(){
			$("div.which-model div#models li.right .expandedPopup").hide();
			$("div.which-model div#models li.right a.popupSmall").show();
			$("div.which-model div#models li.right .tabNew").css("top", 0);
		});
	}

	if ($(".popper").length) {bigPopup();}
/*---------- end of Popup above central car in "Which model carousel" ----------------*/
/* display drive call to action */
		function drive(){
			$('#which-drive').addClass('call');
			$('div.actionDrive').slideDown(200, 'swing');
			setTimeout(unDrive, 8000);
			
			$.cookie(modelName, 1 + parseInt($.cookie(modelName)), {'path':'/'});
		}
		
		function unDrive(){
			$('div.actionDrive').slideUp(200, 'swing');
			$('#which-drive').removeClass('call');
		}

	if($('#which-drive').length == 1){
		
		var hr = document.location.href;
		var patt=/new\/([a-z\-]+)\//;
		var result=patt.exec(hr);
		var modelName = result[1];

		if(hr.indexOf('?p=') != -1){
			var actionCountdown = 5000;
		}else{
			var actionCountdown = 10000;	
		}
		
		if($.cookie(modelName) == null){
			$.cookie(modelName, 0, {'path':'/'});
		}
		if(parseInt($.cookie(modelName)) < 2){
			setTimeout(drive, actionCountdown);
		}
	}
	
	var $whichTable = $('#which-model-data');
	if( $whichTable.length == 1 ){
		//find any occurance of bluemotion technology and wrap in span with bmColor
		$('tr th', $whichTable).each(function(){
			var sth = this.innerHTML;
			if ( sth.indexOf('BlueMotion Technology') != -1 ){
				this.innerHTML = sth.replace('BlueMotion Technology', '<span class="bmColor">BlueMotion Technology</span>**<br/>');
			}
			
		});
	}
	
	var $whichEngineTable = $('#which-engine');
	if( $whichEngineTable.length == 1 ){
		//find any occurance of bluemotion technology and wrap in span with bmColor
		$('tr:first td', $whichEngineTable).each(function(){
			var sth = this.innerHTML;
			if ( sth.indexOf('BlueMotion Technology') != -1 ){
				this.innerHTML = sth.replace('BlueMotion Technology', '<span class="bmColor">BlueMotion Technology</span>**<br/>');
			}
			
		});
	}	
	
});

