/* GENERAL FUNCTIONS
-----------------------------------------------------*/

function classRows() {
	var myTABLES = document.getElementsByTagName('tbody');	//Only cycle through rows in the table body
	for (var x=0;x<myTABLES.length;x++) {
	   var myTR = myTABLES[x].getElementsByTagName('tr');
	   for (var i=0;i<myTR.length;i++) {
		   if (myTR[i].className == '') {		//Only add a class if there isn't already a class
			   if (i%2) {
				   myTR[i].className = 'even';	//Add even class to even rows
			   } else {
				   myTR[i].className = 'odd';	//Add odd class to odd rows
			   }
		   }
	   }
	}
}

function getElementsByClass(searchClass,node,tag) {
	var classElements = new Array();
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp('(^|\\s)'+searchClass+'(\\s|$)');
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}
/*

function fixLabels() {
	var myLABELS = document.getElementsByTagName('label');
	for (var x=0;x<myLABELS.length;x++) {
		myLABELS[x].onclick = function() {labelAction(this)};
	}
}

function labelAction(label) {
	var inputs = '';
	if (inputs = label.getElementsByTagName('input')) {
		switch(inputs[0].type) {
			case 'text':
			case 'password':
				inputs[0].focus();
				break;
			case 'radio':
			case 'checkbox':
				inputs[0].checked='checked';
				break;
		}
	} else if (inputs = label.getElementsByTagName('textarea')) {
		//NOT WORKING YET FOR TEXTAREAS
		inputs[0].focus();
	}
}
*/

/*PRODUCT PAINT AND DECOR PAGE
-----------------------------------------------------*/
function resetBg(background) {
	Element.setStyle(background.element, {backgroundColor:''});
}

function switchImage(image) {
	function updateAjax(thing) {
		query='test';
		var url = 'http://www.dynamaxcorp.com/ajax-get-data.php';
		var pars = 'mode=360&q='+image;
		new Ajax.Updater(thing.element.id, url, {method:'get', parameters:pars, onComplete:function(){ new Effect.Appear(thing.element.id);},asynchronous:true, evalScripts:true});

	}
	new Effect.Fade('contain', 
	{ duration: 0.5,
		afterFinish: updateAjax });
}

/*PRODUCT FEATURE PAGE
-----------------------------------------------------*/
function switchFeature(feat,model) {
	function updateAjax2(thing) {
		var url = 'http://www.dynamaxcorp.com/ajax-get-data.php';
		var pars = 'mode=feature&q='+feat+'&r='+model;
		new Ajax.Updater(thing.element.id, url, {method:'get', parameters:pars, onComplete:function(){ new Effect.Appear(thing.element.id);},asynchronous:true, evalScripts:true});
	}
	new Effect.Fade('container', { duration: 0.5, afterFinish: updateAjax2 });
}



/*PRODUCT GUIDE PAGE
-----------------------------------------------------*/
function updateNow() {
	var items = document.getElementById('find-models').getElementsByTagName('input');
	var newlookup = Array();
	i=0;
//	document.write('<br /><br /><h3>Create Values</h3> ');
	for (x=0; x<items.length; x++) {
		if (items[x].type == 'radio' && items[x].checked == true && items[x].value != "") {
			newlookup[i] = items[x].value;
//			document.write('<br /><strong>'+items[x].id+':</strong> ');
			i++;
		}
	}
//	document.write('<br /><br /><h3>Match Values</h3> ');
	var models = document.getElementById('guide').getElementsByTagName('a');
	for (x=0; x<models.length; x++) {
//		document.write('<br /><strong>'+models[x].className+':</strong> ');
		var switcher = '';
		for (i=0; i<newlookup.length; i++) {
			if (models[x].className.match(newlookup[i])) {
				switcher += newlookup[i]+':true,';
			} else {
				switcher += newlookup[i]+':false,';
			}
		}
		if (switcher.match('false')) {
			Element.setStyle(models[x], {opacity:'.2', filter:'alpha(opacity=20)'});
		} else {
			Element.setStyle(models[x], {opacity:'.99', filter:'alpha(opacity=99)'});
		}
//		document.write (switcher+'<br />');
	}
//	document.write('<br /><br />');
}

function addUpdate() {
	if (document.getElementById('find-models')) {
		var items = document.getElementById('find-models').getElementsByTagName('input');
		for (x=0; x<items.length; x++) {
			items[x].onclick = function() {updateNow()};
			if (items[x].type == 'submit') {
				Element.setStyle(items[x], { display:'none'});
			}
		}
	}
}

function nextPrev(item) {
	new Effect.toggle(item,'slide');
}

window.onload=function(){
	classRows();
	addUpdate();
//	fixLabels();
}
