jQuery(function($) {

    // Fix background image caching problem
    if (jQuery.browser.msie) {
        try {
            document.execCommand("BackgroundImageCache", false, true);
        } catch(err) {}
    }

	$("a.thickbox").each(function(){
	
		//If IE hide selects on starting thickbox
	
		if($.browser.msie)
		{
			$("a.thickbox").click(function(){
				$("#content form select").css("visibility","hidden");
			});
		}             	
	});   

    // Set up credit card / direct debit switcher
    
    $('#paymentCC').click(function() {
       $('div#creditCardSection').fadeIn('slow'); 
       $('div#directDebitSection').hide();
    });
    
    $('#paymentDD').click(function() {
       $('div#directDebitSection').fadeIn('slow');
       $('div#creditCardSection').hide();  
    });
    
    $(document.getElementById('othercommerce/ExpressCheckout.billingAdrress.autoFillAddress')).click(function() {
       $('div#addressSelectWrapper').show();
    });
    
    // Set up shopping basket switcher
    
    //$("#insert-basket").replaceWith("<li>ss</li>");
    
    // open basket
    
    $('#open-basket').click(function() {
        $('#shopping-basket').fadeIn(200);
    });
    
    // fade in
    
    $("#shopping-basket").bind("mouseenter",function() {
        $('#shopping-basket').stop().fadeTo(500, 1);
    });
    
    // fade out
    
    $("#shopping-basket").bind("mouseleave",function() {
		$('#shopping-basket').animate({opacity: 1.0}, 2000).fadeOut(2000);
	});
    
    // fade out

    $('#shopping-basket h2').click(function() {
       	$('#shopping-basket').stop().fadeTo(500, 1);     
    });
    
    // *********** PHASE I
    // Add to basket buttons / open basket highlight added item
	//$('.add-basket-button').bind('submit', function() {
	//    var form = $(this);
	//    $.post(this.action, form.serialize(), function(response) {
	//        var parts = response.split('<!-- DIVIDER -->');
	//        $('#items').html(parts[0]);
	//        $('#total-cost').html(parts[1]);
	//        $('#shopping-basket').fadeIn(500);
    //        $('#shopping-basket tbody tr:first').addClass("added-to-basket");
    //        $('#shopping-basket').animate({opacity: 1.0}, 2000).fadeOut(2000);
    //        
    //        var mappings = {
    //        	addToBasket:			['addToBasketPreview'],
    //       	addToBasketPreview:		['addToBasket'],
    //        	addToBasketMultiple:	['addToBasket', 'addToBasketPreview']
    //        };
    //        
    //        var ids = mappings[form[0].id] || [],
    //            i = ids.length, otherForm,
    //            replacement = '<li><div class="added">Added to basket</div></li>';
    //        
    //        while (i--) {
    //            otherForm = $('#' + ids[i]);
    //            otherForm.before(replacement);
    //            otherForm.remove();
    //        }
    //        
    //        form.before(replacement);
    //        form.remove();
	//    });
	//    return false;
	//});
	
	
	// ############ set up thickbox
	   
	// visibility of content
	$("#preview-window").css("display", "block");
	$("#btn-preview").click(function () { 
		$("img#shadow").css("display", "block")
	});
	
	// initialise counters
	var items = $("#pdf-browser .items a").length;
	//Not Used
	$("#shown-pages").replaceWith("<p id='shown-pages'>&nbsp;</p>");	
	
	// page browser
	//$("#pdf-browser").scrollable({size:3, items:'.items', prev:'#previous', next:'#next'});
	
	// load image
	$("#pdf-browser .items a").click(function () { 
		
		// update counters
		var current = ($("#pdf-browser .items a").index(this)) + 1;
		$("#controls p#current-page")
			.replaceWith("<p id='current-page'><strong>Previewing</strong> Page:" + current + "</p>");
		
		// image switch
		$("#preview-window img#pdf-page")
			//.css("background", "url(" + this.href + ") no-repeat left top")
			.replaceWith("<img id='pdf-page' src='"+ this.href +"' width='700px' alt='' />");

		return false;
		
	});
	
	
});

function disableSubmit(s)
{
	// When we disable the submit button we lose its value so copy to hidden field instead
	var r = document.getElementById("submit-temp");
	r.name = s.name; 
	r.value = s.value; 
	
	s.name = "null";
	s.disabled = true; //'disabled';
	s.className = s.className + ' submit-disabled';
	s.value = 'Please wait...';
	return true;
}

function disableSubmitButtons(f)
{
	for (i = 0; i < f.length; i++) 
	{
		var s = f.elements[i];
		if(s.type == 'submit')
		{
			disableSubmit(s);
		}
	}
}

function goUrl(slct)
{
	window.location.href = slct.options[slct.options.selectedIndex].value;
}

