function getElementsByClass (obj, searchClass, tag)
{
   var returnArray = [];
   tag = tag || '*';
   var els = obj.getElementsByTagName(tag);
   var pattern = new RegExp('(^|\\s)'+searchClass+'(\\s|$)');
   for (var i = 0; i < els.length; i++) {
      if ( pattern.test(els[i].className) ) {
         returnArray.push(els[i]);
      }
   }
   return returnArray;
}



var paintingSize = "16x16";
var maintainProportion = false;
var isSizeSelected = false;
var origProportion = 1;


function _showPrice(responseTxt, responseStat)
{
	document.getElementById("price").innerHTML = responseTxt;
}
function _getPrice()
{
	var area, price;
	
	var aSize = paintingSize.split("x");
	aSize[0] = parseFloat(aSize[0]);
	aSize[1] = parseFloat(aSize[1]);
	
	area = aSize[0] * aSize[1];
	
	var oAjax = new Ajax(CMS_BASE_HREF + '_system/support/calculate-price.ajax.php', _showPrice);
	oAjax.update("area=" + area);
}
function getFraction(frac)
{
	frac = (frac == 0)
		? '.0'
		: (frac / 8).toString().replace(/^0/, "");
	return frac;
}
function _setImageChooserHeight(oF, newHeight) {
	var h = Math.floor(newHeight);
	var frac = newHeight - h;
	oF.height.value = h;
	
	var aFractions = [0.0, 0.125, 0.25, 0.375, 0.5, 0.625, 0.75, 0.875];
	var f = 0;
	for (var i in aFractions) {
		if (frac >= aFractions[i]) {
			f = aFractions[i];
		} else {
			break;
		}
	}
	oF.height_fraction.value = i - 1;
}
function calculatePrice(o) {
	var origProportion = document.getElementById("img_proportion").getAttribute("value");
	var orientation = document.getElementById("img_orientation").getAttribute("value");

	switch (o.tagName.toUpperCase()) {
		case 'INPUT':
			if (o.type.toLowerCase() == "radio") {
				// Radio (predefined size).
				var aSize = o.value.split("x");
				var w = Math.floor( aSize[0] );
				var wf = Math.floor( (aSize[0] - w) * 8);
				var h = Math.floor( aSize[1] );
				var hf = Math.floor( (aSize[1] - h) * 8);
				document.getElementById("width").value = w;
				document.getElementById("width_fraction").value = wf; 
				document.getElementById("height").value = h; 
				document.getElementById("height_fraction").value = hf;
				
				// getElementById is buggy in Microsoft Explorer 
				// http://www.quirksmode.org/bugreports/archives/2005/09/documentgetElementById_may_return_element_with_a_n.html
				var oCheckbox = document.getElementById("maintain_proportion");
				if (document.all || window.opera) {
					oCheckbox = oCheckbox.nextSibling;
				}
				oCheckbox.checked = false;
				maintainProportion = false;
				
				paintingSize = o.value;
				_getPrice();
			} else if (o.type.toLowerCase() == "checkbox") {
				maintainProportion = o.checked;
				// maintainProportion = !maintainProportion;
				if (maintainProportion) {
					// Re-calculate the height using the original image proportion.
					var currWidth = parseFloat(o.form.width.value) + parseFloat(o.form.width_fraction.value * 0.125);
					// angelus
					// if(o.form.img_width.value <= o.form.img_height.value)
					//	var newHeight = parseFloat(currWidth / origProportion);
					//else	
					var newHeight = parseFloat(currWidth / origProportion);
					newHeight = parseFloat( Math.ceil( newHeight * 8 ) / 8 );

					_setImageChooserHeight(o.form, newHeight);
					paintingSize = currWidth + 'x' + newHeight;
					_getPrice();
				}
				var cRadios = document.getElementsByTagName("input");
				for (var i = 0, ii = cRadios.length; i < ii; i++) {
					// Uncheck the radios.
					if (cRadios[i].type && cRadios[i].type.toLowerCase() == "radio") {
						cRadios[i].checked = false;
					}
				}
				return;
			}
			break;
		
		case 'SELECT':
			// Custom size
			var cRadios = document.getElementsByTagName("input");
			for (var i = 0, ii = cRadios.length; i < ii; i++) {
				// Uncheck the radios.
				if (cRadios[i].type && cRadios[i].type.toLowerCase() == "radio") {
					cRadios[i].checked = false;
				}
			}
			
			var width, height;
			height = parseFloat(o.form.height.value + getFraction(o.form.height_fraction.value));
			width = parseFloat(o.form.width.value + getFraction(o.form.width_fraction.value));
			if (paintingSize && maintainProportion) {
				if (o.id == "height" || o.id == "height_fraction") {
					height = parseFloat(o.form.height.value + getFraction(o.form.height_fraction.value));
					switch (orientation) {
						case "portrait":
							//width = height / origProportion;
							//break;
						case "landscape":
							width = height * origProportion;
							break;
						default:
							width = height;
					}
				} else if (o.id == "width" || o.id == "width_fraction") {
					width = parseFloat(o.form.width.value + getFraction(o.form.width_fraction.value));
					switch (orientation) {
						case "portrait":
							//height = width * origProportion;
							//break;
						case "landscape":
							height = width / origProportion;
							break;
						default:
							height = width;
					}
				}
				
				var minWidth = parseFloat(o.form.width.firstChild.value);
				var minHeight = parseFloat(o.form.height.firstChild.value);
				var maxWidth = parseFloat(o.form.width.lastChild.value + getFraction(o.form.width_fraction.lastChild.value));
				var maxHeight = parseFloat(o.form.height.lastChild.value + getFraction(o.form.height_fraction.lastChild.value));
				
				if (width < minWidth || width > maxWidth) {
					width = (width < minWidth) ? minWidth : maxWidth;
					switch (orientation) {
						case "portrait":
							// height = width * origProportion;
							// break;
						case "landscape":
							height = width / origProportion;
							break;
						default:
							height = width;
					}
				}
				if (height < minHeight || height > maxHeight) {
					height = (height < minHeight) ? minHeight : maxHeight;
					switch (orientation) {
						case "portrait":
							// width = height / origProportion;
							// break;
						case "landscape":
							width = height * origProportion;
							break;
						default:
							width = height;
					}
				}

				width = Math.ceil( width * 8 ) / 8;
				height = Math.ceil( height * 8 ) / 8;
				
				o.form.width.value = Math.floor(width);
				o.form.height.value = Math.floor(height);
				
				o.form.width_fraction.value = Math.floor((width - Math.floor(width)) * 8);
				o.form.height_fraction.value = Math.floor((height - Math.floor(height)) * 8);
			}
			paintingSize = width + "x" + height;
			_getPrice();
			break;
	}
	
	isSizeSelected = true;
}



