var intro_animate = true;

/**
 * Executed when document is finished loading elements into DOM
 */
$(document).ready(function()
{
	// display confirmation dialog whenever link with className "confirm" is slicked
	$(".confirm").click(function(){
		return confirm("Vai tiešām vēlaties veikt šo darbību?");
	});

	loadFlashBG();
});

/**
 * Load car models according to selected car make
 *
 * If nothing is selected, empty list
 *
 * @param int make_id Car make ID
 * @param boolean empty If true, empty record is added as first element
 * @param int model_id Car model ID If true, this model is automatically selected
 */
function changeCarMake(make_id, empty, model_id)
{
	if (make_id != "")
	{
		$.getJSON(webroot+"car_models/get_models/"+make_id, {id: $(this).val(), ajax: "true"}, function(j){

			var options = "";

			if (typeof(model_id) == "undefined")
			{
				model_id = -1;
			}

			if (empty)
			{
				options += '<option value="">&nbsp;</option>';
			}

			for (var i = 0; i < j.length; i++)
			{
				selected = "";

				if (j[i].optionValue == model_id)
				{
					selected = "selected='selected'";
				}

				options += '<option '+selected+' value="' + j[i].optionValue + '">' + j[i].optionDisplay + '</option>';
			}

			$("#CarModelId").html(options);
			$("#car-model-container").show();
		});
	}
	else
	{
		$("#car-model-container").hide();
		$("#CarModelId").empty();
	}

	$("#btnAddCarModel").attr("rel", make_id);
}

/**
 * Change car image
 *
 * @param object el "this" element
 * @param string img Car image filename
 * @return boolean (always false)
 */
function changeCarImage(el, img)
{
	$(".gallery > .thumbs > li").removeClass("active");

	$(el).parent("li").addClass("active");

	$("#car-main-img").attr("src", webroot+"img/cars/medium/"+img);

	return false;
}

/**
 * Calculates leasing payments.
 *
 *
 */
function calculate()
{
	var type = $("#CalcType").val();
	var price = $("#CalcPrice").val();
	var period = $("#CalcPeriod").val();
	var interest_rate = $("#CalcInterestRate").val();
	var advance_money = $("#CalcAdvanceMoney").val();
	var advance_percents = $("#CalcAdvancePercents").val();
	var residual_value = $("#CalcResidualValue").val();

	if (advance_money == "")
	{
		advance_money = price * advance_percents / 100;
	}

	if (type == "2")
	{
		residual_value = 0;
	}

	//var params = "?type="+type+"&price="+price+"&period="+period+"&interest_rate="+interest_rate+"&advance_money="+advance_money+"&advance_percents="+advance_percents+"&residual_value="+residual_value

	var params = "type:"+type+"/price:"+price+"/period:"+period+"/interest_rate:"+interest_rate+"/advance_money:"+advance_money+"/advance_percents:"+advance_percents+"/residual_value:"+residual_value;

	window.open (webroot+"pages/calc/"+params, "calculator", "width=600,height=600,scrollbars=1")

	return;
}

function changeCalcType(type)
{
	if (type == "2")
	{
		$("#residual-value").hide();
		$("#CalcResidualValue").val("0");
	}
	else
	if (type == "1")
	{
		$("#residual-value").show();
	}
	else
	if (type == "0")
	{
		$("#CalcAdvanceMoney").val($("#CalcPrice").val() * $("#CalcAdvancePercents").val() / 100);
	}

	calcMonthlyPayment();
}

function calcMonthlyPayment()
{
	var monthly_payment = 0;
	var type = $("#CalcType").val();
	var price = $("#CalcPrice").val();
	var period = $("#CalcPeriod").val();
	var interest_rate = $("#CalcInterestRate").val();
	var advance_money = $("#CalcAdvanceMoney").val();
	var advance_percents = $("#CalcAdvancePercents").val();
	var residual_value = $("#CalcResidualValue").val();

	if (type == "2")
	{
		$("#residual-value").hide();
		residual_value = 0;
	}
	else
	{
		$("#residual-value").show();
	}

	advance_money = price * advance_percents / 100;
	$("#CalcAdvanceMoney").val(advance_money);

	var i = interest_rate/100/12;

	//monthly_payment = (price - advance_money - residual_value/100*price) / ((1-(1 / Math.pow(1+i, period))) / i);
	monthly_payment = i * ((price - advance_money)-((price * residual_value / 100) / (Math.pow(i+1, period)))) / (1 - (1 / Math.pow(i+1, period)));

	$("#CalcMonthlyPayment").val("EUR "+formatCurrency(monthly_payment));

	//$monthly_payment = $i * (($p["price"] - $p["advance_money"])-(($p["price"] * $p["residual_value"] / 100) / (pow($i + 1, $p["period"])))) / (1 - (1 / pow($i + 1, $p["period"])));
}


