var music = false;

$(document).ready(function() {

	resizeBg()             			
	$(window).resize(function() {$('#bg').cycle('destroy');resizeBg()});
		
	$("#dream").click(function() {
	   showLogin();
	});	
	
	checkMusic();
	
});

function checkMusic(){
    if(!music){
        // Music
	    var so = new SWFObject("music/music.swf", "", "30", "30", "9", "#ffffff");
        so.addParam("quality","high");
        so.addParam("menu","false");
        so.addParam("wmode","transparent");
        so.write("music");
        music = true;
    }else{
        $('#music').empty();
        music = false;        
    }
}

function bgSlide(){
    $('#bg').cycle({
		fx: 'fade',
		speed:  2000,
		timeout:  8000,
		random: 1
	});
}

function showLogin() {
    $("#logo").animate({opacity:0}, 1000);
    $("#dream").animate({top: 425, opacity:0}, 1000);
    $("#login").animate({top: 0}, 1000);
    $("#header").delay(1000).animate({top: 0}, 800);
}

function resizeBg(){
	if (($(window).width()/$(window).height()) < (1280/800)) {
	    $("#bg img").each(function(index) {
            $(this).height($(window).height());
            $(this).width($(window).height()*1.6);
        });
	} else {
	    $("#bg img").each(function(index) {
            $(this).width($(window).width());
            $(this).height($(window).width()/1.6);
        });
	}
	bgSlide();
}

function loadPage(_page){
    $("#firstBlock").hide();
    $("#content").width(960).empty().fadeIn(1500);
    $("#content").html('<div id="loader"><img src="images/ajax-loader.gif" alt=""/><br/>Caricamento in corso...</div>');
    
    $.ajax({
      url: _page,
      cache: false,
      success: function(_html){
        $("#content").html(_html);
        $("#footer").css("bottom",0);
      }
    });    
    
}

function check_login(){
    $("#messaggio").html("Login in corso, attendere prego.")
    $.ajax({
      url: "AJAX.login.aspx?tessera=" + $("#codice").val() + "&cognome=" + $("#cognome").val(),
      type: "POST",
      cache: false,
      success: function(_response){
        if(_response=='ok'){ 
            $("#m_login").hide();
            $("#m_logout").show();
            $("#m_speciali").show();
            $("#m_offerte").show();
            loadPage("speciali.aspx");
        }else if(_response=='registrazione'){
            loadPage("formRegistrazione.aspx");
        }else{
            $("#messaggio").html(_response);
        }
      }
    });
}

function openDetail(_ID,_indexVisible, _index){
    //alert(_ID);
    var position = _indexVisible % 3;
    
    $("#ElencoOfferte li:visible").fadeTo(0, 0.2);
    $("#ElencoOfferte li:eq(" + _index + ")").fadeTo('fast', 1).addClass("print");
    
    var _html = '<li id="dettaglio">';
    _html += '</li>';
   
    
    if (position==0 && $("#ElencoOfferte li:eq(" + _index + ")").nextAll('li:visible:first').nextAll('li:visible:first').length!=0){
        $("#ElencoOfferte li:eq(" + _index + ")").nextAll('li:visible:first').nextAll('li:visible:first').after(_html);
        $("#dettaglio").css("background-position","-810px 0px");
    }else if (position==1 && $("#ElencoOfferte li:eq(" + _index + ")").nextAll('li:visible:first').length!=0){
        $("#ElencoOfferte li:eq(" + _index + ")").nextAll(':visible:first').after(_html);
        $("#dettaglio").css("background-position","-490px 0px");
    }else{
        $("#ElencoOfferte li:eq(" + _index + ")").after(_html);
        if(position==0){
            $("#dettaglio").css("background-position","-810px 0px");
        }else if (position==1){
            $("#dettaglio").css("background-position","-490px 0px");
        }else{
            $("#dettaglio").css("background-position","-170px 0px");
        }
    } 
    
    $.ajax({
      url: "AJAX.dettaglio.aspx?idOfferta=" + _ID,
      type: "POST",
      cache: false,
      success: function(_response){
        $("#dettaglio").html(_response);
      }
    });   
    
    $('html, body').animate({scrollTop:$("#ElencoOfferte li:eq(" + _index + ")").position().top+140}, 'slow', function(){ $("#dettaglio").slideDown();});         
}

function closeDetail(){
    $('#dettaglio').slideUp('slow', function() {
        $('#dettaglio').remove();
        $('#ElencoOfferte li:visible').fadeTo('fast', 1).removeClass("print");;
    });
}


// ELENCO OFFERTE
var mesi = new Array();
mesi[0] = "Gennaio";
mesi[1] = "Febbraio";
mesi[2] = "Marzo";
mesi[3] = "Aprile";
mesi[4] = "Maggio";
mesi[5] = "Giugno";
mesi[6] = "Luglio";
mesi[7] = "Agosto";
mesi[8] = "Settembre";
mesi[9] = "Ottobre";
mesi[10] = "Novembre";
mesi[11] = "Dicembre";

function unique(Arr){
    Arr=Arr.sort();
    i=0;
    while(i<Arr.length){
        if(Arr[i]==Arr[i+1]){
            Arr.splice(i,1);
            i--
        }
        i++
    }
    return Arr;    
}

function euroToNum(euro){
    var eur = euro.split("€ ").join("");
    eur = eur.split(",00").join("");
    eur = eur.split(".").join("");
    eur = parseFloat(eur);
    return eur;
}

