function rating()
{
	var rates = document.getElementsByTagName("ul");
	for (i = 0; i < rates.length; i ++)
	{
		if (rates[i].className.indexOf("star-rating") != -1)
		{
			rates[i]._lis = rates[i].getElementsByTagName("li");
			
			rates[i].onmouseover = function() {
				for (k = 0; k < this._lis.length; k++)
				{
					if (this._lis[k].className.indexOf("active") != -1)
					{
						this._active = this._lis[k];
						this._lis[k].className = this._lis[k].className.replace("active", "");
					}
				}
			}
			
			for (j = 0; j < rates[i]._lis.length; j ++)
			{
				rates[i]._lis[j].onclick = function(){
					this.parentNode._active = this;
					_in = this.parentNode.parentNode.getElementsByTagName("input");
					if(_in[0])
					{
						_in[0].value = this.firstChild.innerHTML;
					}
				}
			}
			
			rates[i].onmouseout = function() {
				if (this._active && this._active.className.indexOf("active") == -1)
				{
					this._active.className += " active";
				}
			}
		}
	}
}

function checkReview()
{
	var _error = '';
	document.getElementById('ratings-block').innerHTML = _error;
	_ins = document.getElementById('review-form').getElementsByTagName('input');
	for(i = 0; i < _ins.length; i++)
	{
		if(_ins[i].value == '')
		{
			_error = 'Please fill all required fields';
		}
	}
	
	_sels = document.getElementById('review-form').getElementsByTagName('select');
	for(i = 0; i < _sels.length; i++)
	{
		if(_sels[i].value == '')
		{
			_error = 'Please fill all required fields';
		}
	}
	
	if(_error)
	{
		document.getElementById('ratings-block').innerHTML = _error;
		return false;
	}
	else return true;
}

jQuery.fn.simpleLightboxCompare = function(_options){
	// defaults options	
	var _options = jQuery.extend({
		lightboxContentBlock: '.lightbox',
		faderOpacity: .0,
		faderBackground: '#000000',
		closeLink:'a.close',
		from: 'first'
	},_options);
	
	return this.each(function(){
		var _this = jQuery(this);
		_this.lightboxContentBlock = _options.lightboxContentBlock;
		_this.faderOpacity = _options.faderOpacity;
		_this.faderBackground = _options.faderBackground;
		_this.closeLink = _options.closeLink;
		var _fader;
		
		var _lightbox = $(_this.lightboxContentBlock);
		
		if (!jQuery('div.lightbox-fader').length)
			_fader = $('body').append('<div class="lightbox-fader"></div>');
		
		_fader = jQuery('div.lightbox-fader');
		_lightbox.css('zIndex',999);
		_fader.css({
			opacity:_this.faderOpacity,
			backgroundColor:_this.faderBackground,
			display:'none',
			position:'absolute',
			top:0,
			left:0,
			zIndex:998,
			textIndent: -9999
		}).text('$nbsp');
		
		_this.click(function(){
			_fader.fadeIn(300, function(){
				jQuery('.lightbox').fadeOut(400);
				_lightbox.fadeIn(400);
				jQuery.fn.simpleLightboxCompare.positionLightbox();
				if(typeof(hideSelectBoxes) == 'function') hideSelectBoxes(_lightbox);
			});
			
			_ins = document.getElementById('review-form').getElementsByTagName('input');
			for(i = 0; i < _ins.length; i++)
			{
				_ins[i].value == '';
			}
			_sels = document.getElementById('review-form').getElementsByTagName('select');
			for(i = 0; i < _sels.length; i++)
			{
				_sels[i].selectedIndex = 0;
			}
			_sels = document.getElementById('review-form').getElementsByTagName('textarea');
			for(i = 0; i < _sels.length; i++)
			{
				_sels[i].value = '';
			}
			$('#review-model').attr('value',$('#review-model-'+_options.from).attr('value'));
			$('#review-year').attr('value',$('#review-year-'+_options.from).attr('value'));
			$('#review-trim').attr('value',$('#trim-car-'+_options.from).attr('value'));
			document.getElementById('ratings-block').innerHTML = _error;
			return false;
		});
		jQuery(_this.closeLink).click(function(){
			_lightbox.fadeOut(400, function(){
				_fader.fadeOut(300);
				if(typeof(showSelectBoxes) == 'function') showSelectBoxes(_lightbox);
			});
			return false;
		});
		
		jQuery.fn.simpleLightboxCompare.positionLightbox = function () {
			var _height = 0;
			var _width = 0;
			var _minWidth = $('body > div').outerWidth();
			if (window.innerHeight) {
				_height = window.innerHeight;
				_width = window.innerWidth;
			} else {
				_height = document.documentElement.clientHeight;
				_width = document.documentElement.clientWidth;
			}
			var _page = $('body');
			if (_lightbox.length) {
				if (_height > _page.innerHeight()) _fader.css('height',_height); else _fader.css('height',_page.innerHeight());
				if (_width < _minWidth) {_fader.css('width',_minWidth);} else {_fader.css('width','100%');}
				if (_height > _lightbox.innerHeight()) {
					if (!window.innerHeight) {
						_lightbox.css({
							position:'absolute',
							top: (document.documentElement.scrollTop + (_height - _lightbox.outerHeight()) / 2)+"px"
						});
					} else {
						_lightbox.css({
							position:'fixed',
							top: ((_height - _lightbox.outerHeight()) / 2)+"px"
						});
					}
				}
				else {
					_lightbox.css({
						position:'absolute',
						top: 0
					});
				}
				if (_width > _lightbox.outerWidth()) _lightbox.css({left:(_width - _lightbox.outerWidth()) / 2 + "px"});
				else _lightbox.css({position:'absolute',left: 0});
			}
		}
		
		jQuery(window).resize(function(){
			jQuery.fn.simpleLightboxCompare.positionLightbox();
		});
		if (!window.innerHeight) {
			jQuery(window).scroll(function(){
				jQuery.fn.simpleLightboxCompare.positionLightbox();
			});
		}
		
		jQuery.fn.simpleLightboxCompare.positionLightbox();
		
	});
}




