/*********************
//* jQuery Multi Level CSS Menu #2- By Dynamic Drive: http://www.dynamicdrive.com/
//* Last update: Nov 7th, 08': Limit # of queued animations to minmize animation stuttering
//* Menu avaiable at DD CSS Library: http://www.dynamicdrive.com/style/
*********************/

//Specify full URL to down and right arrow images (23 is padding-right to add to top level LIs with drop downs):
var detect = navigator.userAgent.toLowerCase();
var OS,browser,version,total,thestring;
if (checkIt('chrome')) {
	document.write('<link href="kc-default-chrome.css" rel="stylesheet" type="text/css">');
}

function checkIt(string)
{
	place = detect.indexOf(string) + 1;
	thestring = string;
	return place;
}  

var arrowimages={down:['downarrowclass', 'kc-default-images/blank.gif', 3], right:['rightarrowclass', 'kc-default-images/blank.gif']}

var jqueryslidemenu={

animateduration: {over: 200, out: 100}, //duration of slide in/ out animation, in milliseconds

buildmenu:function(menuid, arrowsvar){
	jQuery(document).ready(function($){
		$("a[rel^='lightbox']").prettyPhoto();// Select all links that contains lightbox in the attribute rel
		$("img[class^='kCommerce_border']").each(function (i) {
				var imgwidth = $(this).width();
				var newwidth = $(this).css("width");
				if (imgwidth == 0) {
					imgwidth = parseInt(newwidth);
				}
				var divwidth = imgwidth+20;
				var float = $(this).css("float");
				var marginright = $(this).css("margin-right");
				var marginleft = $(this).css("margin-left");
				var alignment = $(this).attr("align");
				var space = $(this).attr("hspace");
				$(this).css({'margin-right' : '0px'}); 
				$(this).css({'margin-left' : '0px'}); 
				$(this).css({'float' : 'none'});  
				if (alignment) {
					var float = $(this).attr("align");
					$(this).attr({align: ""});
				}
				if (space) {
					var marginright = $(this).attr("hspace")+'px';
					var marginleft = $(this).attr("hspace")+'px';
					$(this).attr({hspace: ""});
				}
				$(this).wrap('<div class="kcframe1" style="float:'+float+'; width:'+divwidth+'px; margin-left:'+marginleft+'; margin-right:'+marginright+';"></div>');
				$(this).before('<span class="kcframe2"></span><span class="kcframe3" style="width:'+imgwidth+'px"></span><span class="kcframe4"></span>');
				$(this).after('<span class="kcframe7"></span><span class="kcframe8" style="width:'+imgwidth+'px"></span><span class="kcframe9"></span>');
				$(this).wrap('<span class="kcframe5"><span class="kcframe6" id="prod_image"></span></span>');
				
		}); 
		var $mainmenu=$("#"+menuid+">ul")
		var $headers=$mainmenu.find("ul").parent()
		$headers.each(function(i){
			var $curobj=$(this)
			var $subul=$(this).find('ul:eq(0)')
			this._dimensions={w:this.offsetWidth, h:this.offsetHeight, subulw:$subul.outerWidth(), subulh:$subul.outerHeight()}
			this.istopheader=$curobj.parents("ul").length==1? true : false
			$subul.css({top:this.istopheader? "0px" : 0})
			$curobj.children("a:eq(0)").css(this.istopheader? {paddingRight: arrowsvar.down[2]} : {}).append(
				'<img src="'+ (this.istopheader? arrowsvar.down[1] : arrowsvar.right[1])
				+'" class="' + (this.istopheader? arrowsvar.down[0] : arrowsvar.right[0])
				+ '" style="border:0;" />'
			)
			$curobj.hover(
				function(e){
					var $targetul=$(this).children("ul:eq(0)")
					this._offsets={left:$(this).offset().left, top:$(this).offset().top}
					var menuleft=this.istopheader? 0 : this._dimensions.w
					menuleft=(this._offsets.left+menuleft+this._dimensions.subulw>$(window).width())? (this.istopheader? -this._dimensions.subulw+this._dimensions.w : -this._dimensions.w) : menuleft
					if ($targetul.queue().length<=1) //if 1 or less queued animations
						$targetul.css({left:menuleft+"px", width:this._dimensions.subulw+'px'}).slideDown(jqueryslidemenu.animateduration.over)
				},
				function(e){
					var $targetul=$(this).children("ul:eq(0)")
					$targetul.slideUp(jqueryslidemenu.animateduration.out)
				}
			) //end hover
		}) //end $headers.each()
		$mainmenu.find("ul").css({display:'none', visibility:'visible'})
	}) //end document.ready
}
}

