$(function(){
	Cufon.replace('.cfon');
	Cufon.now();
	
	$("div.topMenu").find("li.first").bind("mouseover", function(){
		$("div.topMenu").addClass("topMenu-fact");
	});
	$("div.topMenu").find("li.last").bind("mouseover", function(){
		$("div.topMenu").addClass("topMenu-lact");
	});
	$("div.topMenu").find("li.first").bind("mouseout", function(){
		$("div.topMenu").removeClass("topMenu-fact");
	});
	$("div.topMenu").find("li.last").bind("mouseout", function(){
		$("div.topMenu").removeClass("topMenu-lact");
	});
	
	cuSel({changedEl: ".dpSelect", visRows: 10, scrollArrows: true});
	
	$("#bankRates").bind("change", function(){
		if(typeof arFinance != 'undefined'){
			if(typeof arFinance[$(this).val()] != 'undefined'){
				cache("#rates_buy_usd").text(arFinance[$(this).val()]["RATES"]["USD"][0]);
				cache("#rates_sell_usd").text(arFinance[$(this).val()]["RATES"]["USD"][1]);
				cache("#rates_buy_eur").text(arFinance[$(this).val()]["RATES"]["EUR"][0]);
				cache("#rates_sell_eur").text(arFinance[$(this).val()]["RATES"]["EUR"][1]);
				if(typeof arFinance[$(this).val()]["RATES"]["RUB"] != 'undefined'){
					cache("#rates_buy_rub").text(arFinance[$(this).val()]["RATES"]["RUB"][0]);
					cache("#rates_sell_rub").text(arFinance[$(this).val()]["RATES"]["RUB"][1]);
				}else{
					cache("#rates_buy_rub").text("нет");
					cache("#rates_sell_rub").text("нет");
				}
			}
		}
	});
	
	$("#fuelRates").bind("change", function(){
		if(typeof arFuel != 'undefined'){
			if(typeof arFuel[$(this).val()] != 'undefined'){
				cache("#rates_a80").text(arFuel[$(this).val()]["RATES"]["DT"]);
				cache("#rates_a92").text(arFuel[$(this).val()]["RATES"]["A92"]);
				cache("#rates_a95").text(arFuel[$(this).val()]["RATES"]["A95"]);
			}
		}
	});
	cache("#cuselFrame-fuelRates").css("z-index", 1);
});

var globalCache = {};
function cache(selector){
	key = selector.replace(" ", "");
	if(typeof globalCache[key] != "undefined"){
		return globalCache[key];
	}else{
		if($(selector).length > 0){
			globalCache[key] = $(selector);
			return globalCache[key];
		}else{
			return $(selector);
		}
	}
}

function viewDetailPic(el){
	src = $(el).attr('href');
	title = $(el).attr('title');
	if(src.indexOf('jpg') != -1 || src.indexOf('png') != -1 || src.indexOf('gif') != -1){
		var backFon = '<div id="backFon" style="position: absolute; width:100%;height:100%;top:0px;left:0px;background-color: #000;opacity:0.3;filter:alpha(opacity=30);z-index:5555;"></div>';
		$("body").prepend(backFon);
		$("#backFon").css({'height':$(document).height()+'px'});
		var pic = '<div id="picHolder" style="z-index:9999;background-color:#fff;position:absolute;padding:10px;border:1px solid #000;"><img src="'+src+'" alt="'+title+'" title="'+title+'"/></div>';
		$("body").prepend(pic);

		$("#picHolder").css({'top':($(document).scrollTop() + ($(window).height() / 2) - ($("#picHolder").outerHeight() / 2))+'px', 'left':(($(window).width() / 2) - ($("#picHolder").outerWidth() / 2))+'px'});
		$("#backFon").bind("click", function(){
			$("#backFon").remove();
			$("#picHolder").remove();
		});
		
		$("#picHolder").bind("click", function(){
			$("#backFon").remove();
			$("#picHolder").remove();
		});
	}
}
