﻿/**
 * Trova i box con classe "allclick".
 * All'interno cerca dei link, ed estende l'href sul click di tutto il box
 * @return null
 */
function allClick()
{
try
{
	var clickbox = $('.allclick');
	clickbox.each(function()
	{
		var $this = $(this);
		$this.css('cursor','pointer');
		if($('a',this).is('[target="_blank"]')){
			$this.bind('click', function(event){ 
				event.preventDefault();
				window.open($('a',this).attr('href'));}
			);
		}else{
			$this.bind('click', function(){window.location.href=$('a',this).attr('href');});
		}
	});
}
catch(e) {toDebug(e.toString());}
}


function columnHeight()
{
try
{		
 	if($('#colonna-sx').innerHeight() <= $('#colonna-dx').innerHeight() ){
		$('#categorie').height($('#colonna-dx').innerHeight() - 121 - 19);
	}else{
		$('#colonna-dx').height($('#colonna-sx').innerHeight());
		$('#categorie').height($('#colonna-sx').height() + 10);
	}
}
catch(e) {toDebug(e.toString());}
}


function setupForm()
{
try
{
	$("#loginForm .submit").bind('click',function(event)
	{
		$(this).parent().submit();
	});
        
 	$("#contactForm .submit").bind('click',function(event)
	{

                errore = "";
                
                if($("#email").val() == $("#email").attr('originalValue') || !$("#email").val().match(/^([a-z0-9._-]+@[a-z0-9._-]+\.[a-z]{2,4}$)/i)){
                        errore = 'Inserire un\'indirizzo email valido';
                }else if($("#argomento").val() == $("#argomento").attr('originalValue')){
                        errore = 'Inserire un\'argomento email valido';
                }
                /*
                }else if(!$('#privacy').is(':checked')){
                        errore = 'Autorizzare il trattamento dei dati personali';
                }
                */

                
                if(errore == ""){
                    var datastr ='p=contact&email=' + $("#email").val() + '&argomento=' + addslashes(encodeURIComponent($("#argomento").val()));
                    $("#risposta").css("background-color","#629DA5");
                    $("#risposta").slideDown();
                    $("#risposta").html("Invio in corso..");
                    setTimeout("sendContact('"+datastr+"')",1000);
                }else{
                    $("#risposta").css("background-color","#629DA5");
                    $("#risposta").slideDown();
                    $("#risposta").html(errore);

                }
                return false;
		//$(this).parent().submit();
	});       
	                
 	$("#downloadForm .submit").bind('click',function(event)
	{

                errore = "";
                
                if($("#email").val() == $("#email").attr('originalValue') || !$("#email").val().match(/^([a-z0-9._-]+@[a-z0-9._-]+\.[a-z]{2,4}$)/i)){
                        errore = 'Inserire un\'indirizzo email valido';
                }

                if(errore == ""){
                    var datastr ='p=download&email=' + $("#email").val() + '&speciale=' + $("#speciale").val() + '&n=' + $("#pagina").val();
                    $("#risposta").css("background-color","#629DA5");
                    $("#risposta").slideDown();
                    $("#risposta").html("Invio in corso..");
                    setTimeout("sendDownload('"+datastr+"')",1000);
                }else{
                    $("#risposta").css("background-color","#629DA5");
                    $("#risposta").slideDown();
                    $("#risposta").html(errore);

                }
                return false;
		//$(this).parent().submit();
	});       
                                
                                                
                                                                            	
	$(".input_form:text").bind('focus blur',function(event)
	{
		var value = $(this).attr("originalValue");
		if (event.type == "focus") {
			if ($(this).val() == "" || $(this).val() == value) {
				$(this).val("");
			}
		}else {
			if ($(this).val() == "") {
				$(this).val(value);
			}
		}
	});	
	
}
catch(e) {toDebug(e.toString());}
}

function addslashes( str ) {  
    return (str+'').replace(/([\\"'])/g, "\\$1").replace(/\0/g, "\\0");  
}  

function sendContact(datastr){
	$.ajax({
		type: "POST",
		url: "index.php",
		data: datastr,
		cache: false,
		success: function(html){
		$("#risposta").html("Richiesta inviata con successo");
		$("#risposta").css("background-color","#629DA5");
		setTimeout('$("#risposta").slideUp()',1000);
	}
	});
}

function sendDownload(datastr){
	$.ajax({
		type: "POST",
		url: "index.php",
		data: datastr,
		cache: false,
		success: function(html){
		$("#risposta").html("Richiesta inviata con successo");
		$("#risposta").css("background-color","#629DA5");
		setTimeout('$("#risposta").fadeOut()',1000);
	}
	});
}

/*funzione per richiamare gotovetrine su vetrine*/
function assignClick()
{
	$('a','#speciali').bind('click', function(){	
		return settaVisita($(this).attr("param1"), $(this).attr("param2"));
	});		
	$('a','#colonna-dx article').bind('click', function(){	
		return settaVisita($(this).attr("param1"), $(this).attr("param2"));
	});			
}

function settaVisita(user, speciale)
{		
	if(user != ""){
		// Se ho un idstruttura aggiungo il click nel CMS
		var updateurl= 'index.php?p=view&user='+user+'&speciale='+speciale;		
		$.get(updateurl);	
	
	}
	// Aggiornamento Analytics
	//pageTracker._trackPageview ('/away/' + nome);	
	/*
	alert(updateurl)
	return false;
	*/
}

//function settaFormInformazioni(){

//}
/*****************************************************************************************/
function includeIe6Nomore()
{
	if($.browser.msie && $.browser.version.match('6.'))
		$('#ie6nomore').load('ie6nomore.html');
}
//	inizio funzioni di debug
debug_msg = false;
function toDebug(string)
{
	if (debug_msg)
	{
		if((typeof window.console=="undefined")) {alert(string);}
		else {
			console.error();
			console.group(string);
			console.trace();
			console.groupEnd();
		}
	}
}
function toConsole(string)
{
	if((typeof window.console=="undefined")) {alert(string);}
	else console.log(string);
}
//fine funzioni di debug

function $j(argument)
{
	var result = jQuery(argument);
	if (result.length == 0) return null;
	else return result;
}

$j(document).ready(function()
{ 

	allClick();
	columnHeight();
	setupForm();
	//$("a[rel^='prettyPhoto']").prettyPhoto({theme: 'facebook'});
    

        
});

$j(window).load(function()
{
    assignClick();   
});

$j(window).resize(function()
{

});

/*****************************************************************************************/