function LoadResponse(data){
	jQuery.each(data.select, function(i, field_list) {
		var active_select = $(".ajax-load-field[name*='"+i+"']"); 
		ClearSelect(active_select);
		$(field_list).each(function(cnt,row){
		var name = row.name;
			if (!row.name && row.year)
			{
				name = row.year;
			}
			active_select.append("<option value=\""+row.id+"\">"+name+"</option>");
		});
		active_select.removeAttr('disabled');
	});
}

function setReviewForm(from,to)
{
	if(from) from = "_" + from;
	$('#review-model-'+to).attr('value',$('#select_model'+from).attr('value'));
	$('#review-year-'+to).attr('value',$('#select_year'+from).attr('value'));
}
function ClearSelect(active_select)
{
	if (active_select.hasClass('not-clear')) return;
	var def_value = active_select.children('option:first');
	if (!def_value) return;
	active_select.children().remove();	
	active_select.append(def_value);
}

function LoadResponseFirst(data){
	jQuery.each(data.select, function(i, field_list) {
		var active_select = $(".ajax-load-first[name*='"+i+"']"); 
		ClearSelect(active_select);
		$(field_list).each(function(cnt,row){
		var name = row.name;
			if (!row.name && row.year)
			{
				name = row.year;
			}
			active_select.append("<option value=\""+row.id+"\">"+name+"</option>");
		});
		active_select.removeAttr('disabled');
	});
}

function LoadResponseSecond(data){
	jQuery.each(data.select, function(i, field_list) {
		var active_select = $(".ajax-load-second[name*='"+i+"']"); 
		ClearSelect(active_select);
		$(field_list).each(function(cnt,row){
		var name = row.name;
			if (!row.name && row.year)
			{
				name = row.year;
			}
			active_select.append("<option value=\""+row.id+"\">"+name+"</option>");
		});
		active_select.removeAttr('disabled');
	});
}

function updateReviewsFirst(){
	$('a.open-write-first').simpleLightboxCompare({
		lightboxContentBlock:'.popup-write',
		closeLink:'a.close',
		from:'first'
	});
}

function updateReviewsSecond(){
	$('a.open-write-second').simpleLightboxCompare({
		lightboxContentBlock:'.popup-write',
		closeLink:'a.close',
		from:'second'
	});
}
function clearAll()
{
	
	document.getElementById('select_make').selectedIndex = 0;
	$('.specific-make-field,.specific-model-field,#select_make').each(function(){
			$(this).attr('disabled','disabled');
	});
	ClearSelect($('#select_model'));
	ClearSelect($('#select_year'));
}

