$(document).ready(function(){

	$("#body").pngFix();
	loadChat();

	// google map
	if ($("#map").is(":visible")){initMap();$("#map").corner("20px");}

	// cv form submit
	$("#cv_send").click(function(){
		$("#cv_msg").html('<img id="loader" src="/img/loader3.gif" width="31" height="31" border="0" />').load("/libraries/includes/actions/forms.php", {
			action:"CV", cv_sname:$("#cv_sname").val(), cv_name:$("#cv_name").val(), cv_pat:$("#cv_pat").val(),
			cv_bdateyear:$("#cv_bdateyear").val(), cv_bdatemonth:$("#cv_bdatemonth").val(), cv_bdateday:$("#cv_bdateday").val(),
			cv_place:$("#cv_place").val(), cv_family:$("#cv_family").val(), cv_specialty:$("#cv_specialty").val(), cv_address:$("#cv_address").val(),
			cv_phone:$("#cv_phone").val(), cv_type:$("input[name='cv_type']:checked").val(), cv_type_cause:$("#cv_type_cause").val(),
			cv_qual:$("#cv_qual").val(), cv_work:$("input[name='cv_work']:checked").val(),
			cv_call:$("input[name='cv_call']:checked").val(), cv_call_name:$("#cv_call_name").val(), cv_call_phone:$("#cv_call_phone").val(),
			cv_aboutwork:$("#cv_aboutwork").val(), cv_salary:$("#cv_salary").val(), cv_uniwork:$("#cv_uniwork").val(), cv_unisaid:$("#cv_unisaid").val(),
			cv_mil:$("input[name='cv_mil']:checked").val(), cv_mil_cause:$("#cv_mil_cause").val(),
			cv_drive:$("input[name='cv_drive']:checked").val(), cv_education:$("#cv_education").val(), cv_languages:$("#cv_languages").val(),
			cv_computer:$("#cv_computer").val(), cv_seminars:$("#cv_seminars").val(), cv_notes:$("#cv_notes").val()
		}, function(){
			$('html, body').animate({scrollTop:0}, 'slow');
			if ($("#cv_msg .error").is(":visible")==false) $("#cv_form").hide();
		}).css({padding:"0 0 20px 0"}).show();
	});

	// anonses clip
	var clipTimeout;
	autoChange();
	$("#clipBack").click(function(){change('back');});
	$("#clipNext").click(function(){change('next');});
	$("#clipBack").hover(function(){if ($("#clipBack").attr("class")=="clipBack") $("#clipBack").removeClass().addClass("clipOverBack");});
	$("#clipNext").hover(function(){if ($("#clipNext").attr("class")=="clipNext") $("#clipNext").removeClass().addClass("clipOverNext");});
	$("#clipBack").mouseout(function(){if ($("#clipBack").attr("class")=="clipOverBack") $("#clipBack").removeClass().addClass("clipBack");});
	$("#clipNext").mouseout(function(){if ($("#clipNext").attr("class")=="clipOverNext") $("#clipNext").removeClass().addClass("clipNext");});

	//maindialog
	$("#want").change(function(){$(".header #message").html('<img id="loader" src="/img/loader3.gif" width="31" height="31" border="0" />').load("/libraries/includes/actions/maindialog.php", {action:"true", lang:currLang, id:$(this).val()});});


	// rounded corners
	$(".head, .menu .selected, .content .kurs, .calculator, #consult, .rightside #box, .rightside #box2, #shop").corner("20px");
	$(".input, #dialogbox, .graybtn, .orangebtn, .content .important").corner("10px");
	$(".liga img").uncorner();
	$(".liga .photo .smallphoto, .liga .photo .largephoto, .liga .photo").corner("20px");

	// calculator
	$("#form_count").keypress(function(e){if(e.which!=8&&e.which!=0&&(e.which<48||e.which>57))return false;});
	$("#form_payment").keypress(function(e){if(e.which!=8&&e.which!=0&&(e.which<48||e.which>57))return false;});
	$("#form_percent").keypress(function(e){if(e.which!=8&&e.which!=0&&(e.which<48||e.which>57))return false;});
	$("#btn_calculate").click(function(){
		var calValute = $("#form_valute").val(),
			calCount = $("#form_count").val(),
			calTime = $("#form_time").val(),
			calTimeVal = $("#form_time_val").val(),
			calPayment = $("#form_payment").val(),
			calPercent = $("#form_percent").val();
		if (!calCount||calCount==0) {$("#form_count").css({border:"solid 1px red"});return false;} else $("#form_count").css({border:"solid 1px #6a757c"});
		if (!calPercent||calPercent==0) {$("#form_percent").css({border:"solid 1px red"});return false;} else $("#form_percent").css({border:"solid 1px #6a757c"});
		$("#calculateResult").css({padding:"15px"}).show();
		$("#calculateResult .result").html('<img src="/img/loader3.gif" width="31" height="31" border="0" />').slideDown("fast").load("/libraries/includes/actions/cal.loan.php", {action:"true", lang:currLang, valute:calValute, count:calCount, timeval:calTimeVal, time:calTime, pay:calPayment, percent:calPercent});
	});

	// credit chance
	$("#cc1").keyup(function(e){var count = $("#cc1").val() - $("#cc2").val() - $("#cc3").val();$("#cc4").val(count);if(e.which!=8&&e.which!=0&&(e.which<48||e.which>57)) return false;});
	$("#cc2").keyup(function(e){var count = $("#cc1").val() - $("#cc2").val() - $("#cc3").val();$("#cc4").val(count);if(e.which!=8&&e.which!=0&&(e.which<48||e.which>57)) return false;});
	$("#cc3").keyup(function(e){var count = $("#cc1").val() - $("#cc2").val() - $("#cc3").val();$("#cc4").val(count);if(e.which!=8&&e.which!=0&&(e.which<48||e.which>57)) return false;});
	
	// dialogs
    $("#inetbank").click(function(){
	//openDialogBox("inetbank");
	window.location='http://online.unibank.az/'+currLang+'/index.shtml';
	});
    $("#onlinePayments").click(function(){
	//openDialogBox("inetbank");
	window.location='http://billpay.unibank.az/';
	});
    $("#callme").click(function(){openDialogBox("callme");});
    $("#onlinechat").click(function(){openDialogBox("onlineform");});
    $("#lastvisit").click(function(){openDialogBox("lastvisit");});
	$("#btnConsult").click(function(){openDialogBox("onlineform", true);});
    $("#exit").click(function(){$("#dialogbox").removeClass();$("#dialogbox").hide();});

	//sitemap
	$(".sitemap").click(function(){
		$("#dialogbox").hide();
		if ($("#sitemap").is(":hidden")) {
			$(".head").uncorner().corner("round tl tr 20px");
			$(".sitemap img").attr("src","/img/bottom.png");
			$("#sitemap").slideDown("slow", function() {
				$(this).show();
			});
			$("#sitemap").corner("round bl br 20px cc:#f47123");
		} else {
			$(".sitemap img").attr("src","/img/next.gif");
			$("#sitemap").slideUp("slow", function() {
				$(".head").uncorner().corner("20px");
				$(this).hide();
			});
		}	
	});

	// tabs
    if ($(".tabs").is(":visible")==true) {
		openTabContent("currency");
		$("#tabnews").click(function(){openTabContent("news");});
		$("#tabcurrency").click(function(){openTabContent("currency");});
		$("#tablink").click(function(){openTabContent("link");});
		$("#tabsite").click(function(){openTabContent("site");});
	}

	// internet bank
	$(".content .inetbank .input").css({border:'solid 1px #d4d4d4'});
	$(".content .inetbank #login").focus(function(){$("#login").val("");});
	$(".content .inetbank #pass").focus(function(){$("#pass").val("");});

	// search
	var what;
	var item = 0;
    $("#what").focus(function(){if (!what) {$("#what").val("");what = true;}});
	$("#what").blur(function(){$("#searchwindow").hide();});
	$("#searchwindow").hover(function(){$("#searchwindow").show();});
	$("#what").keyup(function(e){if (e.which==40) {item++;if (item>searchCount) item=searchCount;selectTag("down", item);} else if (e.which==38) {item--;if (item<0) item=0;selectTag("up", item);} else {item=0;$("#searchwindow").load("/libraries/includes/actions/search.tag.php", {action:"true",lang:currLang,text:$("#what").val()}, function(data){if (data) {$("#searchwindow").show().corner("10px");} else $("#searchwindow").hide();});}});
	$("#what").keydown(function(e){if (e.which==13) {if ($("#sw1").css("text-decoration")=="underline") search("sw1", "html");else if ($("#sw2").css("text-decoration")=="underline") search("sw2", "html");else if ($("#sw3").css("text-decoration")=="underline") search("sw3", "html");else $("#search").click();}});
	$("#search").click(function(){search("what", "value");});
	$("#searchwindow").hide();
	

	// faq
	$(".faq_text").corner("10px").css('border', 'solid 1px #c3c3c3');
	$("#btn_faq").click(function(){
		var text = $("#faq_text").val();
		if (text) {
			$(".askyour").html('<img id="loader" src="/img/loader3.gif" width="31" height="31" border="0" />');
			$.post("/libraries/includes/actions/faq.php", {action:"true",lang:currLang,text:text}, function(data){
				if (data) $(".askyour").html(data); else $("#faq_text").focus();
			});
		} else $("#faq_text").focus();
	});


	// form
	$(".form .form_contact select, .form .form_input, .form .form_text").css({border:'solid 1px #c3c3c3'});
	var input1, input2, textrea1;
	$(".form #form_name").focus(function(){if(!input1){$(this).val("");input1=true;}});
	$(".form #form_email").focus(function(){if(!input2){$(this).val("");input2=true;}});
	$(".form #form_text").focus(function(){if(!textrea1){$(this).html("");textrea1=true;}});
	$(".form #btn_complain").click(function(){
		$(".form").hide();
		$("#form_msg").html('<img id="loader" src="/img/loader3.gif" width="31" height="31" border="0" />').load("/libraries/includes/actions/complain.php", {action:"true", lang:currLang, name:$(".form #form_name").val(), email:$(".form #form_email").val(), text:$(".form #form_text").val()}, function(data){
			if (!data) $(".form").show();
		});
	});
	$(".form #btn_contact").click(function(){
		$(".form").hide();
		$("#form_msg").html('<img id="loader" src="/img/loader3.gif" width="31" height="31" border="0" />').load("/libraries/includes/actions/contact.php", {action:"true", lang:currLang, to:$(".form #form_contact").val(), name:$(".form #form_name").val(), email:$(".form #form_email").val(), text:$(".form #form_text").val()}, function(data){
			if (!data) $(".form").show();
		});
	});

	if ($(".content #module").is(":visible")==true) {
		$("#module .left").corner("round tl bl 20px");
		$("#module .right").corner("round tr br 20px");
		$("#module .right .img img").corner("20px");
		loadOperation("start", operation, order);
		$(".content #module .back").click(function(){order--;if (order<=0) order=count;loadOperation("back", operation, order);});
		$(".content #module .next").click(function(){order++;if (order>count) order=1;loadOperation("next", operation, order);});
	}

	// loans
	$(".content .loan img").corner("20px");
	/*	
	// corners
	$(".orange, .orange2, #calculator,  #branchlist, #branchmap").corner("20px");
	*/
});