/**
 *
 *
 * @param int amount
 * @return string
 */
function formatCurrency(amount)
{
	var cents = amount * 100;

	cents = Math.round(cents);

	str = "" + cents;

	len = str.length;

	return str.substring(0, len - 2) + "." + str.substring(len - 2, len);
}

/**
 *
 *
 * @param i Banner's numeric representation
 * @param boolean manual If true, stop automatic animation
 * @return boolean (always false)
 */
function loadIntro(i, manual)
{
	if (manual || (!manual && intro_animate))
	{
		//$("#offer-content").hide().html($("#ban"+i).html()).fadeIn("fast");
		$("#offer-content").html($("#ban"+i).html());

		$(".ban li").removeClass("active");
		$("#ban-li-"+i).addClass("active");

		if (!manual)
		{
			var next_i = i + 1;

			if ($("#ban"+next_i).length == 0)
			{
				var next_i = 1;
			}

			setTimeout("loadIntro("+next_i+", 0)", 10000);
		}
		else
		{
			intro_animate = false;
		}
	}

	return false;
}

/**
 * Toggle section addon
 *
 * @param int id Section addon ID
 * @return boolean (always false)
 */
function showSectionAddon(id)
{
	$(".section-addons li").removeClass("active");

	if (!$("#section_addon_"+id).is(":visible"))
	{
		$("#section-addon-"+id).addClass("active");
	}

	if (jQuery.browser.msie)
	{
		$("#section_addon_"+id).toggle();
		loadFlashBG();
	}
	else
	{
		$("#section_addon_"+id).slideToggle(500);
		setTimeout("loadFlashBG()", 700);
	}

	return false;
}

function loadFlashBG()
{
	$("#flashbg-container").css("height", (parseInt($(".innerpage").height())-100)+"px");
	$("#flashbg").css("height", (parseInt($(".innerpage").height())-100)+"px");
}


/**
 * Print email link (protection from spambots)
 * If javascript is disabled, nothing is visible
 *
 * @param string p1 Part 1 - this is username
 * @param string p2 Part 2 - this is domain and TLD
 */
function writemail(p1, p2)
{
	document.write("<a href='mailto:"+p1+"@"+p2+"'>"+p1+"@"+p2+"</a>");
}



	function getX(el)
	{
		var curleft = 0;

		if (el.offsetParent)
		{
			while (el.offsetParent)
			{
				curleft += el.offsetLeft;
				el = el.offsetParent;
			}
		}
		else if (el.x)
		{
			curleft += el.x;
		}

		return curleft;
	}

	function getY(el)
	{
		var curtop = 0;

		if (el.offsetParent)
		{
			while (el.offsetParent)
			{
				curtop += el.offsetTop;
				el = el.offsetParent;
			}
		}
		else if (el.y)
		{
			curtop += el.y;
		}

		return curtop;
	}

	function showTooltip(city, x, y)
	{
		page_x = getX(document.getElementById("page"));

		karte_x = getX(document.getElementById("img_karte")) - page_x;
		karte_y = getY(document.getElementById("img_karte"));

		var tooltip = document.getElementById("tooltip");

		tooltip.innerHTML = info[city];

		var offset_y = $("#tooltip").height() - 82;

		tooltip.style.left = karte_x + x - 125 + "px";
		tooltip.style.top = karte_y + y - 200 - offset_y + "px";

		tooltip.style.display = "block";
	}

	function showTooltip1(city, x, y)
	{
		page_x = getX(document.getElementById("page"));

		karte_x = getX(document.getElementById("img_karte1")) - page_x;
		karte_y = getY(document.getElementById("img_karte1"));

		var tooltip = document.getElementById("tooltip");

		if (city == "Centralais birojs")
		{
			$("#tooltip").addClass("tooltip-big");
		}
		else
		{
			$("#tooltip").removeClass("tooltip-big");
		}

		tooltip.innerHTML = info[city];

		var offset_y = $("#tooltip").height() - 82;

		tooltip.style.left = karte_x + x - 125 + "px";
		tooltip.style.top = karte_y + y - 200 - offset_y + "px";

		tooltip.style.display = "block";
	}


	function hideTooltip()
	{
		var tooltip = document.getElementById("tooltip");
		tooltip.style.display = "none";
	}