/// <reference path="/Scripts/Intellisense-jquery/jQuery-vsdoc.js">
$(function () {
	initDialog();
	initDatepicker();
	gestioneMenu();
	//Autosize('ul.menuNew li', 600);
});

function initDatepicker() 
{
	//$('.CALENDARIO').datepicker();
}

function initDialog() 
{
	$('.DIALOG').one('click', function () {

		var dialogUrl = this.href;
		var dialogHeight = $(this).attr('dialogheight') == null ? 300 : $(this).attr('dialogheight');
		var dialogWidth = $(this).attr('dialogwidth') == null ? 500 : $(this).attr('dialogwidth');
		var dialogTitle = $(this).attr('dialogtitle') == null ? 'Programmacasa' : $(this).attr('dialogtitle');
		var loading = $('<img src="/Resources/ajax-loader.gif" alt="loading..." />');

		dialogHeight = parseInt(dialogHeight);
		dialogWidth = parseInt(dialogWidth);
		
		var dialog = $('<div style="display:none"></div>').append(loading.clone());
		dialog.load(dialogUrl).dialog({
			height: dialogHeight,
			width: dialogWidth,
			modal: true, 
			title: dialogTitle,
			open: function () {
				// scrollbar fix for IE
				$('body').css('overflow', 'hidden');
			},
			close: function () {
				// reset overflow
				$('body').css('overflow', 'auto');
			}
			 });

		$(this).click(function () {
			dialog.dialog('open');
			return false;
		});
		
		//Cosė il browser non segue il link
		return false;
	});
}

function CaricaVideo(Src,Titolo) {
   return AC_FL_RunContent( 'codebase','http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,28,0','width','160','height','100','align','','title',Titolo,'src',Src,'quality','high','pluginspage','http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash','movie',Src ); //end AC code
}

function IsEmail(emailAddress) {
	var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
	return pattern.test(emailAddress);
}

function gestioneMenu() {

	$(function () {
		var nomePagina = location.href.substr(location.href.lastIndexOf("/") + 1).split(/[?#]/)[0];
		//nomePagina = '/' + nomePagina.toLowerCase();
		nomePagina = location.pathname.toLowerCase();

		$('#navigation ul li a').each(function (n) {
			if ($(this).attr('href') == nomePagina) {
				$(this).attr('class', 'selected');
			}
			else {
				//$($(this).children()[0]).removeAttr('class', 'selected');
			}
		});
	});
}

function Autosize(classe, larghezzaFissa) {

	var actualWidth = larghezzaFissa;
	var totalLIWidth = 0;

	// Calculate total width of list items
	var lis = $(classe);

	lis.each(function () {
		totalLIWidth += $(this).width();
	});

	// Work out how much padding we need
	var requiredPadding = Math.round(((actualWidth - totalLIWidth) / lis.length) / 2);

	// To account for rounding errors, the error is going to be forced into the first tab.
	var roundingErrorFix = (requiredPadding * lis.length * 2) + totalLIWidth - actualWidth;

	// Apply padding to list items
	var isFirst = true;
	lis.each(function () {
		if (isFirst) {
			$(this).css('padding-left', requiredPadding - roundingErrorFix + 'px')
            .css('padding-right', requiredPadding - roundingErrorFix + 'px');
			isFirst = false;
		}
		else
			$(this).css('padding-left', requiredPadding + 'px')
            .css('padding-right', requiredPadding + 'px');
	});
}

function isChecked(idCheckbox)
{
	return $('#' + idCheckbox + ':checked').val() != null;
}

$.fn.extend({
	isChecked: function () { return false; }
});

function getSlider(idSlider,idHiddenPrezzi, idMostraPrezzi, stepPrezzi) {
	var defaultMin = 0, defaultMax = 1050000;
	var prezziSelezionati = $('#' + idHiddenPrezzi).val().split(';');
	var valoreIllimitato = "10000000";
	var prezzoAMostra = 0;
	if (prezziSelezionati[1] == defaultMax || prezziSelezionati[1] == valoreIllimitato)
		prezzoAMostra = 'illimitato';
	else
		prezzoAMostra = valoreIllimitato;

	if (stepPrezzi == undefined)
		stepPrezzi = 50000;

	
	$('#' + idMostraPrezzi).html('&euro; ' + prezziSelezionati[0] + ' - &euro; ' + prezzoAMostra);
	$('#' + idSlider).slider({
		range: true,
		values: [prezziSelezionati[0], prezziSelezionati[1]],
		min: 0,
		max: defaultMax,
		step: stepPrezzi,
		animate: true,
		slide: function (event, ui) {
			if (ui.values[1] == defaultMax || ui.values[1] == valoreIllimitato) {
				$('#' + idMostraPrezzi).html('&euro; ' + ui.values[0] + ' - illimitato');
				$('#' + idHiddenPrezzi).val(ui.values[0] + ';' + valoreIllimitato);
			}
			else {
				$('#' + idMostraPrezzi).html('&euro; ' + ui.values[0] + ' - &euro; ' + ui.values[1]);
				$('#' + idHiddenPrezzi).val(ui.values[0] + ';' + ui.values[1]);
			}
		}
	});
}
