/* Window Load
---------------------------------------------------------- */

window.onload = function() {
	if(isInURL('showroom')) {
		initScroller();
		prepareShowroomLinks();
		scrollToHighlight();
	}
	
	if(isInURL('results'))
		manageSearchResults();
}


/* Showroom Preparation
---------------------------------------------------------- */

/* Sets oncick event for showroom navigation. */
function prepareShowroomLinks () {
	var links = $ES('a','scroller');

	for(var i=0; i<links.length; i++) {
		links[i].addEvent('click', function(event) {
			setContent(this.className);
		});
		
		if((i+1)%8 == 0) {
			if(links[i].className == "highlight")
				links[i].className = "highlight last";	
			else
				links[i].className = "last";
		}
	}
}

/* Shows given div and hides all other content divs */
function setContent(divID) {
	var divs = document.getElementsByTagName('div');
	var contentWrappers = new Array();
	var j = 0;
	
	// Collect all contentWrapper divs
	for(var i=0; i<divs.length; i++)
		if(divs[i].className == "contentWrapper")
			contentWrappers[j++] = divs[i];
			
	// Hide all contentWrappers and show the one with the given ID		
	for(var i=0; i<contentWrappers.length; i++) {
		if(contentWrappers[i].id != divID) {
			new Fx.Style(contentWrappers[i].id, 'opacity').start(1,0);
			setTimeout('',1000);		
			contentWrappers[i].style.display = 'none';
		} else {
			contentWrappers[i].style.display = "block";
			new Fx.Style(contentWrappers[i].id, 'opacity').start(0,1);
		}
	}
}

/* */
function manageSearchResults() {
	var headers = document.getElementsByTagName('h6');
	
	for(var i=0; i<headers.length; i++) {
		numResults = (parseInt(headers[i].nextSibling.nextSibling.childNodes.length)-1)/2;
		temp = headers[i].innerHTML + '<span class="num">(' + numResults + ')</span>';
		headers[i].innerHTML = temp;
	}
}


/* Helper Functions
---------------------------------------------------------- */

/* Calls the function when the page loads (thanks: Jeremy Keith) */
function addLoadEvent(func) {
	var oldonload = window.onload;
	if(typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}

/* Checks whether or not the given string is in the page url */
function isInURL(string) {
	var url = window.location + "";

	if(url.search(string) > 0)	
		return true;
	return false;
}