function clearCopmare(name){
	if(name)
	{
		$('#car-block-'+name).html('');
		$('#add-vehicle-'+name).css('display','block');
	}
	else
	{
		$('#car-block-first').html('');
		$('#add-vehicle-first').css('display','none');
		$('#car-block-second').html('');
		$('#add-vehicle-second').css('display','none');
	}
	clearAll();
	$('#select_make').removeAttr('disabled');
}

function trimChange(from)
{
	if ($('#trim-car-'+from).attr('value'))
	{
		$('#car-block-'+from).load("?trim="+$('#trim-car-'+from).attr('value')+"&from="+from+"&add=true&model="+$('#review-model-'+from).attr('value')+"&year="+$('#review-year-'+from).attr('value'),'',updateReviewsFirst);
	}
}

function compareAddCar(from)
{
	if(!from)
	{
		if ($('#select_make').attr('value') && $('#select_model').attr('value') && $('#select_year').attr('value'))
		{
			if($('#car-block-first').html() == "")
			{
				$('#car-block-first').load("?from=first&add=true&make="+$('#select_make').attr('value')+"&model="+$('#select_model').attr('value')+"&year="+$('#select_year').attr('value'),'',updateReviewsFirst);
				setReviewForm('', 'first');
			}
			else
			{
				if($('#car-block-second').html() == "")
					$('#car-block-second').load("?from=second&add=true&make="+$('#select_make').attr('value')+"&model="+$('#select_model').attr('value')+"&year="+$('#select_year').attr('value'),'',updateReviewsSecond);
				setReviewForm('', 'second');
				clearAll();
			}
		}
	}
	
	if(from == "first")
	{
		if ($('#select_make_first').attr('value') && $('#select_model_first').attr('value') && $('#select_year_first').attr('value'))
		{
			$('#car-block-first').load("?from=first&add=true&make="+$('#select_make_first').attr('value')+"&model="+$('#select_model_first').attr('value')+"&year="+$('#select_year_first').attr('value'),'',updateReviewsFirst);
			$('#add-vehicle-first').css('display','none');
			setReviewForm(from,from);
		}
	}
	
	if(from == "second")
	{
		if ($('#select_make_second').attr('value') && $('#select_model_second').attr('value') && $('#select_year_second').attr('value'))
		{
			$('#car-block-second').load("?from=second&add=true&make="+$('#select_make_second').attr('value')+"&model="+$('#select_model_second').attr('value')+"&year="+$('#select_year_second').attr('value'),'',updateReviewsSecond);
			$('#add-vehicle-second').css('display','none');
			setReviewForm(from, from);
		}
	}
}

