/* -------------------------------------------------
 * POPUPS
 ------------------------------------------------ */
function showKursPopup(){
	$('.popup-substrate,.popup .close').click(function(){
		$('.popup-substrate').hide();
		$('.orderKurs,.rss,.betterKurs,.yourKurs,.newValute,.valute-p,.tellFriend,.receiveP,.map').hide();
	});
	$('.scheme_off').click(function(){
		$('.popup-substrate').show().height($(document).height());
		$('.map').show().css({'left':$(window).width()/2 - $('.map').get(0).offsetWidth/2,'top':$(document).scrollTop() + $(window).height()/2 - $('.map').get(0).offsetHeight/2});
		return false;
	});
	$('.online-order').click(function(){
		$('.popup-substrate').show().height($(document).height());
		var tab = $.cookie('tab');
		if (($('#popup_directions').length) && tab) {
			$('#popup_directions option').each(function () {
				$(this).attr('selected', 0);
			});
			$('#popup_directions option:eq(' + tab + ')').attr('selected', 'selected');
		}
		$('.orderKurs').show().css({'left':$(window).width()/2 - $('.orderKurs').get(0).offsetWidth/2,'top':$(document).scrollTop() + $(window).height()/2 - $('.orderKurs').get(0).offsetHeight/2});
		return false;
	});
	$('.kurs-popup').click(function(){
		$('.popup-substrate').show().height($(document).height());
		$('.rss').show().css({'left':$(window).width()/2 - $('.rss').get(0).offsetWidth/2,'top':$(document).scrollTop() + $(window).height()/2 - $('.rss').get(0).offsetHeight/2});
		return false;
	});
	$('.receive').click(function(){
		$('.popup-substrate').show().height($(document).height());
		$('.receiveP').show().css({'left':$(window).width()/2 - $('.receiveP').get(0).offsetWidth/2,'top':$(document).scrollTop() + $(window).height()/2 - $('.receiveP').get(0).offsetHeight/2});
		return false;
	});
	$('.your-kurs').click(function(){
		$('.popup-substrate').show().height($(document).height());
		$('.yourKurs').show().css({'left':$(window).width()/2 - $('.yourKurs').get(0).offsetWidth/2,'top':$(document).scrollTop() + $(window).height()/2 - $('.yourKurs').get(0).offsetHeight/2});
		return false;
	});
	$('.better-kurs').click(function(){
		$('.popup-substrate').show().height($(document).height());
		$('.betterKurs').show().css({'left':$(window).width()/2 - $('.betterKurs').get(0).offsetWidth/2,'top':$(document).scrollTop() + $(window).height()/2 - $('.betterKurs').get(0).offsetHeight/2});
		return false;
	});
	$('.notYourValute').click(function(){
		$('.popup-substrate').show().height($(document).height());
		$('.newValute').show().css({'left':$(window).width()/2 - $('.newValute').get(0).offsetWidth/2,'top':$(document).scrollTop() + $(window).height()/2 - $('.newValute').get(0).offsetHeight/2});
		return false;
	});
	$('.valute-b .add-i .moreValute').click(function(){
		$(this).parent().parent().find('.more-valute').toggle();
		var showText = $(this).find('.pseudo').val();
		var hideText = 'Скрыть доп. валюты';
		$(this).find('span').text($(this).find('span').text() == hideText ? showText : hideText);
		return false;
	});
	$('.tell-friend').click(function(){
		$('.popup-substrate').show().height($(document).height());
		$('.tellFriend').show().css({'left':$(window).width()/2 - $('.tellFriend').get(0).offsetWidth/2,'top':$(document).scrollTop() + $(window).height()/2 - $('.tellFriend').get(0).offsetHeight/2});
		return false;
	});
	/*$('.calculate .select').css({'display':'inline-block'});
	if (/MSIE/.test(navigator.userAgent)){
		$('.calculate .select').css({'display':'inline'});
	}
	$('.calculate .select').click(function(){
		$('.popup-substrate').show().height($(document).height());
		$('.valute-p').show().css({'left':$(window).width()/2 - $('.valute-p').get(0).offsetWidth/2,'top':$(document).scrollTop() + $(window).height()/2 - $('.valute-p').get(0).offsetHeight/2});
		$('.calculate .select').removeClass('chosen');
		$(this).addClass('chosen');
	});*/
	if ($('.calculated-data table .card label input').is(':checked')){
		$('.calculated-data .card-info').show();
		$('.commission .no_card').hide();
		$('.commission .with_card').show();
	}
	$('.calculated-data table .card label input').change(function(){
		if ($(this).is(':checked')){
			$('.commission .no_card').hide();
			$('.commission .with_card').show();
			$('.calculated-data .card-info').show();
		} else {
			$('.commission .with_card').hide();
			$('.commission .no_card').show();
			$('.calculated-data .card-info').hide();
		}
	});
}