//build menu with ID="myslidemenu" on page:
jqueryslidemenu.buildmenu("slidemenu", arrowimages);



(function($) {
	$.fn.thumbPopup = function(options)
	{
		//Combine the passed in options with the default settings
		settings = jQuery.extend({
			popupId: "thumbPopup",
			popupCSS: {'border': '7px solid #000000', 'background': '#000000', 'color': '#ffffff', 'font-size': '12px', 'text-align': 'left'},
			imgSmallFlag: "_t",
			imgLargeFlag: "_l",
			cursorTopOffset: 0,
			cursorLeftOffset: 155,
			loadingHtml: "<span style='padding: 5px;'>Loading</span>"
		}, options);
		var thetitle = $(this).attr("alt");
		//Create our popup element
		popup =
		$("<div />")
		.css(settings.popupCSS)
		.attr("id", settings.popupId)
		.css("position", "absolute")
		.appendTo("div[class*='inner']").hide();
		
		//Attach hover events that manage the popup
		$(this)
		.hover(setPopup)
		.mousemove(updatePopupPosition)
		.mouseout(hidePopup);
		
		function setPopup(event)
		{
			var fullImgURL = $(this).attr("src").replace(settings.imgSmallFlag, settings.imgLargeFlag);
			
			$(this).data("hovered", true);
			
			pos = $(this).offset();
			pospos = $(this).position();
			ItsWidth = $(this).outerWidth();
			ItsHeight = $(this).outerHeight();
			posposright = pospos.left+ItsWidth;
			posright = pos.left+ItsWidth;
			posposbottom = pospos.top+ItsHeight;
			//Load full image in popup
			$("<img>")
			.bind("load", {thumbImage: this}, function(event)
			{
				//Only display the larger image if the thumbnail is still being hovered
				if ($(event.data.thumbImage).data("hovered") == true) {
					$(popup).empty().append(this);
					updatePopupPosition(event);
					$(popup).append('<br><strong>'+thetitle+'</strong>');
					$(popup).show();
				}
				$(event.data.thumbImage).data("cached", true);
			})
			.attr("src", fullImgURL);
			//If no image has been loaded yet then place a loading message
			if ($(this).data("cached") != true) {
				$(popup).append($(settings.loadingHtml));
				$(popup).show();
			}
			
			updatePopupPosition(event);			
		}
		
		function updatePopupPosition(event)
		{
			var windowSize = getWindowSize();
			var popupSize = getPopupSize();
			var mywindowsize = windowSize.height + windowSize.scrollTop; 
			var mypopupleft = popupSize.height;
			//$("div[class*='testfield']").text(mywindowsize +" - "+ mypopupleft+", "+ posright+", ");
			if (windowSize.width + windowSize.scrollLeft < posright + popupSize.width + 15){
				$(popup).css("left", pospos.left - popupSize.width-22);
			} else {
				$(popup).css("left", posposright + 15);
			}
			if (windowSize.height + windowSize.scrollTop < pospos.top + popupSize.height){
				$(popup).css("top", posposbottom - popupSize.height);
			} else {
				$(popup).css("top", pospos.top);
			}
		}
		
		function hidePopup(event)
		{
			$(this).data("hovered", false);
			$(popup).empty().hide();
		}
		
		function getWindowSize() {
			return {
				scrollLeft: $(window).scrollLeft(),
				scrollTop: $(window).scrollTop(),
				width: $(window).width(),
				height: $(window).height()
			};
		}
		
		function getPopupSize() {
			return {
				width: $(popup).outerWidth(),
				height: $(popup).outerHeight()
			};
		}

		//Return original selection for chaining
		return this;
	};
})(jQuery);

$(function(){
	$("img[src*='-160-160.jpg']").thumbPopup({
		imgSmallFlag: "-160-160",
		imgLargeFlag: "-350-350"
	});
});





var addtocartReQ = getXmlHttpRequestObject();
function getXmlHttpRequestObject() {
    if (window.XMLHttpRequest) {
    	return new XMLHttpRequest();
    } else if(window.ActiveXObject) {
    	return new ActiveXObject("Microsoft.XMLHTTP");
    } 
}

