// Stop jquery conflicting with magento scriptilicous
  var $j = jQuery.noConflict(); 
 // Use jQuery via $j(...)
 
  // Set up product page Tabs
 var currentTab = 0; // Set to a different number to start on a different tab.
 var hash = window.location.hash;
 
        if(hash == "#review-form") { 
            currentTab = 3; // Set to a different number to start on a different tab.
        }
	function openTab(clickedTab) {
	   var thisTab = $j(".tabbed-box .tabs a").index(clickedTab);
	   $j(".tabbed-box .tabs li a").removeClass("active");
	   $j(".tabbed-box .tabs li a:eq("+thisTab+")").addClass("active");
	   $j(".tabbed-box .tabbed-content").hide();
	   $j(".tabbed-box .tabbed-content:eq("+thisTab+")").show();
	   currentTab = thisTab;
	}
  
 $j(document).ready(function(){
		
 		//Product page tabbed box
	  	$j(".tabbed-box .tabs li a").click(function() { 
	      openTab($j(this)); return false; 
	   	});
	   	$j(".tabbed-box .tabs li a:eq("+currentTab+")").click();
     	
     	// Homepage rotating image
     	$j('#slideshow').cycle({ 
		    fx:    'fade', 
		    speed: 1000,
		    pause: 1,
			pager:  '.slideshowNav'			
		 });
		 
		 // Make full block clickable
		 $j(".money-back, .header-cart").click(function(){
		    	window.location=$j(this).find("a").attr("href");return false;
		 });
		
		 // Change Cursor
		$j('.facebook, .twitter, .money-back, .header-cart').hover(function() {
			$j(this).css('cursor','pointer');
			}, function() {
			$j(this).css('cursor','auto');
		}); 
	 
		// Filter accordian 
		// Accordian Trigger	
		$j('.block-layered-nav dt').click(function() {
				$j(this).next().slideToggle('normal');
				$j(this).toggleClass('closed');
				return false;
			}).next().hide();
		// Accordian Content	
		$j(".block-layered-nav dd").show();
		// Change Cursor
		$j('.block-layered-nav dt').hover(function() {
			$j(this).css('cursor','pointer');
			}, function() {
			$j(this).css('cursor','auto');
		}); 
		 
	
	    
	    // select only the first link in nav
	    $j("#nav li.level0 a:first-child").addClass("toplevel");
	    $j("#nav li.level1 a:first-child").removeClass("toplevel");
			
			$j(function()
			{
				// this initialises the demo scollpanes on the page.
				$j('#pane').jScrollPane({showArrows:true});		
			});
									
			$j(".howToOpen").fancybox({
				'width'				: '75%',
				'height'			: '75%',
				'autoScale'			: false,
				'transitionIn'		: 'none',
				'transitionOut'		: 'none',
				'type'				: 'iframe',
				'overlayShow'		: 'true',
				'overlayColor'		: '#000000',
				'overlayOpacity'	: '0.6'
			});
			
			$j("#contactForm").validate({
			  rules: {
			    captcha: "required",
			    captcha: {
			      equalTo: "#captchaCatcher"
			    }
			  }
			});
			
			$j.extend({
			  getUrlVars: function(){
			    var vars = [], hash;
			    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
			    for(var i = 0; i < hashes.length; i++)
			    {
			      hash = hashes[i].split('=');
			      vars.push(hash[0]);
			      vars[hash[0]] = hash[1];
			    }
			    return vars;
			  },
			  getUrlVar: function(name){
			    return $j.getUrlVars()[name];
			  }
			});

			var byName = $j.getUrlVar('result');
			var byReason = $j.getUrlVar('reason');
			
			if(byName == "newslettersuccess"){
				loadNewsletterThanks();
			}

			if(byName == "success"){
				loadThanks();
			}

			if(byName == "fail"){
				loadFail();
			}
			
			if(byReason == "invalidemail"){
				loadEmail();
			}
			
			$j(function () {
			  $j('.bubbleInfo').each(function () {
			    // options
			    var distance = 10;
			    var time = 250;
			    var hideDelay = 50;

			    var hideDelayTimer = null;

			    // tracker
			    var beingShown = false;
			    var shown = false;

			    var trigger = $j('.trigger', this);
			    var popup = $j('.popup', this).css('opacity', 0);

			    // set the mouseover and mouseout on both element
			    $j([trigger.get(0), popup.get(0)]).mouseover(function () {
			      // stops the hide event if we move from the trigger to the popup element
			      if (hideDelayTimer) clearTimeout(hideDelayTimer);

			      // don't trigger the animation again if we're being shown, or already visible
			      if (beingShown || shown) {
			        return;
			      } else {
			        beingShown = true;

			        // reset position of popup box
			        popup.css({
			          display: 'block' // brings the popup back in to view
			        })

			        // (we're using chaining on the popup) now animate it's opacity and position
			        .animate({
			          opacity: 1
			        }, function() {
			          // once the animation is complete, set the tracker variables
			          beingShown = false;
			          shown = true;
			        });
			      }
			    }).mouseout(function () {
			      // reset the timer if we get fired again - avoids double animations
			      if (hideDelayTimer) clearTimeout(hideDelayTimer);

			      // store the timer so that it can be cleared in the mouseover if required
			      hideDelayTimer = setTimeout(function () {
			        hideDelayTimer = null;
			        popup.animate({
			          opacity: 0
			        }, function () {
			          // once the animate is complete, set the tracker variables
			          shown = false;
			          // hide the popup entirely after the effect (opacity alone doesn't do the job)
			          popup.css('display', 'none');
			        });
			      }, hideDelay);
			    });
			  });
			});

});
