var timemenuopen = false;
var regionmenuopen = false;
var viewmenuopen = false;

$(document).ready(function(){   
            
    $("#search").autocomplete("php/interface/search.php", {
        minChars: 2,
        scroll: false,
        highlight: false,
        loadingClass: "ac_loading",
        formatItem: formatSearchResultRow,
        extraParams: { 'language': language, 'city': city }    
    });
                    
    $("#search").result(function(event,item){
                    
        $("#search").attr("value","");
        if (item[0]!=0) {
            $("#showall").click();
            map.setZoom(16);                
            map.panTo(markerstore[item[0]].getLatLng());
            loadTerraceInfo(item[0], escape(item[1]));
        }
    });
    
    $("input:radio").click(function() {
        hideAndShowMarkers(this.value);
        updateVisibleTerraces();
    });
    
    $("body").click(function() {
        if (timemenuopen)
            $("#selecttime").click();
        if (regionmenuopen)
            $("#selectregion").click();        
        if (viewmenuopen)
            $("#terracesonview").click(); 
    });
            
    $("#selecttime").toggle(function(){
        var pos = $("#selecttime").offset();    
        var eWidth = $("selecttime").outerWidth();
        var mWidth = $("selecttime").outerWidth();
        var left = (pos.left + eWidth - mWidth) + "px";
        //show the menu directly over the placeholder  
        $("#timeselection").css( { 
                position: "absolute",
                zIndex: 5000,
                left: left, 
                top: "160px"
        } );
        makeTimeSelection();            
        $("#timeselection").show('fast');
        timemenuopen = true;
    },function(){
        $("#timeselection").hide('slow');
        timemenuopen = false;
    });
    
    $("#selectregion").toggle(function(){
        var pos = $("#selectregion").offset();    
        var eWidth = $("selectregion").outerWidth();
        var mWidth = $("selectregion").outerWidth();
        var left = (pos.left + eWidth - mWidth) + "px";
        //show the menu directly over the placeholder  
        $("#regionselection").css( { 
                position: "absolute",
                zIndex: 5000,
                left: left, 
                top: "160px"
        } );        
        $("#regionselection").show('fast');
        regionmenuopen = true;
    },function(){
        $("#regionselection").hide('slow');
        regionmenuopen = false;
    });
    
    $("#terracesonview").toggle(function(){
        var pos = $("#terracesonview").offset();    
        var eWidth = $("terracesonview").outerWidth();
        var mWidth = $("terracesonview").outerWidth();
        var left = (pos.left + eWidth - mWidth) + "px";
        //show the menu directly over the placeholder  
        $("#terracesonviewselection").css( { 
                position: "absolute",
                zIndex: 5000,
                left: left, 
                top: "190px"
        } );        
        map.disableScrollWheelZoom();
        $("#terracesonviewselection").show('fast');
        viewmenuopen = true;
    },function(){
        $("#terracesonviewselection").hide('slow');
        viewmenuopen = false;
        map.enableScrollWheelZoom();
    });
             
    $("#previoustime").click(function () {
        previousTime();         
    });    
    
    $("#nexttime").click(function () {
        nextTime();         
    });
    
    if (language=="fi") {
        $("#terracedialog").dialog({
            autoOpen: false,
            bgiframe: true,
            position: 'center',
            width: 550,
            height: 620,
            modal: true,
            resizable: false,            
            buttons: { Sulje: function() { $(this).dialog('close');} }
        });
        $("#marketingdialog").dialog({
            autoOpen: false,
            bgiframe: true,
            position: 'center',
            width: 500,
            height: 400,
            modal: true,
            resizable: false,            
            buttons: { Sulje: function() { $(this).dialog('close');} }
        });
        $("#helpdialog").dialog({
            autoOpen: false,
            bgiframe: true,
            position: 'center',
            width: 500,
            height: 500,
            modal: true,
            resizable: false,            
            buttons: { Sulje: function() { $(this).dialog('close');} }
        });     
        $("#commentdialog").dialog({
            autoOpen: false,
            bgiframe: true,
            position: 'center',
            width: 400,
            height: 550,
            modal: true,
            resizable: false,
            buttons: { Lähetä: function() { sendComment();},
                        Peruuta: function() { $(this).dialog('close');} }
        });    
        $("#competiondialog").dialog({
            autoOpen: false,
            bgiframe: true,
            position: 'center',
            width: 400,
            height: 350,
            modal: true,
            resizable: false,            
            buttons: { Sulje: function() { $(this).dialog('close');} }
        });     
    }else {
        $("#terracedialog").dialog({
            autoOpen: false,
            bgiframe: true,
            position: 'center',
            width: 550,
            height: 620,
            modal: true,
            resizable: false,
            buttons: { Close: function() { $(this).dialog('close');} }
        });    
        $("#helpdialog").dialog({
            autoOpen: false,
            bgiframe: true,
            position: 'center',
            width: 500,
            height: 500,
            modal: true,
            resizable: false,            
            buttons: { Close: function() { $(this).dialog('close');} }
        });         
        $("#commentdialog").dialog({
            autoOpen: false,
            bgiframe: true,
            position: 'center',
            width: 400,
            height: 550,
            modal: true,
            resizable: false,
            buttons: { Send: function() { sendComment(); },
                        Cancel: function() { $(this).dialog('close');} }
        });    
        $("#competiondialog").dialog({
            autoOpen: false,
            bgiframe: true,
            position: 'center',
            width: 400,
            height: 350,
            modal: true,
            resizable: false,            
            buttons: { Close: function() { $(this).dialog('close');} }
        }); 

    }
            
    $('#openterracedialog').click(function() {
        $("#terracedialog_feedback").html();
        $("#terracedialog").dialog('open');
    });
    
    $('#openmarketingdialog').click(function() {
        $("#marketingdialog").dialog('open');
    });    
    
    $('#openhelpdialog').click(function() {
        $("#helpdialog").dialog('open');
    }); 

    $('#opencompetiondialog').click(function() {
        $("#competiondialog").dialog('open');
    }); 
        
    $('#randomterrace').click(function() {
        selectRandomTerrace();
    });         
        
    $("#comment_fi").keyup(function() {
        $("#commentdialog_feedback").html("");
        calculateCharacters(200, "#comment_fi", "#comment_fi_characters");
    });
        
    $("#comment_en").keyup(function() {
        $("#commentdialog_feedback").html("");        
        calculateCharacters(200, "#comment_en", "#comment_en_characters");
    });
    
    $("#comment_nick").keyup(function() {
        $("#commentdialog_feedback").html("");        
    });
    
    $("#terrace_name").keyup(function() {
        $("#terracedialog_feedback").html("");        
    });
     
    $("#terrace_address").keyup(function() {
        $("#terracedialog_feedback").html("");        
    });
    
    $("#terrace_sunstart").keyup(function() {
        $("#terracedialog_feedback").html("");        
    });
    
    $("#terrace_sunend").keyup(function() {
        $("#terracedialog_feedback").html("");        
    });
    
    $("#terrace_comments").keyup(function() {
        $("#terracedialog_feedback").html("");        
    });
    
    $("#terrace_email").keyup(function() {
        $("#terracedialog_feedback").html("");        
    });
             
    $("#terracedialog").css("visibility","visible");
    $("#commentdialog").css("visibility","visible");
    $("#helpdialog").css("visibility","visible");
    $("#marketingdialog").css("visibility","visible");
    $("#competiondialog").css("visibility","visible");
                
    makeTimeSelection();
    updateNextAndPreviousVisibility();
    initializeMap();
    updateMarkers();
    updateWeather();
    updateExtraWeatherInfo();
    loadRandomTerraces();
    setTimeout("refreshContent()", 10*60*1000); 
            
    $("#weathertitle").click(function () {
        updateWeather();         
    });
    
    // IE6 PNG transparency fix      
        
    $('body').supersleight({shim: 'images/transparent.gif'});
    
    konami = new Konami()
    konami.load("http://www.hs.fi/kuvat/iso_webkuva/1135226215445.gif");
    
});