function add_to_cart(number) {
	var the_info = '';
	for (i=1; i<=number; i++) {
		the_info += '&price'+i+'='+document.getElementById("price_"+i).value+'&size'+i+'='+document.getElementById("size2_"+i).value+'&qty'+i+'='+document.getElementById("qty_"+i).value;
	}
	if (addtocartReQ.readyState == 4 || addtocartReQ.readyState == 0) {
		day = new Date();
		id = day.getTime();
		addtocartReQ.open("GET", 'kCommerce/includes/cart.php?addtocart='+document.getElementById('filename').value+'&file='+escape(document.getElementById('imgfile').value)+'&number='+number+the_info+'&ajaxed=1&time='+id, true);
		addtocartReQ.onreadystatechange = added_to_cart; 
		addtocartReQ.send(null);
	}			
}

function added_to_cart() {
	if (addtocartReQ.readyState == 4) {
		if (addtocartReQ.responseText) {
			added_bits = addtocartReQ.responseText.split('|');
			document.getElementById("mini_cart").innerHTML = added_bits[3];
		} else {
			alert("AHHHHH!");
		}
	}
}





  $(document).ready(function() {
	$(".atc").live("click", function(){
		var prodid=$(this).attr('rel');
		var oktogo='';
		var dontgoagain='';

	day = new Date();
		thetime = day.getTime();
		for (i=1; i<=parseInt(prodid); i++) {
	if (document.getElementById("qty_"+i).value) {
		if (isFinite(document.getElementById("qty_"+i).value)) {
			main_unerrorfield('qty_'+i, thetime);
		oktogo = 1;
		}  else {
			dontgoagain = 1;
			main_errorfield('qty_'+i, '<strong>Not a Number</strong><br>The value entered is not a number', thetime);
		}
	}
	}
	if (oktogo && !dontgoagain) {
		var animate = 1;
		if (checkIt('msie')) {
if (detect.charAt(place + thestring.length) < 7) {
	animate = '';
}}
if (animate == 1) {
	var productX 		= $("#prod_image").offset().left;
	var productY 		= $("#prod_image").offset().top;
	var basketX 		= $("#mini_cart").offset().left;
	var basketY 		= $("#mini_cart").offset().top;
	var gotoX 			= basketX - productX;
	var gotoY 			= basketY - productY;
	var newImageWidth 	= $("#prod_image").width() / 3;
	var newImageHeight	= $("#prod_image").height() / 3;
	$("#prod_image img")
	.clone()
	.prependTo("#prod_image")
	.css({'position' : 'absolute'})
	.animate({opacity: 0.4}, 100 )
	.animate({opacity: 0.0, marginLeft: gotoX, marginTop: gotoY, width: newImageWidth, height: newImageHeight}, 500);
}
$(this).text('View Your Basket >');
$(this).removeClass();
$(this).addClass('cart_link_new');

	add_to_cart(prodid);
	} else {
		if (!dontgoagain) {
		main_errorfield('qty_1', '<strong>No QTY</strong><br>Please enter at least 1 qty next to <br> size you wish to purchase', thetime);
		}
	}
    });
	$(".cart_link_new").live("click", function(){
		location = 'cart.htm';
    });

});
  function dn() {
  }
  
  
  
  function update_qty(qtybox,qty,prodid)
{
var oldVal = qtybox.value;
	setTimeout(checkIfDone, 750);
	
	function checkIfDone()
	{
		var newVal = qtybox.value;
		if(newVal == oldVal)
		{
			checkstock(qtybox,qty,prodid)
		}
	}
}




function checkstock(qtybox,qty,prodid) {
	day = new Date();
		thetime = day.getTime(); 
	if (parseInt(qtybox.value) > parseInt(qty)) {
		
		main_errorfield('qty_box_'+prodid, '<strong>Stock Warning</strong><br>This item only has '+qty+' in stock.<br>Your Qty has been altered to reflect this.', thetime);
		qtybox.value=qty;
	} else {
		main_unerrorfield('qty_box_'+prodid, thetime);
	}
}

function main_errorfield(elm,msg,thetime) {
			document.getElementById(elm).style.borderWidth = '3px';
			document.getElementById(elm).style.borderStyle = 'solid';
			document.getElementById(elm).style.borderColor = 'red';
			document.getElementById(elm).onmouseover = function(){tooltip.show(msg);};
			document.getElementById(elm).onmouseout = function(){tooltip.hide();};
			document.getElementById(elm).onkeypress = function(){tooltip.hide();};
}
	function main_unerrorfield(elm,thetime) {
			document.getElementById(elm).style.borderWidth = '';
			document.getElementById(elm).style.borderStyle = '';
			document.getElementById(elm).style.borderColor = '';
			document.getElementById(elm).onmouseover = function(){};
			document.getElementById(elm).onmouseout = function(){};
	}

function goToPage(page) {
location='?kCommerce_section2=setpage&p='+page+'&loc='+escape(location);
}