function popupDispatcher(){
	var statuses = {status_success: 'success', status_failed: 'error'};
	$.each(statuses, function(i, n){
		if ($('#' + i).length) {
			var text = $('#' + i).val();
			$.jGrowl(text, {'themeState' : n});
		}
	});
}
/* -------------------------------------------------
 * TOGGLE
 ------------------------------------------------ */
function contactsData(){
	$('.contacts .streets a').each(function(i){
		$('.contacts .streets a:eq('+i+')').click(function(){
			$('.contact-data-tab').hide();
			$('.contact-data-tab:eq('+i+')').show();
			$('.contacts .streets a').closest('li').removeClass('active');
			$(this).closest('li').addClass('active');
			return false;
		});
	});
}
function addressInfo(){
	$('.address-info-menu a').each(function(i){
		$('.address-info-menu a:eq('+i+')').click(function(){
			$('.address-info-tab').hide();
			$('.address-info-tab:eq('+i+')').show();
			$('.address-info-menu a').parent('li').removeClass('active');
			$(this).parent('li').addClass('active');
			
			$('.contacts .streets a').closest('li').removeClass('active');
			$('.contacts .streets a:eq('+i+')').closest('li').addClass('active');
			$('.contact-data-tab').hide();
			$('.contact-data-tab:eq('+i+')').show();
			
			$('.valute-b-tab').hide();
			$('.valute-b-tab:eq('+i+')').show();
			
			$('.time-tab').hide();
			$('.time-tab:eq('+i+')').show();
			
			$.cookie('tab', i, {expires: 14});
			return false;
		});
	});
}
/* -------------------------------------------------
 * CURRENCIES & CALCULATOR
 ------------------------------------------------ */
function chooseAction(){
	$('.calculate ins.choosed input[type="radio"]').attr('checked','checked');
	$('.calculate ins').click(function(){
		$('.calculate ins').removeClass('choosed');
		$(this).addClass('choosed');
		$('input[type="radio"]',$(this)).attr('checked', 'checked');
	});
}
function chooseValute(){
	$('.valute-p a').click(function(){
		$('.calculate .select.chosen').text($(this).text());
		$('.popup-substrate').hide();
		$('.orderKurs,.rss,.betterKurs,.yourKurs,.newValute,.valute-p').hide();
		if ($('.calculate .select.chosen').hasClass('s1')){
			$('.calculate input.valute1').val($(this).attr('rel'));
		}
		if ($('.calculate .select.chosen').hasClass('s2')){
			$('.calculate input.valute2').val($(this).attr('rel'));
		}
		return false;
	});
}
function currencyData(){
	$('.valute-b-tab::first').show();
}
function upTimeData(){
	$('.time-tab::first').show();
}
/* -------------------------------------------------
 * TABS
 ------------------------------------------------ */
function setCurrentTab(){
	if ($('.address-info-menu a').length) {
		var tab = $.cookie('tab');
		if (tab) $('.address-info-menu a:eq(' + tab + ')').click();
		else $('.address-info-menu a:eq(0)').click();
	}
}
/* -------------------------------------------------
 * CALCULATION
 ------------------------------------------------ */
function initToggle(){
	$('.toggleResults .hide').click(function(){
		$('.toggleResults span').removeClass('open');
		$('.toggleResults span').addClass('close');
		$('.calculated-data').animate({
			'height': 'toggle', 'opacity': 'toggle'
		}, 'slow');
	});
	
	$('.toggleResults .show').click(function(){
		$('.toggleResults span').removeClass('close');
		$('.toggleResults span').addClass('open');
		$('.calculated-data').animate({
			'height': 'toggle', 'opacity': 'toggle'
		}, 'slow');
	});
}

function interchange(){
	$('#interchange').click(function(){
		if ($('#calc_currency_from').length && $('#calc_currency_to').length) {
			var from = $('#calc_currency_from').val();
			var to = $('#calc_currency_to').val();
			if (from != to) {
				$("#calc_currency_from [value='" + to + "']").attr('selected', 'selected');
				$("#calc_currency_to [value='" + from + "']").attr('selected', 'selected');
			}
			calcInit();
		}
	});
}
/* -------------------------------------------------
 * INIT
 ------------------------------------------------ */
function initAll(){
	showKursPopup();
	contactsData();
	addressInfo();
	chooseAction();
	chooseValute();
	currencyData();
	upTimeData();
	setCurrentTab();
	initToggle();
	interchange();
	popupDispatcher();
}
$(document).ready(function(){
	initAll()
});
/* -------------------------------------------------
 * DEVELOPMENT
 ------------------------------------------------ */
function c(value){
	console.log(value);
}
/* ---------------------------------------------- */

