/* -----------------------------------------------------------------
	JQUERY CUSTOM FUNCTION -- SLIDE TOGGLE
----------------------------------------------------------------- */
jQuery.fn.slideToggle = function(speed, easing, callback) {
	return this.animate({width: 'toggle',opacity: 'toggle'}, speed, easing, callback);  
};
/* -----------------------------------------------------------------
	JQUERY CUSTOM FUNCTION -- SLIDE IN
----------------------------------------------------------------- */
jQuery.fn.slideIn = function(speed, easing, callback) {
	return this.animate({width: 'show',opacity: 'show'}, speed, easing, callback);  
};
/* -----------------------------------------------------------------
	JQUERY CUSTOM FUNCTION -- SLIDE OUT
----------------------------------------------------------------- */
jQuery.fn.slideOut = function(speed, easing, callback) {
	return this.animate({width: 'hide',opacity: 'hide'}, speed, easing, callback);  
};
/* -----------------------------------------------------------------
	JQUERY CUSTOM FUNCTION -- BANNER
----------------------------------------------------------------- */
jQuery.fn.banner = function(vars) {

	var element     = this;
	var pauseSeconds     = (vars.pauseSeconds*1000);
	var transitionSeconds     = (vars.transitionSeconds*1000);
	var current     = null;
	var timeOutFn   = null;
	var faderStat   = true;
	var mOver       = false;
	var items       = $("#" + element[0].id + "Content ." + element[0].id + "Image");
	var itemsDiv   = $("#" + element[0].id + "Content ." + element[0].id + "Image div");
	
	var fadeElement = function(isMouseOut) {
		var thisTimeOut = (isMouseOut) ? (pauseSeconds) : pauseSeconds;
		thisTimeOut = (faderStat) ? 10 : thisTimeOut;
		if(items.length > 0) {
			timeOutFn = setTimeout(makeSlider, thisTimeOut);
		}
	}
	
	var makeSlider = function() {
		current = (current != null) ? current : items[(items.length-1)];
		var currNo      = jQuery.inArray(current, items) + 1
		currNo = (currNo == items.length) ? 0 : (currNo - 1);
		var newMargin   = $(element).width() * currNo;
		if(faderStat == true) {
			$(items[currNo]).fadeIn((transitionSeconds));
			$(itemsDiv[currNo]).slideIn((transitionSeconds), function() {
				faderStat = false;
				current = items[currNo];
				if(!mOver) {
					fadeElement(false);
				}
			});
		} else {
			$(itemsDiv[currNo]).slideOut((transitionSeconds));
			$(items[currNo]).fadeOut((transitionSeconds), function() {
				faderStat = true;
				current = items[(currNo+1)];
				if(!mOver) {
					fadeElement(false);
				}
			});
		}
	}
	
	makeSlider();
	
};
/* -----------------------------------------------------------------
	JQUERY CUSTOM FUNCTION -- ANIMATING BACKGROUNDS
----------------------------------------------------------------- */
(function($) {
	$.extend($.fx.step,{
	    backgroundPosition: function(fx) {
            if (fx.state === 0 && typeof fx.end == 'string') {
                var start = $.curCSS(fx.elem,'backgroundPosition');
                start = toArray(start);
                fx.start = [start[0],start[2]];
                var end = toArray(fx.end);
                fx.end = [end[0],end[2]];
                fx.unit = [end[1],end[3]];
			}
            var nowPosX = [];
            nowPosX[0] = ((fx.end[0] - fx.start[0]) * fx.pos) + fx.start[0] + fx.unit[0];
            nowPosX[1] = ((fx.end[1] - fx.start[1]) * fx.pos) + fx.start[1] + fx.unit[1];           
            fx.elem.style.backgroundPosition = nowPosX[0]+' '+nowPosX[1];
            
           function toArray(strg){
               strg = strg.replace(/left|top/g,'0px');
               strg = strg.replace(/right|bottom/g,'100%');
               strg = strg.replace(/([0-9\.]+)(\s|\)|$)/g,"$1px$2");
               var res = strg.match(/(-?[0-9\.]+)(px|\%|em|pt)\s(-?[0-9\.]+)(px|\%|em|pt)/);
               return [parseFloat(res[1],10),res[2],parseFloat(res[3],10),res[4]];
           }
        }
	});
})(jQuery);
/* -----------------------------------------------------------------
	JQUERY GERENRL FUNCTIONS
----------------------------------------------------------------- */
jQuery(document).ready(function() {

	/* Enable banner */
	if(jQuery('#banner').html() !== null) {
	
		/* set BANNER */
		$("#banner").banner({pauseSeconds: 6, transitionSeconds: 1});
	
		/* set BANNER TAGLINE opacticy */
		$('.bannerImage').children('div').children('h2').css({opacity: 0.8});
	}

	/* Enable product thumbs */
	jQuery('#product-thumbs > li').each(function(i) {
		jQuery(this).bind('mouseover', function() {
			var src = jQuery('img', this).attr('src').replace(/-small/, '-medium');
			var href = jQuery('a', this).attr('rel');
			var alt = jQuery('img', this).attr('alt');
			var title = jQuery('a', this).attr('title');

			jQuery('#img-src').attr('src', src).attr('alt', alt);
			jQuery('#img-href').attr('href', href).attr('title', title);
		});
	});
	
	//active navigation
	jQuery('#menu_product li ul').css("display", "none");
	
	jQuery('#menu_product li.active ul').css("display", "block");
	
	jQuery('#menu_product li ul li.active').parent('ul').css("display", "block");

	/* set HOVER class */
	$("li").hover(function () { $(this).addClass('hover');}, function () {$(this).removeClass('hover');});
	
	/* set NAVIGATION opacticy */
	$('#navigation').css({opacity: 0.7});
	
	/* set ALPHA opacticy */
	$('.alpha').css({opacity: 0.5});
	
	/* set COPYRIGHT opacticy */
	$('#copyright').css({opacity: 0.6});
	
	/* set LINKS opacity */
	$('#footer ul.navigation li a').fadeTo("0", 0.6);
	$('#footer ul.navigation li a')
	.mouseover(function(){
		$(this).stop().fadeTo("500", 0.95);
	})
	.mouseout(function(){
		$(this).stop().fadeTo("500", 0.6);
	})
	
	/* set FOOTER LOGO opacticy */
	$('#footer-logo').fadeTo("0", 0.3);
	$("#footer-logo")
	.mouseover(function(){
		$(this).stop().fadeTo("500", 0.95);
	})
	.mouseout(function(){
		$(this).stop().fadeTo("500", 0.3);
	})
	
	/* set SITE BY opacticy */
	$('#site-by').fadeTo("0", 0.3);
	$('#site-by')
	.mouseover(function(){
		$(this).stop().fadeTo("500", 0.95);
	})
	.mouseout(function(){
		$(this).stop().fadeTo("500", 0.3);
	})
	
	/* set PRODUCT BROWSE HOVER opacticy */
	$('.product.browse a.product-image img')
	.mouseover(function(){
		$(this).stop().fadeTo("500", 0.00);
	})
	.mouseout(function(){
		$(this).stop().fadeTo("500", 1.0);
	})
					
	/* set left column FEATURED */
	$('a.single').css({backgroundPosition: "-311px -856px"});
	$('a.single img').css({top: "0"});
	$('a.single')
	.mouseover(function(){
		$(this).stop().animate({backgroundPosition:"(-311px -856px)"}, {duration:500});
		$('a.single img').stop().animate({top:"-60px"}, {duration:500});
	})
	.mouseout(function(){
		$(this).stop().animate({backgroundPosition:"(-311px -806px)"}, {duration:500});
		$('a.single img').stop().animate({top:"0"}, {duration:500});
	})
	$('a.all')
	.mouseover(function(){
		$(this).css({backgroundPosition: "-261px -856px"});
		$(this).stop().animate({backgroundPosition:"(-161px -856px)"}, {duration:500});
	})
	.mouseout(function(){
		$(this).css({backgroundPosition: "-261px -856px"});
		$(this).stop().animate({backgroundPosition:"(-161px -856px)"}, {duration:500});
	})
	
	/* set right column heading opacticy */
	$('#right-column h1').css({opacity: 0.99});
});
/* ----------------------------------------------------
ORDER FORM SCRIPTS - Generously provided by Ben
----------------------------------------------------- */
String.prototype.reverse = function(){
	splitext = this.split("");
	revertext = splitext.reverse();
	reversed = revertext.join("");
	return reversed;
}

