function showLoginForm () {
	inelement = document.getElementById('form_login');
	outelement = document.getElementById('greeting_login');
	transvar = 0;
	makeTransition ();
}

function showPhotos () {
	inelement = document.getElementById('photosalbum');
	if (inelement.style.display == 'none') {
		document.getElementById('photo_button').setAttribute ('class', 'p_button_active');
		document.getElementById('price_button').setAttribute ('class', 'p_button');
		outelement = document.getElementById('fileprices');
		transvar = 0;
		makeTransition ();
	}
}

function showPrices () {
	inelement = document.getElementById('fileprices');
	if (inelement.style.display == 'none') {
		document.getElementById('price_button').setAttribute ('class', 'p_button_active');
		document.getElementById('photo_button').setAttribute ('class', 'p_button');
		outelement = document.getElementById('photosalbum');
		transvar = 0;
		makeTransition ();
	}
}

function makeTransition () {
	if (transvar > 100) {
		outelement.style.display = 'none';
	} else {
		if (transvar == 0) {
			inelement.style.opacity = 0;
			inelement.style.filter = 'alpha(opacity=0)';
			inelement.style.display = 'block';
			inelement.style.zIndex = '1';
			outelement.style.zIndex = '0';
			if (is_ie) {
				transvar += 4;
			} else {
				transvar += 2;
			}
		} else {
			inelement.style.opacity = (transvar/100);
			inelement.style.filter = 'alpha(opacity='+transvar+')';
			outelement.style.opacity = ((100 - transvar)/100);
			outelement.style.filter = 'alpha(opacity='+(100 - transvar)+')';
			if (is_ie) {
				transvar += 4;
			} else {
				transvar += 2;
			}
		}
		waittransition ();
	}
}

function waittransition () {
	setTimeout (makeTransition, 5);
}

var inelement;
var outelement;
var transvar;

function checkvoteform () {
	var voteform = document.getElementById('voteform');
	var inputs = voteform.getElementsByTagName('input');
	var onlyzeros = true;
	for (var i=0; i<inputs.length; i++) {
		if (inputs[i].name.match (/^criteria/i) && inputs[i].value != 0) {
			onlyzeros = false;
		}
	}
	if (onlyzeros) {
		alert ("Вы или не выставили ни одной оценки или настолько ненавидите этого дилера?\n\nВыставьте хоть одну оценку");
	} else {
		voteform.submit();
	}
}

function changebg (divid, id) {
	if (!getvotevalue (divid)) {
		var i;
		for (i=1; i<=id; i++) {
			document.getElementById(divid+'rate'+i).style.backgroundPosition = '0 30px';
		}
	
		document.getElementById('criteria_'+divid).innerHTML = id+',0';
	}
}

function restorebg (divid, id) {
	if (!getvotevalue (divid)) {
		for (var i=1; i<=id; i++) {
			document.getElementById(divid+'rate'+i).style.backgroundPosition = '0 15px';
		}
		document.getElementById('criteria_'+divid).innerHTML = '0,0';
	}
}

function fixvalue (divid, id) {
	for (var i=1; i<=id; i++) {
		document.getElementById(divid+'rate'+i).style.backgroundPosition = '0 0';
	}
	for (i=i; i<6; i++) {
		document.getElementById(divid+'rate'+i).style.backgroundPosition = '0 15px';
	}

	document.getElementById('criteria_'+divid).innerHTML = id+',0';

	var voteform = document.getElementById('voteform');
	var inputs = voteform.getElementsByTagName('input');
	for (i=0; i<inputs.length; i++) {
		if (inputs[i].name == 'criteria_'+divid) {
			 inputs[i].value = id;
		}
	}
}

function getvotevalue (divid) {
	var voteform = document.getElementById('voteform');
	var inputs = voteform.getElementsByTagName('input');
	var i;
	for (i=0; i<inputs.length; i++) {
		if (inputs[i].name == 'criteria_'+divid && inputs[i].value != 0) {
			return inputs[i].value;
		}
	}
	return false;
}

function reset (divid) {
	for (var i=1; i<6; i++) {
		document.getElementById(divid+'rate'+i).style.backgroundPosition = '0 15px';
	}

	document.getElementById('criteria_'+divid).innerHTML = '0,0';

	var voteform = document.getElementById('voteform');
	var inputs = voteform.getElementsByTagName('input');
	for (i=0; i<inputs.length; i++) {
		if (inputs[i].name == 'criteria_'+divid) {
			 inputs[i].value = 0;
		}
	}
}

init ();

function init () {
	if (null === document.getElementById('banner_block')) {
		setTimeout (init, 10);
	} else {
		div_width = document.getElementById('banner_block').offsetWidth;
		move_val = div_width;
		setInterval (autoslide, 8000);
	}
}

var direction;
var div_width;
var move_val;
var current;
var moving;
var finished = true;

function slideleft() {
	if (finished) {
		finished = false;
		var results = find_active ();
		var active_banner = results[0];
		var banner_quantity = results[1];
		if (banner_quantity > 1) {
			if (active_banner == banner_quantity - 1) {
				var for_move = 0;
			} else {
				var for_move = active_banner + 1;
			}
			current = document.getElementById('banner'+active_banner);
			moving = document.getElementById('banner'+for_move);
			moving.style.display = 'block';
			moving.style.right = '-'+div_width+'px';
		
			toleft ();
		}
	}
}

function slideright() {
	if (finished) {
		finished = false;
		var results = find_active ();
		var active_banner = results[0];
		var banner_quantity = results[1];
		if (banner_quantity > 1) {
			if (active_banner == 0) {
				var for_move = banner_quantity - 1;
			} else {
				var for_move = active_banner - 1;
			}
			current = document.getElementById('banner'+active_banner);
			moving = document.getElementById('banner'+for_move);
			moving.style.display = 'block';
			moving.style.left = '-'+div_width+'px';
		
			toright ();
		}
	}
}

