var url = "http://"+document.domain+"/";
var curSplash = 0;
var splash = null;
var splTimer = null;
jQuery(document).ready(function() {
/*
    var regEmail = /\/([a-zA-Z0-9\-\.]+)\.m\.([a-zA-Z0-9\-\.]+)\/([a-zA-Z]{2,6})\//g;
    document.body.innerHTML = document.body.innerHTML.replace(regEmail, '$1@$2.$3');
*/
	$("a[rel='lightbox-item']").colorbox({transition:"fade"});
	$(".fbv").validationEngine();
	
	$("a.onoff").bind("click", function() {
		showHide($("#"+$(this).attr("rel")));
	});
	
	splash = $("div.splash img");
	if (splash.length) {
		$(splash[0]).fadeIn("fast");
		splTimer = setInterval(changeSplash, 9000);
	}
	
	$("div.nav div").bind("mouseover", function(){$(this).addClass("hover");});
	$("div.nav div").bind("mouseout", function(){$(this).removeClass("hover");});
	$("div.nav div").bind("click", nav);
	
	$(".details").bind("click", function() {
		var tab = $(this).parent().find("table");
		$(this).hide();
		tab.slideDown("slow");
		
	});
	
	$("#getstat").bind("click", function(){
/*
		var data = $("#stat").serialize();
		$.ajax({
			  type: 'POST',
			  url: 'http://ya.ru/',
			  data: data,
			  success: function(data) {
				  alert(data)
			  }
			});
*/
	});
	$("#country").autocomplete('toolscalc.php', {
		multiple: false,
		extraParams: {"act":"country"},
		dataType: "json",
		parse: function(data) {
			return $.map(data, function(row) {
				return {
					data: row,
					value: row.name,
					result: row.name
				}
			});
		},
		formatItem: function(item) {
			return format(item);
		}
	}).result(function(e, item) {
		if (item.type == 2) {
			showObl(item);
		} else showCity(item,"country");
	});
	
	$("#newcalc").bind("click", newCalc);
	$("form#calc").bind("submit", calc);
	
	$("td.tab").bind("click", changeDirect);
});

function changeDirect() {
	var rel = $(this).attr("rel");
	$("#direct").attr("value", rel);
	$("td.tab").removeClass("act");
	$(this).addClass("act");
}

function checkLocation() {
	
}

function showObl(item) {
	$("#country1").attr("value", item.name);
	$(".obl").show();
	$("#country").attr("disabled", "true");
	$("#obl").focus();
	$("#obl").autocomplete('toolscalc.php', {
		multiple: false,
		extraParams: {"act":"obl", "pid":item.id},
		dataType: "json",
		parse: function(data) {
			return $.map(data, function(row) {
				return {
					data: row,
					value: row.name,
					result: row.name
				}
			});
		},
		formatItem: function(item) {
			return format(item);
		}
	}).result(function(e, item) {
		showCity(item,"obl");
	});
}

function showCity(item, field) {
	$("#country1").attr("value", $("#country").attr("value"));
	$("#obl1").attr("value", $("#obl").attr("value"));
	$("#"+field).attr("disabled", "true");
	$(".city").show();
	$("#city").focus();
	$("#city").autocomplete('toolscalc.php', {
		multiple: false,
		extraParams: {"act":"city", "pid":item.id},
		dataType: "json",
		parse: function(data) {
			return $.map(data, function(row) {
				return {
					data: row,
					value: row.name,
					result: row.name
				}
			});
		},
		formatItem: function(item) {
			return format(item);
		}
	}).result(function(e, item) {
		$("#city").attr("disabled", true);
		$("#city1").attr("value", item.name);
	});

}

function newCalc() {
	$("#obl").unautocomplete();
	$("#city").unautocomplete();
	$("#obl").attr("disabled", false);
	$("#city").attr("disabled", false);
	$("#country").attr("disabled", false);
	$("#country").attr("value","");
	$("#obl").attr("value","");
	$("#city").attr("value","");
	$(".obl").hide();
	$(".city").hide();
	$("#weight").attr("value","");
	$("#gab1").attr("value","");
	$("#gab2").attr("value","");
	$("#gab3").attr("value","");
	$("#country1").attr("value", "");
	$("#obl1").attr("value", "");
	$("#city1").attr("value", "");
	$(".calcError").html("");
	$(".calcResultContent").html("");
	$(".calcAltContent").html("");
}

function format(item) {
	return item.name+(item.alt?" <font class=alt>"+item.alt:"</font>");
}

function calc() {
	$.getJSON("toolscalc.php", $("#calc").serialize(), calcResult);
	return false;
}

function calcResult(data, status, jq) {
	$("div.calcError").html(data.error);
	$("div.calcResultContent").html(data.res);
	//$("div.calcAltContent").html(data.alt);
	$("div.calcAltPrim").html(data.tmp);
}


function statok(data) {
	alert(data);
}

function showHide(el) {
	if (el.data('currentState') == "on") {
		el.data('currentState', 'off');
		el.slideUp("fast");
		
	}
	else {
		el.data('currentState', 'on');
		el.slideDown("fast");
		
	}
}

function changeSplash() {
	$(splash[curSplash]).stop(true, true);
	$(splash[curSplash]).fadeOut("slow");
	curSplash++;
	if (curSplash==splash.length) curSplash = 0;
	$(splash[curSplash]).stop(true, true);
	$(splash[curSplash]).fadeIn("slow");
}

function nav() {
	clearInterval(splTimer);
	$(splash[curSplash]).stop(true, true);
	splTimer = setInterval(changeSplash, 5000);
	var step = $(this).attr("rel")*1;
	$(splash[curSplash]).fadeOut("slow");
	curSplash += step;
	if (curSplash==splash.length) curSplash = 0;
	if (curSplash==-1) splash.length = splash.length-1;
	$(splash[curSplash]).stop(true, true);
	$(splash[curSplash]).fadeIn("slow");
}
