var redirect = /^http:\/\/marotti\.pl\/$/;

if( redirect.test(window.location.href) ) {
	window.location.href = 'http://marotti.pl/pl/';
} 

var counter = 0;

$(function() {
	setInterval("changeHeader()", 10000);
	$("#sub_handler").bind('mouseenter', submenuShow);
	$("#sub_menu").bind('mouseleave', submenuHide);
	$("#doLogin").click(function() {
		ajaxCall('login.php', 'POST', 'p=' + $("#passowrd").val(), doLogin);
	});
	$('body').keypress(function(e) {
		if(e.keyCode == 27) {
			$("#login").fadeOut('slow');
		}
	});
	$("#logout").click(function() {
		$("#login").fadeOut('slow')
	});
	if(window.location.href.indexOf('informacje') != -1) counter = 2190;
	if(window.location.href.indexOf('ofirmie') != -1) counter = 1825;
	if(window.location.href.indexOf('concept') != -1 || 
		window.location.href.indexOf('nowymodel') != -1) counter = 1460;
	if(window.location.href.indexOf('media') != -1 || 
		window.location.href.indexOf('ukrytastrona') != -1) counter = 1095;
	if(window.location.href.indexOf('kontakt') != -1) counter = 730;
	if(window.location.href.indexOf('galeria') != -1) counter = 365;
});

function ajaxCall(_script, _method, _message, _function) {
	if(window.XMLHttpRequest) request = new XMLHttpRequest();
	else request = new ActiveXObject("Microsoft.XMLHTTP");;
	if(request) {
		var ajaxFolder = '';
		request.open(_method, ajaxFolder + encodeURI(_script));
		if(_method == "POST") request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
		request.onreadystatechange = function () {
			if(request.readyState == 4) {
				if(request.status == 200) {
					_function();
				}
			}
		}
		request.send( encodeURI(_message) );
	} else {
	  alert("Niestety, musisz zainstalowac nowsza przegladarke.");
	}
}

function changeHeader() {
	$("#page_top").animate({"backgroundPosition": "0 " + (counter += 365) + "px"}, 1500, function() {
		if(counter == 2555) {
			$("#page_top").css('background-position', '0 0');
			counter = 0;
		}
	});
}

function submenuShow() {
	$("#sub_menu").fadeIn('fast');
}

function submenuHide() {
	$("#sub_menu").fadeOut('fast');
}

function login() {
	$("#login").fadeIn('slow');
}

function doLogin() {
	if(request.responseText == 'error') {
		alert('Wpisane hasło jest błędne!');
		$("#passowrd").val('');
	} else {
		window.location.href = request.responseText;
	}
}

function popupHide() {
	$("#popup").fadeOut('fase');
}

function popupShow(_item) {
	switch(_item) {
		case 'hamulce':
			$("#popup").html('<p style="font-size:8pt"><b>Brakes</b>: disk brakes without brake assist</p>');
		break;
		
		case 'ogumienie':
			$("#popup").html('<p style="font-size:8pt"><b>Tires</b>:<br /><br /><b>Front</b>: 255/45R17<br /><b>Back</b>: 315/45R17</p>');
		break;
		
		case 'silnik':
			$("#popup").html('<p style="font-size:8pt;padding-right:0"><b>Engine</b>:<br />160-190 KM<br /><br /><b>Gearbox</b>:<br />6 speed sequential gearbox, electric reverse gear</p>');
			break;
			
		case 'masa':
			$("#popup").html('<p style="font-size:8pt"><b>Proper Weight</b>: 440 kg,<br /><b>Length</b>: 3200 mm,<br /><b>Width</b>: 1800 mm</p>');
			break;
		
		case 'zawieszenie':
			$("#popup").html('<p style="font-size:8pt"><b>Suspension</b>:<br /><br /><b>Front</b>: double rocker arm<br /><b>Back</b>: roller rocker arm, shock absorption system fully regulated</p>');
			break;
	}
	$("#popup").fadeIn('fase');
}