// popola select
function popolaDestinazioni(){
    var Adestinazioni = new Array();
    $("#destinazione").html("");
    $('#ElencoOfferte li a.destinazione').each(function(index) {
        Adestinazioni.push($(this).text())
    });
    
    $.each(unique(Adestinazioni), function(index, value) { 
      $("#destinazione").append("<option value='" + (index+1) + "'>" + value + "</option>");
    });
}

function popolaMese(){
    $("#mese").html("");        
    var Amesi = new Array();
    $('#ElencoOfferte li span.partenzaIl').each(function(index) {
        Amesi.push($(this).text().split("/")[1]);
    });
    
    $.each(unique(Amesi), function(index, value) { 
      $("#mese").append("<option value='" + (parseFloat(value)) + "'>" + mesi[parseFloat(value)-1] + "</option>");
    });
}

function popolaPrezzo(){
    $("#prezzo").html("");
    var prezzi1 = false;
    var prezzi2 = false;
    var prezzi3 = false;
    $('#ElencoOfferte li td.prezzo').each(function(index) {
        if(euroToNum($(this).text())<750){
            prezzi1 = true;
        }else if(euroToNum($(this).text())>1500){
            prezzi3 = true;
        }else{
            prezzi2 = true;
        }
    });
    
    if(prezzi1){
        $("#prezzo").append("<option value='1'>fino a € 750</option>")
    }
    if(prezzi2){
        $("#prezzo").append("<option value='2'>da € 750 a € 1500</option>")
    }
    if(prezzi3){
        $("#prezzo").append("<option value='3'>oltre € 1500</option>")
    }        
}

function matchSelect(){
    
      var collDestinazioni = new Array();
      var collMese = new Array();
      var collPrezzo = new Array();
    
    $('#ElencoOfferte li').each(function(index) {
        var match = true;
        
        
        // Scelto Destinazione
        if($("#destinazione").val()!= null){
            if($("#destinazione option:selected").text()!=$(this).find("a.destinazione").text()){
                match = false;
            }
        }
        
        
        // Scelto Mese
        if($("#mese").val()!= null){
            var mese = $(this).find("span.partenzaIl").text().split("/")[1];
            if($("#mese").val()!= parseFloat(mese)){
                match = false;
            }
        }
        
        // Scleto Prezzo
        if($("#prezzo").val()!= null){
            
            var prezzo = euroToNum($(this).find("td.prezzo").text());

            if($("#prezzo").val()=="1"){
                if(!(prezzo<750)){
                    match = false;
                }
            }
            
            if($("#prezzo").val()=="2"){
                if(!(prezzo>=750 && prezzo<=1500)){
                    match = false;
                }
            }
            
            if($("#prezzo").val()=="3"){
                if(!(prezzo>1500)){
                    match = false;
                }
            }                
        
        }   
                 
        if(match){
            collDestinazioni.push($(this).find("a.destinazione").text());
            collMese.push($(this).find("span.partenzaIl").text().split("/")[1]);
            collPrezzo.push(euroToNum($(this).find("td.prezzo").text()));
        }
        
    });
    
    $("#destinazione").html("");
    $.each(unique(collDestinazioni), function(index, value) { 
      $("#destinazione").append("<option value='" + (index+1) + "'>" + value + "</option>");
    });
    if($("#destinazione option").length==1){
        $("#destinazione option").attr("selected","selected");
    }
    
    $("#mese").html("");
    $.each(unique(collMese), function(index, value) { 
      $("#mese").append("<option value='" + parseFloat(value) + "'>" + mesi[parseFloat(value)-1] + "</option>");
    }); 
    if($("#mese option").length==1){
        $("#mese option").attr("selected","selected");
    }            
    
    $("#prezzo").html("");
    var rangePrezzi = new Array();
    $.each(unique(collPrezzo), function(index, value) { 
          if(value<750){
            rangePrezzi.push("1|fino a € 750");
          }else if(value>1500){
            rangePrezzi.push("3|oltre € 1500");
          }else{
            rangePrezzi.push("2|da € 750 a € 1500");
          }
    }); 
    $.each(unique(rangePrezzi), function(index, value) {
        $("#prezzo").append("<option value='" + value.split("|")[0] + "'>" + value.split("|")[1] + "</option>");
    });
    if($("#prezzo option").length==1){
        $("#prezzo option").attr("selected","selected");
    }         
    

}

function search(){
    $('#ElencoOfferte li').fadeTo('fast', 1);
    $('#ElencoOfferte li').hide();
    $("#nada").hide();
    $('#ElencoOfferte li').each(function(index) {
        var match = true;
        
        // Destinazione
        if($("#destinazione").val()!= null){
            if($("#destinazione option:selected").text()!=$(this).find("a.destinazione").text()){
                match = false;
            }
        }
        
        // mese
        if($("#mese").val()!= null){
            var mese = $(this).find("span.partenzaIl").text().split("/")[1];
            if($("#mese").val()!=parseFloat(mese)){
                match = false;
            }
        }
        
        // prezzo
        if($("#prezzo").val()!= null){
            
            var prezzo = euroToNum($(this).find("td.prezzo").text());
            
            if($("#prezzo").val()=="1"){
                if(!(prezzo<750)){
                    match = false;
                }
            }
            
            if($("#prezzo").val()=="2"){
                if(!(prezzo>=750 && prezzo<=1500)){
                    match = false;
                }
            }
            
            if($("#prezzo").val()=="3"){
                if(!(prezzo>1500)){
                    match = false;
                }
            }                
        
        }            
        
        if(match){
            $(this).show();
        }
    });    
    
    if($('#ElencoOfferte li:visible').size()==0){
        $("#nada").show();
    }   
}     


