var bootType = 'all';
var bootGender = 'all';

function openDocument(url)
{
    window.open(url,'doc',"width=950,height=750,copyhistory=no,status=yes,directories=no,menubar=yes,location=no,resizable=yes,scrollbars=yes,toolbar=no");
}

selOpen = function(active, id, val) {

		var doRedirect = false;		
		$('#' + id + ' > li').removeClass('on').removeClass('shown');
		$(active).parent().addClass('shown');
		selShow(id);
		var getArr = getUrlVars();
		var addStr = '';
		if (id == 'st')
		{
			if (val != bootType)
			{
				bootType = val;
				addStr = '?stype=' + val;
				var gender = $('#ss li.on a').attr('rel');
				if (getArr.gender && getArr.gender != 'all' && getArr.gender != 'false') addStr += '&gender=' +  getArr.gender;
				doRedirect = true;
			}
		}else if(id == 'ss' )
			{
				if (val != bootGender)
				{
					bootGender = val;
					addStr = '?gender=' + val;
					var stype = $('#st li.on a').attr('rel');
					if (getArr.stype && getArr.stype != 'all' && getArr.stype != 'false') addStr += '&stype=' +  getArr.stype;
					doRedirect = true;
				}
			}
		if (doRedirect) window.location = '/content/catalogue/' + addStr;
}
selShow = function(id) {
	if ($('#' + id).hasClass('shown')) 
	{
		$('#' + id + ' > li').each(function(el)
		{
			if (!$(this).hasClass('opener')) {
				$(this).removeClass('on');
				if ($(this).hasClass('shown')) {
					$(this).removeClass('shown');
					$(this).addClass('on');
				}
			}
		});
		$('#' + id).removeClass('shown');
	} else {
		
		$('#' + id + ' > li').each(function(el)
		{
			
			if (!$(this).hasClass('opener')) {
				if ($(this).hasClass('on')) {
					$(this).addClass('shown');
				} else {
					
					$(this).addClass('on');
				}
			}
		});
		$('#' + id).addClass('shown');
	}
	return false;
}

delRow = function(active, iID) {
	
	$.post('/content/catalogue/cart/' , {action: 'delinv', inventoryID: iID},  function(resp)
	{
		if (resp.success == 1) 
		{
			$('span.sum_inv').html(resp.sum_inv);
			$('span.sum_sizes').html(resp.sum_sizes);
			$('span.sum_total').html(resp.sum_total);
			if (resp.sum_inv == 0)
			{
				$('div.infoblock_item').hide();
				$('tr.nothing').show();
			}
			$(active).parents('tr').remove();
		}
	}, 'json');
	return false;
}
delDiv = function(active) {
	
	var parID = $(active).parent().attr('id');
	$.post('/content/catalogue/cart/' , {action: 'delfromcart', key: parID},  function(resp)
	{
		if (resp.success == 1) 
		{
			$('span.sum_inv').html(resp.sum_inv);
			$('span.sum_sizes').html(resp.sum_sizes);
			$('span.sum_total').html(resp.sum_total);
			$(active).parent().remove();
		}
		
	}, 'json');
	
	
	return false;
}
switchMap = function(active) {
	if ($(active).parent().parent().hasClass('ajaxopen')) {$(active).parents('div').addClass('on');}
	$('div.ajax').removeClass('ajaxopen');
	if ($(active).parent().parent().hasClass('on')) {
		$(active).parent().parent().removeClass('on');
	} else {
		$(active).parent().parent().addClass('ajaxopen');
	}
	return false;
}
switchMap2 = function(active, hrefa, flah) {
	/*
	$$('ul.map_r li').each(function(el){
		$(el).removeClassName('on');
		$(el).up().next().removeClassName('mapb');
	});
	*/
	$('ul.map_r li').removeClass('on');
	$('ul.map_r li').parent().next().removeClass('mapb');
	
	$(active).parent().addClass('on');
	if (flah) {
		$(active).parents('ul').next().addClass('mapb');
	} else {
		$(active).parents('ul').next().removeClass('mapb');
	}
	$('#map_link').attr('href', hrefa);
	return false;
}

