// JavaScript Document
<!--
/*Shadowbox.init({
	skipSetup: true,
	players: ["img", "html"]
});*/

function calculateTotal(postage)
{
	var totalprice = total+postage;
	$('#checkoutPrice').html('');
	$('#checkoutPrice').html("Total: &pound;" + totalprice.toFixed(2));	
}

function checkAddress(formobj){

	// Enter name of mandatory fields
	var fieldRequired = Array("name", "Line1", "PostCode");
	// Enter field description to appear in the dialog box
	var fieldDescription = Array("Your Name", "Line 1", "Post/Zip Code");
	// dialog message
	var alertMsg = "Please complete the following fields:\n";
	var l_Msg = alertMsg.length;

	for (var i = 0; i < fieldRequired.length; i++){
		var obj = formobj.elements[fieldRequired[i]];

		if (obj){
			switch(obj.type){
			case "select-one":
				if (obj.selectedIndex == -1 || obj.options[obj.selectedIndex].text == ""){
					alertMsg += " - " + fieldDescription[i] + "\n";
				}
				break;
			case "select-multiple":
				if (obj.selectedIndex == -1){
					alertMsg += " - " + fieldDescription[i] + "\n";
				}
				break;
			case "text":
			case "textarea":
			case "password":
				if (obj.value == "" || obj.value == null){
					alertMsg += " - " + fieldDescription[i] + "\n";
				}
				break;
			default:
			}

			if (obj.type == undefined){
				var blnchecked = false;
				for (var j = 0; j < obj.length; j++){
					if (obj[j].checked){
						blnchecked = true;
					}
				}

				if (!blnchecked){
					alertMsg += " - " + fieldDescription[i] + "\n";
				}
			}
		}
	}
	

	if (alertMsg.length == l_Msg){
		return true;
	}else{
		alert(alertMsg);
		return false;
	}
}

