function externalLinks(){if(!document.getElementsByTagName){return }var C=document.getElementsByTagName("a");for(var B=0;B<C.length;B++){var A=C[B];if(A.getAttribute("href")&&A.getAttribute("rel")=="external"){A.target="_blank"}}}$(document).ready(function () { externalLinks() });

$(function(){	
	$('#header-nav li').hover(function(){
		$(this).addClass('over');
		}, function() {
		$(this).removeClass('over');
	});
	//$('#search-category').selectbox();
	if ($.browser.msie) {
		$("#search-category")
			.mouseover(function(){
				$(this)
				.data("origWidth", $(this).css("width"))
					.css("width", "auto");
			})
	
			.blur(function(){
				$(this).css("width", $(this).data("origWidth"));
			})
			
			.change(function(){
				$(this).css("width", $(this).data("origWidth"));
			})
			
	}
});

function switchProductTab(){
	///$('ul#product-detail-tabs li').removeClass("pd-current-tab");
	///$.post("inc/gettabcontent.php","towhat="+towhat+"&product="+productid+"&area="+area,function(data){$('#content-switcher').html(data);});
	///$('#content-switcher').load($(this).attr('src')+'#content-switcher');
	
}

$(function(){
	$('a.tab').click(function(e){
		e.preventDefault();
		$('ul#product-detail-tabs li').removeClass('pd-current-tab');
		$(this).parent().toggleClass('pd-current-tab');
		$('#content-switcher').html('<p>Loading...</p>');
		var data = $(this).attr('rel').split("|");
		
		$.get("inc/product-"+data[0]+".php", {'id' : data[1],'area' : data[2]}, function(data){
																						 $('#content-switcher').html(data);
																						 })
		
		//return false;
	});		   
});

function getDeliveryCosts(country){
	document.getElementById('Country').selectedIndex = document.getElementById('selCountry').selectedIndex;
	$.post("inc/getdelivery.php","country="+country,function(data){
			var returnedarray = data.split("|||");
			$('#deliveryswitcher').html(returnedarray[0]);
			$('#checkprice').html(returnedarray[1]+" Delivery");
			$('#spantotal').html(Math.round((parseFloat($('#makeprice').html())+parseFloat(returnedarray[1]))*100)/100);
			$('#email-submit').remove();
	});
}

function switchDeliveryBand(bandid){
	$.post("inc/switchband.php","band="+bandid, function(data){
			var returnedarray = data.split("|||");
			$('#checkprice').html(data+" Delivery");
			$('#spantotal').html(Math.round((parseFloat($('#makeprice').html())+parseFloat(data))*100)/100);
			$('#email-submit').remove();
	});
}

function checkCartForm(){
	var fail = false;
	$('#cart-form :input').each(function(){
		$(this).removeClass('fail');	 
		if(this.id!="Address2"&&this.id!="BillingAddress2"&&this.id!="newpassword"&&this.id!="newconfirmpassword"){
			if(this.value==""){
				$(this).addClass('fail');
				fail = true;
			}
		}
	});
	
	if(fail==true){
		alert("Some fields are required and must be filled out before you can continue.");
		return false;
	}else{
		if(document.getElementById('newpassword').value!=document.getElementById('newconfirmpassword').value){
			alert("The password you entered does not match the confirmation.");
			
			$('#newpassword,#newconfirmpassword').addClass("fail");
			return false;
		}else{
			return true;
		}
	}
}

function switchImage(image,title){
	$('#ichange').html('<a href="thumb.php?src=i/'+image+'&amp;w=550&amp;h=&amp;zc=0" rel="lightbox" class="currentimage" title="'+title+'"><img id="currentimage" src="thumb.php?src=i/'+image+'&amp;w=260&amp;h=&amp;zc=0" alt="'+title+'" /></a>');
	$("a[rel^='lightbox']").prettyPhoto();
}

