//
//	Defaults & Tracking
//
var	browseByMode = '';
var trailersHTML = '';
var timeoutOut 	 = '';
var timeoutShow  = '';
var	trailerURL	 = '';
var	slickURL	 = '';
var mediaURL	 = '';

//
//	jQuery
//
$(document).ready(function(){
		
	
	trailerURL	= $("body").attr('trailerurl');
	slickURL	= $("body").attr('slickurl');
	mediaURL	= $("body").attr('mediaurl');
	
	if($("#log_out").html()) {
		$("#log_out").fadeTo("fast", 0.7);
	}
	
	//
	//	Alerts
	//
	$("#alert_header, #close_alert").click(function() {
		$("#alerts").hide();
	});
	
	//
	//	Field tips
	//
	$(".form_field").each(function(i) {
		var height = $(this).siblings('.field_tip').height();
		//$(this).siblings('.field_tip').css({ marginTop: '-'+(height+0)+'px'}).fadeTo("fast",0.5);
		$(this).siblings('.field_tip').css({ marginTop: '-'+(height+0)+'px'});
	});
	
	// Png fade is bugged in IE8
	//$(".form_field").focus(function(i) {
	//	$(this).siblings('.field_tip').fadeTo("fast", 1.0);
	//}).blur(function(i) {
	//	$(this).siblings('.field_tip').fadeTo("fast",0.5);
	//});
	
	//
	//	Defaults
	//
	trailersHTML = $("#trailers").html();
	$("#trailers").html(""); // Clear so we dont double up on IDs with blockUI
	
	//
	//	Location selector
	//
	$("#select_list").click(function() {
		$("#select_list_popup").show();
	});
	
	
	$("#select_list_label").click(function() {
		$("#select_list_popup").hide();
	});
	
	//
	//	Buttons
	//
	$(".button_table img").live('mouseover',function(){
		$(this).attr({ out: $(this).attr('src'), src: $(this).attr('ova') });
	}).live('mouseout',function(){
		$(this).attr({ src: $(this).attr('out') });
	});
	
	//
	//	Ads
	//
	if($("#leasespace_one").attr("img")) {
		$("#leasespace_one").html('<a href="'+$("#leasespace_one").attr("click")+'" target="'+$("#leasespace_one").attr("clicktarget")+'"><img src="'+$("#leasespace_one").attr("img")+'" border="0"></a>');
	}
	if($("#leasespace_two").attr("img")) {
		$("#leasespace_two").html('<a href="'+$("#leasespace_two").attr("click")+'" target="'+$("#leasespace_two").attr("clicktarget")+'"><img src="'+$("#leasespace_two").attr("img")+'" border="0"></a>');
	}
	
	//
	// Browse by
	//
	
	// Open by cinema
	$("#by_cinema img, .sessions_by_cinema").mouseover(function() {
		$(this).attr({ src: '/images/browse_by_cinema_ova.jpg' });
		
	}).mouseout(function() {
		$(this).attr({ src: '/images/browse_by_cinema.jpg' });
		
	}).click(function(){
		
		var cinemaID = $(this).attr("cinemaid");
		
		$("#browse_by_title").attr({ src: '/images/browse_by/browse_by_cinema_header.jpg' });
		$("#browse_by_content").html('<img src="'+'/images/blue_loading_ani.gif" alt="Loading" width="129" height="17" id="browse_by_loading" />');
		$("#browse_by").fadeIn();
		
		// Track state
		browseByMode = 'cinema';
		
		// Get content via ajax
		var html = $.ajax({ 
			type: "POST", 
			url: '/home/ajax=by_cinema', 
			data: "cinema_id="+(cinemaID?cinemaID:0)+"&rand="+rand(),
			async: false 
		}).responseText;

		$("#browse_by_content").html(html);
		
	});
		
	// Open by film
	$("#by_film img").mouseover(function() {
		$(this).attr({ src: '/images/browse_by_film_ova.jpg' });
		
	}).mouseout(function() {
		$(this).attr({ src: '/images/browse_by_film.jpg' });
		
	});
	
	$("#by_film img").click(function(){
		$("#browse_by_title").attr({ src: '/images/browse_by/browse_by_film_header.jpg' });
		$("#browse_by_content").html('<img src="'+'/images/blue_loading_ani.gif" alt="Loading" width="129" height="17" id="browse_by_loading" />');
		$("#browse_by").fadeIn();
		
		// Track state
		browseByMode = 'film';
		
		// Get content via ajax
		var html = $.ajax({ 
			type: "POST", 
			url: '/home/ajax=by_film', 
			data: "rand="+rand(),
			async: false 
		}).responseText;
		
		$("#browse_by_content").html(html);
		
		
	});

	
	// Close browse by
	$("#browse_by_title").click(function() {
		 $("#browse_by").fadeOut();
	});
	
	
	//
	// Browse by UI
	//

	// By film
	$("#select_film li").live('click',function() {
		
		var cinemaID 	= $(this).attr("cinemaid");
		var filmID 	= $(this).attr("filmid");
		
		$("#select_film li").removeClass().addClass("browse_by_selection");
		$(this).removeClass().addClass("browse_by_click");
		
		// This is second step, go to film dont ajax in content
		if(browseByMode=='cinema') {
			if(filmID) window.location = '/films/film_id='+filmID+'&cinema_id='+cinemaID;
			return true;
		}
		
		$("#select_cinema").html('<img src="'+'/images/blue_loading_ani.gif" alt="Loading" width="129" height="17" id="browse_by_loading" />');
	
		// Get content via ajax
		var html = $.ajax({ 
			type: "POST", 
			url: '/home/ajax=cinemas', 
			data: "film_id="+filmID+"&rand="+rand(),
			async: false 
		}).responseText;
		
		$("#select_cinema").html(html);
	});
	
	// By cinema
	$("#select_cinema li").live('click',function() {
		
		var cinemaID 	= $(this).attr("cinemaid");
		var filmID 	= $(this).attr("filmid");
		
		$("#select_cinema li").removeClass().addClass("browse_by_selection");
		$(this).removeClass().addClass("browse_by_click");
		
		// This is second step, go to cinema dont ajax in content
		if(browseByMode=='film') {
			if(filmID) window.location = '/films/film_id='+filmID+'&cinema_id='+cinemaID;
			return true;
		}
		
		$("#select_film").html('<img src="'+'/images/blue_loading_ani.gif" alt="Loading" width="129" height="17" id="browse_by_loading" />');
		
		// Get content via ajax
		var html = $.ajax({ 
			type: "POST", 
			url: '/home/ajax=films', 
			data: "cinema_id="+cinemaID+"&rand="+rand(),
			async: false 
		}).responseText;
		
		$("#select_film").html(html);
	});
		
		
	//
	// Navigation
	//
	$("#navigation img").mouseover(function() {
		
		var src=$(this).attr('src');

		if(src.indexOf("focus")<0) {
			var imgName = $(this).attr("imgname");
			$(this).attr({ src: '/images/nav/ova/'+imgName+'.jpg' });
		}
	}).mouseout(function() {
		
		var src=$(this).attr('src');
		
		if(src.indexOf("focus")<0) {
			var imgName = $(this).attr("imgname");
			$(this).attr({ src: '/images/nav/'+imgName+'.jpg' });
		}
	});
	
	
	//
	//	Trailers
	//
	$(".showtrailer").live('click',function(){
		
		var flv = $(this).attr("flvid");
		
		// blockUI
		$.blockUI({ 
			css: { 
				border: 'none', 
				paddingLeft: '4px', 
				paddingTop: '4px', 
				height: '409px', 
				width: '644px',
				top: '80px',
				left: '50%',
				marginLeft: '-322px',
				backgroundColor:'#fff',
				cursor: 'auto'
			}, 
			overlayCSS:  {  
				backgroundColor: '#000000',  
				opacity: '0.8',
				cursor: 'auto'
			}, 
			message: trailersHTML,
			applyPlatformOpacityRules: true	  
		});
		
		$("#trailer_box").html('<a href="http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash" target="_blank"><img src="/images/get_flash.jpg" alt="Get Flash" width="275" height="97" border="0" /></a>');
		
		// swfObject
		var params = {
			allowfullscreen: "true",
			allowscriptaccess: "always",
			wmode: "opaque"
		};
		
		var flashVars = {
			file: trailerURL+flv+".flv",
			width: "640",
			height: "360",
			//autostart: "true",
			stretching: "exactfit",
			//displayclick: "none",
			//bufferlength: "5",
			volume: "100"
		};
		
		var attributes = {
		};
		
		swfobject.embedSWF(
		mediaURL+"/mediaplayer.swf", 
		"trailer_box", 
		"640", 
		"360", 
		"9.0.0", 
		false, 
		flashVars, 
		params, 
		attributes
		);		
		
		// Buttons
		$("#trailer_session_times").mouseover(function() {
			$(this).attr({ src: '/images/trailers/trailer_session_times_ova.jpg' });
		}).mouseout(function() {
			$(this).attr({ src: '/images/trailers/trailer_session_times.jpg' });	
		}).attr({ 
			filmid: flv 
		}).click(function() {
			 $.unblockUI({ fadeOut: 0 }); 
			 window.location = '/films/film_id='+$(this).attr("filmid");
		});
	
		$("#trailer_close").mouseover(function() {
			$(this).attr({ src: '/images/trailers/trailer_close_ova.jpg' });
		}).mouseout(function() {
			$(this).attr({ src: '/images/trailers/trailer_close.jpg' });	
		}).click(function() {
			 $.unblockUI({ fadeOut: 0 }); 
		});
		
		// Misc
		$.ajax({ 
			type: "POST", 
			url: '/ajax/ajax=view_trailer', 
			data: "film_id="+flv+"&rand="+rand(),
			async: true 
		});
	
	});
	
	//
	//	Ajax Modal
	//
	$(".quick_help").live('click',function(){
		
		var url	= "topic="+$(this).attr("topic");
		showModal('Quick Help',url,'modal_help');
	
	});

});

