jQuery(document).ready(function(){

	/** Currencies **/	
	$(function() {
		function getCurVals() {

			var loader = '<img src="/img/ajax_loader.gif" alt="" style="padding: 30px;" />';
			var bank_val = $('#bank').val();
			var cur_val = $('input:radio[name=cur]:checked').val();
			
			$('#currenciesGrahp').html(loader);
			
			if ($('#currenciesGrahp')) {
				var ret = $.ajax({
					url: "/currencies/index/",
					data: ({cur : cur_val, type : bank_val}),
					success: function(ret){
						$('#currenciesGrahp').html(ret);
					}
				});
			}
		}
		
		$('#bank').change(getCurVals);
		$("input[name$='cur']").change(getCurVals);
		
		// run script at start 
		$('#bank').trigger('change');
	});
	
	// Subscribe
	$(function() { 
	    $("#subscribe").overlay({expose: {color: '#000', opacity: 0.5}, close: false});

		if ($("#linkToMap")) {
		    $("#linkToMap").bind('click', function(e){
				var overSelector = $("#linkToMap").attr('rel');
				var over = ($(overSelector));
				over.toggle();
			});
		}

		$("#toSubscribe form").bind('submit', function(event){
			var SubscribeEmail = $('#SubscribeEmail').val();					// get value
			var ret = $.post("/subscribes/add", {"data[Subscribe][email]": SubscribeEmail},	// send data
				function (data) {
					if (data == 'ok') {
						$('#subscribe').text('e-mail добавлен');
					} else {
						$('#subscribe').text('e-mail не добавлен');
					}
				}, 'text');								// label text
			$("#toSubscribe .close").trigger("click");							// close window
			return false;
		});
		$("#toSubscribe a").click(function(event){
			$("#toSubscribe form").submit();
			return false;
		})
	});

	// Filials
	$(function() { 
	    $("div#city_name-gl").overlay({expose: {color: '#000', opacity: 0.5}, close: false});
	
		$("#overlay table td span").click(function(event){
			selCity = $(this).text();

			$('#city_name-gl').text(selCity);
			// $.post("/backend.php", {"img": src});
			$("#overlay .close").trigger("click");
		})
	});

	// Regions
	$(function() {
		$('div#oprosSelectRegion').overlay({expose: {color: '#000', opacity: 0.5}, close: false});
		$("#overlay2 table td span").click(function(event){
			selCity = $(this).text();
			$('#oprosSelectRegion').text(selCity);
			$('#input100').attr('value', selCity);			// hidden input
			$("#overlay2 .close").trigger("click");
		})
	});

		// citySelectorInit();
	
	/** adjust height **/
	// setup
	var picHeight = 390;
	var picWidth = 320;
	var minHeight = 330;
	var wrapper = $('.picWrapper');
	var container = $('.picWrapper div');
	// logic
	if (wrapper) {
		var minusH = wrapper.height() - minHeight;
		var minusW = wrapper.width() - picWidth;
		// if (minusH < 0) container.height( picHeight + minusH );
		if (minusW < 0) container.width( wrapper.width() );

		// fix (not Mozzila)
		if(!$.browser.mozilla){
			container.css("top", (minusH - 60) + 'px');
		}

		container.show();
	}
	
	/** Contacts **/
	$('dl.subtopic').hide();
	show_more();

	$("#topic").change(function() {
		show_more();
	});

	function show_more() {
		var subId = $('#topic option:selected').attr('id');
		$('dl.subtopic').hide();

		$('#subId').val('topic' + subId);					// hidden

		if($('#topic' + subId).length > 0){
			$('#topic' + subId).show();
		}
	}

	/** Opros **/
	var privateSteps = $('#step2, #step3, #step4, #step5, #step6, #step7, #step8, #step9, #step10, #step11, #step12, #step100, #step101, #step102');
	var privateUsers = $('#step5, #step6');

	var legalSteps = $('#step13, #step14, #step15, #step16, #step17, #step18, #step19, #step20, #step21, #step22, #step23');
	var legalUsers = $('#step15, #step16');

	hideSteps($('fieldset'));
	$('fieldset.victorinaQ').show();		// show victorina

	$('#step1').show();
	$('#step1 input').click(function (){

		if($('#step1 input:checked').attr('id') == 'input1-0'){
			hideSteps(legalSteps);
				privateSteps.show();
			hideSteps(privateUsers);
			hideSteps($('#step8, #step10'));
				$('#step24').show();
		} else {
			hideSteps(privateSteps);
				legalSteps.show();
			hideSteps(legalUsers);
			hideSteps($('#step18, #step20'));
				$('#step24').show();
		}
	});

	function hideSteps(steps){
		steps.hide();
		steps.find('input[type=radio], input[type=checkbox]').not('input[name=legal_branch], inpurt[name=legal_main_bank], input[name=private_sex]').attr('checked', false);
		steps.find('input[type=text], textarea').val('');
	}

	// FIZ

	// Halyk User
	$('#step2 input').click(function (){
		if($('#step2 input:checked').attr('id') == 'input2-0'){
			privateUsers.show();
			hideSteps($('#step11'));
		} else {
			hideSteps(privateUsers);
			$('#step11').show();	// new user?
		}
	});
	// What hate?
	$('#step7 input').click(function (){
		if($('#step7 input:checked').attr('id') == 'input7-1'){
			$('#step8').show();
		} else {
			hideSteps($('#step8'));
		}
	});
	// Other banks?
	$('#step9 input').click(function (){
		if($('#step9 input:checked').attr('id') == 'input9-0'){
			$('#step10').show();
		} else {
			hideSteps($('#step10'));
		}
	});


	// URI

	// Halyk User
	$('#step13 input').click(function (){
		if($('#step13 input:checked').attr('id') == 'input13-0'){
			legalUsers.show();
			hideSteps($('#step22'));
		}else{
			hideSteps(legalUsers);
			$('#step22').show();	// new user?
		}
	});
	// What hate?
	$('#step17 input').click(function (){
		if($('#step17 input:checked').attr('id') == 'input17-1'){
			$('#step18').show();
		} else {
			hideSteps($('#step18'));
		}
	});
	// Other banks?
	$('#step19 input').click(function (){
		if($('#step19 input:checked').attr('id') == 'input19-0'){
			$('#step20').show();
		} else {
			hideSteps($('#step20'));
		}
	});

	if ($("#accordion")) {
	    $("#accordion").accordion({ autoHeight: false });
	}


	/** Icos **/
	if ($('.menu_notes')) {
		
		jQuery.each($('.menu_notes li a'), function(){
			switch ($(this).text()) {
				case 'On-line выписка для юридических лиц':
				case 'Заңды тұлғаларға оn-line үзінді көшірме':
					$(this).css('background', 'url(/img/ico_statement.gif) no-repeat');
					break;
				case 'Тарифы':
				case 'Тарифы':
					$(this).css('background', 'url(/img/ico_tarif.gif) no-repeat');
					break;
				case 'Опрос посетителей сайта':
				case 'Сайтқа кірушілердің пікірі':
					$(this).css('background', 'url(/img/ico_opros.gif) no-repeat');
					break;
				case 'Ответы на часто задаваемые вопросы':
				case 'Жиі қойылатын сұрақтарға жауап':
					$(this).css('background', 'url(/img/ico_faq.gif) no-repeat');
					break;
				case 'Кредитный калькулятор':
				case 'Кредиттік калькулятор':
					$(this).css('background', 'url(/img/ico_calc.gif) no-repeat'); 
					$(this).attr('href', '/articles/view/credit');
					break;
				case 'Депозитный калькулятор':
				case 'Депозиттік калькулятор':
					$(this).css('background', 'url(/img/ico_calc.gif) no-repeat');
					$(this).attr('href', '/articles/view/deposit');
					break;
				case 'Калькулятор «Плюс Кредит»':
					$(this).css('background', 'url(/img/ico_calc.gif) no-repeat');
					$(this).attr('href', '/articles/view/plus');
					break;
				case 'Анкета качества сервиса':
				// case 'Депозиттік калькулятор':
					$(this).css('background', 'url(/img/ico_anketa.gif) no-repeat');
					$(this).attr('href', '/articles/view/56');
					break;
			}
			// console.log(this);
		})
		
		// if ($('.menu_notes li a').text() == 'Тарифы') {
		// 	console.log($('.menu_notes li a'));
		// 	$(this).css('background', 'url(/img/ico_tarif.gif) no-repeat');
		// }
	}
	
	/** Search **/
	if ($('#sections')) {
		
		function toggleSearch() {
			if($('#sections').is(':checked')){
				$('#sections-list input').attr('disabled', '');
				$('#sections-list label').attr('style', 'color: #000000');
			}else{
				$('#sections-list input').attr('disabled', 'disabled');
				$('#sections-list label').attr('style', 'color: #909090');
			}
		}
		toggleSearch();
		$('#sections, #all-site').click(function() {
			toggleSearch();
		});
	}
	
	/** Collateral **/
	if ($('#collateralFind')) {
		// $('#collateralFind').bind('click', funciton(){
		// 	
		// });
	};

});