$(function () {
  $('.sales-product-holder').each(function () {
    // options
    var distance = 10;
    var time = 250;
    var hideDelay = 500;

    var hideDelayTimer = null;

    // tracker
    var beingShown = false;
    var shown = false;
    
    var trigger = $('.trigger', this);
    var popup = $('.popup', this).css('opacity', 0);

    // 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;

        // reset position of popup box
        popup.css({
          top: -100,
          left: -33,
          display: 'block' // brings the popup back in to view
        })

        // (we're using chaining on the popup) now animate it's opacity and position
        .animate({
          top: '-=' + distance + 'px',
          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({
          top: '-=' + distance + 'px',
          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 expand(linkid,mode){
	$('#expand-'+linkid).toggle();
	$.post("inc/expander.php",{ expand: linkid, mode: mode },function(){if(mode=="all"){$('#link-'+linkid).removeClass('arrow-closed').addClass('arrow-open');$('#expand-'+linkid).show();}else{$('#link-'+linkid).toggleClass("arrow-closed").toggleClass("arrow-open");}});
}

function expandAll(){
	var ea = "";
	$('.filter-icon').each(function(){
		ea = this.id.replace("link-","");
		expand(ea,"all");
	});
}

jQuery.ajaxq = function (queue, options)
{
	// Initialize storage for request queues if it's not initialized yet
	if (typeof document.ajaxq == "undefined") document.ajaxq = {q:{}, r:null};

	// Initialize current queue if it's not initialized yet
	if (typeof document.ajaxq.q[queue] == "undefined") document.ajaxq.q[queue] = [];
	
	if (typeof options != "undefined") // Request settings are given, enqueue the new request
	{
		// Copy the original options, because options.complete is going to be overridden

		var optionsCopy = {};
		for (var o in options) optionsCopy[o] = options[o];
		options = optionsCopy;
		
		// Override the original callback

		var originalCompleteCallback = options.complete;

		options.complete = function (request, status)
		{
			// Dequeue the current request
			document.ajaxq.q[queue].shift ();
			document.ajaxq.r = null;
			
			// Run the original callback
			if (originalCompleteCallback) originalCompleteCallback (request, status);

			// Run the next request from the queue
			if (document.ajaxq.q[queue].length > 0){
				$('#search-keyword').addClass('input-loading');
				document.ajaxq.r = jQuery.ajax (document.ajaxq.q[queue][0]);
			}
		};

		// Enqueue the request
		document.ajaxq.q[queue].push (options);

		// Also, if no request is currently running, start it
		if (document.ajaxq.q[queue].length == 1){
			$('#search-keyword').addClass('input-loading');
			document.ajaxq.r = jQuery.ajax (options);
		}
	}
	else // No request settings are given, stop current request and clear the queue
	{
		if (document.ajaxq.r)
		{
			document.ajaxq.r.abort ();
			document.ajaxq.r = null;
		}

		document.ajaxq.q[queue] = [];
	}
}


function searchSuggest(type,noprocess){
	var keyword = $('#search-keyword').val();
	// alert(keyword);
	var category = $('#search-category').val();
	if(type=="sales"){
		var snew = $('#header-form-new').val();
		var sused = $('#header-form-used').val();
	}
	if(noprocess=="process"){
		$('#search-dropdown').show();
	}else{
		$('#search-dropdown').show();
		if(keyword.length > 2 && noprocess!='process'){
			$.ajaxq("searchqueue");
			$.ajaxq ("searchqueue", {
				url: "inc/search-suggest.php",
				type: 'post',
				cache: false,
				data: ({ "type" : type, "keyword" : keyword, "isnew" : snew, "isused" : sused, "category" : category }),
				success: function(data)
				{
					$('#search-keyword').removeClass('input-loading');
					$('#search-dropdown').html(data);
				}
			});
			$("body").click(function(e){
				if(!$(e.target).closest('#search-keyword').length) {
					$('#search-dropdown').hide();
					return true;
				}
			});
		}else{
			$('#search-keyword').removeClass('input-loading');
			$('#search-dropdown').hide();
		}
	}
}
