$('document').ready(function(){
    
    if(jsvars.newsletter_success == true){
    	$('#win_subscribe_success').dialog({
    		autoOpen: true,
    		width: 400,
    		height: 150,
    		modal: true,
    		resizable: false,
    		buttons: {
    			"ok": function() { 
    				$(this).dialog("close"); 
    			}
    		}
    	});
    }
    
	
	// mainmenu
	$('#win_newsletter').dialog({
		autoOpen: false,
		width: 400,
		height: 400,
		modal: true,
		resizable: false,
		buttons: {
			"Chiudi": function() { 
				$(this).dialog("close"); 
			}
		},
		beforeClose: function(event, ui){
		    $('#newsletter_response').hide();
		    $('#newsletter_form').show();
		}
	});
	
	if (window.location.hash == '#win_newsletter') {
	    $('#win_newsletter').dialog('open');
	}
	
	$('.newsletter').live('click', function(){
		$('#win_newsletter').dialog('open');
		return false;
	});
	
	// leftmenu
	var mntab1 = $('#menutab1');
	var mntab2 = $('#menutab2');
	var curmenu = 1;
	
	var heightmnu = 50 + Math.max(mntab1.outerHeight(), mntab2.outerHeight());
	$('#lftmenu_cont').height(heightmnu).css('visibility', 'visible');
	
	$('#menutab2').hide();
	mntab2.css('visibility', 'visible');
	
	$('#tab2_btn').live('click', function(){
		if(curmenu == 1){
			curmenu = -1; //moving
			$('#menutab1').hide('slide', {direction: 'left'}, 800);
			$('#menutab2').show('slide', {direction: 'right'}, 800, function(){ curmenu = 2; });
			$('#tab1_btn').parent().removeClass('sel'); $('#tab2_btn').parent().addClass('sel');
		}
	});
	
	$('#tab1_btn').live('click', function(){
		if(curmenu == 2){
			curmenu = -1; //moving
			$('#menutab2').hide('slide', {direction: 'right'}, 800);
			$('#menutab1').show('slide', {direction: 'left'}, 800, function(){ curmenu = 1; });
			$('#tab2_btn').parent().removeClass('sel'); $('#tab1_btn').parent().addClass('sel');
		}
	});
	
	// gallery
	Shadowbox.init({
		overlayOpacity: 0.9,
		initialWidth: 600,
		initialHeight: 600,
		onOpen: hack_controls,
		onChange: set_shadowbox_title
	});
    
    function set_shadowbox_title(){
        var raw_description = Shadowbox.gallery[Shadowbox.current].title;
        Shadowbox.gallery[Shadowbox.current].title = "";
        //var splitted_description = raw_description.split(' ## -- ## ');
        //var copyright = (splitted_description[1]) ? splitted_description[1] : '';
        var copyright = raw_description;
        $('#sb-title').html(copyright + '<a onclick="Shadowbox.close()" title="Close" id="sb-nav-close" style=""></a>');
        $("#sb-title-inner").html('');
    }
    
    function hack_controls(){
		var sb_info_inner_html = $("#sb-info-inner").html();
		$("#sb-info-inner").prepend("<div id='sb_ctrls_cont'></div>");
		$sb_ctrls_cont = $("#sb_ctrls_cont");
		$("#sb-nav-next, #sb-counter, #sb-nav-previous").remove();
		
		$sb_ctrls_cont.html(sb_info_inner_html);
		$sb_ctrls_cont.find("#sb-nav").remove();
		
		set_shadowbox_title();
	}
	
	$("#sb-body-inner").live("click", function(){
        Shadowbox.next();
    });
	
	// form
	$('.clearfilters').live('click', function(){
		$(':input', $(this).closest('form')).each(function() {
			var type = this.type;
			var tag = this.tagName.toLowerCase(); // normalize case
			if (type == 'text' || type == 'password' || tag == 'textarea') this.value = "";
			else if (type == 'checkbox' || type == 'radio') this.checked = false;
			else if (tag == 'select') this.selectedIndex = '';
		});
	});
	
	// events
	$('.listcol').live('click', function(){
		location.href = $($(this).find('a')[0]).attr('href');
	});
});


function submit_newsletter(type){
    var fields = {
        fd_name: $('#fd_name').val(),
        fd_surname: $('#fd_surname').val(),
        fd_email: $('#fd_email').val(),
        fd_nws: $('#fd_nws').val(),
        type: type
    };
    
    $('#fld-'+fields.fd_nws+'-'+fields.fd_nws+'-name').val(fields.fd_name); //fields.type+'-'+
    $('#fld-'+fields.fd_nws+'-'+fields.fd_nws+'-surname').val(fields.fd_surname); //fields.type+'-'+
    $('#fld-'+fields.fd_nws+'-'+fields.fd_nws+'-email').val(fields.fd_email); //fields.type+'-'+
    
    // console.log(fields);
    // console.log('#frm-'+fields.fd_nws+'-'+fields.type);
    // return false;
    
    $('#win_newsletter').dialog('close');
    $('#frm-'+fields.fd_nws+'-'+fields.type).submit();
    return false;
}

function is_empty(what){
    return ([ null, '' ].indexOf(what) == -1) ? false : true;
}

function is_valid_email(email) {
    var email_filter = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
    
    return email_filter.test(email);
}



