var viewportwidth;
var viewportheight;

//BROWSER HEIGHT (available height allowing for browser toolbars etc.)

function getBrowserSizes(){
	 
	 // the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight
	 
	 if (typeof window.innerWidth != 'undefined')
	 {
		  viewportwidth = window.innerWidth,
		  viewportheight = window.innerHeight
	 }
	 
	// IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)
	
	 else if (typeof document.documentElement != 'undefined'
		 && typeof document.documentElement.clientWidth !=
		 'undefined' && document.documentElement.clientWidth != 0)
	 {
		   viewportwidth = document.documentElement.clientWidth,
		   viewportheight = document.documentElement.clientHeight
	 }
	 
	 // older versions of IE
	 
	 else
	 {
		   viewportwidth = document.getElementsByTagName('body')[0].clientWidth,
		   viewportheight = document.getElementsByTagName('body')[0].clientHeight
	 }
	
	//-->
	
}


// COMPLETES DELIVERY ADDRESS THE SAME AS STANDARD ADDRESS
function same_delivery() {
	
	if (document.getElementById('use_billing'))
	{
		
		// SPAN TO SHOW BUTTON WHICH AUTO-COMPLETES DELIVERY ADDRESS
		document.getElementById('use_billing').innerHTML	=	'<input type="checkbox" class="checkbox" name="same_address" id="same_address" value="Y" />';
		document.getElementById('use_billing').innerHTML	+=	'<label for="same_address">Please deliver to the billing address above</label>';
		
		var from_fields	=	new Array();
		var to_fields	=	new Array();
		
		from_fields[0]='first_name';
		from_fields[1]='surname';
		from_fields[2]='telephone_number';
		from_fields[3]='address_1';
		from_fields[4]='address_2';
		from_fields[5]='town';
		from_fields[6]='county';
		from_fields[7]='postcode';
		from_fields[8]='country';
		
		to_fields[0]='delivery_first_name';
		to_fields[1]='delivery_surname';
		to_fields[2]='delivery_telephone_number';
		to_fields[3]='delivery_address_1';
		to_fields[4]='delivery_address_2';
		to_fields[5]='delivery_town';
		to_fields[6]='delivery_county';
		to_fields[7]='delivery_postcode';
		to_fields[8]='delivery_country';
		
		var default_values=new Array();
		
		for (i=0;i<to_fields.length; i++)
		{
			field_id=to_fields[i];
			default_values[i]=document.getElementsByName(to_fields[i])[0].value;
		}
		
		// ACTION WHEN USER CLICKS BUTTON
		document.getElementById('same_address').onclick=function()
		{
			if (document.getElementById('same_address').checked==true) {
				for (i=0;i<from_fields.length;i++) {
					document.getElementsByName(to_fields[i])[0].value=document.getElementsByName(from_fields[i])[0].value;
				}
			}
			else {
				for (i=0;i<default_values.length;i++) {
					document.getElementsByName(to_fields[i])[0].value=default_values[i];
				}
			}
		}
	}
}


function showDiv(divID){
	
	if($('#'+divID).is(":hidden"))
	{
		$('#'+divID).fadeIn('slow');
		$('#'+divID).slideDown('slow');
	}
	return false;
}


function showRegistrationForm()
{	
	if($('#customerDetails').is(":hidden"))
	{
		$('#customerDetails').fadeIn('slow');
		$('#customerLogin').slideUp('slow');
	}
	return false;
}


// Image thumbnail select function
function select_thumb_image(title, large_path, xlarge_path) {
	
	element = document.getElementById('productImageLarge');
	if (!element) { return; }
	
	img_str = '<img src="'+large_path+'" alt="'+title+'" />';
	if (xlarge_path) { img_str += '<a href="'+xlarge_path+'" class="thickbox" title="'+title+'">+ Enlarge Image</a>'; }
	
	element.innerHTML = img_str;
	
	// refresh thickbox as the dom has changed
	if (xlarge_path) { tb_init('a.thickbox'); }
}



function loadYoutubeVideo(videoID)
{
	element = $('#productImageLarge');
	if (!element) { return; }
	
	embed_code	=	'<object width="450" height="450"><param name="movie" value="http://www.youtube.com/v/'+videoID+'&hl=en_GB&fs=1&rel=0"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/'+videoID+'&hl=en_GB&fs=1&rel=0" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="450" height="450"></embed></object>';
	
	
	element.html(embed_code);
	
}




