$(document).ready(function() {		
	$("#from dt a").click(function() {
		$(".dropdown dd ul").hide();
		$("#from dd ul").toggle();
	});
				
	$("#from dd ul li a").click(function() {
		var text = $(this).html();
		$("#from dt a span").html(text);		
		var iso =  $("#from").find("span.value span.iso").html();
		updaterate();
		$(".fromiso").html(iso);	
		$(".dropdown dd ul").hide();
	});

	$("#into dt a").click(function() {
		$(".dropdown dd ul").hide();
		$("#into dd ul").toggle();
	});

	function updaterate(){
		var from = $("#from").find("span.value span.coefficient").html();
		var into = $("#into").find("span.value span.coefficient").html();
		rate = (from/into);
		rate = (Math.floor(rate*10000))/10000;
		$(".torate").html(rate);
	}
				
	$("#into dd ul li a").click(function() {
		var text = $(this).html();
		$("#into dt a span").html(text);
		var iso =  $("#into").find("span.value span.iso").html();
		updaterate();
		$(".intoiso").html(iso);
		$(".dropdown dd ul").hide();
		
	});		

	$("#branches dt a").click(function() {
		$(".dropdown dd ul").hide();
		$("#branches dd ul").toggle();
	});

	$("#branches dd ul li a").click(function() {
		var text = $(this).html();
		$("#branches dt a span").html(text);
		$(".branchsel").html(text);
		$(".dropdown dd ul").hide();
	});	

	var tariffs = '';
	if($('#calculator').length){
		tariffs = $("#calculator span.tariffs").html();
		tariffs = jQuery.parseJSON(tariffs);
	}

	$("#calculator .calculate").click(function(e) {
		e.preventDefault();
		var value = $("input.amount").val()
		var from = $("#from").find("span.value span.coefficient").html();
		var into = $("#into").find("span.value span.coefficient").html();
		
		var tar = (value*from);
		var calc = tar/into;
		
		if(ie){ tar = tar/iecoef; }
		else{ tar = tar/gbcoef; }
		
		calc = (Math.floor(calc*100))/100;
		calc = calc.toFixed(2);
		
		var tarcharge = 0;
		
		if(tar>6000 && !ie){
			alert('Please Contact Unity Link regarding this transaction.');
			tarcharge = 'N/A';
		} else if(tar>5000 && !ie){
			tarcharge = (tar*0.03).toFixed(2);
		} else {
			var found = false;
			jQuery.each(tariffs, function (i, item) {
				if(tar<=item.amount && !found){
					found = true;
					tarcharge = item.fee;
				}
			});
			if(!found){
				tarcharge = tariffs[tariffs.length-1].fee;
			}
		}
		
		var iso =  $("#into").find("span.value span.iso").html();
		
		$("#calculator .result").val(calc);
		$("#calculator .toiso").html(iso);
		$("#calculator .tariff").html(tarcharge);
	});

	function getImage(image) {
		var path = "images/flags/";
		return (path + image);
	}

	$(document).bind('click', function(e) {
		var $clicked = $(e.target);
		if (! $clicked.parents().hasClass("dropdown"))
			$(".dropdown dd ul").hide();
	});

});

function slideSwitch() {
var $active = $('#slideshow IMG.active');

if ( $active.length == 0 ) $active = $('#slideshow IMG:last');

var $next =  $active.next().length ? $active.next()
	: $('#slideshow IMG:first');

$active.addClass('last-active');

$next.css({opacity: 0.0})
	.addClass('active')
	.animate({opacity: 1.0}, 1000, function() {
		$active.removeClass('active last-active');
	});
}

$(function() {
setInterval( "slideSwitch()", 5000 );
});