hoverer = function () {
	$('table.orderbin tbody th').mouseover(function(e){$(e.target).parent().addClass('on');}).mouseout(function(e){$(e.target).parent().removeClass('on');});
	$('table.orderbin td.data div.hf').mouseover(function(e){$(e.target).addClass('on');}).mouseout(function(e){$(e.target).removeClass('on');});
	$('table.orderbin td.data div.hf a.del').mouseover(function(e){$(e.target).parent().addClass('on');}).mouseout(function(e){$(e.target).parent().removeClass('on');});
	$('table.orderbin th a.del').mouseover(function(e){$(e.target).parents('tr').addClass('on');}).mouseout(function(e){$(e.target).parents('tr').removeClass('on');});
	$('table.orderbin td.data div.hf span').mouseover(function(e){$(e.target).parent().addClass('on');}).mouseout(function(e){$(e.target).parent().removeClass('on');});
}

addShower = function(active) {
	$('div.add_size').hide();
	$('div.add_size_butt').show();
	$(active).parent().hide();
	$(active).parent().prev().show();
	/*
	$('div.add_size').each(function(el){
		$(el).setStyle({display:'none'});
		$(el).next().setStyle({display:'block'});
	});
	$(active).up().setStyle({display:'none'});
	$(active).up().previous().setStyle({display:'block'});
	*/
	return false;
}

addSize = function(active, iID)
{
	var quantity = $('#bq_' + iID).val();
	if (isNaN(parseInt(quantity)) || parseInt(quantity) == 0) return false; 
	$(active).parent().hide();
	
	
	var ddID = 'sizes_' + iID + '_' + $('#sel_size_' + iID).val() + '_' + $('#sel_type_' + iID).val();
	var dID = '#' + ddID;
	
	$.post('/content/catalogue/cart/' , {action: 'add2cart2', inventoryID: iID, quantity: quantity, size: $('#sel_size_' + iID).val(), key: $('#sel_type_' + iID).val()},  function(resp)
	{
		if (resp.success == 1)
		{
			$('span.sum_inv').html(resp.sum_inv);
			$('span.sum_sizes').html(resp.sum_sizes);
			$('span.sum_total').html(resp.sum_total);
			if ($(dID).length > 0)
			{
				jqDiv = $(dID);
				var jStr = jqDiv.find('span').text();
				if (!isNaN(parseInt(jStr))) jqDiv.find('span').text(parseInt(jStr) + parseInt(quantity));
				
			}else
				{
					var jqDiv = $(document.createElement('div'));
					jqDiv.addClass('wrapper');jqDiv.addClass('hf');
					jqDiv.attr('id', ddID);
					
					var jLabel = $(document.createElement('label'));
					jLabel.append(document.createTextNode($('#sel_size_' + iID + ' :selected').text()));
					
					var jSpan = $(document.createElement('span'));
					
					jSpan.append(document.createTextNode($('#bq_' + iID).val()));
					
					var jA2 = $(document.createElement('a'));
					jA2.attr('href', 'javascript:void(0);');
					jA2.addClass('del');
					jA2.click(function(e) {delDiv(e.target);});
					
					var jImg = $(document.createElement('img'));
					jImg.attr('src', '/i/s.gif');
					
					jqDiv.append(jLabel);
					jqDiv.append(jSpan);
					var bTxt = $('#sel_title_' + iID).val();
					//if(bTxt.length > 58) bTxt = bTxt.substr(0,55) + '...';
					jqDiv.append(document.createTextNode(bTxt));
					
					jA2.append(jImg);
					jqDiv.append(jA2);
					
					jqDiv.mouseover(function(){$(this).addClass('on');}).mouseout(function(){$(this).removeClass('on');});	
					$('#boot_' + iID).append(jqDiv);
					//$(active).parent().prev().append(jqDiv);
				}
			$(active).parent().next().show();			
		}
		
	}, 'json');
	return false;
}