function checkCartForm(oF, msg)
{
	if (!isSizeSelected)
	{
		alert(msg);
		return false;
	}
	return true;
}



function getElementsByClassName(oElm, strClassName, strTagName)
{
	var aElements = (strTagName == "*" && oElm.all)
		? oElm.all
		: oElm.getElementsByTagName(strTagName);
	
	var aReturnElements = [];
	
	strClassName = strClassName.replace(/\-/g, "\\-");
	var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
	
	var oElement;
	for (var i = 0, ii = aElements.length; i < ii; i++)
	{
		oElement = aElements[i];     
		if (oRegExp.test(oElement.className))
		{
			aReturnElements.push(oElement);
		}
	}
	return (aReturnElements)
}




function Ajax(url, callbackFunction)
{
	var that = this;
	this.updating = false;
	
	this.abort = function()
	{
		if (that.updating)
		{
			that.updating = false;
			that.AJAX.abort();
			that.AJAX = null;
		}
	}
	
	this.update = function (passData, postMethod)
	{
		if (that.updating)
		{
			return false;
		}
		that.AJAX = null;
		if (window.XMLHttpRequest)
		{
			that.AJAX = new XMLHttpRequest();
		}
		else
		{
			that.AJAX = new ActiveXObject("Microsoft.XMLHTTP");
		}
		
		if (null == that.AJAX)
		{
			return false;
		}
		else
		{
			that.AJAX.onreadystatechange = function()
			{
				if (4 == that.AJAX.readyState)
				{
					that.updating = false;
					that.callback(that.AJAX.responseText, that.AJAX.status, that.AJAX.responseXML);
					that.AJAX = null;
				}
			}
			
			that.updating = new Date();
			if (/post/i.test(postMethod))
			{
				var uri = urlCall + "?" + that.updating.getTime();
				that.AJAX.open("POST", uri, true);
				that.AJAX.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
				that.AJAX.send(passData);
			}
			else
			{
				var uri = urlCall + "?" + passData + "&ajaxtimestamp=" + (that.updating.getTime());
				that.AJAX.open("GET", uri, true);
				that.AJAX.send(null);
			}
			return true;
		}
	}
	
	var urlCall = url;
	this.callback = callbackFunction || function(){};
}