function showModal(title,url,ajax) {

		var width = 550;

		// Get content via ajax
		var html = $.ajax({ 
			type: "POST", 
			url: '/ajax/ajax='+ajax, 
			data: url+"&rand="+rand(),
			async: false 
		}).responseText;
		
		var shell = '<div id="modal_dialog"><div id="modal_banner"><div id="close_modal"><img src="/images/modal_dialogs/modal_close.jpg" width="32" height="31"></div><span>'+title+'</span></div><div id="modal_form_content">'+html+'</div></div>';
	
		// blockUI
		$.blockUI({ 
			css: { 
				border: 'none', 
				top: '80px',
				// Add on the css margin
				width: (width+8)+'px',
				left: '50%',
				marginLeft: '-'+(width/2)+'px',
				backgroundColor:'#fff',
				cursor: 'auto'
			}, 
			overlayCSS:  {  
				backgroundColor: '#000000',  
				opacity: '0.8',
				cursor: 'auto'
			}, 
			message: shell,
			applyPlatformOpacityRules: true	  
		});
		
		$("#close_modal").click(function(){
			$.unblockUI({ fadeOut: 0 }); 
		});
		
		submitModal();
}



function rand() {
	return Math.floor(Math.random()*99999999999+1);
}


function quickInfo() {

	$("#quick_info").mouseover(function() {
		clearTimeout(timeoutOut);
	}).mouseout(function() {
		timeoutOut = setTimeout('$("#quick_info").hide()',10);
		clearTimeout(timeoutShow);
	});
	
	$(".film_slick").mouseover(function(){

		clearTimeout(timeoutOut);
		clearTimeout(timeoutShow);

		if($("#quick_info").css("display")=='none') {
			$("#quick_info").hide();
		}
		
		var filmID = $(this).attr("filmid");
		var offset	= $(this).offset();
		
		timeoutShow = setTimeout('showQuickInfo('+filmID+','+offset.left+','+offset.top+')',500);

	}).mouseout(function() {
		timeoutOut = setTimeout('$("#quick_info").hide()',10);
		clearTimeout(timeoutShow);
	});
	
}

