//-------------------------------------------------------------------
$.fx.speeds._default = 1000;
var is_submit = false;
//-------------------------------------------------------------------
function calcInit(){
	var addressFormatting = function(text){
		var newText = text;
		var findreps = [
			{find:/^([^\-]+) \- /g, rep: '<i class="ui-selectmenu-item-label">$1</i>'},
			{find:/([^\|><\(\)]+)$/g, rep: '<i class="ui-selectmenu-item-content">$1</i>'}
		];
		
		for(var i in findreps){
			newText = newText.replace(findreps[i].find, findreps[i].rep);
		}
		return newText;
	}
	
	$('#calc_currency_from').selectmenu({
		icons: [
			{find: '.avatar'}
		],
		bgImage: function() {
			return 'url(' + $(this).attr('title') + ')';
		},
		style: 'dropdown', 
		menuWidth: 300,
		format: addressFormatting
	});
	
	$('#calc_currency_to').selectmenu({
		icons: [
			{find: '.avatar'}
		],
		bgImage: function() {
			return 'url(' + $(this).attr('title') + ')';
		},
		style: 'dropdown', 
		menuWidth: 300,
		format: addressFormatting
	});
}
//-------------------------------------------------------------------
function calculateInit() {
	$('#calculateMe').validationEngine('attach');
	$('.f-calc').submit(function() {
		if ($('#calculateMe').validationEngine('validate')) {
			calcHandler();
		} else {
			$('#orderMe').validationEngine('hide');
			$('.calculated-data').hide();
			clearHint();
			$('.toggleResults span').removeClass();
		}
		return false;
	});
}
//-------------------------------------------------------------------
function calcHandler() {
	var direction_id = $.cookie('tab');
	if (!direction_id) direction_id = 0;
	var options = {
		target: '#calculateMe',
		dataType: 'json',
		beforeSubmit: checkIt,
		success: takeIt,
		timeout: 5000,
		url: '/calculate?calc_direction_id=' + direction_id,
		cache: false
	};
	
	$('#calculateMe').ajaxSubmit(options);
}
//-------------------------------------------------------------------
function orderFormInit() {
	$('#orderMe').submit(function() {
		if ($('#orderMe').validationEngine('validate')) {
			orderHandler();
		}
		return false;
	});
}
//-------------------------------------------------------------------
function orderInit() {
	$('#orderMe').validationEngine('attach');
}
//-------------------------------------------------------------------
function orderHandler() {
	var options = {
		target: '#orderMe',
		dataType: 'json',
		beforeSubmit: checkIt,
		success: orderThanks,
		timeout: 5000,
		url: '/calculate/order',
		cache: false
	};
	$('#orderMe').ajaxSubmit(options);
}
//-------------------------------------------------------------------
function checkIt(formData, jqForm, options) {
	return true; 
}
//-------------------------------------------------------------------
function orderThanks(data, statusText) {
	if (data){
		if (data.type == 'ok') {
			$('#orderMe').validationEngine('hide');
			$('#calculateMe').validationEngine('hide');
			$.jGrowl('Ваш заказ принят');
			$('#calc_sum').val('');
			$('#errorMessage').hide();
			$('#successMessage').hide();
			$('#total').html('');
			clearHint();
			$('#calc_service').val('');
			$('.calculated-data').hide();
		} else {
			$('#successMessage').hide();
			$('#errorMessage').html(data.context);
			$('#errorMessage').show();
			$('.calculated-data').show();
		}
	}
}
//-------------------------------------------------------------------
function takeIt(data, statusText) {
	if (data){
		if (data.type == 'ok') {
			$('#errorMessage').hide();
			$('#successMessage').show();
			$('#total').html(data.context.total);
			if (data.context.hint) {
				$('#hint').html(data.context.hint);
				$('#hint').css('display', 'block');
			}
			else clearHint();
			//$('#calc_service').val(data.context.service);
			orderInit();
		} else {
			$('#successMessage').hide();
			$('#errorMessage').html(data.context);
			$('#errorMessage').show();
			$('#orderMe').validationEngine('hide');
			clearHint();
		}
		$('.calculated-data').show();
		$('.toggleResults span').addClass('open');
	}
}
//-------------------------------------------------------------------
function clearHint() {
	$('#hint').html('');
	$('#hint').hide();
}
//-------------------------------------------------------------------
if (jQuery){
	$(document).ready(function(){
		calcInit();
		calculateInit();
		orderFormInit();
	});
}
//-------------------------------------------------------------------