function addProduct(model, quantity){
	var after;
	var i=($("#prod_count").val()*1)+1;
	$("#prod_count").val(i);
	if(i==2){ after="#product1"; } else { after="#product"+(i-1); }
	$("#product1").clone().insertAfter(after).css("display","none").attr("id","product"+i).children("h3").html("Product #" + i).parent().animate({'height': 'toggle', 'opacity': 'toggle','easing': 'linear'}, 'slow');
	/*if(1){ $("#footer").css("top",$("#product1").css("height")); };*/
	$("#product"+i).children().each(function () {
		$(this).children().each(function () {
			if($(this).attr('id')!=""){
				var id=$(this).attr('id');
				var val='';
				if(id.split("_")[0] == 'model' && model != undefined) {
					val = model;
				} else if(id.split("_")[0] == 'quantity' && quantity != undefined) {
					val = quantity;
				}
				id=id.split("_")[0] + "_" + i;
				//is input node
				$(this).attr('id',id);
				$(this).attr('name',id);
				$(this).val(val);
			} else {
				//is label node
				$(this).attr('for',$(this).next().attr('id').reverse().substr(1).reverse()+i);
			};
		});
	});
}

function delProduct(mylink){
	delid=$(mylink).attr('id').split("_")[1];
	if($("#prod_count").val()==1){
		alert("Must have at least one item!");
		return;
	}
	$("#product"+delid).animate({'height': 'toggle', 'opacity': 'toggle','easing': 'linear'}, 'slow','linear',function(){$(this).remove();} );
	for(var i=delid; i<=($("#prod_count").val()*1)+1; i++){
		if (i==delid){
			$("#product"+i).children("h3").html("Deleting Product...");
		}else{
			$("#product"+i).children("h3").html("Product #" + (i-1));


			$("#product"+i).children().each(function () {
				$(this).children().each(function () {
					if($(this).attr('id')!=""){
						var id=$(this).attr('id');
						id=id.split("_")[0] + "_" + (i-1);
						//is input node
						$(this).attr('id',id);
						$(this).attr('name',id);
					} else {
						//is label node
						$(this).attr('for',$(this).next().attr('id').reverse().substr(1).reverse()+i);
					};
				});
			});
			$("#product"+i).attr('id',"product"+(i-1));

		}
	}
	
	$("#prod_count").val(($("#prod_count").val()*1)-1);
}
