// JavaScript Document
var jsFile = "/scripts/jquery_gatracker.js";
document.write('<script type="text/javascript" src="' + jsFile + '"></script>'); 

$(document).ready(function(){
	$.gaTracker('UA-2247551-2'); //Prod
	//$.gaTracker('UA-8365840-1'); //Stage
	$(".contact-promo").hover(function() {
		 $("span.phone-number").slideDown(400);
	}, function() {
		 $("span.phone-number").slideUp(400);
	});
	
	$('.view-newsletters li a').click( function() {
        window.open( $(this).attr('href'), "popup1", "location=0,status=0,scrollbars=1,resizable=1,width=680,height=500" );
        return false;
    });
	
	createproductrollover('.view-featured-products .views-view-grid .product');
	createproducttabs('.product .product-content');
	createmediaplayer('.product .media');

});

function createproductrollover(parent) {
	$('h4', parent).hide();
	$('p', parent).hide();
	$('div.store-thumbs', parent).hide();

	$('body').prepend('<div class="popups" />');
	var popups = $('.popups');

  $(parent).each(function () {
    // options
    var distance = 10;
    var time = 250;
    var hideDelay = 0;
	var popupIndex = $(parent).index($(this));
    var hideDelayTimer = null;
	
	//Add popup
	var html = '<div class="popup promo" id="popup'+popupIndex+'"><div class="promo-header"><h3>'+$('h3', this).text()+'</h3><h4>'+$('h4', this).text()+'</h4></div><div class="promo-body"><p>'+$('p', this).text()+'</p>'+$('div.store-thumbs', this).html()+'</div></div>';
	$(popups).prepend(html);
    var popup = $('#popup'+popupIndex, popups);
	
    // tracker
    var beingShown = false;
    var shown = false;
    var trigger = $(this);

	//Set position
	var triggerPosition = $(this).offset();
	var popupHeight = $(popup).outerHeight();
	var popupLeft = triggerPosition.left + 100;
	var popupTop = triggerPosition.top - $(popup).outerHeight() + $(this).outerHeight();

	// Set position of popup box
	  //opacity: 0,
	 $(popup).css("display","none");
	 $(popup).css("left",popupLeft + "px");
	 $(popup).css("top",popupTop + "px");
	 
	/*$(popup).css({
	  left: popupLeft + 'px',
	  top: popupTop + 'px',
	  display: 'none',
	});*/

    // set the mouseover and mouseout on both element
    $([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;

		 $(popup).css("display","block");
		
        // (we're using chaining on the popup) now animate it's opacity and position
        /*.animate({
          opacity: 1
        }, time, 'swing', 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
        }, time, 'swing', 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);
    });
  });
  
}
function createproducttabs(parent) {
	$(parent).prepend('<div class="product-content-tabs"><ul></ul></div>');
	var tablist = $('div.product-content-tabs ul', parent);
	if (tablist.length) {
		var headers = $('h2', parent);
		$(headers, parent).each(function(index) {
			tablist.append('<li>' + $(this).text() + '</li>');
		});
		changetab (headers, 0);
	
		var tabs = $('li', tablist);
		setcurrent(tabs, tabs[0]);
		//Space Evenly
		overallwidth = $(tablist).parent().width()-(tabs.length*22);
		$(tabs).width(overallwidth/tabs.length + 'px');
	
		$(tabs).click(function() {
			var index = $(tabs).index(this);
			$(tabs)
			setcurrent(tabs, $(this));
			changetab(headers, index);
			return false;
		}); 
	}
}

function changetab (headers, index) {
	var currentheader = headers[index];
	$(currentheader).nextAll().show();
	$(currentheader).prevAll().andSelf().hide();
	if (index < headers.length-1) {
		var nextheader = headers[index + 1];
		$(nextheader).nextAll().andSelf().hide();
	}
}
function setcurrent(elements, element) {
	$(elements).removeClass('current');
	$(element).addClass('current');
}

function createmediaplayer(parent) {
	var thumbimages = $('.product-images ul li', parent)
	if (thumbimages.length) {
		var mediaplayerimages = $('.media-player div.image', parent)
		setmediaplayerimage(mediaplayerimages, 0, parent);
		$(thumbimages).click(function() {
			var index = $(thumbimages).index(this);
			setmediaplayerimage(mediaplayerimages, index, parent);
			return false;
		}); 
	
		var thumbvideos = $('.product-videos ul li', parent)
		var mediaplayervideos = $('.media-player div.video', parent)
		$(thumbvideos).click(function() {
			var index = $(thumbvideos).index(this);
			setmediaplayervideo(mediaplayervideos, index, parent);
			return false;
		}); 
	}
}

function setmediaplayerimage(mediaplayerimages, index, parent) {
	mediaplayerimage = $(mediaplayerimages[index]);
	hideallmediaplayer(parent);
	$(mediaplayerimage).show();
	$('a', mediaplayerimage).jqzoom();
}

function setmediaplayervideo(mediaplayervideos, index, parent) {
	mediaplayervideo = $(mediaplayervideos[index]);
	hideallmediaplayer(parent);
	$(mediaplayervideo).show();
	//Play
}

function hideallmediaplayer(parent) {
	var mediaplayeritems = $('.media-player div.image, .media-player div.video', parent)
	$(mediaplayeritems).hide();
}