/*************enhance input on #searchContainer #quickSearch input ***********/
	function enhanceInput(theInput) {
	if(!theInput.is('textarea')) {
		var orig = theInput.attr('value');			
	     theInput.focus(function() {
	        if($(this).val() == orig) {
	            $(this).val('');
	        } 
	     }).blur(function() {
	         if($(this).val() == "") {
	             $(this).val(orig);
	         }
	     });
	} else {
		var orig = theInput.html();
		 theInput.focus(function() {
	        if($(this).html() == orig) {
	            $(this).html('');
	        } 
	     }).blur(function() {
	         if($(this).html() == "") {
	             $(this).html(orig);
	         }
	     });
	}

}
/****************************************** plugins ***************************************/

(function($){  
  
    $.fn.extend({   
          
        //pass the options variable to the function  
        simpleSlide: function(options) {  
  
  
            //Set the default values, use comma to separate the settings, example:  
            var defaults = {  
                fadeSpeed: 1500,
				rotateSpeed: 4000
            }  
            // merges options that are passed in with the defaults (wooo!)
            var options =  $.extend(defaults, options);  
  			// for each element that the plugin is called on...
            return this.each(function() {  
	
              	var o = options;                
                var slideLength;
				var slideScratch;
				var $imageWrap = $(this);                  
				var $mainImages = $imageWrap.children('img');

				function init() {
					slideLength = $mainImages.length;
					slideScratch = slideLength -1;
					fadeIt();
				}

				function fadeIt() {
					setTimeout(function(){
						if(slideScratch == 0) {
							slideScratch = slideLength -1;
							$mainImages.eq(slideScratch).fadeIn(o.fadeSpeed, function() {
								$mainImages.show();

							});
						} else {
							$mainImages.eq(slideScratch).fadeOut(o.fadeSpeed);
							slideScratch --;
						}

						fadeIt();	
					}, o.rotateSpeed);
				}
				// initialise the slideshow
				init(); 
              
            });  
        }  
    });  
      
})(jQuery);


$(document).ready(function(){
	
    // loads rollover script
    SwapImage.rollover.init();
	enhanceInput($('#searchContainer #quickSearch input'));

	/************************* DOM ELEMENTS *********************/
	theDOM = {
	    sameAddressCheckbox: $('#BillingAddressTheSame'),
		billingHideBox: $('#billingHide')
	}
	
	$.ie6hover();
		
	/************************* HANDLERS *********************/


	if($('#home').length) {
		$('#mainImage').simpleSlide({rotateSpeed: 4500});
	}
	
	if($('#checkout').length) {
		billingAddress.init();
		$('#BillingAddressTheSame').change(function() {
			billingAddress.billingSlide();
		});

		$('#Address').change(copyToBillingDetails);
		$('#Town').change(copyToBillingDetails);
		$('#Postcode').change(copyToBillingDetails);
		$('#completeSafe').hide();
		
		$('#completePurchase').click(function() {
			$('#completeSafe').show();
		});
	}
	
	if($('#shoppingBasket').length) {
		$('#shippingCost select').change(function(){
			$(this).closest("form").submit();
		});
	}

});

billingAddress = function() {
	function init() {
		if(theDOM.sameAddressCheckbox.attr('checked') == true) {
			$('#billingHide').hide();
		}
	}
	
	function billingSlide() {
		if(theDOM.sameAddressCheckbox.attr('checked') == false) {
			theDOM.billingHideBox.slideDown('fast');
		} else {
		    theDOM.billingHideBox.slideUp('fast');
		    $('#BillingAddress').val($('#Address').val());
		    $('#BillingTown').val($('#Town').val());
		    $('#BillingPostcode').val($('#Postcode').val());
		}
	}
	
	return {
		init: init,
		billingSlide: billingSlide
	}
} ();

function copyToBillingDetails() {
    if (theDOM.sameAddressCheckbox.attr('checked') == true) {
        $('#BillingAddress').val($('#Address').val());
        $('#BillingTown').val($('#Town').val());
        $('#BillingPostcode').val($('#Postcode').val());
    }
}

// image rollovers

SwapImage = {};

SwapImage.rollover =
{
   init: function()
   {
      this.preload();

      $('.hover').css('cursor', 'pointer').hover(function () { 
          $(this).attr( 'src', SwapImage.rollover.newimage($(this).attr('src')) ); 
      }, function () { 
          $(this).attr( 'src', SwapImage.rollover.oldimage($(this).attr('src')) ); 
      });
   },

   preload: function()
   {
      $(window).bind('load', function() {
         $('.hover').each( function( key, elm ) { $('<img>').attr( 'src', SwapImage.rollover.newimage( $(this).attr('src') ) ); });
      });
   },

   newimage: function( src ) { return src.replace(/\.(\w{3})/g, "Hover.$1"); },
   oldimage: function( src ){ return src.replace(/Hover\.(\w{3})/g, ".$1"); }
};
 

// fonts we're using

var baker = {
   src: '/assets/swf/bakerSignet.swf',
   selectable: true
 };
 


//activate them

sIFR.activate(baker);

//target elements to replace



sIFR.replace(baker, {
   selector: 'h1, #homeLeft h2, #innerLeft h3,#innerLeft h2, #innerRight h3, #inner #remainingCats h2, .padLeft h1, #topProducts h2, #mainProducts h3'
   ,css: [
     '.sIFR-root { color: #008b0d; }'
     
   ],
   wmode: 'transparent'
});



/*FANCY BOX EXAMPLE

if ($('.imgRight').length > 0) {
    $("a.fancy").fancybox({
        'zoomSpeedIn': 200,
        'zoomSpeedOut': 200,
        'overlayShow': true,
        'overlayOpacity': 0
    });
}*/