var clipFinal = false;
var clipFirstWidth = 0;
var clipLastWidth = 970;
function change(type){
	if (type=='back') {
		clipFirstWidth -= 238;
		clipLastWidth -= 238;
		if (clipFirstWidth<=0) {
			$("#clipBack").removeClass().addClass("clipNoBack");
			if (clipFirstWidth<0) {
				clipFirstWidth = 0;
				clipLastWidth = 970;
			}
		}
		$("#clipNext").removeClass().addClass("clipNext");
	}
	if (type=='next') {
		clipFirstWidth += 238;
		clipLastWidth += 238;
		$("#clipBack").removeClass().addClass("clipBack");
		if (clipFirstWidth>=maxWidth) {
			$("#clipNext").removeClass().addClass("clipNoNext");
			clipFinal = true;
		}
	}
	if (clipFinal) {
		clipFirstWidth = 0;
		clipLastWidth = 970;
		clipFinal = false;
		$("#clipNext").removeClass().addClass("clipNext");
	}
	var b = browser();
	if (b!="IE6") $("#clipMovie").animate({clip:"rect(0,"+clipLastWidth+"px,270px,"+clipFirstWidth+"px)",left:"-"+clipFirstWidth+"px"});
	else $("#clipMovie").css({clip:"rect(0,"+clipLastWidth+"px,270px,"+clipFirstWidth+"px)",left:"-"+clipFirstWidth+"px"});
	clearTimeout(clipTimeout);
	clipTimeout = setTimeout("autoChange()", 5000);
}