emptyBin = function() {
	$('#clear_cart').submit();
	return false;
}
objShift = 0;
scrollIt = function(sval) {   
	objShift = objShift + sval;
   bigDiff = diff+objShift;
   if(bigDiff>=diff){$('scrollist').up(1).previous(1).addClassName('off');} else {$('scrollist').up(1).previous(1).removeClassName('off');}
   if (bigDiff<=3){$('scrollist').up(1).previous().addClassName('off');} else {$('scrollist').up(1).previous().removeClassName('off');}
   //alert(diff+objShift);
	$('scrollist').style.marginLeft = objShift + 'px';	
}
widthChecker = function(id) {
   hold_w = $(id).up().getWidth();
   if(hold_w<ul_w){
      diff = ul_w - hold_w;
   } else {
      $(id).up(1).previous().addClassName('off');
      $(id).up(1).previous(1).addClassName('off');
   }
}



function getUrlVars()
{
	var res = {};
	var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
	for(var i = 0; i < hashes.length; i++)
	{
		hash = hashes[i].split('=');
		res[hash[0]] = hash[1];
	}
	return res;
}

function image(imgName, width, height)
{
	win = window.open('','comp',"width=" + width + ",height=" + height + ",copyhistory=no,status=no,directories=no,menubar=no,location=no,resizable=no,scrollbars=no,toolbar=no");
	win.document.write('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><title>Kiln-direct.com</title></head><body style="margin: 0px; padding: 0px;">');
	win.document.write('<img src="' + imgName + '" border="0" alt="close" style="cursor: pointer; cursor: hand;" onclick="self.close();" />');	
	win.document.write('</body></html>');
	win.document.close();
}

/*
var handleProductMarkers = function () {
	setTimeout(function () {
		
		var flashObj = document["product"] || window["product"],
			markerClassName = 'ref-marker',
			offsetGap = 25, // title css gap
			markers = $$('[class*=' + markerClassName + ']'),
			flashElement = $('product-wrapper');
		
		var align = function () {
			markers.each(function(el, i) {
				var c = el.className;
				var idx = c.substr(markerClassName.length, c.length);
				var posY = $(el).cumulativeOffset()[1];
				var offsetY = $(flashElement).cumulativeOffset()[1];
				flashObj.SetVariable("location" + idx + "Y", posY - offsetY + offsetGap);
			});
		}
		
		align();
		
		Event.observe(window, 'resize', function () {
			align();
		});
		
	}, 1000);
}

var handleProductPoints = function (data) {
	
	var setFlashVars = function (data) {
		
		var flash = document["product"] || window["product"];
		
		flash.SetVariable("point1X", data.point1[0]);
		flash.SetVariable("point1Y", data.point1[1]);
		flash.SetVariable("point2X", data.point2[0]);
		flash.SetVariable("point2Y", data.point2[1]);
		flash.SetVariable("point3X", data.point3[0]);
		flash.SetVariable("point3Y", data.point3[1]);
		flash.SetVariable("point4X", data.point4[0]);
		flash.SetVariable("point4Y", data.point4[1]);
		flash.SetVariable("szPathToPic", data.img);
		
	}
	
	// set initial variables
	setFlashVars(data[0][0]);
	
	$$('.boot_spec_holder ul').each(function (block, block_idx) {
		block.select('li').each(function (item, item_idx) {
			item.select('a')[0].observe('click', function (e) {
				
				var flash = document["product"] || window["product"];
				
				this.up('ul').select('li').invoke('removeClassName', 'on');
				this.up('li').addClassName('on');
				
				setFlashVars(data[block_idx][item_idx]);
				
			});
		});
	});
	
}
*/