function toleft () {
	current.style.left = '-'+(div_width-move_val)+'px';
	moving.style.right = '-'+move_val+'px';
	setTimeout (minusone, 10);
}

function toright () {
	current.style.right = '-'+(div_width-move_val)+'px';
	moving.style.left = '-'+move_val+'px';
	setTimeout (plusone, 10);
}

function minusone () {
	if (move_val == 0) {
		current.style.display = '';
		current.style.left = '';
		current.style.right = '';
		moving.style.right = '-'+(div_width-move_val+10)+'px';
		direction = 0;
		returntoright ();
	} else {
		if (Math.round (move_val/10) < 10) {
			if (move_val < 5) {
				move_val = 0;
			} else {
				move_val -= 10;
			}
				toleft ();
		} else {
			move_val -= Math.round (move_val/10);
			toleft ();
		}
	}

}

function plusone () {
	if (move_val == 0) {
		current.style.display = '';
		current.style.left = '';
		current.style.right = '';
		moving.style.left = '-'+(div_width-move_val+10)+'px';
		direction = 1;
		returntoleft ();
	} else {
		if (Math.round (move_val/10) < 10) {
			if (move_val < 5) {
				move_val = 0;
			} else {
				move_val -= 10;
			}
				toright ();
		} else {
			move_val -= Math.round (move_val/10);
			toright ();
		}
	}

}


function returnfromright () {
	moving.style.right = (move_val * -1)+'px';
	setTimeout (returntoright, 10);
}


function returnfromleft () {
	moving.style.left = (move_val * -1)+'px';
	setTimeout (returntoleft, 10);
}

function returntoright () {
	if (move_val == 0 && direction == 1) {
		moving.style.display = 'block';
		moving.style.left = '';
		moving.style.right = '';
		move_val = div_width;
		finished = true;
	} else {
		if (move_val == -50) { direction = 1; }
		if (direction == 0) {
			move_val -= 5;
		} else {
			move_val += 5;
		}
		returnfromright ();
	}
}


function returntoleft () {
	if (move_val == 0 && direction == 0) {
		moving.style.display = 'block';
		moving.style.left = '';
		moving.style.right = '';
		move_val = div_width;
		finished = true;
	} else {
		if (move_val == -50) { direction = 0; }
		if (direction == 1) {
			move_val -= 5;
		} else {
			move_val += 5;
		}
		returnfromleft ();
	}
}

function find_active () {
	var banner_div = document.getElementById('banner_block');
	var in_divs = banner_div.getElementsByTagName('div');
	var banner_quantity = 0;
	var active_banner;
	var i;
	for (i=0; i<in_divs.length; i++) {
		if (in_divs[i].id.match (/^banner/i)) {
			banner_quantity++;
			if (in_divs[i].style.display == 'block') {
				active_banner = in_divs[i].id.substr (6);
				active_banner = parseInt (active_banner);
			}
		}
	}
	return [active_banner,banner_quantity];
}

var wait = 0;

function autoslide () {
	if (wait == 0) {
		slideleft ();
	} else {
		wait--;
	}
}

function check_comment_form (comment_type) {
	var comment_text = document.getElementById('comment_text').value;
	var default_text = document.getElementById('default_text').innerHTML;
	if ( comment_text == default_text || comment_text == '') {
		alert ('Вы не ввели текст комментария. Нечего отправлять');
	} else {
		var type_input = document.getElementById('comment_type').value = comment_type;
		document.comment_form.submit();
	}
}

function slide_down () {
	var dealer_table = document.getElementById('dealer_table');
	dealer_slider = document.getElementById('dealer_slider');

	var trs = dealer_table.getElementsByTagName('tr');
	var start_point = trs[4].offsetTop;
	var end_point = dealer_table.offsetHeight - trs[4].offsetTop;
	running_offset = end_point - start_point;
	first_offset = start_point;
	for (var i=4; i<trs.length; i++) {
		tr_arr.push (trs[i]);
	}
	shift_direction = 'down';
	shift_it ();

}

function slide_up () {
	var dealer_table = document.getElementById('dealer_table');
	var trs = dealer_table.getElementsByTagName('tr');
	for (var i=(trs.length - 1); i>3; i--) {
		tr_arr.push (trs[i]); 
	}
	shift_direction = 'up';
	shift_it ();

}

var tr_arr = [];
var dealer_slider;
var shift_direction;

function shift_it () {
	if (tr_arr.length > 0) {
		var tds = tr_arr[0].getElementsByTagName('td');
		for (var t=0; t<tds.length; t++) {
			if (shift_direction == 'down') {
				tds[t].removeAttribute('style');
			} else {
				tds[t].setAttribute('style', 'display:none');
			}
		}
		tr_arr.shift();
		setTimeout (shift_it, 20);
	} else {
		var imgs = dealer_slider.getElementsByTagName('img');
		var as = dealer_slider.getElementsByTagName('a');
		if (shift_direction == 'down') {
			imgs[0].src = imgs[0].src.replace (/rating/, 'rating_');
			as[0].setAttribute('onClick', 'slide_up \(\);return false;');
		} else {
			imgs[0].src = imgs[0].src.replace (/rating_/, 'rating');
			as[0].setAttribute('onClick', 'slide_down \(\);return false;');
		}
	}
}

if (navigator.userAgent.match (/msie/i)) {
	var is_ie = true;
} else {
	var is_ie = false;
}