function autoChange(){
	$("#clipNext").click();
	clipTimeout = setTimeout("autoChange()", 5000);
}

function selectTag(type, id){
	for (i=1;i<=3;i++) {
		$("#sw"+i).css({"text-decoration":"none"});
	}
	$("#sw"+id).css({"text-decoration":"underline"});
}

function search(field, type){
	if (type=="value") search = $("#"+field).val();
	else if (type=="html") search = $("#"+field).html()
	$.post("/libraries/includes/actions/search.reg.php", {action:"true", lang:currLang, text:search}, function(){
		window.location = '/'+currLang+'/search/?'+ search;
	});
}

function mainAnons(id, type){
	if (type==1) {
		$(".main-module #box"+id).css({color:"#f47022", cursor:"pointer"});
		$(".main-module #box"+id+" .clear").html('<img src="/img/bg/next2.png" width="4" height="7" border="0" />');
	} else {
		$(".main-module #box"+id).css({color:"#6d787f"});
		$(".main-module #box"+id+" .clear").html('<img src="/img/bg/next1.png" width="4" height="7" border="0" />');
	}
}

function loadOperation(type, id, order){
    $("#module .right").html('<img id="loader" src="/img/loader3.gif" width="31" height="31" border="0" />').load("/libraries/includes/actions/operation.php", {
        action: "true",
		lang: currLang,
		type: type,
		id: id,
		order: order
    }, function(){
		var b = browser();
		if (b!="IE6") $("#module .right .img img").corner("20px");
	});
}

