$(document).ready(function() {
	
	// Open the promo pop-up if the hash tag is present
	
	if (document.location.hash == '#MARS-en') {
		DoPop('marsPopup','en');
	}
	else if (document.location.hash == '#MARS-fr') {
		DoPop('marsPopup','fr');
	}

// TBWA VIDEOS STUFF STARTS
	// pops up the TBWA video plinth onload - home & bunny pages only
	/*if ($('#tbwaPopup').length != 0) {	
		initTBWApop();
	}*/	
	// swaps out info panel & adds highlight on rollover
	$("#tbwaPopup a").live('mouseover', function() {
		var thisID = $(this).attr('id');
		//alert("this ID was " + thisID);
		$('a span').each(function(index, a) {
            $(a).removeClass('view');
    	});	
		$("#" + thisID + " span").addClass('view');
		$("#vInfo").html("<img src='./promos/tbwa-video/info" + thisID + ".jpg' alt='info' />");
	});
	// shows solo video display on clicking movie in plinth
	$("#tbwaPopup a").live('click', function() {
		var thisID = $(this).attr('id');
		var thisLang = $("#tbwaPopup").attr('name');
		
		$.modal.close();
		
		if (thisID == 'Kenny-en') {
			$("#tbwaPlay-Kenny-en").modal({onClose: function () { $.modal.close(); DoPop('TBWA', thisLang); }});
			$("#tbwaPlay-Kenny-en .youtube").load("./promos/tbwa-video/movie_" + thisID + ".html");		
		}
		else if (thisID == 'Ricky-en') {
			$("#tbwaPlay-Ricky-en").modal({onClose: function () { $.modal.close(); DoPop('TBWA', thisLang); }});
			$("#tbwaPlay-Ricky-en .youtube").load("./promos/tbwa-video/movie_" + thisID + ".html");		
		}
		else if (thisID == 'Flick-en') {
			$("#tbwaPlay-Flick-en").modal({onClose: function () { $.modal.close(); DoPop('TBWA', thisLang); }});
			$("#tbwaPlay-Flick-en .youtube").load("./promos/tbwa-video/movie_" + thisID + ".html");		
		}
		else if (thisID == 'Click-en') {
			$("#tbwaPlay-Click-en").modal({onClose: function () { $.modal.close(); DoPop('TBWA', thisLang); }});
			$("#tbwaPlay-Click-en .youtube").load("./promos/tbwa-video/movie_" + thisID + ".html");	
		}
		else if (thisID == 'Flick-fr') {
			$(".movieSolo").addClass('frVid');
			$("#tbwaPlay-Flick-fr").modal({onClose: function () { $.modal.close(); DoPop('TBWA', thisLang); }});
			$("#tbwaPlay-Flick-fr .youtube").load("./promos/tbwa-video/movie_" + thisID + ".html");		
		}
		else if (thisID == 'Click-fr') {
			$(".movieSolo").addClass('frVid');
			$("#tbwaPlay-Click-fr").modal({onClose: function () { $.modal.close(); DoPop('TBWA', thisLang); }});
			$("#tbwaPlay-Click-fr .youtube").load("./promos/tbwa-video/movie_" + thisID + ".html");	
		}
		
		$(".vInfoPlay").html("<img src='./promos/tbwa-video/info" + thisID + ".jpg' alt='info' />");	
			
		countViews(thisID);
		
	});	
// TBWA VIDEOS STUFF ENDS	
		
});

function DoPop(popupName, lang) {
	
	var thisPop = popupName;
	var thisLang = lang;
	//alert("Popup trigger was received. The selected language was " + thisLang + " popup requested was " + thisPop);	
		
	 if (thisPop == 'marsPopup') {
	
			$('#marsPopup').modal();
			$('#marsPopup').html("<img src='./promos/mars/Mars-" + thisLang + ".jpg' />");
	
		}
		
		if (thisPop == 'TBWA') {
			
			$('#tbwaPopup').modal();
			$('#tbwaPopup').load('promos/tbwa-video/tbwaPlinth-' + thisLang + '.html');
			
		}
		
		else if (thisPop == 'TBWAsolo') {
			var thisID = $("#tbwaVideo").attr("title") + "-" + thisLang;
			$('#tbwaVideo').modal();
			
				if(thisLang == 'fr'){
					$("#movieSolo").addClass('frVid');
				}
				
			if (thisID == 'Kenny-en') {
				$(".fbBtn").append('<fb:like href="http://www.youtube.com/energizerca#p/c/3/obJM-P6HH7c" show_faces="false" width="450" font="arial"></fb:like>');
			}
			else if (thisID == 'Ricky-en') {
				$(".fbBtn").append('<fb:like href="http://www.youtube.com/watch?v=37LNzH6NrWA" show_faces="false" width="450" font="arial"></fb:like>');
			}
			else if (thisID == 'Flick-en') {
				$(".fbBtn").append('<fb:like href="http://www.youtube.com/watch?v=flodMj-DyhI" show_faces="false" width="450" font="arial"></fb:like>');
			}
			else if (thisID == 'Click-en') {
				$(".fbBtn").append('<fb:like href="http://www.youtube.com/watch?v=vNl6mNvZr5s" show_faces="false" width="450" font="arial"></fb:like>');
			}
			else if (thisID == 'Flick-fr') {
				$(".fbBtn").append('<fb:like href="http://www.youtube.com/watch?v=CcIfbpf2JRg" show_faces="false" width="450" font="arial"></fb:like>');
			}
			else if (thisID == 'Click-fr') {
				$(".fbBtn").append('<fb:like href="http://www.youtube.com/watch?v=NtGEq8doDeg" show_faces="false" width="450" font="arial"></fb:like>');
			}
			
			$("#movieSolo .youtube").load("./promos/tbwa-video/movie_" + thisID + ".html");
			$("#vInfo").html("<img src='./promos/tbwa-video/info" + thisID + ".jpg' alt='info' />");
			
			countViews(thisID);
		}		
		
}

// TWBA SPECIFIC FUNCTIONS START
// initialises TBWA video plinth in correct language on page load
function initTBWApop(){
	var thisLang = $("#tbwaPopup").attr('name');
	DoPop('TBWA', thisLang);
}
// counts TBWA video views
function countViews(movieID){
	dataString =  "videoID="+movieID;
		$.ajax({
		type: "POST", 
		url: "tbwa-views.php",
		data: dataString,
		dataType: "json"
 });					
}
