// Global scripts (all pages)


$().ready(function() {
	
	
	/*$("#from-city").parent("p").addClass("off");
	$("#from-city").attr("disabled", "disabled");
	$("#to-city").parent("p").addClass("off");
	$("#to-city").attr("disabled", "disabled");
	
	
	
	$("#from-country").autocomplete('/quote/api/get_countries', {
		minChars: 0,
		max: 100,
		autoFill: true,
		mustMatch: false,
		matchContains: false,
		scrollHeight: 220,
		width: 125
	});
	
	$('#from-country').result(function(event, data, formatted) {
		$("#from-city").parent(".off").removeClass("off");
		$("#from-city").removeAttr("disabled").focus();
	});
	
	$("#from-city").autocomplete('/quote/api/get_cities', {
		minChars: 0,
		max: 100,
		autoFill: true,
		mustMatch: false,
		matchContains: false,
		scrollHeight: 220,
		width: 125,
		extraParams: {
			country: function() { return $("#from-country").val(); }
		}

	});
	
	
	$("#to-country").autocomplete('/quote/api/get_countries', {
		minChars: 0,
		max: 100,
		autoFill: true,
		mustMatch: false,
		matchContains: false,
		scrollHeight: 220,
		width: 125
	});
	
	$('#to-country').result(function(event, data, formatted) {
		$("#to-city").parent(".off").removeClass("off");
		$("#to-city").removeAttr("disabled").focus();
	});
	
	$("#to-city").autocomplete('/quote/api/get_cities', {
		minChars: 0,
		max: 100,
		autoFill: true,
		mustMatch: false,
		matchContains: false,
		scrollHeight: 220,
		width: 125,
		extraParams: {
			country: function() { return $("#to-country").val(); }
		}

	});*/
	
	$('#revolve').cycle( {
		timeout: 5000,
		pause: 1
	});
	
	$('.standard-popup').bind('click', function() {
		window.open($(this).attr('href'),'advance_popup','width=700,height=570,scrollbars=yes');		
		return false;
	});
	
	
	$('.calc-popup').bind('click', function() {
		window.open($(this).attr('href'),'box_calc','width=644,height=345,scrollbars=no');		
		return false;
	});
	
	$('#convert-form').submit(convertHandler);
	$('#convert').click(convertHandler);
	
	function convertHandler()
	{
		//Get all the values
		var amount = $('#amount').val();
		var from = $('#fromCurrency').val();
		var to = $('#toCurrency').val();
		
		if (amount == '')
		{
			$('.currency-convert.amount label').fadeOut('fast', function() {
				$(this).css('color', '#C42C2C');
				$(this).fadeIn('fast');
			});
			
			return false;
		}
		
		//Make data string
		var dataString = "amount=" + amount + "&from=" + from + "&to=" + to;
		
		$.ajax({
			type: "POST",
			url: "/quote/currency_converter/convert",
			dataType: "json",
			data: dataString,
			
			success: function(data) {
				
				$('#convert-form fieldset').fadeOut('fast', function() {
					// put received response into result div
					$('#from-number').text(data.from_amount);
					$('#from-name').text(data.from_currency);
					$('#to-number').text(data.to_amount);
					$('#to-name').text(data.to_currency);
					
					// show results div
					$('#results').fadeIn();
					$('#convert').attr("id", "convert-again");
					
					$('#convert-again').unbind();					
					$('#convert-again').click(formResetHandler);				
				});	
							
			}				
		});
		return false;
	}
	
	function formResetHandler()
	{
		$('#results').fadeOut('fast', function() {
			
			$('.currency-convert.amount label').css('color', '#464646');
			$('#convert-form fieldset').fadeIn();
			
			$('#convert-again').attr("id", "convert");
			$('#convert').unbind();	
			$('#convert').click(convertHandler);
			
			$('#convert-form #amount').focus();
		});
		
		return false;
	}

});