function delivery_option(){
	
	countryID	=	$(this).val();
	
	if(countryID==0) return;
	
	//REMOVE DELIVERY OPTION SELECT
	if($("#select_delivery_option")) $("#select_delivery_option").remove();
	
	$(this).css("display", "none");
	
	$("#choose_delivery").append('<img src="images/ajax-loader.gif" id="loading" width="16" height="16" />');
	
	//AJAX CALL
	$.ajax(
	{		
		type: "POST",
		url: "libs/includes/ajax.php",
		data: "ACTION=DELOPTIONS&COUNTRYID="+countryID,
		success: function(optionsStr)
		{
			$("#loading").remove();			
			
			if(optionsStr.length>0)
			{
				html	=	'<select name="select_delivery_option" id="select_delivery_option">'+optionsStr+'</select>';
				$("#choose_delivery").append(html);
				
				$("#select_delivery_option").bind('change', get_order_totals);
			}
			else
			{
				get_order_totals();	
			}
			$("#select_delivery_country").css("display", "block");
		}
	});

}


function get_order_totals(){
	
	optionID	=	$("#select_delivery_option").val();
	countryID	=	$("#select_delivery_country").val();
	
	//AJAX CALL
	$.ajax(
	{		
		type: "POST",
		url: "libs/includes/ajax.php",
		data: "ACTION=GETTOTALS&COUNTRYID="+countryID+"&OPTIONID="+optionID,
		success: function(dataString)
		{
			//SPLIT DATA STRING TO GET EACH PART
			var dataArr			=	dataString.split("&");
			var delivery_price	=	dataArr[0].replace("DELIVERY=", "");
			var total_price		=	dataArr[1].replace("TOTAL=", "");
			
			$("#delivery_price").html("&pound;"+delivery_price);	
			$("#total_price").html("&pound;"+total_price);
			
			if($("#WP_amount").length>0) $("#WP_amount").attr('value', total_price);
		}
	});
}


/*
*
*
*
*/

function validate_basket(){
	
	if($("#select_delivery_country").val()==0)
	{
		alert("Please select a country for delivery");
		$("#select_delivery_country").focus();
		return false;
	}
	
	if($("#select_delivery_option").length>0 && $("#select_delivery_option").val()==0)
	{
		alert("Please select an area/region for delivery");
		$("#select_delivery_option").focus();
		return false;
	}
}




/*	--------------------------------------------------------------------------  */
/*	--------------	JQUERY DOCUMENT READY RUNTIME PROCESSES  -----------------  */
/*	--------------------------------------------------------------------------  */


$(document).ready(function(){

	//REMOVE ADD TO BASKET NOTICE
	if($("#addedToBasketNotice").length>0)
	{
		setTimeout(function(){$("#addedToBasketNotice").hide(); }, 8000);
	}
	
	//MORE INFO CATEGORY DESCRIPTION
	if($("#categoryMoreInfo").length>0) 		$('#categoryMoreInfo').bind('click', function(){
																								if($('.fullDescription').is(":hidden"))
																								{
																									//$('.fullDescription').fadeIn('slow');
																									$('.fullDescription').slideDown('slow');
																								}
																								return false;
																								});
	
	//CHECKOUT REGISTRATION FORM
	if($("#checkoutRegister").length>0) 		$('#checkoutRegister').bind('click', showRegistrationForm);
	
	//BASKET CHANGE DELIVERY COUNTRY
	if($("#select_delivery_country").length>0)	$("#select_delivery_country").bind('change', delivery_option);
	
	//BASKET CHANGE DELIVERY OPTION
	if($("#select_delivery_option").length>0)	$("#select_delivery_option").bind('change', get_order_totals);
	
	if($('.checkoutButton').length>0)			$('.checkoutButton').bind('click', validate_basket);
	if($('#checkoutConfirm').length>0)			$('#checkoutConfirm').bind('click', validate_basket);
	
	//COPY DELIVERY/BILLING DETAILS
	same_delivery();
	
	
		//FILTER ORDERS
	$("#sortby").change(function()
	{
		if($(this).val()!='')
		{
			window.location	=	$(this).val();	
		}
	});
	
	
});