function showQuickInfo(filmID,offsetLeft,offsetTop) {
		
	// Get content via ajax
	var html = $.ajax({ 
		type: "POST", 
		url: '/ajax/ajax=quick_info', 
		data: "film_id="+filmID+"&rand="+rand(),
		async: false 
	}).responseText;

	$("#quick_info").html(html);
	
	var height	= $("#quick_info").height();
	var	top		= (offsetTop-(height/2))+100;
	
	
	$("#quick_info").css({ left: (offsetLeft+117)+'px', top: top+'px' }).show()
	
	$(".film_slick_header").click(function(){
		$("#quick_info").hide();
	});
	
	$(".showtrailer").mouseover(function(){
		$(this).attr({ src: '/images/quick_info/play_trailer_ova.jpg' });
	}).mouseout(function(){
		$(this).attr({ src: '/images/quick_info/play_trailer.jpg' });
	}).click(function() {
		$("#quick_info").hide();
	});
}

function highlightRequired() {
	for(i=0;i<arguments.length;i++){
		$("#"+arguments[i]).attr({ style: 'background-color:#FDCCCC' });
	};
}

function Left(str, n)
/***
        IN: str - the string we are LEFTing
            n - the number of characters we want to return

        RETVAL: n characters from the left side of the string
***/
{
        if (n <= 0)     // Invalid bound, return blank string
                return "";
        else if (n > String(str).length)   // Invalid bound, return
                return str;                // entire string
        else // Valid bound, return appropriate substring
                return String(str).substring(0,n);
}