$(document).ready(function(){	
						  
/*	Shadowbox.setup("a.large", {
		players: ["img"]
	});*/
						   
	function bubbletarget(e)
	{
		if(e && e.originalTarget){
			var target = e.originalTarget.id;
		} else if(e && e.target){
			var target = e.target.id;
		} else if(document.activeElement && document.activeElement.tagName != "BODY"){
			var target = document.activeElement.id;
		}
		return target;
	}
	
	// buy now buttons on homepage
	$(".buyNowButton").live("click", function(e){
		$('.buyNowButtonOver').hide();
		target = bubbletarget(e);
		$('#' + target + 'full').show('fast');
	});
	
	$('.buyNowButtonOver').live("click", function(e){
		$('.buyNowButtonOver').hide();						  
	});

	// tooltip
	$(".infowindow").tooltip({ position: "center right", delay: 0, events: {
		tooltip: 'mouseenter',
		}
	});
	
	// view film info tabs
	$(".infotab").bind("click", function(e){					 
		$('.infobox').slideUp();
		
		target = bubbletarget(e);
		$('#outerContainer').css('visibility','hidden');
		$('#outerContainer').css('height', '0px');
		$('#imageScroller').css('height', '0px');
		$('#viewer').css('height', '0px');
		$('#' + target + 'full').slideDown();
		$('a.infotab').removeClass('pinkLink');
		$('a.showgallery').removeClass('pinkLink');
		$('a#' + target).addClass('pinkLink');
		$(".galleryContainer").css('height', '0px');
		
		return false;
	});	
	
	$(".showgallery").bind("click", function(e){
		$('.infobox').hide();
		$('#outerContainer').css('visibility','visible');
		$('#outerContainer').css('height', '420px');
		$('#imageScroller').css('height', '202px');
		$('#viewer').css('height', '182px');
		$('a.infotab').removeClass('pinkLink');
		$('a#gallery').addClass('pinkLink');
		$(".galleryContainer").css('height', '420px');
		
		return false;
	});
	
	$(".featurenav").live("click", function(e){
									  
		target = bubbletarget(e);			  
									  
		var url = target.replace(/:/g, "/");
		$.get(url, function(data){
						
			$('#featured').hide();
			$('#featured').html("");
			$('#featured').html(data);
			$('#featured').show();
		});
	});	
	
	$(".latestnav").live("click", function(e){
		
		target = bubbletarget(e);
		
		var url = target.replace(/:/g, "/");
		$.get(url, function(data){
						
			$('#latest').hide();
			$('#latest').html("");
			$('#latest').html(data);
			$('#latest').show();
		});
	});	
	
	$(".popularnav").live("click", function(e){
		
		target = bubbletarget(e);
		
		var url = target.replace(/:/g, "/");
		$.get(url, function(data){
						
			$('#popular').hide();
			$('#popular').html("");
			$('#popular').html(data);
			$('#popular').show();
		});
	});
	
	$(".mightlikenav").live("click", function(e){
		
		target = bubbletarget(e);
		
		var url = target.replace(/:/g, "/");
		$.get(url, function(data){
						
			$('#mightlike').hide();
			$('#mightlike').html("");
			$('#mightlike').html(data);
			$('#mightlike').show();
		});
	});
	
	$(".relatednav").live("click", function(e){
		
		target = bubbletarget(e);
		
		var url = target.replace(/:/g, "/");
		$.get(url, function(data){
						
			$('#related').hide();
			$('#related').html("");
			$('#related').html(data);
			$('#related').show();
		});
	});
	
	$(".buytab, .buybutton").live("click", function(e){	
		
		target = $(this).attr('id');
		
		$.getJSON("/cart/add" + target, function(data) {
			$('#miniitems').html(data.items);
			$('#miniprice').html(data.total);
			$("#basket").animate( { backgroundColor: '#FF0000' }, 1000)
				.animate( { backgroundColor: '#b6d90d' }, 1000);

		});							 
	});
	
	
	$(".openrater").bind("click", function(e){	
		$('#rater').show();
	});
	
	$.getJSON("/cart/mini", function(data) {
		$('#miniitems').html(data.items);
		$('#miniprice').html(data.total);
		
	});
	
	
	// checkout submit form validation
	$('.submit').bind("click", function(){	

		if($("input[name=cardholder_address]:checked").val() == undefined)
		{
			alert('You must select a card holder address');
			return false;
		}
		
		if($("input[name=postage]").val() == undefined)
		{
			alert('You must select a delivery address');
			return false;
		}
		
		if($("input[name=tandc]:checked").val() == undefined)
		{
			alert('You must agree to the terms and conditions');
			return false;
		}
		
		$.post('/cart/confirm', $('#checkout').serialize(), function(data) {									 
			window.location.replace("/cart/redirect/order_id/" + data + "?" + $('#checkout').serialize());
		});
		
		return false;
	});
	
	$(function() {
		$("#slider").slider({
			value:100,
			min: 0,
			max: 5,
			step: 0.5,
			slide: function(event, ui) {
				$("#amount").val('Your rating: ' + ui.value + '/5');
			}
		});
		$("#amount").val('Your rating: ' + $("#slider").slider("value") + '/5');
	});
	
	$('#saveRating').bind("click", function(){
		
		$.get("/films/rate/film_id/" + $("input[name=film_id]").val() + "/rating/" + $("#slider").slider("value"), function(data){
			window.location.reload();
		});
	});
	
	$('#postage').change(function() {
		window.location.replace("/cart/index/upgrade_postage/" + $('#postage').val());
	});
	
	$('.helpLink').bind("click", function(e){	
		target = bubbletarget(e);
		$('.helpul').hide();
		$('ul#' + target).show('fast');
	});
	
	$('.delivery_address').change(function(e){
		address_id = $("input[name=delivery_address]:checked").val();
		
		$.get("/cart/getpostageoptions/address_id/" + address_id, function(data) {
			$('#postageoptions').html('');
			$('#postageoptions').html(data);
			calculateTotal(postage);
		});	
	});
	
	$('#sortdrop').change(function(e){
		var sort_by = $("#sort_by").val();
		var pathname = self.location.pathname + "?sort_by=" + sort_by;
		//alert(pathname);
		self.location.href = pathname;
	});
});
-->
