/*********************
//* 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">');
}

if (checkIt('msie')) {
if (detect.charAt(place + thestring.length) < 7) {
	document.write('<link href="kc-default-ie6.css" rel="stylesheet" type="text/css">');
}
if (detect.charAt(place + thestring.length) >= 7 && detect.charAt(place + thestring.length) < 8) {
	document.write('<link href="kc-default-ie7.css" rel="stylesheet" type="text/css">');
}
if (detect.charAt(place + thestring.length) >= 8) {
	document.write('<link href="kc-default-ie8.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', 13], 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($){
		/*	$("img.jqzoom").jqueryzoom({
				xzoom: 200, //zooming div default width(default width value is 200)
				yzoom: 200, //zooming div default width(default height value is 200)
				offset: 10 //zooming div default offset(default offset value is 10)
				//position: "right" //zooming div position(default position value is "right")
			});*/
		$("a[rel^='lightbox']").prettyPhoto();// Select all links that contains lightbox in the attribute rel
		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', top:'0px'}).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);
jqueryslidemenu.buildmenu("slidemenu2", arrowimages);
jqueryslidemenu.buildmenu("slidemenu3", arrowimages);
jqueryslidemenu.buildmenu("slidemenu4", arrowimages);



$("div.clickable").click(function(){    
		window.location = $(this).attr("url");
});


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

function add_to_cart(prodid) {
	if (addtocartReQ.readyState == 4 || addtocartReQ.readyState == 0) {
		day = new Date();
		id = day.getTime();
		addtocartReQ.open("GET", 'kCommerce/includes/cart.php?addtocart='+prodid+'&qty='+document.getElementById('qty_'+prodid).value+'&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("bb_"+added_bits[0]).innerHTML = added_bits[2];
			document.getElementById("bb_"+added_bits[0]).href = added_bits[1];
			document.getElementById("mini_cart").innerHTML = added_bits[3];
		}
	}
}

function add_option_to_cart(prodid,optname,current) {
	if (addotocartReQ.readyState == 4 || addotocartReQ.readyState == 0) {
		day = new Date();
		id = day.getTime();
		addotocartReQ.open("GET", 'kCommerce/includes/cart.php?addtocart='+prodid+'&product_option='+optname+'&qty='+document.getElementById('qty_'+prodid+'_'+current).value+'&ajaxed=1&time='+id, true);
		addotocartReQ.onreadystatechange = added_o_to_cart; 
		addotocartReQ.send(null);
	}			
}

