/*
 * Orlando FlexTicket
 * 
 * File: orlando.js
 * Desc: JS controller
 * Requires: jquery-1.2.1.pack.js
 * 
 * Date			Author					Comments
 * 18.12.07		Dave Bullough (Headland)			Created
 * 25.02.09		Stevea (Headland) Added support for DVDs
**/


$(document).ready(
function () {
	jQuery.fn.exists = jQuery.fn.size;
	function DOMHelper() {
		/*
		 * Imports a JS file - appends to HEAD
		**/
		DOMHelper.prototype.importJS = function (path, cache) {
			var newScript = document.createElement("script");
			$(newScript).attr({ language: "javascript", type: "text/javascript", src: "/static/" + path });
			if (cache) {
				document.getElementsByTagName('head')[0].appendChild(newScript);
			} else {
				$("head", document).append(newScript);
			}
		}
		/*
		 * Loads a single object with external files to cache
		**/
		DOMHelper.prototype.importDomObj = function (pathObj) {
			if (pathObj.scripts) {
				for (var i = 0; i < pathObj.scripts.length; i++) {
					this.importJS(pathObj.scripts[i], pathObj.cache);
				}
			}
			if (pathObj.images) {
				for (i = 0; i < pathObj.images.length; i++) {
					$(this._imageCache).attr({ src: "/assets/images/" + pathObj.images[i] });
				}
			}
		}
		/*
		 * Returns the page's HCMS ID
		**/
		DOMHelper.prototype.getHCMSID = function () {
			if (!$("#hcms-pathfinder").exists()) return "";
			return $("#hcms-pathfinder").attr("content");
		}
		/*
		 * Create a random string
		**/
		DOMHelper.prototype.s4 = function () {
			return (((1+Math.random())*0x10000)|0).toString(16).substring(1);
		}
		/*
		 * Create a GUID
		**/
		DOMHelper.prototype.newGUID = function () {
			var g = "";
			var i = 0;
			while (i < 16) {
				g += this.s4();
				i++;
			}
			return g;
		}
	}
	//new dom helper object
	var dom = new DOMHelper();
	//import preloaded elements
	/*dom.importDomObj(
		{
			scripts: [
				"swfobject.js",
				"jquery.pngfix.js"
				],
			cache: true
		}
	);*/
	//fired on window load (all preloaded elements are loaded now so we can access the other scripts)
	$(window).load(function () {
		/*
		 * Generic objects to use throughout the site
		**/
		//the flash modules to load for this guid
		var flashObjects = {
			"BE32F463-4E62-4ABD-9E88-70A4C727342B":{swf:"videoModule.swf", width:"523", height:"240"} //use 218 for none-video nav	//homepage  //old orlando: 9EC7F29E-FA59-4D7A-9E7D-7035E3E0A8B3
		}
		//alert messages to be displayed through the site
		var alertMessages = {
			exit:{
				msg:"You are about to enter an online ticket store run by one of our associated American companies. \n Any personal information you provide and the transaction will be handled solely in accordance with relevant US laws and that company's terms and conditions, terms of use and privacy policy which you should read. \n\n Please click on 'OK' to continue or cancel to return to the Buy Tickets page. \n\n Please note you will need to allow pop ups to access this site.",
				url:"store" //http://commerce.4adventure.com/estore/scripts/skins/OFT/TicketsFlex.aspx
			}
		}
		//images to load for chosen postcards
		var postcardImages = {
			universal:{
				img1:"/images/postcards/U1.jpg",
				img2:"/images/postcards/U2.jpg",
				cardID:1
			},
			islands:{
				img1:"/images/postcards/I1.jpg",
				img2:"/images/postcards/I2.jpg",
				cardID:2
			},
			seaworld:{
				img1:"/images/postcards/S1.jpg",
				img2:"/images/postcards/S2.jpg",
				cardID:3
			},
			wet:{
				img1:"/images/postcards/W1.jpg",
				img2:"/images/postcards/W2.jpg",
				cardID:4
			},
			busch:{
				img1:"/images/postcards/B1.jpg",
				img2:"/images/postcards/B2.jpg",
				cardID:5
			},
			aquatica:{
				img1:"/images/postcards/A1.jpg",
				img2:"/images/postcards/A2.jpg",
				cardID:6
			}
		}
		/*
		 * Page specific functionality
		**/
		//fix the png transparency problems in ie6
		if ($.browser.msie && $.browser.version < 7) {
			$("#logo, img[@src$=.png]").pngfix();
		}
		//try to load the flash data fro this page. catch the error if we don't have a dataset for it in flashObjects
		//check if the flash loader div exists and write in the content
		if ($("#flash-content").exists()) {
			var flashOK = false;
			try {
				flashObjects[dom.getHCMSID()].swf;
				flashOK = true;
			} catch (e) {
				
			}
			//ok to continue
			if (flashOK) {
				try {
					var flashObj = new SWFObject("/flash/" + flashObjects[dom.getHCMSID()].swf, "flash", flashObjects[dom.getHCMSID()].width, flashObjects[dom.getHCMSID()].height, "8", "#ffffff");
					flashObj.addParam("scale", "tl");
				} catch (e) {
					//alert(e.name + ": " + e.message);
					return;
				}
				flashObj.addParam("menu", "false");
				flashObj.write("flash-content");
			}
		}
		//popup links - generic
		$("a.popup").each(
			function() {
				if (!$(this).attr("popurl")) $(this).attr( {"popurl":$(this).attr("href")} );
				$(this).attr( {"href":"#", "title":"Link will open in a popup window"} );
				$(this).bind(
					"click",
					function () {
						var win = window.open($(this).attr("popurl"), "popup", "width=400,height=300,toolbar=0,directories=0,menubar=0,status=1,resizable=1,location=0,scrollbars=1,copyhistory=0");
						win.window.focus();
						if ($.browser.msie) return void(0);
						return false;
					}
				);
			}
		);
		//set a unique cookie for exit tracking
		if ($.cookie("userID") == null) {
			$.cookie("userID", dom.newGUID(), { expires: 999, path: "/" });
		}
		//buy tickets button
		/*if ($("#buy-tickets-button").exists()) {
			$("#buy-tickets-button").bind(
				"click",
				function () {
					var blProceed = confirm(alertMessages.exit.msg);
					var siteURL = alertMessages.exit.url;
					if (blProceed) {
						pcBox = window.open("/exit/" + encodeURIComponent(siteURL),"pcBox","width=780,height=550,toolbar=1,directories=0,menubar=1,status=1,resizable=1,location=1,scrollbars=1,copyhistory=0");
						pcBox.window.focus();
					}
				}
			);
		}*/
		//setup click events for the postcard page
		if ($("#parks").exists()) {
			$("#parks area").each(
				function () {
					$(this).bind(
						"click",
						function() {
							var img1, img2, cardID, cardStr;
							cardStr = $(this).attr("title").toString().split(" ")[0].toLowerCase();
							$("#postcardimg1").attr( {"src":postcardImages[cardStr].img1} );
							$("#postcardimg2").attr( {"src":postcardImages[cardStr].img2} );
							$("#postcardid").attr( {"value":postcardImages[cardStr].cardID} );
							return false;
						}
					);
				}
			);
		}
	});
	
	$('.videolink').each(function() {
		$(this).click(function() {
			$('.videoimage').each(function() {
				var id = $(this).parent().attr('id');
				$(this).attr('src', '/images/btn_vid_' + id + '_off.jpg');
			});
			
			var id = $(this).attr('id');
			$(this).children('img').attr('src', '/images/btn_vid_' + id + '_on.jpg');
			fnShowVideo(id);
			return false;
		});
	});
	
	fnShowVideo('universal-oft');
	//$('#aquatica').children('img').attr('src', '/images/btn_vid_aquatica_on.jpg');
});

var params = {
	bgcolor: '#FFFFFF'
	//bgcolor: '#7BB0E9',
	//wmode: 'transparent'
};

/*function fnShowVideo(strMovie) {
	var flashvars = {
		videoFile: '/flash/videos/' + strMovie + '.flv'
	};			
	$.swfobject.embedSWF(
		'/flash/dvd-player.swf',
		'videoHolder', '320',
		'277',
		'9.0.0',
		'expressInstall.swf',
		flashvars,
		params
	);
}*/


// modified by Ewa Skibinska 29/06/2009
// the swf movies did not play with dvd-player_new.swf
function fnShowVideo(strMovie) {
	//alert( strMovie );
	var flashvars = {
		/*videoFile: '/flash/videos/' + strMovie + '.flv'*/
	};
	$.swfobject.embedSWF(
		'/flash/videos/' + strMovie + '.swf',
		'videoHolder', '320',
		'277',
		'9.0.0',
		'expressInstall.swf',
		flashvars,
		params
	);
}