var itemImages = new Array();

function ChangePic(newPicSrc, newPicHref, description, colorID, changeOptions)
{
	picObj = document.getElementById("mainpic");
	picObj.src = newPicSrc;
	picObj.alt = description;

	hrefObj = document.getElementById("mainpichref");
	hrefObj.href = newPicHref;
	hrefObj.title = description;

	if (changeOptions)
	{
		$('SelectOption').value = "";
		var options = $A($('SelectOption').childNodes);
		options.each(function(node){ if (node.tagName == 'OPTION' && node.value.substr(7, 3) == colorID) { node.parentNode.value = node.value; }});
	}

	return false;
}

function SetImage(sku)
{
	if (sku.length > 0)
	{
		var colorID = sku.substr(7, 3);
		for (var i = 0; i < itemImages.length; i++)
		{
			if (itemImages[i]["colorID"] == colorID)
			{
				ChangePic(itemImages[i]["middle"], itemImages[i]["large"], itemImages[i]["description"], itemImages[i]["colorID"], false);
			}
		}
	}
	else if (itemImages.length > 0)
	{
		ChangePic(itemImages[0]["middle"], itemImages[0]["large"], itemImages[0]["description"], itemImages[0]["colorID"], false);
	}
}

function CheckQuantity(elm)
{
	if ($('SelectOption') && !$F('SelectOption'))
	{
		alert('Select Option');
		return false;
	}

	if (!parseInt($F('quantityInp')))
	{
		alert('Input quantity');
		return false;
	}

	$('add2cartForm').submit();

	return false;
}

function TitleSearch(path)
{
	titleObj = document.getElementById("titleQuery");
	if (titleObj.value.length > 0)
	{
		document.location.href = path+"search.php?q="+titleObj.value;
		return false;
	}
	else
	{
		alert("Please enter search string");
		titleObj.focus();
		return false;
	}
}

function GetAbsolutePos(el)
{
	var SL = 0, ST = 0;
	var is_div = /^div$/i.test(el.tagName);
	if (is_div && el.scrollLeft)
		SL = el.scrollLeft;
	if (is_div && el.scrollTop)
		ST = el.scrollTop;
	var r = { x: el.offsetLeft - SL, y: el.offsetTop - ST };
	if (el.offsetParent)
	{
		var tmp = GetAbsolutePos(el.offsetParent);
		r.x += tmp.x;
		r.y += tmp.y;
	}
	return r;
}

function LinkWindow(linkURL, options)
{
	if (window.event)
	{
		window.event.cancelBubble = true;
		window.event.returnValue = false;
	}

	if (!options)
		options = {};

	var width = options.width || null;
	var height = options.height || null;

	if (!width)
	{
		width = Math.ceil(screen.width/2);
		if (width < 650) width = 650;
	}

	if (!height)
	{
		var height = Math.ceil(screen.height*0.6);
		if (height < 400) height = 400;
	}

	var toolbar = options.toolbar || 0;
	var location = options.location || 0;
	var directories = options.directories || 0;
	var menubar = options.menubar || 0;
	var scrollbars = options.scrollbars || 1;
	var resizable = options.resizable || 0;
	var status = options.status || 0;
	var fullscreen = options.fullscreen || 0;
	var name = options.name || "Additional";

	var left = options.left || Math.ceil(screen.width/4);
	var top = options.top || Math.ceil(screen.height*0.1);
	var oTarget = window.open(linkURL, name, "height="+height+",width="+width+",scrollbars="+scrollbars+",left="+left+",top="+top+",toolbar="+toolbar+",location"+location+",directories"+directories+",menubar"+menubar+",resizable"+resizable+",status"+status+",fullscreen"+fullscreen);
	oTarget.focus();
	return false;
}

function pr(obj)
{
	var res = '';
	if (Object.isString(obj) || Object.isNumber(obj))
	{
		res += obj;
	}
	else
	{
		$H(obj).each(function(el)
		{
			res += el.key + ' = ' + el.value + '<br />';
		});
	}

	var debug = $('debug');
	if (!debug)
	{
		debug = new Element('div', {id:'debug'});
		$(document.body).insert(debug);
	}

	debug.insert(res);
}

var AjaxRequest = Class.create(
{
	initialize: function(url, options)
	{
		this.url = url || '';
		this.options = options || {};
		this.onSuccess = Object.isFunction(this.options.onSuccess) ? this.options.onSuccess : Prototype.emptyFunction;
		this.onFailure = Object.isFunction(this.options.onFailure) ? this.options.onFailure : Prototype.emptyFunction;
		this.parameters = this.options.parameters || {};
		this.method = this.options.method || 'post';
		this.caching = this.options.caching || false;
		this.loader = this.options.loader || null;
		this.user = this.options.user || null;
		this.password = this.options.password || null;
		this.async = this.options.password || true;

		this.send();
	},

	send : function()
	{
		var req = new JsHttpRequest();
		req.onreadystatechange = function()
		{
			if (req.readyState == 4)
			{
				if (req.responseJS)
				{
					this.onSuccess(req.responseJS);
				}
			}
			else
			{
				this.onFailure(req);
			}
		}

		var fx = req.onreadystatechange;
		req.onreadystatechange = fx.bind(this);

		req.loader = this.loader;
		req.caching = this.caching;
		req.open(this.method, this.url, this.async, this.user, this.password);

		req.send(this.parameters);
	}
});