$(document).ready(function(){

	// **************************************************************************************************************************
	// GENERAL ACTIONS

	// external links
	$("a[rel='external']").click(function() {
		return !window.open($(this).attr("href"));
	});
	$('.hide-with-js').hide(); // use this class to hide the message "please open this link in new window"

	// fade in messages
	$('.successMsg').hide();
	$('.successMsg').fadeIn(1000);
	$('.errorMsg').hide();
	$('.errorMsg').fadeIn(1000);
	$('.warningMsg').hide();
	$('.warningMsg').fadeIn(1000);

	// non-clickable links
	$(".disabled").click( function() {
		return false;
	});

	$('.form-submitter').click(function() {
		$(this).parents('form').submit();
		return false;
	});

	// clear inputs
	clearInputs(".clear-inputs, #search input, #login-top input, #post-favor-form input");

	// Header error message
	$(".errorMsg-overlay, .errorMsg-after").hide();
	$(".errorMsg-overlay, .errorMsg-after").fadeIn(300, function(){
		var elem = $(this);
		setTimeout(function(){
			$(elem).fadeOut(300);
		}, 1200);
	});

	// Sliding doors:
	$('.text-input input').focus(function() {
		$(this).css('background-position',  'left -25px');
		$(this).parent().css('background-position',  'right -25px');
	});
	$('.text-input input').blur(function() {
		$(this).css('background-position',  'left 0px');
		$(this).parent().css('background-position',  'right 0px');
	});

	// END GENERAL ACTIONS
	// **************************************************************************************************************************

	// CUSTOM SIZED BOXES:
	/*
		Specify the width in the 'title' attribute
		Add extra styling for the content via the '.box-content' div in site-styles.css,
		not the '.box' div
	*/
	$('.box').each(function() {
		var html  = $(this).html();
		var width = parseInt($(this).attr('title'));
		$(this).attr('title', '');
		$(this).css('width', width);

		var newHtml = '';
		// Header:
		newHtml += '<div class="header clearfix">';
		newHtml += '<div class="cl">&nbsp;</div>';
		newHtml += '	<div class="m" style="width: ' + (width-10) + 'px">&nbsp;</div>';
		newHtml += '	<div class="cr">&nbsp;</div>';
		newHtml += '</div>';
		// Content:
		newHtml += '<div class="content clearfix">';
		newHtml += '	<div class="lc" style="width: ' + (width-3) + 'px">';
		newHtml += '		<div class="box-content clearfix">';
		newHtml += html;
		newHtml += '		</div>';
		newHtml += '	</div>';
		newHtml += '	<div class="rc">&nbsp;</div>';
		newHtml += '</div>';
		// Footer:
		newHtml += '<div class="footer clearfix">';
		newHtml += '	<div class="cl">&nbsp;</div>';
		newHtml += '	<div class="m" style="width: ' + (width-10) + 'px">&nbsp;</div>';
		newHtml += '	<div class="cr">&nbsp;</div>';
		newHtml += '</div>';

		// And needed scripts:
		newHtml += '<script type="text/javascript">imagePreview();</script>';
//		newHtml += '<script type="text/javascript"> $(\'.print-btn\').click(function() {';
//		newHtml += '	$(\'#dsg-table\').printElement(); return false;   }); </script>';

		$(this).html(newHtml);
		$(this).find('.rc').height($(this).find('.lc').height());
	});
	// Boxes that need to match height:
	var boxMaxHeightRC = 0;
	var boxMaxHeightBC = 0;
	$(".box.matchHeight").each(function() {
		if($(this).find('.rc').height() > boxMaxHeightRC)
			boxMaxHeightRC = $(this).find('.rc').height();
		if($(this).find('.box-content').height() > boxMaxHeightBC)
			boxMaxHeightBC = $(this).find('.box-content').height();
	});
	$(".box.matchHeight").find('.rc').height(boxMaxHeightRC);
	$(".box.matchHeight").find('.box-content').height(boxMaxHeightBC);

	$(".frequency").blur(function(){
		var series = $(".series").val();
		var frequency = $(".frequency").val();

		frequency_to_esr(series, frequency);
		return false;
	});

	// Glossary / Faq
	$("dl.glossary dt").addClass("clearfix");

	$("dl.glossary dt").click(function(){
		var c = $(this).hasClass("active");
		if (!c) {

			var dlElem = $(this).parent().attr("title");

			$('dl.glossary[title="'+dlElem+'"]').find("dd.active").hide();

			$('dl.glossary[title="'+dlElem+'"]').find("dt").removeClass("active");
			$('dl.glossary[title="'+dlElem+'"]').find("dd").removeClass("active");

			$(this).addClass("active");
			$(this).next().addClass("active").show();
		}
		return false;
	});

	// Banners Homepage
	nrBannerMax = $("#homepage-banners li").size();
	timer = setTimeout(getNextItem, interval);
	currentBanner = 1;
	$("#homepage-banners li").hide();
	changeImage();

	// Font replacement
	replaceFonts();

	$(".glossary-images").hide();
	$(".glossary-images").each(function(){
		var id_gl= $(this).attr("id");
		var array_id = id_gl.split('_');
		//alert(id_gl);
		var hmtl= $(this).html();
		//alert(hmtl);
		$('dd#cont'+array_id[1]+' .'+array_id[0]).html(hmtl);

	});
});