var connTime;
function connOnlineChat(){
	$.post("/libraries/includes/actions/dialog.accept.php", {action:"true"}, function(data){
		if (data=='OK') {
			clearTimeout(connTime);
			openDialogBox("consult");
		} else if (data=='DIE') {
			clearTimeout(connTime);
			openDialogBox("onlineform");
		} else connTime = setTimeout("connOnlineChat()", 1000);
	});
}

var oldData;
function loadChat(act){
	$.get("/libraries/includes/actions/dialog.chat.php", {action:"true",act:act,lang:currLang}, function(data){
		if (act=="start" && data) {
			$("#chat").html(data);
			$("#chat").scrollTop(99999);
		} else if (data=="DIE") {
			clearTimeout(connTime);
			$("#dialogbox").hide();
			//openDialogBox("onlineform");
			return false;
		} else if (data && data!=oldData) {
			oldData = data;
			$("#chat").append(data);
			$("#chat").scrollTop(99999);
		}
		if ($("#dialogbox").is(":hidden") && data) $("body").append('<embed src="/img/alert.mp3" hidden="true" autostart="true" loop=false />');
	});
	connTime = setTimeout("loadChat('load')", 1000);
}

function browser () {
	if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)){
		 var ieversion = new Number(RegExp.$1)
		 if (ieversion<=6) return "IE6";
	}
	return "";
}

function openDialogBox(type, one){
	$("#dialogbox").css({top:'',left:'',padding:'',bottom:'',right:'',margin:'',width:''});
	if (one) {
		$("#dialogbox").removeClass();
		$("#dialogbox").hide();
		$("#dialogbox #center").load("/libraries/includes/site/dialog/dialog."+type+".php", {
			action: "true",
			lang:currLang,
			prv: prvExpert
		}, function(data){
			var b = browser();
			if (b!="IE6") $(".input, .button").corner("10px");
			$("#dialogbox").show();
		});
	} else {
		$("#dialogbox").removeClass();
		$("#dialogbox").hide();
		$("#dialogbox #center").load("/libraries/includes/site/dialog/dialog."+type+".php", {
			action: "true",
			lang:currLang
		}, function(data){
			$("#dialogbox").addClass(type+"dialog");
			if ($("#dialogbox").hasClass("consultdialog")) loadChat('start');
			$("#dialogbox").show();
			var b = browser();
			if (b!="IE6") $(".input, .chat, .textarea, .input2, .input3, .button").corner("10px");
			if (type=="subsnews" || type=="subsrate") $("#dialogbox").uncorner().corner("round tl tr 10px"); else $("#dialogbox").corner("10px");
		});
	}
}

function openTabContent(type){
	$("#dialogbox").hide();
	$("#tabcontent").html('<img id="loader" src="/img/loader2.gif" width="31" height="31" border="0" />');
    $(".tabs ul li").removeClass();
    $("#tab"+type).addClass("selected");
	$("#tab"+type).corner("top 5px");
    $("#tabcontent").load("/libraries/includes/site/tab/tab."+type+".php", {action:"true", lang:currLang});
}

function loadNews(type){
    if (type=='start') id=0;
    else if (type=='prev') id--;
    else if (type=='next') id++;
    if (id>4) id=0;
    if (id<0) id=4;
    $("#news").load("/libraries/includes/actions/news.php", {action:"true", lang:currLang, id:id});
}

function goTo(link){
	window.location += link;
    
}

function goToPath(link){
    window.location = link;
}

function openPhoto(id){
	$('.liga .bigphoto').hide();
	if (id) $('.liga #photo_'+id).show();
}
