// Setup JS events when the DOM is ready
$(document).ready(function(){
    dropdowns();
	//resizeBrowser();
	
	// Popup links
	$("a.popup").click("this.target='_blank'").attr("title","Link opens in a new window");
	
	// Search
	$("#quicksearch a").click( adv_search );
	
	// Checkout signin
	$("#sign_in #password").click( function(){ $("#registered1").attr("checked",true); });
	$("#sign_in #password").keypress( function(){ $("#registered1").attr("checked",true); });
	
	// Payment page
	if ($("#copy_address").length > 0) copy_address();
    if ($("#place_order").length > 0) place_order();

	// Homepage banner animation
	if ($("#banners").length > 0) $.slideshow('banners', 7500);
	
	// 3D Secure Form
	if ($("#secure_authentication").length > 0) submit_secure_form();
	
    
});


// jQuery fader
// Based on: http://portfolio.gizone.co.uk/applications/slideshow/
$.slideshow = function (containerId, timeout) {
	var current = 0;
	var id = '#' + containerId;
	$(id).css({position:'relative'});
	var slides = $(id).children().get();
	for ( var i = 0; i < slides.length; i++ ) {
		$(slides[i]).css({zIndex:(slides.length - i), position:'absolute', top:'0', left:'0'});
	}
	setTimeout((function(){$.slideshow.next(slides, timeout, current);}), timeout);
}
$.slideshow.next = function (slides, timeout, current) {
	for (var i = 0; i < slides.length; i++) {
		var slide = slides[(current + i) % slides.length];
		$(slide).css({zIndex:(slides.length - i)});
	}
	// IE doesn't seem to support .show() after it has been faded out, so we use .fadeIn("fast")
	$(slides[current]).fadeOut('slow', 
			function(){$(slide).css({zIndex:'0', opacity:1}).fadeIn("fast");}
			);
	
	current = (current + 1) % slides.length;
	setTimeout((function(){$.slideshow.next(slides, timeout, current);}), timeout);
}



// Advanced search
function adv_search()
{
    var fs = $("#advanced_search");
    if (fs.is(':visible')) {
        fs.hide();
        $("#normal_search a").removeClass('active');
    } else {
        fs.slideDown("medium");
        $("#normal_search a").addClass('active');
    }
	return false;
}

// Events for delivery address copying
function copy_address()
{
    $("#copy_address")[0].style.visibility = "visible";
	$("#copy_address_chk")[0].checked = false;
	$("#copy_address_chk").click( copy_delivery );
	$("#delivery_address input").keyup( 
	    function(){
	        if ($("#copy_address_chk")[0].checked == true) copy_delivery();
	        }
	    );
    $("#billing_address input[@type='text']").change( 
        function(){ $("#copy_address_chk")[0].checked = false;} );
}

// Copy delivery address
function copy_delivery()
{
    $("#order_billing_first_name").val($("#order_delivery_first_name").val());
    $("#order_billing_last_name").val($("#order_delivery_last_name").val());
    $("#order_billing_address1").val($("#order_delivery_address1").val());
    $("#order_billing_address2").val($("#order_delivery_address2").val());
    $("#order_billing_city").val($("#order_delivery_city").val());
    $("#order_billing_county").val($("#order_delivery_county").val());
    $("#order_billing_postcode").val($("#order_delivery_postcode").val());
}

// Place order
function place_order()
{
    $("#place_order p")[0].style.display = "none";
    $("#place_order input").click( 
        function(){ 
            $("#place_order input")[0].style.display = "none";
            $("#place_order img")[0].style.display = "inline";
            return true;
        });
}


// Reset default text in input box
function clearbox()
{
    if (!this.default_value) this.default_value = this.value;
    
    if (this.value == '') {
        this.value = this.default_value;
    } else if (this.value == this.default_value) {
        this.value = '';
    }
}

// IE emulation of hover on the dropdown LI elements
function dropdowns()
{
	if (jQuery.browser.msie) {
		// Old school suckerfish dropdowns for IE
		$("#tabs li").hover(
    	    function(){ 
				this.className += " sfhover";
				$("fieldset.options").css({visibility:"hidden"});
				},
    	    function(){ 	
				this.className = this.className.replace(new RegExp(" sfhover\\b"), "");
				$("fieldset.options").css({visibility:"visible"});
				}
    	    );
	}  
}





function change_image(id)
{
	$("#product_image img").each(function(i){
		if (this.id == id){
			$(this).show();
			$("#"+this.id+"t").addClass("active");
		} else {
			$(this).hide();
			$("#"+this.id+"t").removeClass("active");
		}
	});
	return false;
}




function resizeBrowser() {
    var w = 1024;
    var ps = getPageSize();
    if (ps.width < w) ps.width = w;
    setPageSize(ps)
}

// Page size
function getPageSize() {
    var ps = {width: 0, height: 0};
    var de = document.documentElement;
    ps.width = window.innerWidth || self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth;
    ps.height = window.innerHeight || self.innerHeight || (de&&de.clientHeight) || document.body.clientHeight;
    return ps;
}

function setPageSize(ps) {
    var de = document.documentElement;
    
    if (window.innerWidth){
        window.innerWidth = ps.width;
        //window.innerHeight = ps.height;
    } else if (self.resizeTo){
        self.resizeTo(ps.width, ps.height);
    } else if (self.innerWidth) {
        self.innerWidth = ps.width;
        //self.innerHeight = ps.height;
    } else if (de&&de.clientWidth) {
        de.clientWidth = ps.width;
        //de.clientHeight = ps.height;
    } else if (document.body.clientWidth) {
        document.body.clientWidth = ps.width;
        //document.body.clientHeight = ps.height
    }
}