function initPage()
{
	if (document.getElementById)
	{
		if (document.getElementById("rightSideBar"))
		{
			document.getElementById("rightSideBar").style.height = document.getElementById("leftcolumn").clientHeight + "px";
			document.getElementById("rightsidebottom").style.bottom = "0px";
		}
		
		var aBookmarkLinks = getElementsByClassName(document, "bookmark", "a");
		if (aBookmarkLinks)
		{
			for (var i = 0, ii = aBookmarkLinks.length; i < ii; i++)
			{
				aBookmarkLinks[i].onclick = function()
				{
					window.open(this.href);
					return false;
				}
			}
		}
		
		// Egalize "last viewed paintings" box heights.
		var aLastViewed = getElementsByClassName(document, "lastviewed", "div");
		if (aLastViewed)
		{
			var maxHeight = 0;
			for (var i = 0, ii = aLastViewed.length; i < ii; i++)
			{
				if (aLastViewed[i].clientHeight > maxHeight)
				{
					maxHeight = aLastViewed[i].clientHeight;
				}
			}
			for (var i = 0, ii = aLastViewed.length; i < ii; i++)
			{
				aLastViewed[i].style.height = maxHeight + "px";
			}
		}
		
		// Painting size chooser popups
		// FORM#paintingSizeForm .box2 H4 A.popupinfo {
		var aPopups = getElementsByClass(document, "popupinfo", "A");
		for (var i = 0, ii = aPopups.length; i < ii; i++)
		{
			aPopups[i].onmouseover = function()
				{
					this.parentNode.nextSibling.style.display = "block";
					if (this.className.indexOf("iefix") > -1)
					{
						document.getElementById("width").style.visibility = "hidden";
						document.getElementById("width_fraction").style.visibility = "hidden";
						document.getElementById("height").style.visibility = "hidden";
						document.getElementById("height_fraction").style.visibility = "hidden";
					}
				}
			aPopups[i].onmouseout = function()
				{
					this.parentNode.nextSibling.style.display = "none"
					if (this.className.indexOf("iefix") > -1)
					{
						document.getElementById("width").style.visibility = "visible";
						document.getElementById("width_fraction").style.visibility = "visible";
						document.getElementById("height").style.visibility = "visible";
						document.getElementById("height_fraction").style.visibility = "visible";
					}
				}
		}
		
		
		// Left column Subjects popup menu.
		var cSubjectsMenuLi = document.getElementById("subjectMenu").getElementsByTagName("LI");
		for (var i = 0, ii = cSubjectsMenuLi.length; i < ii; i++)
		{
			cSubjectsMenuLi[i].onmouseover = function()
				{
					this.className = this.className.replace(/\bhover\b/, "");
					this.className += " hover";
				}
			cSubjectsMenuLi[i].onmouseout = function()
				{
					this.className = this.className.replace(/\bhover\b/, "");
				}
		}
	}
}
if (window.addEventListener)
{
	window.addEventListener("load", initPage, false);
}
else if (window.attachEvent)
{
	window.attachEvent("onload", initPage);
}
else if (window.onload)
{
	window.onload = initPage;
}

function bookmarksite(title, url) {
if (document.all)
	window.external.AddFavorite(url, title);
else if (window.sidebar)
	window.sidebar.addPanel(title, url, "");
}

function ajaxsearch() {
//If too few characters entered spare us from search
if (document.getElementById("q").value.length<4) { setTimeout("document.getElementById(\"search_hint\").style.display=\"none\"",300); return true; }
document.getElementById("search_hint").style.display="";
var xmlHttpS; try { xmlHttpS=new XMLHttpRequest(); }
catch (e) { try { xmlHttpS=new ActiveXObject("Msxml2.XMLHTTP"); }
  catch (e) { try { xmlHttpS=new ActiveXObject("Microsoft.XMLHTTP"); }
    catch (e) { alert("Your browser does not support AJAX!"); return false; }
    }
  }
xmlHttpS.onreadystatechange=function() { if(xmlHttpS.readyState==4) { document.getElementById("search_hint").innerHTML = xmlHttpS.responseText; } }
xmlHttpS.open("GET","/includes/ajax-search.php?name="+document.getElementById("q").value,true);
document.getElementById("search_hint").style.display="";
xmlHttpS.send(null);
}
function shinthide(){
setTimeout("document.getElementById(\"search_hint\").style.display=\"none\"",1000)
}