// Banner Slideshow
var currentBanner = 1;
var nrBannerMax;
var timer;
var interval = 6000;
function changeImage() {
	//$("#homepage-banners li").hide();
	$("#banner"+currentBanner).fadeIn(500);
	$("#banner"+currentBanner).css('z-index', '80');
}
function getNextItem() {
	var next;
	if (parseInt(currentBanner)==parseInt(nrBannerMax)) {
		next = 1;
	} else {
		next = currentBanner + 1;
	}
	$("#banner"+currentBanner).fadeOut(500);
	currentBanner = next;
	changeImage();
	clearTimeout(timer);
	timer = setTimeout(getNextItem, interval);
}

// Font replacement
function replaceFonts() {
	Cufon.replace('.avenir, #menu li, #header #call-us', { fontFamily: 'Avenir Heavy', hover: true, hoverables: { a: true, button: true }});
	Cufon.replace('h1, h2, h3', { fontFamily: 'Avenir Heavy'});
}

// Ajax:
function ajax_states_restrict(id_country, id_state) {
	var selected_country_id 		= $('#' + id_country).val();

	$.ajax({
	    type: "POST",
	    url: path + "ajax/restrict-states",
	    data: {
	        id_country: selected_country_id
	    },
		dataType: 'json',
	    success: function(data){
	    		var options = '';
				for (var i = 0; i < data.length; i++) {
				  options += '<option value="' + data[i].optionValue + '">' + data[i].optionDisplay + '</option>';
				}
				$("select#" + id_state).html(options);
	    }
	});
}


function clearInputs(x) {
	$(x).each(function(i){
		var inputValue = $(this).val();
		var inputTitle = $(this).attr("title");

		if (inputValue=="" || inputValue==inputTitle) {
			//alert(inputTitle);
			$(this).focus(function(){
				if ($(this).val()==inputValue) {
					$(this).val("");
				}
			});
			$(this).blur(function(){
				if ($(this).val()=="") {
					$(this).val(inputValue);
				}
			});
		}

	});


}

function frequency_to_esr(series, frequency) {
	$.ajax({
		type: "POST",
		url: path + "ajax/find-esr",
		data: {
			series: series,
			frequency: frequency
		},
		dataType: 'json',
		success: function(data){
			var options = '';
			for (var i = 0; i < data.length; i++) {
				options += '<option value="' + data[i].esr + '">' + data[i].esr + '</option>';
			}
			$(".esr").html(options);
		}
	});
}

