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
	 }
	
	//-->
	
}

// Image thumbnail select function
function select_thumb_image(title, large_path, xlarge_path) {
	
	element = document.getElementById('productImageLarge');
	if (!element) { return; }
	
	img_str	=	"<table>\n<tr>\n<td align=\"center\" valign=\"middle\">";
	img_str += '<img src="'+large_path+'" alt="'+title+'" />';
	
	if (xlarge_path) { img_str += '<a href="'+xlarge_path+'" class="thickbox" title="'+title+'">+ Enlarge Image</a>'; }
	
	img_str	+=	"</td>\n</tr>\n</table>";
	element.innerHTML = img_str;
	
	// refresh thickbox as the dom has changed
	if (xlarge_path) { tb_init('a.thickbox'); }
}



/*
	CAROUSEL CALLBACK
*/

function carousel_initCallback(carousel)
{
    // Disable autoscrolling if the user clicks the prev or next button.
    carousel.buttonNext.bind('click', function() {
        carousel.startAuto(0);
    });
 
    carousel.buttonPrev.bind('click', function() {
        carousel.startAuto(0);
    });
 
    // Pause autoscrolling if the user moves with the cursor over the clip.
    carousel.clip.hover(function() {
        carousel.stopAuto();
    }, function() {
        carousel.startAuto();
    });
};



/*
*
* externalLinks
*
*/

function externalLinks(){
	jQuery("a[href^='http:']").not("[href*='follifootfarm.co.uk']").attr('target','_blank');
	jQuery("a[rel='external']").attr('target','_blank');
}

// COMPLETES DELIVERY ADDRESS THE SAME AS STANDARD ADDRESS
function same_delivery() {
	
	if (document.getElementById('use_billing'))
	{
		
		//HTML
		document.getElementById('use_billing').innerHTML	=	'<label for="same_address"><strong>Please deliver to the billing address above</strong></label>';
		document.getElementById('use_billing').innerHTML	+=	'<input type="checkbox" name="same_address" id="same_address" value="Y" />';
		
		
		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 changeDeliveryCountry(val)
{
	window.location	=	'index.php?url=basket&delivery_country_id='+val;	
}


/*	--------------------------------------------------------------------------  */
/*	--------------	JQUERY DOCUMENT READY RUNTIME PROCESSES  -----------------  */
/*	--------------------------------------------------------------------------  */


$(document).ready(function(){
						
	//PARSE EXTERNAL LINKS
	externalLinks();		
	
	//COPY CHECKOUT ADDRESSES
	same_delivery();		
						
	//FEATURED PRODUCTS					
	if($("#featured").length>0)
	{
		jQuery('#featured').jcarousel({
			auto: 5,
			scroll:1,
			animation:800,
			wrap: 'last',
			buttonNextHTML: null,
        	buttonPrevHTML: null,
			initCallback: carousel_initCallback
		});
	}
	
	if($("#flashMap").length>0)
	{		
		var so = new SWFObject("flash/map.swf", "map", "956", "509", "10", "#FFFFFF");
		so.addParam("swliveconnect", "true");
		so.addParam("swliveconnect", "always");
		so.addParam("allowScriptAccess", "always");
		so.addParam("quality", "high");
		so.addParam("wmode", "opaque");
		so.addParam("menu", "false");
		so.useExpressInstall('flash/expressinstall.swf');
		so.write("flashMap");
	}
	
	//REMOVE ADD TO BASKET NOTICE
	if($("#addedToBasket"))
	{
		setTimeout(function(){$("#addedToBasket").fadeOut('slow'); }, 6000);
	}
	
	/*
	if($('.checkoutButton').length > 0)
	{
		$('.checkoutButton').bind('click', function(){
			
			var message	= "Teachers and Schools please note:\n\nYou can order these items via fax or post by clicking the relevant button below";
				message	+= "\n\nClick OK to continue to the checkout or click Cancel to stay on this page." 
			
			if(!confirm(message)) return false;
		});
	}
	*/
	
	$(".lettersounds_flash").fancybox({
		'width'				: 960,
		'height'			: 720,
		'padding'			: 0,
		'autoScale'			: false,
		'transitionIn'		: 'fade',
		'transitionOut'		: 'fade',
		'centerOnScroll'	: true,
		'titleShow'			: false
	});
	
	
});

