window.addEvent( 'domready', init );

function init() {
	loadPhotos();
	
	if ($('manufacturer')) {
		var myFormValidation = new Validate('myForm',{errorClass: 'red'});
	}

$$('#manufacturer select').addEvent('change', function(e) {
	e = new Event(e).stop();

	var url = "/update_list/"+this.value;
	$('manu-spinner').style.display = "block"
	
	$('options_model').disabled = false
	
	
	new Ajax(url, {
		method: 'get',
		update: $('model_div'),
		onComplete: function () { 
					$('manu-spinner').style.display = "none";
					$('model_div').style.display = "block";
					$$('#manufacturer select').removeEvents('change', function(e) {});
					$$('#model_div select').removeEvents('change', function(e) {});
					init();
					}
	}).request();
});

$$('#model_div select').addEvent('change', function(e) {
	e = new Event(e).stop();
		
	var hasSub = this.value.test("true");
						
	if (hasSub == true) {
		var newVal = this.value.slice(5)

		var url = "/update_list_2/"+newVal;
		$('manu-spinner').style.display = "block"
		$('options_model').name = "nil"	
		
		new Ajax(url, {
			method: 'get',
			update: $('submodel_div'),
			onComplete: function () { 
						$('manu-spinner').style.display = "none";
						$('submodel_div').style.display = "block";
						$$('#manufacturer select').removeEvents('change', function(e) {});
						$$('#model_div select').removeEvents('change', function(e) {});
						init();
					}
		}).request();
	
	} else {
		$('submodel_div').style.display = "none";
	}
});

}


function loadPhotos() {

	node = document.getElementById("products");
 
 	if (node != undefined) {
	if (node.childNodes.length > 0) {
		for (var i=0; i<node.childNodes.length; i++) {
			var child = node.childNodes[i];
			if (child.nodeName == "LI") {
				var thisid = child.id.replace(/p/, '');
				child.style.backgroundImage="url('/_shared/uploads/photos/products/"+thisid+"/front/thumb/image.jpg')";
			}	
		}
	}
	}
	
	node = document.getElementById("sidebar");
 
	if (node.childNodes.length > 0) {
		for (var i=0; i<node.childNodes.length; i++) {
			var child = node.childNodes[i];
			if (child.nodeName == "LI" && child.id != "") {
				var thisid = child.id.replace(/s/, '');
				child.style.backgroundImage="url('/_shared/uploads/photos/ads/"+thisid+"/front/image.jpg')";
			}	
		}
	}
}

function changeBgPhoto(target, url) {
	node = document.getElementById("main-photo");
	node.style.backgroundImage = "url("+url+")";
}
 
function fbs_click() {
	u=location.href;
	t=document.title;
	window.open('http://www.facebook.com/sharer.php?u='+encodeURIComponent(u)+'&t='+encodeURIComponent(t),'sharer','toolbar=0,status=0,width=626,height=436');
	return false;
}