var $jq = jQuery.noConflict(); // This is for the shop

$jq(document).ready(function() {

	if(jQuery.isFunction(jQuery.fn.fancybox)){
		$jq(".enquire").fancybox({
				'width' : 700,
				'height' : 580,
				'autoScale' : false,
				'overlayShow' : true,
				'transitionIn' : 'elastic',
				'transitionOut' : 'elastic',
				'type' : 'iframe'
			});
	}
	
	$jq(".popup").click(function(){
		newwindow=window.open($jq(this).attr('href'),'Social','height=340,width=600');
		if (window.focus) {
			newwindow.focus();
		}
		return false;
	});
	
	if(jQuery.isFunction(jQuery.fn.selectbox)){
		$jq("#searchselect").selectbox();
	}
	
	var height = $jq("#productmore").height();
	$jq("#productmore").hide().css({ height : 0 });
	
	/* ############# NAVIGATION FUDGES THAT CAN'T BE DONE IN CSS ############# */
	
	$jq("#hardwarelinks li").first().addClass('active');
	
	$jq("#hardwarelinks li a.sidelink").hover(
		function () {
			$jq("#hardwarelinks li").removeClass('active');
			$jq(this).closest("li").addClass('active');
		},
		function () {
			//$jq("#hardwarelinks li").removeClass('active');
			//$jq("#hardwarelinks li").first().addClass('active');
		});
		
	$jq("#hardwarelinks li a.safety").hover(function(){
		$jq("#hardwarelinks li").removeClass('active');
		$jq("#hardwarelinks li").first().addClass('active');
		
	});
	
	$jq(".navedge").hover(function(){ // Top and bottom navigation safety net
		$jq("#hardwarelinks li").removeClass('active');
		$jq("#hardwarelinks li").first().addClass('active');
		
	});
	
	$jq(".navleft").hover(function(){ // Left navigation safety net
		$jq("#hardwarelinks li").removeClass('active');
		$jq("#hardwarelinks li").first().addClass('active');
		
	});
	
	$jq(".navright").hover(function(){ // Right navigation safety net
		$jq("#hardwarelinks li").removeClass('active');
		$jq("#hardwarelinks li").first().addClass('active');
		
	});

	
	$jq("#logo").live("hover", function(){ // anytime the top level is hovered
		if($jq(".dropdown_4columns").css("top").replace("px","") < 500){ // we check to see the position of the large menu
			//$jq(".side2").css("top", "-12px"); // and move them to the right places
			//$jq(".side3").css("top", "-999em");
			//$jq(".side148").css("top", "-999em");
		}
	});
	
	
	/*
	$jq('#menu li').hover(function() {
      $jq(this).addClass('ie6hover');
	  $jq(this+" a").addClass('ie6hover');
    }, function() {
      $jq(this).removeClass('ie6hover');
	  $jq(this+" a").removeClass('ie6hover');
    });
*/

	
	/* ############# HEADER ANIMATION ############# */
	
	$jq("#icons img").click(function(){
		$jq("#graphic div.image:eq(" + j + ")").stop().fadeOut();
		j = $jq(this).attr("class")-1;
	});

	var j = 0;
	var jmax = $jq("#graphic div.image").length -1;
	if(jmax != 0){
		$jq("#graphic div.image").hide();
		function cycleThruHeader(){
			$jq("#icons img").attr("src", "/assets/images/structure/animationIcon.png");
			$jq("#icons img:eq(" + j + ")").attr("src", "/assets/images/structure/animationIconActive.png");
	
	$jq("#graphic div.image:eq(" + j + ")")
				.fadeIn(500)
				.delay(6000)
				.fadeOut(500, function(){
					(j == jmax) ? j=0 : j++;
					cycleThruHeader();
				});
			};
		cycleThruHeader();
	}
	
	/* ############# PRODUCT IMAGE ZOOM  ############# */
	
	if(jQuery.isFunction(jQuery.fn.addpowerzoom)){
		$jq("#zoomimage").addpowerzoom({
			defaultpower: 2,
			magnifiersize:[100, 100],
			largeimage: $jq("#zoomimage").attr('alt')
		});
	}

	/* ############# ACCORDION ############# */
	
	if(jQuery.isFunction(jQuery.fn.accordion)){
		$jq("#accordion").accordion({
			autoHeight: false
		});
	}
	
	/* ############# TABS ############# */
	
	// Set heights
	var maxHeight = $jq("#team").height(); // The very minimum is the height of the team box
	$jq('.tabheight').each(function() {
		maxHeight = Math.max(maxHeight, $jq(this).height());
	}).css('height',maxHeight); // Then if any boxes are taller we expanf them all to that
	
	// Start tabs
	$jq(".tab_content").hide();
	$jq("ul.tabs li:nth-child(2)").addClass("active").show();
	$jq(".tab_content:nth-child(2)").show();
	
	// Click tabs
	$jq("ul.tabs li").click(function() {
		$jq("ul.tabs li").removeClass("active");
		$jq(this).addClass("active");
		$jq(".tab_content").fadeOut(1); // Better than hide(); which makes the page jump
		var activeTab = $jq(this).find("a").attr("href");
		$jq(activeTab).fadeIn();
		return false;
	});
	
	/* ############# PRODUCT MORE CONTENT ############# */
	
	$jq("#prmore").click(function(){
		if ($jq("#productmore").is(':visible') ) {
			$jq("#prmore").text("more");
			$jq("#productmore").animate({ height: 0 }, { duration: 500, complete: function () {
				$jq("#productmore").hide();
			  } 
			});
		  } else {
			$jq("#prmore").text("less");
			$jq("#productmore").show().animate({ height : height }, { duration: 500 });
		  }
	  return false;
	});
	
	/* ############# PRODUCT MORE CONTENT ES ############# */
	
		$jq("#prmorees").click(function(){
		if ($jq("#productmore").is(':visible') ) {
			$jq("#prmorees").text("mas");
			$jq("#productmore").animate({ height: 0 }, { duration: 500, complete: function () {
				$jq("#productmore").hide();
			  } 
			});
		  } else {
			$jq("#prmorees").text("menos");
			$jq("#productmore").show().animate({ height : height }, { duration: 500 });
		  }
	  return false;
	});
	
	
	/* ############# SUBMIT STOCK FORM ############# */

	$jq("#stockform").submit(function(){
		var fail = false;
		var keepheight = $jq("#stockform").height();
		$jq('.required').each(function(index) {
			$jq(this).removeClass("isrequired");
			if(!$jq(this).val()){
				$jq(this).addClass("isrequired");
				fail = true;
			}
		});
	
		if(!fail==true){
			$jq.post($jq("#stockform").attr('action'), $jq("#stockform").serialize(),
				function(data) {
				$jq("#stockform").fadeOut(function(){
					$jq("#stockform").html(data);
					$jq("#stockform").height(keepheight)
					$jq("#stockform").fadeIn();
				});
			});
		}
		return false;
	});
	
	/* ############# SHOP MENU ############# */
	
	$jq("#selectedshopoption a").click(function(){
		$jq("#shopmenu").show();
		return false;		
	});
	
	$jq("body").click(function(){
		$jq("#shopmenu").hide();
	});
	
			
});