$(document).ready(function(){

	/**
	* Change type block
	*/
	
	if (!$('#select_make').attr('value'))
	{
		$('.specific-make-field,.specific-model-field,.specific-year-field').each(function(){
				$(this).attr('disabled','disabled');
		});
	}
	else
	{
		$('.specific-make-field').each(function(){
				$(this).removeAttr('disabled');
				//ClearSelect($(this));
		});
	}

	$('#select_make').change(function(){
		if($(this).attr('value'))
		{
			$('.specific-make-field').each(function(){
				$(this).removeAttr('disabled');
			});
			$('.specific-model-field,.specific-year-field').each(function(){
				ClearSelect($(this));
				$(this).attr('disabled','disabled');
			});		
			$.getJSON("?ajax=true&make="+$(this).attr('value'),LoadResponse);			
		}
		else
		{
			$('.specific-make-field,.specific-model-field,.specific-year-field').each(function(){
				$(this).attr('disabled','disabled');
				ClearSelect($(this));
			});		
		}
	});
	
	$('#select_model').change(function(){
		if($(this).attr('value'))
		{
			$('.specific-model-field').each(function(){
				$(this).removeAttr('disabled');
			});
			$('.specific-year-field').each(function(){
				ClearSelect($(this));
				$(this).attr('disabled','disabled');
			});	
			$.getJSON("?ajax=true&model="+$(this).attr('value'),LoadResponse
	        );			
		}
		else
		{
			$('.specific-model-field,.specific-year-field').each(function(){
				$(this).attr('disabled','disabled');
				ClearSelect($(this));
			});		
		}
	});
	
	$('#select_make_first').change(function(){
		if($(this).attr('value')!='select year')
		{
			$('.specific-year-field').each(function(){
				$(this).removeAttr('disabled');
			});
			$.getJSON("?ajax=true&year="+$(this).attr('value'),LoadResponse
	        );			
		}
		else
		{
			$('.specific-year-field').each(function(){
				$(this).attr('disabled','disabled');
				ClearSelect($(this));
			});		
		}
	});

	//first form
	
	if (!$('#select_make_first').attr('value'))
	{
		$('.specific-make-first,.specific-model-first,.specific-year-first').each(function(){
				$(this).attr('disabled','disabled');
		});
	}
	else
	{
		$('.specific-make-first').each(function(){
				$(this).removeAttr('disabled');
				//ClearSelect($(this));
		});
	}
	
	$('#select_make_first').change(function(){
		if($(this).attr('value'))
		{
			$('.specific-make-first').each(function(){
				$(this).removeAttr('disabled');
			});
			$('.specific-model-first,.specific-year-first').each(function(){
				ClearSelect($(this));
				$(this).attr('disabled','disabled');
			});		
			$.getJSON("?ajax=true&make="+$(this).attr('value'),LoadResponseFirst);			
		}
		else
		{
			$('.specific-make-first,.specific-model-first,.specific-year-first').each(function(){
				$(this).attr('disabled','disabled');
				ClearSelect($(this));
			});		
		}
	});
	
	$('#select_model_first').change(function(){
		if($(this).attr('value'))
		{
			$('.specific-model-first').each(function(){
				$(this).removeAttr('disabled');
			});
			$('.specific-year-first').each(function(){
				ClearSelect($(this));
				$(this).attr('disabled','disabled');
			});	
			$.getJSON("?ajax=true&model="+$(this).attr('value'),LoadResponseFirst
	        );			
		}
		else
		{
			$('.specific-model-first,.specific-year-first').each(function(){
				$(this).attr('disabled','disabled');
				ClearSelect($(this));
			});		
		}
	});
	
	$('#select_year_first').change(function(){
		if($(this).attr('value')!='select year')
		{
			$('.specific-year-first').each(function(){
				$(this).removeAttr('disabled');
			});
			$.getJSON("?ajax=true&year="+$(this).attr('value'),LoadResponseFirst
	        );			
		}
		else
		{
			$('.specific-year-first').each(function(){
				$(this).attr('disabled','disabled');
				ClearSelect($(this));
			});		
		}
	});
	
	//second form
	if (!$('#select_make_second').attr('value'))
	{
		$('.specific-make-second,.specific-model-second,.specific-year-second').each(function(){
				$(this).attr('disabled','disabled');
		});
	}
	else
	{
		$('.specific-make-second').each(function(){
				$(this).removeAttr('disabled');
				//ClearSelect($(this));
		});
	}
	
	$('#select_make_second').change(function(){
		if($(this).attr('value'))
		{
			$('.specific-make-second').each(function(){
				$(this).removeAttr('disabled');
			});
			$('.specific-model-second,.specific-year-second').each(function(){
				ClearSelect($(this));
				$(this).attr('disabled','disabled');
			});		
			$.getJSON("?ajax=true&make="+$(this).attr('value'),LoadResponseSecond);			
		}
		else
		{
			$('.specific-make-second,.specific-model-second,.specific-year-second').each(function(){
				$(this).attr('disabled','disabled');
				ClearSelect($(this));
			});		
		}
	});
	
	$('#select_model_second').change(function(){
		if($(this).attr('value'))
		{
			$('.specific-model-second').each(function(){
				$(this).removeAttr('disabled');
			});
			$('.specific-year-second').each(function(){
				ClearSelect($(this));
				$(this).attr('disabled','disabled');
			});	
			$.getJSON("?ajax=true&model="+$(this).attr('value'),LoadResponseSecond
	        );			
		}
		else
		{
			$('.specific-model-second,.specific-year-second').each(function(){
				$(this).attr('disabled','disabled');
				ClearSelect($(this));
			});		
		}
	});
});

function initReview()
{
	$("div.popup-write").appendTo("body");
	rating();
}

if (window.addEventListener) window.addEventListener("load", initReview, false);
else if (window.attachEvent) window.attachEvent("onload", initReview);



 

