(function($, undefined)
{
    $(function()
	{
		//All JS functions to be included in here, not the template files!
        
        $('#exhibiton_slideshow').cycle({
    		fx: 'fade' // choose your transition type, ex: fade, scrollUp, shuffle, etc...
    	});
        
        //Hide Delivery address
        if ($("#shipping-address").is(":checked"))
        {
            $("#ship-address").css("display","none");
        }
        
        $("#shipping-address").click(function()
        {
          if ($("#shipping-address").is(":checked"))
          {
            $("#ship-address").hide("fast");
          }
          else
          {     
            $("#ship-address").show("fast");
          }       
        });
		
		//Counties & US States
		$('#us-states').hide();
		$('#us-ship-states').hide();
		$('.uk-county').hide();
		$('.us-states').hide();
		
		if($('#country').val()=='US')
		{
			$('#personal .us-states').slideDown();
		}
		
		if($('#country').val()=='GB')
		{
			$('#personal .uk-county').slideDown();
		}
		
		if($('#shipping-country').val()=='US')
		{
			$('#personal .us-states').slideDown();
		}     
		
		$('#country').change(function()
		{
			if($('#country').val()=='US')
			{
				$('#personal .us-states').slideDown();
			}
			if(($('#country').val()!='US')&&($('#personal .us-states').is(':visible')))
			{
				$('#personal .us-states').slideUp();
			}
			
			//UK Counties
			if($('#country').val()=='GB')
			{
				$('#personal .uk-county').slideDown();
			}
			if(($('#country').val()!='GB')&&($('#personal .uk-county').is(':visible')))
			{
				$('#personal .uk-county').slideUp();
			}
		});
		
		$('#ship-country').change(function()
		{
			if($('#ship-country').val()=='US')
			{
				$('#ship-address .us-states').slideDown();
			}
			
			if(($('#ship-country').val()!='US')&&($('#ship-address .us-states').is(':visible')))
			{
				$('#ship-address .us-states').slideUp();
			}
			
			//UK Counties
			if($('#ship-country').val()=='GB')
			{
				$('#ship-address .uk-county').slideDown();
			}
			
			if(($('#ship-country').val()!='GB')&&($('#ship-address .uk-county').is(':visible')))
			{
				$('#ship-address .uk-county').slideUp();
			}
		});
        
        $('#hideselectaddress').hide();
        $('#hideselectaddressship').hide();
        
        // All the basket postcode ajax stuff
        // when billing postcode submit is clicked
        $('#postcode-searchbtn').click(function (event) {
            event.preventDefault();
            postcode_lookup('billing');
        });
        // when shipping postcode submit is clicked
        $('#ship-postcode').click(function (event) {
            event.preventDefault();
            postcode_lookup('shipping');
        });
        
        // Do the AJAX postcode lookup
        function postcode_lookup(type) {
            // set variables depending on if its billing/shipping.
            // $pcid = postcode input box id
            // $pcselect = "select address" select box id
            // $unhide = hide address wrap div id
            if(type === 'billing') {
                $pcid = $('#postcode-search');
                $pcselect = $('#address_list');
                $unhide = $('#hideselectaddress');
            }else if(type === 'shipping') {
                $pcid = $('#shipping-postcode');
                $pcselect = $('#shipping_address_list');
                $unhide = $('#hideselectaddressship');
            }
            
            // get postcode value from input
            $postcode = $pcid.val();
            // strip spaces
            $postcode = $postcode.replace(' ', '');
            // set up ajaxurl with the postcode
            $ajaxurl = "../includes/ajax_postcode.php?postcode=" + $postcode;
            
            // use load to put the output inside the select box
            $pcselect.load($ajaxurl);
            // show the select box
            $unhide.css('display', 'inline');
        }
        
        // when address list is changed
        $('#address_list').change(function () {
            // get the value of the newly selected option
            $value = $('#address_list option:selected').val();
            
            // if the value is not null (i.e. is an address)
            if ($value != '') {
                // change the address
                change_address('billing');               
            }
        });
        // when address list is changed
        $('#shipping_address_list').change(function () {
            // get the value of the newly selected option
            $value = $('#shipping_address_list option:selected').val();
            
            // if the value is not null (i.e. is an address)
            if ($value != '') {
                // change the address
                change_address('shipping');               
            }
        });
        
        // change address function
        function change_address($type) {
            // set up variables depending on billing/shipping
            // $selected = selected option in address list
            // $address1 = address line 1 id
            // $address2 = address line 2 id
            // $city = city id
            // $county = UK county id (if used) - if not comment out.
            if ($type == 'billing') {
                $selected = $('#address_list option:selected');
                $address1 = $('#address1');
                $address2 = $('#address2');
                $city = $('#city');
                $county = $('#billing-county option');
            } else if ($type == 'shipping') {
                $selected = $('#shipping_address_list option:selected');
                $address1 = $('#shipping-address1');
                $address2 = $('#shipping-address2');
                $city = $('#shipping-city');
                $county = $('#shipping-county option');
            }
            
            // get selected values inner text
            $seltext = $selected.text();
            
            // split the selected text into an array of parts we can use
            var address_lines = $seltext.split(", ");
            
            // set input boxes values to the values of the array
            $address1.val(address_lines[0]);
        	$address2.val(address_lines[1]);
        	$city.val(address_lines[2]);
            
            // this deals with counties. Comment out if not needed.
            $searchcounty = address_lines[3].toLowerCase();
            $newcounty = $county.each(function () {
                if ($(this).text().toLowerCase().indexOf($searchcounty) > -1) {
                    $(this).attr('selected',true);
                }
            });
        }
        
        // for basket postcode on pressing enter.
        var  TextBox = $('#postcode-search');
        var code =null;
        // on keypress do this
        TextBox.keypress(function(e)
        {
            // get keycode
            code= (e.keyCode ? e.keyCode : e.which);
            // if keycode is 13 (enter)
            if (code == 13) {
                postcode_lookup('billing');
                e.preventDefault();
            }
        });
        
        var  TextBox = $('#shipping-postcode');
        var code =null;
        // on keypress do this
        TextBox.keypress(function(e)
        {
            // get keycode
            code= (e.keyCode ? e.keyCode : e.which);
            // if keycode is 13 (enter)
            if (code == 13) {
                postcode_lookup('shipping');
                e.preventDefault();
            }
        });
		
	});
	
})(jQuery);

function show_basket()
{

 fade = 0;

 document.getElementById('popoutbasket').style.display = 'none';

 document.getElementById('popoutbasket').style.opacity = '0';

 document.getElementById('popoutbasket').style.filter = 'alpha(opacity=0)';

 document.getElementById('popoutbasket').style.display = 'block';

 intervalID = setInterval(appear, 10);

}



function appear()

{

 document.getElementById('popoutbasket').style.opacity = fade/100;

 document.getElementById('popoutbasket').style.filter = 'alpha(opacity=' + fade + ')';

 fade+=2;

 if (fade==100)

 {

 clearInterval(intervalID);

 }

}



function close()

 {

 document.getElementById('popoutbasket').style.display = 'none';

 document.getElementById('popoutbasket').style.opacity = '0';

 document.getElementById('popoutbasket').style.filter = 'alpha(opacity=0)';

 }
