$(function() {
	var itemsTime = new Array();

	$('#itemsTime').hide();

	$('#itemsTime div').each(function(e, i){
		itemsTime.push($(i).text());
	});

	window.onload = function(){

		$('#stage ul').simpleCarousel({
			btnNext 	: '#stage .next',
			btnPrev 	: '#stage .prev',
			navigation	: '#stage .controls',
			auto		: true,
			autoTime	: 5000,
			beforeStart : function(i, el, opt){opt.autoTime = parseInt(itemsTime[i]);}
		});
	};


	$('#stage li').each(function(i,el){
		if($(el).attr('rel')){$(el).append('<a href="'+ $(el).attr('rel')+'" class="flash_ancher"></a>');}
	})

	$('.news_carousel ul').simpleCarousel({
		animateProparty	: 'top',
		navigation		: '.news_carousel .controls',
		auto			: true,
		autoTime		: 8000,
		itemWidth 		: 238
	});

	$("#menuinfo > ul").accordion({
//		changestart: function(event, ui) {
//			ui.newHeader.parent().prependTo($('#menuinfo ul'))
//		}
	});

	$('#calc').change(function(){
		get_currencies();
	});

	$('.calculate').click(function(){
		calculate();
	});

	get_currencies();

	function get_currencies()
	{
		var current_val = 'value_sell';//$('#calc').val();
		var parameters	= {'method' : 'currency_state', 'val' : current_val};
		var url			= MAG.controller_url + 'application/' + MAG.ajax;

		$.get(url, parameters, function(html){
			$('.valuta_content_1').html(html);
			$('#currency_slider ol').simpleCarousel({
				btnNext : '.valuta_content_1 .next',
				btnPrev : '.valuta_content_1 .prev',
				itemWidth : 175
			});
		});
	}

	function calculate()
	{
		var from_val =  $('#from_val').val();
		var to_val =  $('#to_val').val();
		var course = $('#calc').val();
		var qty = $('#quantity_val').val();

		if(!qty)
		{
			qty = 1;
			$('#quantity_val').val(1);
		}

		var parameters	= {'method' : 'calculate', 'from_val' : from_val, 'to_val' : to_val, 'qty' : qty};
		var url			= MAG.controller_url + 'application/' + MAG.ajax;

		$.get(url, parameters, function(response) {
			$('.valuta_results').show();
			$('#result_val').html(response);
		});
	}

$('a.quotes').click(function() {

    	//Get the screen height and width
    	var maskHeight = $(document).height();
    	var maskWidth = $(window).width();

	    //Set heigth and width to mask to fill up the whole screen
		$('#mask').css({'width':maskWidth,'height':maskHeight});

		//transition effect
		$('#mask').fadeIn(1000);
		$('#mask').fadeTo("slow", 0.8);

		//Get the window height and width
		var winH = $(window).height();
		var winW = $(window).width();
		var rates = $('#get-rates');

		//Set the popup window to center
		rates.css('top',  winH/2 - rates.height()/2);
		rates.css('left', winW/2 - rates.width()/2);

		//transition effect
		rates.fadeIn(2000);

	});


	 //if close button is clicked
	$('.window .close').click(function (e) {
		//Cancel the link behavior
		e.preventDefault();

		$('#mask').hide();
		$('.window').hide();
	});

	//if mask is clicked
	$('#mask').click(function () {
		$(this).hide();
		$('.window').hide();
	});
 })
