
/* MM.CORE.JS */

$(function(){$('#gallery a').lightBox();});
$(function(){$('.entry a').lightBox();});


var index_fader={
    num:-1,
    cur:-99,
    old:-1,
    step:1,
    intervall:6*1000,
    fadeintervall:700,
    holderid:'index_fader',
    controlid:'index_fadercontrols',
    screens:null,
    timer:null,
    running:true,
    stop:false,
    screenchooser:true,
    playresume:true,
    init:function(){
        this.screens=jQuery('#'+this.holderid).children('.screen');
        this.num=this.screens.length;
        if(this.num==0){
            return
        }
        this.screens.each(function(i){
            jQuery(index_fader.screens[i]).mouseover(function(){
                index_fader.running=false
            });
            jQuery(index_fader.screens[i]).mouseout(function(){
                index_fader.running=true
            });
            if(index_fader.screenchooser){
                jQuery('#'+index_fader.controlid).append('<a href="#" id="index_faderctrl'+i+'" onclick="index_fader.fadeTo('+i+');return false;" onfocus="this.blur();">'+(i+1)+'</a>')
            }
        });
        jQuery('#'+index_fader.controlid).mouseover(function(){
            index_fader.running=false
        });
        jQuery('#'+index_fader.controlid).mouseout(function(){
            index_fader.running=true
        });
        if(this.playresume){
            jQuery('#'+index_fader.controlid).append('<a href="#" onclick="index_fader.startStop();return false;" class="playresume" onfocus="this.blur();">&nbsp;</a>')
        }
        if(this.step<0){
            this.fadeTo(this.num-1)
        }else{
            this.fadeTo(0)
        }
        if(this.stop){
            this.startStop()
        }
    },
    fadeTo:function(pos){
        window.clearTimeout(this.timer);
        this.old=this.cur;
        this.cur=pos;
        this.fade();
        this.timer=window.setTimeout('index_fader.walk();',this.intervall);
        if(!this.stop){
            this.startStop()
        }
    },
    startStop:function(){
        this.stop=!this.stop;
        if(this.stop){
            jQuery('#'+index_fader.controlid+' a.playresume').addClass('fadernavon')
        }else{
            jQuery('#'+index_fader.controlid+' a.playresume').removeClass('fadernavon')
        }
    },
    walk:function(){
        if(this.running&&!this.stop){
            window.clearTimeout(this.timer);
            this.old=this.cur;
            if(this.cur==-99){
                this.cur=0
            }else{
                this.cur+=this.step
            }
            if(this.cur>=this.num){
                this.cur=0
            }else if(this.cur<0){
                this.cur=this.num-1
            }
            this.fade()
        }
        this.timer=window.setTimeout('index_fader.walk();',this.intervall)
    },
    fade:function(){
        if(this.old>-1){
            jQuery('#index_faderctrl'+index_fader.old).removeClass('fadernavon');
            jQuery(this.screens[this.old]).fadeOut(this.fadeintervall,function(){})
        }
        if(this.cur>-1){
            jQuery(this.screens[this.cur]).fadeIn(this.fadeintervall,function(){
                jQuery('#index_faderctrl'+index_fader.cur).addClass('fadernavon')
            })
        }
    }
};

//CONTROLLO FORMATO EMAIL
function checkEmail(email){
    if (email.search(/^\w+((-\w+)|(\.\w+))*\@\w+((\.|-)\w+)*\.\w+$/) != -1)
        return 1;
    else
        return 0;
}

//CONTROLLO X FORM CONTATTI
function ctrlContact() {

    var col_err = "#FFFF55";
    var col_def = "";

    var frm = $('#frmContact');
    var name = $('#t_nome');
    var tel = $('#t_tel');
    var email = $('#t_email');
    var descr = $('#t_desc');

    var name_value = name.val();
    var tel_value = tel.val();
    var email_value = email.val();
    var descr_value = descr.val();

    //NOME E COGNOME
    if (name_value == '' || name_value == 'undefined' || name_value.length < 3){
        alert("Il nominativo non e' corretto");
        name.select();
        name.focus();
        name.attr("style", "background-color: " + col_err);
        return false;
    }else{
        name.attr("style", "background-color: " + col_def);
    }

    //TELEFONO
    if (tel_value == '' || tel_value == 'undefined'){
        alert("Il telefono non è corretto");
        tel.select();
        tel.focus();
        tel.attr("style", "background-color: " + col_err);
        return false;
    }else{
        tel.attr("style", "background-color: " + col_def);
    }

    //INDIRIZZO EMAIL
    if(checkEmail(email_value) == 0){
        alert("Indirizzo email non valido");
        email.select();
        email.focus();
        email.attr("style", "background-color: " + col_err);
        return false;
    }else{
        email.attr("style", "background-color: " + col_def);
    }

    //DESRIZIONE
    descr_value = $.trim(descr_value);

    if(descr_value == '' || descr_value == 'undefined' || descr_value.length < 10){
        alert("Inserire una descrizione (min. 10 caratteri)");
        descr.select();
        descr.focus();
        descr.attr("style", "background-color: " + col_err);
        return false;
    }else{
        descr.attr("style", "background-color: " + col_def);
    }

    frm.attr("action", "sendmail.php");
    frm.submit();
    alert("Richiesta inviata correttamente)");
    return true;
}