function added_o_to_cart() {
	if (addotocartReQ.readyState == 4) {
		if (addotocartReQ.responseText) {
			added_bits = addotocartReQ.responseText.split('|');
			document.getElementById("bb_"+added_bits[0]).innerHTML = added_bits[2];
			document.getElementById("bb_"+added_bits[0]).href = added_bits[1];
			document.getElementById("mini_cart").innerHTML = added_bits[3];
		}
	}
}



  $(document).ready(function() {
	$(function(){$(document).pngFix();});
    $(".buy_this").click(function () {
	if ($(this).attr('href').indexOf('cart') == -1) {
	var prodid=$(this).attr('id').split('_');
	var bists=$(this).attr('rel').split('_');
	var prodid=prodid[1];
	var divname = 'atc_'+prodid;
	var stock=bists[0];
	var price=bists[1];;
	if (stock == 1) {
		var what = 'is';
	} else {
		var what = 'are';
	}

if (stock != '0') {	
	document.getElementById(divname).innerHTML ='<div class="closethis">Close</div><table width="240" border="0" cellspacing="5" cellpadding="0"><tr><td colspan="2" align="center">There '+what+' <span class="stock_fig">'+stock+'</span> available @ &pound;'+price+'</td></tr><tr><td width="69">Qty: <span id="qty_box_'+prodid+'"><input name="qty_'+prodid+'" type="text" id="qty_'+prodid+'" size="2" style="width:25px;" value=1 onKeyUp="update_qty(this,\''+stock+'\',\''+prodid+'\')"></span></td><td width="114"><div class="cart_button"><a href="javascript:dn()" rel="'+prodid+'" class="atc">Add to Cart &gt;</a></div></td></tr></table>';
} else {
	document.getElementById(divname).innerHTML ='<div class="closethis">Close</div><table width="240" border="0" cellspacing="5" cellpadding="0"><tr><td colspan="2" align="center">There '+what+' <span class="stock_fig">'+stock+'</span> available @ &pound;'+price+'</td></tr><tr><td width="69">Qty: <span id="qty_box_'+prodid+'"><input name="qty_'+prodid+'" type="text" id="qty_'+prodid+'" size="2" style="width:25px;" value=1 onKeyUp="update_qty(this,\''+stock+'\',\''+prodid+'\')" disabled></span></td><td width="114"><div class="cart_button_r"><a href="javascript:dn()" rel="'+prodid+'" style="cursor:normal;">Out of Stock</a></div></td></tr></table>';
}
		
		
	  $("#"+divname).slideDown('fast');
	}
      });
	

    $(".buy_this_o").click(function () {
	if ($(this).attr('href').indexOf('cart') == -1) {
	var content = '';
	var prodid=$(this).attr('id').split('_');
	var prodid=prodid[1];
	var divname = 'atc_'+prodid;
	var bits=$(this).attr('rel').split('|');
	var current = 1;
	for(option in bits) {
		if (bits[option]!='') {
	var obists=bits[option].split('_');
	var stock=obists[0];
	var price=obists[1];
	var name=obists[2];
	if (stock == 1) {
		var what = 'is';
	} else {
		var what = 'are';
	}
	content +='<tr><td colspan="2" align="center">There '+what+' <span class="stock_fig">'+stock+'</span> '+name+' available @ &pound;'+price+'</td></tr>';
	
if (stock != '0') {	
	content +='<tr><td width="69">Qty: <span id="qty_box_'+prodid+'_'+current+'"><input name="qty_'+prodid+'_'+current+'" type="text" id="qty_'+prodid+'_'+current+'" size="2" style="width:25px;" value=1 onKeyUp="update_qty(this,\''+stock+'\',\''+prodid+'_'+current+'\')"></span></td><td width="114"><div class="cart_button"><a href="javascript:dn()" rel="'+prodid+'_'+current+'|'+name+'" class="aotc">Add to Cart &gt;</a></div></td></tr>';
} else {
	content +='<tr><td width="69">Qty: <span id="qty_box_'+prodid+'_'+current+'"><input name="qty_'+prodid+'_'+current+'" type="text" id="qty_'+prodid+'_'+current+'" size="2" style="width:25px;" value=1 onKeyUp="update_qty(this,\''+stock+'\',\''+prodid+'_'+current+'\')" disabled></span></td><td width="114"><div class="cart_button_r"><a href="javascript:dn()" style="cursor:normal;">Out of Stock</a></div></td></tr>';
}
	current++;
	}}
		document.getElementById(divname).innerHTML = '<div class="closethis">Close</div><table width="240" border="0" cellspacing="5" cellpadding="0">'+content+'</table>';
	  $("#"+divname).slideDown('fast');
	}
      });


	
	
	$(".closethis").live("click", function(){
		$(this).parent().slideUp('fast');
    });
	$(".atc").live("click", function(){
		var prodid=$(this).attr('rel');
		var divname = 'atc_'+prodid;
		var animate = 1;
		$("#"+divname).slideUp('fast');
		if (checkIt('msie')) {
if (detect.charAt(place + thestring.length) < 7) {
	animate = '';
}}
if (animate == 1) {
	var productX 		= $("#prod_image_" + prodid).offset().left;
	var productY 		= $("#prod_image_" + prodid).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_" + prodid).width() / 3;
	var newImageHeight	= $("#prod_image_" + prodid).height() / 3;
	$("#prod_image_" + prodid + " img")
	.clone()
	.prependTo("#prod_image_" + prodid)
	.css({'position' : 'absolute'})
	.animate({opacity: 0.4}, 100 )
	.animate({opacity: 0.0, marginLeft: gotoX, marginTop: gotoY, width: newImageWidth, height: newImageHeight}, 500);
}
	add_to_cart(prodid);
    });
	$(".aotc").live("click", function(){
		var prodid=$(this).attr('rel').split('|');
		var name = prodid[1];
		var prodid = prodid[0];
		prodid=prodid.split('_');
		current = prodid[1];
		prodid = prodid[0];
		var divname = 'atc_'+prodid;
		var animate = 1;
		if (checkIt('msie')) {
		if (detect.charAt(place + thestring.length) < 7) {
		animate = '';
		}}
if (animate == 1) {
		$("#"+divname).slideUp('fast');
	var productX 		= $("#prod_image_" + prodid).offset().left;
	var productY 		= $("#prod_image_" + prodid).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_" + prodid).width() / 3;
	var newImageHeight	= $("#prod_image_" + prodid).height() / 3;
	$("#prod_image_" + prodid + " img")
	.clone()
	.prependTo("#prod_image_" + prodid)
	.css({'position' : 'absolute'})
	.animate({opacity: 0.4}, 100 )
	.animate({opacity: 0.0, marginLeft: gotoX, marginTop: gotoY, width: newImageWidth, height: newImageHeight}, 500);
}
	add_option_to_cart(prodid,name,current);
    });

});


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 update_search_color(mana, image) {

	if (document.getElementById('search_colour_B').checked === true && document.getElementById('search_colour_U').checked === true && document.getElementById('search_colour_G').checked === true && document.getElementById('search_colour_R').checked === true && document.getElementById('search_colour_W').checked === true && document.getElementById('search_colour_C').checked === true) {
	document.getElementById('search_colour_B').checked = false;
	document.getElementById('search_icon_B').src = 'kc-default-images/soff-mana-black.png';
	document.getElementById('search_colour_U').checked = false;
	document.getElementById('search_icon_U').src = 'kc-default-images/soff-mana-blue.png';
	document.getElementById('search_colour_G').checked = false;
	document.getElementById('search_icon_G').src = 'kc-default-images/soff-mana-green.png';
	document.getElementById('search_colour_R').checked = false;
	document.getElementById('search_icon_R').src = 'kc-default-images/soff-mana-red.png';
	document.getElementById('search_colour_W').checked = false;
	document.getElementById('search_icon_W').src = 'kc-default-images/soff-mana-white.png';
	document.getElementById('search_colour_C').checked = false;
	document.getElementById('search_icon_C').src = 'kc-default-images/soff-mana-none.png';
	}
	if (document.getElementById('search_colour_'+mana).checked === true) {
		document.getElementById('search_colour_'+mana).checked = false;
		document.getElementById('search_icon_'+mana).src = 'kc-default-images/soff-mana-'+image+'.png';
	} else {
		document.getElementById('search_colour_'+mana).checked = true;
		document.getElementById('search_icon_'+mana).src = 'kc-default-images/son-mana-'+image+'.png';
	}
}
function update_search_type(mana) {
		if (document.getElementById('search_type_N').checked === true && document.getElementById('search_type_T').checked === true && document.getElementById('search_type_R').checked === true) {
	document.getElementById('search_type_N').checked = false;
	document.getElementById('search_text_N').style.color = '#999999';
	document.getElementById('search_type_R').checked = false;
	document.getElementById('search_text_R').style.color = '#999999';
	document.getElementById('search_type_T').checked = false;
	document.getElementById('search_text_T').style.color = '#999999';
	}
	if (document.getElementById('search_type_'+mana).checked === true) {
		document.getElementById('search_type_'+mana).checked = false;
		document.getElementById('search_text_'+mana).style.color = '#999999';
	} else {
		document.getElementById('search_type_'+mana).checked = true;
		document.getElementById('search_text_'+mana).style.color = '#ffffff';
	}
}
function dn() {
}

function setppage(page) {
	location ='by_page_ajax.php?page='+page;
}
function setpsort(sort2) {
	location ='by_page_ajax.php?sort='+sort2;
}
function setaview() {
	location ='by_page_ajax.php?view=1';
}
function setpview() {
	location ='by_page_ajax.php?view=2';
}

function secure_checkout(linked) {
	if (document.getElementById('update_delivery').value) {
		window.location = linked;
	} else {
		alert('Please select your delivery');
	}
}
