$(document).ready(function(){
	
	$('.locations .drop a').click(function(){
		$.ajax({
		   type: "GET",
		   url: '/ru/ajax/content/location/'+$(this).attr('rel'),
		   success: function(msg){
			 location.reload(true);
		   }
		 });
	});
	$('.news-page select').change(function(){
		$form=$(this).parents('form');
		$action=$form.attr('action');
		$year=$form.find('select[name=y] option:selected').val();
		$month=$form.find('select[name=m] option:selected').val();
		window.location.href=$action+'date/'+$year+'-'+$month;
	});
	$('#country-list .box .expand').live('click',function(){
		$('#country-list .additional').fadeIn('slow');
		$('#country-list .box .expand').hide();
		return false;
	});
	$('#country-list .close').live('click',function(){
		$('#country-list .box').hide();
		listOpen=false;
		return false;
	});
	var listOpen=false;
	$('#country-list').click(function(){
		if(!listOpen)$(this).append($('.country-list').html());
		listOpen = true;
		//$('.country-list').insertAfter($(this)).css('display','inline');
	});

	$('.map-popup .close').click(function(){
		$(this).parents('.map-popup').css('visibility', 'hidden');
		return false;
	});
	$('.question a.link').click(function(){
		$(this).parent().find('.popup').fadeIn();
		return false;
	});
	$('.question .popup a.close').click(function(){
		$(this).parent().fadeOut();
		return false;
	});
	
	var cAnimate=false;
	$('.header .contacts .phone').hover(function(){
		if(cAnimate==true) return false;
		cAnimate=true;
		$('.header .contacts.hover').fadeIn('slow', function(){cAnimate=false;});
	},
	function(){
		if(cAnimate==true) return false;
		cAnimate=true;
		$('.header .contacts.hover').fadeOut('slow', function(){cAnimate=false;});
	});
	$('.toggle-list h2 a').click(function(){
		$(this).parent().next().slideToggle();
		return false;
	});
	var langMotion=0;
	$('.locations .langs').hover(function(){
		$object=$(this).find('.drop');
		//setTimeout(300, function(){});
		$object.stop(true, true).slideToggle('fast');
		return false;
	});
	
	var animate=false;
	if(animate==false)$('.cards a').hover(function(){
		$(this).find('img').animate({top:'-15px'}, 200);						 
	},
	function(){
		$(this).find('img').animate({top:'0px'}, 200);
	});
	if(animate==false)$('.visuals .small-block').hover(function(){
		$(this).find('.block').animate({marginTop:'-30px'}, 200);										  
	}, 										  
	function(){
		$(this).find('.block').animate({marginTop:'0px'}, 200);
	});
	
	$block=$('.big-block.current');
	function changeBig(direction, $curr){
		$coming=$('#big-'+$curr);
		$block.animate({bottom:'-600px'}, 300, function(){
					$block.removeClass('current');
					//$coming.css('bottom', '-600px');
					$coming.animate({bottom:'0px'}, 300, function(){
						$coming.addClass('current');
						$block=$coming;
					});
					
				});
		
		$('.tabs .current').removeClass('current');
		
		$('#tab-'+$curr).addClass('current');
		return false;
	}
	
	
	function changeprev(object){
		$object=object.prev();
		$object.animate({left:'820px'}, 800, function(){
			$object.css('left', 'auto');
			$object.addClass('next').removeClass('prev');
			
			animate=false;
		});
		
		
	}
	function slidenext(object){
		$curr=$(object).attr('id').replace('small-','')*1;
		$(object).animate({left:'2000px'}, 800, function(){
			$(object).removeClass('next');
		});
		$prev= $('#small-'+($curr-1));
		$coming = $('#small-'+($curr-2));
		//$prev.css('left', '-2000px;');
		if($curr-2==0){$coming = $('#small-'+smallCount);}
		if($curr-2<0){$prev=$('#small-'+ smallCount); $coming = $('#small-'+2);}
		
		$coming.css('left', '-2000px').animate({left:'-460px'}, 800);
		$prev.animate({left:'820px'}, 800, function(){
			$prev.removeClass('prev').addClass('next');										   
			animate=false;
		});
		changeBig('left', $curr);
	}
	var smallCount=$('.small-block').size();
	
	function slideprev(object){
		$curr=$(object).attr('id').replace('small-','')*1;
		$(object).animate({left:'-2000px'}, 800, function(){
			$(object).removeClass('prev');
		});
		$next = $('#small-'+($curr+1));
		$coming = $('#small-'+($curr+2));
		$next.css('left', '2000px;');
		if($curr+2>smallCount){$coming = $('#small-'+1);}
		if($curr+1>smallCount){$next=$('#small-'+ 1); $coming = $('#small-'+2);}
		
		$coming.css('left', '2000px').animate({left:'820px'}, 800, function(){ $(this).addClass('next');});
		$next.animate({left:'-460px'}, 800, function(){
			$next.removeClass('next').addClass('prev');										   
			animate=false;
		});
		
		changeBig('right', $curr);
	}
	$('.visuals .small-block').live('click',function(){
		if(animate==true)return false;
		animate=true;
		if($(this).hasClass('next'))slidenext($(this));
		else slideprev($(this));
	});
	
	
	$('.tabs li a').click(function(){
		if($(this).parent().hasClass('current')) return false;
		$tab=$(this).parent().attr('id').replace('tab-','')*1;
		//alert($tab);
		$('#small-'+$tab).trigger('click');
		return false;
	});
	
	
	
	$('.left-block .currencies a').click(function(){
		$rel=$(this).attr('rel');
		var curr=0;
		if($rel==0){
			$(this).attr('rel', 1);
			curr=true;
		}
		else {
			$(this).attr('rel', 0);
			curr=false;
		}
		$(this).next().slideToggle();
		$.ajax({
		   type: "GET",
		   url: '/ru/ajax/content/currencies/'+curr,
		   success: function(msg){
			 //alert( "Data Saved: " + msg );
		   }
		 });
		return false;
	});
	
	$('.lightbox-gallery').lightBox();
	$('.lightbox').lightBox();
	
	var galleryCount=$('.gallery .box a').size();
	$('.gallery .next').click(function(){
		$current=$(this).parent().find('.current');	
		$current.fadeOut().removeClass('current');
		$current.next().fadeIn().addClass('current');
		$('.gallery .comment').fadeOut('fast', function(){
			$(this).text($current.next().attr('rel'));
			$(this).fadeIn('fast');
		})
		$('.gallery .prev').fadeIn();
		var next=$current.next().attr('id').replace('pic-', '')*1;
		if(next>=galleryCount-1)$(this).hide();
		//alert(next +' - '+ galleryCount);
		return false;
	});
	$('.gallery .prev').click(function(){						   
		$current=$(this).parent().find('.current');	
		$current.fadeOut().removeClass('current');
		$current.prev().fadeIn().addClass('current');
		
		$('.gallery .comment').fadeOut('fast', function(){
			$(this).text($current.prev().attr('rel'));
			$(this).fadeIn('fast');
		})
		
		$('.gallery .next').show();
		var prev=$current.prev().attr('id').replace('pic-', '')*1;
		if(prev<=0)$(this).hide();
		return false;
	});
	$('.search-form .submit, .search-form .button').click(function(){
		$(this).parent().submit();
		return false;
	});
	$('.search-form, .search form').submit(function() {
			var val = $(this).find('input.search-term').val().replace(/\s\s+/g, ' ');
			if(val=='') return false;
			window.location.href = $(this).attr('action') + val;
			return false;
	});
	$('#deposits-form input').keydown(function(e) {
	  	//calculate();
	});
	$('#deposits-form input').keyup(function(e) {
		//$('#deposits-form').submit();
		//if((e.keyCode < 48) || (e.keyCode > 57))return false;
		calculate();
	});
	$('#deposits-form select').change(function(){															 
		calculate();
	});

	function calculate(){
		$input		= $('#deposit-value').val() * 1;
		$discount	= $('#discount-rate').val() * 1;
		$currency	= $('.amount').find('option:selected').val();
		$period		= $('.period').find('option:selected').val() * 1;
		
		$('.counterRow').each(function(){
			$capitalization = $(this).find('.cap').hasClass('on');
			$json = $.parseJSON($(this).find('.rate input').val());
			
			if		($currency == 'RUR' && $input >= $json.a[0] && $input < $json.b[0])	$json = $json.a;
			else if ($currency == 'RUR' && $input >= $json.b[0])						$json = $json.b;
			
			else if ($currency == 'USD' && $input >= $json.c[0] && $input < $json.d[0])	$json = $json.c;
			else if ($currency == 'USD' && $input >= $json.d[0])						$json = $json.d;
			
			else if ($currency == 'EUR' && $input >= $json.e[0] && $input < $json.f[0])	$json = $json.e;
			else if ($currency == 'EUR' && $input >= $json.f[0])						$json = $json.f;
			else return;
			
			var rates = new Array();
			switch ($period) {
				case 2	: rates = $json.slice(2, 4);	break;
				case 3	: rates = $json.slice(4, 6);	break;
				case 6	: rates = $json.slice(6, 8);	break;
				case 12	: rates = $json.slice(8, 10);	break;
				case 36	: rates = $json.slice(10);		break;
				default	: rates.push($json[1]);			break;
			}
			
			$('.total', this).empty();
			var single = (rates.length == 1) ? true : false;
			
			for (var i in rates) {
				rate = rates[i].replace(",", ".") * 1;
				if ($capitalization) {
					if (rate <= $discount) {
						//P * (1 + I * t / K / 100) n *(1+I*(T-t*n)/K/100) – P
						if(rate<=0)$result=0;
						else $result = $input * (1 + rate * 30.15 / 365 / 100) + $period * (1 + rate * ((30.15 * $period) - (30.15 * $period)) / 365 / 100) - $input;
						//alert($input+' '+rate+' '+$period+' '+$result);
					} else {
						$result = $input;
						for ($i=0; $i < $period; $i++) $result = $result + $result * rate * 30.15 / 365 / 100;
						$result = $result - $input;
					}
				} else {
					if (rate <= $discount) $result = $input * rate * (30.15 * $period) / 365 / 100;
					else $result = [$input * rate * (30.15 * $period) / 365] / 100 - $input * (rate - $discount) * 0.35 * (30.15 * $period) / 365 / 100;
				}
				$result = $result.toFixed(2);
				
				if (!single) {
					HTML = "<div class=\"comment\">Начисление % ежемесячно</em></div>"
						 + "<div>" + $result + "<em>" + $currency + "</em></div>"
						 + "<div class=\"separate\"></div>"
						 + "<div class=\"comment\">Начисление % в конце срока</em></div>";
					single = true;
				} else {
					HTML = "<div>" + $result + "<em>" + $currency + "</em></div>";
				}
				
				$('.total', this).append(HTML);
			}
			$('.rate span', this).text(rates[0]);
		});
	}
	
	$('#deposits-form').submit(function(){
		calculater();
		return false;
	});
	$('.special-block .phone a').click(function(){
		return false;
	});
	$('.question form input[type=text], .question form select, .question form textarea').click(function(){
		if($(this).parents('.ajax-form').hasClass('start')){
			$(this).parents('form').removeClass('start');
			$('.question form input[type=text], .question form textarea').val('');
		}
		//alert(1);
	});
	$('.question .submit').click(function(){
		if($(this).parents('.ajax-form').hasClass('start'))return false;
	});
	
});
function isNumber(e) {
	if ((e.keyCode < 48) || (e.keyCode > 57)){
		e.returnValue = false;
	}
}
