function company() {
	var c = document.getElementById('Company');
	var e = YAHOO.util.Dom.getElementsByClassName('company', 'li');
	if (e) {
		for (var i = 0; i < e.length; i++) {
			if (e[i].className.startsWith('hidden')) {
				if (c.checked = 'checked') {
					e[i].className = e[i].className.substr(7);
				}
			} else {
				e[i].className = 'hidden ' + e[i].className;
			}
		}
	}
}
function assignResizePrice() {
	var r = document.getElementById('price-resize');
	if (r) r.onclick = resizePrice;
}

function resizePrice() {
	var p = document.getElementById('price');
	var r = document.getElementById('price-resize');
	if (p) {
		if (p.className == 'price-small') {
			p.className = 'price-large';
			r.innerHTML = 'If you think the price is too big, <span>click here</span>.';
		} else {
			p.className = 'price-small';
			r.innerHTML = 'Make price <span>bigger</span>';
		}
	}
}

/*
	This object encapsulates a YUI Connection object, it's primary function
	is to load html into a DOM objects InnerHTML
*/
function connectionWrapper(url, id) {
	var connectUrl = url;
	var connectID = id;
	var afterSuccess = null;

	function setAfterSuccess(oAfterSuccess)
	{
		this.afterSuccess = oAfterSuccess;
	}
	;

	var yuiConnect = {
		handleSuccess:function(o) {
			var fadeOut = new YAHOO.util.Anim(connectID, { opacity: { to: 0 } }, 0.2);
			var fadeIn = function(type, args) {
				document.getElementById(connectID).innerHTML = o.responseText;
				var fadeIn = new YAHOO.util.Anim(connectID, { opacity: { to: 1 } }, 0.2);
				fadeIn.animate();
			};
			fadeOut.onComplete.subscribe(fadeIn);
			fadeOut.animate();
		},
		handleFailure:function(o) {
			// Failure handler
			//alert("Unable to load requested page : " + o.status + " " + o.statusText);
		}
	};

	/*
		Define the callback object for success and failure
		handlers as well as object scope.
	*/
	var callback = {
		success:yuiConnect.handleSuccess,
		failure:yuiConnect.handleFailure,
		scope: yuiConnect
	};

	return {
		startConnection: function() {
			YAHOO.util.Connect.asyncRequest('GET', connectUrl, callback, null);
		}
	};
};

requestNewQuote = function() {
	var qGet = new connectionWrapper('/quote.cp2', 'quote');
	qGet.startConnection();
};

loadQuotes = function() {
	var q = document.getElementById('quote');
	if (q) {
		var changeQuotes = setInterval('requestNewQuote()', 30000);
	}
};

addOnLoad = function() {
	var wo = window.onload ? window.onload : function() {
	};
	var f = arguments;
	window.onload = function() {
		for (var i = 0,j = f.length; i < j; i++) {
			f[i]();
		}
		wo();
	};
};

// Rod: Element is not exposed in IE!
//Element.prototype.insertAfter = function(newElement, target) {
var insertAfter = function(parent, newElement, target) {
    //target is what you want it to go after. Look for this elements parent.
	//var parent = this;
	
	//if the parents lastchild is the targetElement...
	if(parent.lastchild == target) {
		//add the newElement after the target element.
		parent.appendChild(newElement);
	} else {
		// else the target has siblings, insert the new element between the target and it's next sibling.
		parent.insertBefore(newElement, target.nextSibling);
	}
};


function descMore() {
	var divs = document.getElementsByTagName("div");
	for(var i = 0; i < divs.length; i++) {
		var div = divs[i];
		if(div.className=="description") {
			var h = div.style.pixelHeight ? div.style.pixelHeight : div.offsetHeight;
			if(h > 180) {
			    try {
			    div.style.height = '180px';
			    div.style.overflow = 'hidden';
			    
			    var more = document.createElement("div");
			    more.style.width = '100%';
			    more.style.marginBottom = div.style.marginBottom;
			    div.style.marginBottom = '0';
			    insertAfter(div.parentNode, more, div);
			    
			    var ellipsis = document.createElement("span");
			    ellipsis.innerHTML = "...";

			    var link = document.createElement("span");
			    link.style.cssFloat = 'right';
			    link.style.styleFloat = 'right';
			    var a = document.createElement("a");
			    a.style.fontSize = '90%';
			    a.style.paddingRight = '1em';
			    a.href = "javascript:(function(){return false))();";
			    a.collapsed = true;
			    a.div = div;
			    a.onclick = function() {
				if(this.collapsed) {
				    this.div.style.height = 'auto';
				    this.div.style.overflow = 'visible';
				    this.collapsed = false;
				    this.innerHTML = 'less &laquo;';
				    ellipsis.innerHTML = '&nbsp;';
				} else {
				    this.div.style.height = '180px';
				    this.div.style.overflow = 'hidden';
				    this.collapsed = true;
				    this.innerHTML = 'more &raquo;';
				    ellipsis.innerHTML = '...';
				}
			    };
			    a.innerHTML = 'more &raquo;';
			    link.appendChild(a);
			    more.appendChild(link);
			    more.appendChild(ellipsis);
			    
			    
			    var clear = document.createElement("div");
			    clear.style.clear = 'rigth';
			    clear.style.fontSize = '1px';
			    clear.style.lineHeight = '0';
			    clear.style.height = '0';
			    clear.style.width = '0';
			    clear.innerText = "&nbsp;";
			    more.appendChild(clear);
			    
			    } catch (e) {
			    alert(e);
			    }
			}
		}
	}
}

addOnLoad(company, loadQuotes, assignResizePrice, Shadowbox.init, descMore);

function submit_variations(form) {
	action = form.action;
	action = action.slice(0, action.indexOf('&var'));
	form.action = action + '&var=' + form[0].value;
	form.submit();

}
