document.observe('dom:loaded', function () { 
   
   bulletinList();
   start();
   clickDiv();
   gallery();
   weather();
   kuvagalleriatable();
   vertaa();
   yhteystiedotkartta();
   if (location.hash == "#otayhteytta") yhteydenottolomake();
   if (location.hash == "#contactform") yhteydenottolomake();
   if (location.hash == "#vertaa") openVertaa();
   if (location.hash == "#comparecottages") openVertaa();
});

function start() {
    if(!$$('div.bg')[0]) return false;
          
    if($('container').getHeight() < document.viewport.getDimensions().height) {
    
        $$('div.bg')[0].setStyle({height: document.viewport.getDimensions().height+'px'});
    
    }
    
}


function bulletinList() {

    if(!$('bulletinList')) return false;
     $('bulletinList').insert('<div id="pageNumbers"></div>');
     
     pageNum = 1;
     
    for (i = 0; i < $$('#bulletinList .bulletinPage').length; i++) {
    
        $$('#bulletinList .bulletinPage')[i].setStyle({display: 'none'});
        $$('#bulletinList .bulletinPage')[0].setStyle({display: 'block'});
        if (!$$('#bulletinList .bulletinPage')[i].empty()) { 
         $('pageNumbers').insert('<a href="javascript:bulletinPage('+pageNum+');" id="bulletinPageNum'+pageNum+'">'+pageNum+'</a> ');
        pageNum++;
        }
    }
    $("bulletinPageNum1").addClassName('active');
    
}


function bulletinPage(pageNum) {

    page = "page" + pageNum;
    bulletinPageNum = "bulletinPageNum" + pageNum;
    
    for (i = 0; i < $$('#bulletinList .bulletinPage').length; i++) {
    
        $$('#bulletinList .bulletinPage')[i].setStyle({display: 'none'});
    }
    
    for (i = 0; i < $$('#pageNumbers a').length; i++) {
        
        $$('#pageNumbers a')[i].removeClassName('active');
    
    }
    
    $(bulletinPageNum).addClassName('active');
    
    
    $(page).setStyle({display: 'block'});
    

}

function clickDiv() {
    if(!$$('.clickDiv')[0]) return false;
    
    if(!$$('.cms_editicon')[0]) {
        $$('.clickDiv').each(function (s, index) {
            
            if($$('.clickDiv a')[index]) {
             s.setAttribute('onclick', 'document.location.href="'+$$('.clickDiv  a')[index].href+'"');
             s.addClassName('onclick');
            }

        });
    }
}

function yhteydenottolomake() {

    if($('contactformDiv')) {
        sulje();
        return;
    }

    url = '/php/yhteydenottolomake.php';
    locationhash = 'otayhteytta';
    
    if($$('body.eng')[0]) {
        url = '/php/yhteydenottolomake_eng.php';
        locationhash = 'contactform';
       
    }
    if($$('body.rus')[0]) {
        url = '/php/yhteydenottolomake_rus.php';
        locationhash = 'contactform';
    }
    
    lomake = new Element('div',{'id':'contactformDiv'});
    contactoverlay = new Element('div',{'id':'contactoverlay'});
    
    new Ajax.Request (url, {
        onSuccess: function(transport) {
            scroll(0,100);
            lomake.update(transport.responseText);
            
            $('container').appendChild(contactoverlay);
            $('contactoverlay').setStyle({opacity: '0.3'});
            $('container').appendChild(lomake);

            $('contactformDiv').hide();
            $('contactformDiv').appear({duration: 0.5});
            $('sulje').observe('click', sulje);
            location.hash = locationhash;
         //   $('5hv').disabled =true;
          //  $('20hv').disabled =true;
          /*
            $('5hv').observe('click', function () {
                if(this.checked == true) {
                    $('5hv').disabled =false;
                    $('20hv').disabled =false;                    
                }
                else {
             $('5hv').disabled =true;
            $('20hv').disabled =true;        
                $('20hv').checked = false;         
                $('5hv').checked = false;         
                }
            });
            */
          /*  $('5hv').observe('click', function () {
                $('20hv').checked = false;
             });
            $('20hv').observe('click', function () {
                $('5hv').checked = false;
             });
            */

            
        }
    });
}

function sulje() {
        $('contactformDiv').fade({duration: 0.2, afterFinish: function() {
         $('contactformDiv').remove();
         $('contactoverlay').remove();
    }});   
    location.hash = "";
}

function gallery() {
    if(!$$('div.galleria')[0]) return false;
    

    $$('a.option').each(function(s) {
       
       s.observe('click', function() {

           galleryNumber(this.id);
          
       });
    });
    
    
    $$('.prev').each(function(s){
        s.observe('click', function() {
         if($$('a.active')[0] == $$('a.option')[0]) {
            num = $$('a.option').length -1;
            galleryNumber($$('a.option')[num].id);
            }
            else galleryNumber($$('a.active')[0].previous(0).id);
            
        });
    });
    $$('.next').each(function(s){
        s.observe('click', function() {
        num = $$('a.option').length -1;
         if($$('a.active')[0] == $$('a.option')[num]) {
            
            galleryNumber($$('a.option')[0].id);
            }
            else galleryNumber($$('a.active')[0].next(0).id);
            
        });
    });    
}

function galleryNumber(elementid) {
    
     $$('a.option').each(function(s) {
                 s.removeClassName('active');
              });
     $(elementid).addClassName('active');
}

function weather() {
    if(!$('saa')) return false;
    
    var url = '/php/weather.php';
    var weathertext = 'Sää mökillä';
    
    if($$('body.eng')[0]) {
        url = '/php/weather_eng.php';
        weathertext = 'Local weather';
    }
    if($$('body.rus')[0]) {
        url = '/php/weather_rus.php';
        weathertext = 'погода в<br />Сайма';
    }
    new Ajax.Request(url, {
        onSuccess: function(transport) {
            $('saa').update(transport.responseText);
            $('weathertext').update(weathertext);
        }
    });
  
}

function formsender(forme) {
    
    err = "";
    today = $('date').innerHTML;
    saapumisYear = $('saapumispaiva').value.split('.')[2];
    saapumisMonth = $('saapumispaiva').value.split('.')[1];
    saapumisDay = $('saapumispaiva').value.split('.')[0];
    saapumispaiva = new Date(Date.UTC(saapumisYear,saapumisMonth-1,saapumisDay,0,0,0,0));
    
    lahtoYear = $('lahtopaiva').value.split('.')[2];
    lahtoMonth = $('lahtopaiva').value.split('.')[1];
    lahtoDay = $('lahtopaiva').value.split('.')[0];
    lahtopaiva = new Date(Date.UTC(lahtoYear,lahtoMonth-1,lahtoDay,0,0,0,0));
   
    if(saapumispaiva < today) {
        err = err+ 'Saapumispäivämäärä on mennyt \n';
    }
    if(lahtopaiva < today) {
        err = err+ 'Lähtemispäivämäärä on mennyt \n';
    }
    if(lahtopaiva < saapumispaiva) {
        err = err+ 'Lähtemispäivämäärä tulee olla Saapumispäivän jälkeen \n';
    }
    
    $$('.required').each(function(s) {
        if(s.value == "") {
            err = err + s.name + ' on pakollinen kenttä \n';
        }
    });
 
    if(err == "") {
        $(forme).request({
        onComplete: function(transport) {
            //alert(transport.responseText);
            $(forme).hide();
            scroll(0,-10000);
            $('kiitos').appear();
        }
        });
        
    }
    else {
       alert(err); 
    }
   
}

function kuvagalleriatable() {
    if(!$('mokkitable')) return false;

    $$('#mokkitable a').each(function(s) {
          
        s.onmouseout = function() {
        
             $$('#mokkitable a').each(function(s) {
                 s.setStyle({opacity: '0.75'});
             });

       };
       s.onmouseover = function() {
       
              $$('#mokkitable a').each(function(s) {
                 s.setStyle({opacity: '0.75'});
             });
       
            this.setStyle({opacity: '1.0'});
       };
       
    });

    $('mokkitable').onmouseout = function() {
             $$('#mokkitable a').each(function(s) {
                 s.setStyle({opacity: '1.00'});
             });
            
    };
    
    
}

function vertaa() {
 //   if($('vertaaid')) {
 //       $('vertaaid').href = 'javascript:openVertaa()';
 //   }
  // if(!$$('a')[0]) return false;
    if($$('.cms_editicon')[0]) return false;
    
    if($$('body.eng')[0]) {
        $$('a').each(function(s) {
            if(s.href.endsWith('/comparecottages/') || s.href.endsWith('/comparecottages/index.html')) s.href = 'javascript:openVertaa()';
        });
    }
    else   if($$('body.rus')[0]) {
        $$('a').each(function(s) {
            if(s.href.endsWith('/comparecottages/') || s.href.endsWith('/comparecottages/index.html')) s.href = 'javascript:openVertaa()';
        });
    }
    else {
        $$('a').each(function(s) {
            if(s.href.endsWith('/vertaa/') || s.href.endsWith('/vertaa/index.html')) s.href = 'javascript:openVertaa()';
        });
    }

}

function openVertaa() {
    vertaa = new Element('div',{'id':'vertaaDiv'});
    vertaaoverlay = new Element('div',{'id':'vertaaoverlay','onclick':'vertaaSulje()'});
    vertaabottom = new Element('div',{'id':'vertaabottom'});
    vertaasulje = new Element('a',{'id':'vertaasulje', 'href':'javascript:vertaaSulje()'});
    vertaasulje2 = new Element('a',{'id':'vertaasulje2', 'href':'javascript:vertaaSulje()'});
    
    url = "/vertaa/index.html?ajax";
    locationhash = 'vertaa';
    if($$('body.eng')[0]) {
        url = '/en/comparecottages/index.html?ajax';
        locationhash = "comparecottages";
    }
    if($$('body.rus')[0]) {
        url = '/ru/comparecottages/index.html?ajax';
        locationhash = "comparecottages";
    }
    
     new Ajax.Request (url, {
        onSuccess: function(transport) {
        location.hash = locationhash;
        scroll(0,120);
        
        vertaa.update(transport.responseText);
        $('container').appendChild(vertaaoverlay);
        $('container').appendChild(vertaa);
        $('vertaaDiv').appendChild(vertaasulje);
        $('vertaaDiv').appendChild(vertaasulje2);
        $('vertaaDiv').appendChild(vertaabottom);
        
        $('vertaaoverlay').setStyle({opacity: '0.3'});
        $('vertaaDiv').appear({duration: 0.5});
        
        if($('content').getHeight() < $('vertaaDiv').getHeight()) {
            $('content').setStyle({height: $('vertaaDiv').getHeight()+'px'});
        }
        
    
    }});
    
    
    
}
function vertaaSulje() {
    $('vertaaDiv').fade({duration: 0.2, afterFinish: function() {
       
            $('content').removeAttribute('style');
        
         $('vertaaDiv').remove();
         $('vertaaoverlay').remove();
    }});   
    location.hash = "";
}

function yhteystiedotkartta() {
    $$('.karttamokit').each(function(s, index) {
        s.observe('mouseover', function() {
           $$('.mokkiloota')[index].setStyle({display: 'block'});
        });
        if(!$$('.adminmode')[0]) {
            s.observe('mouseout', function() {
            $$('.mokkiloota')[index].setStyle({display: 'none'});
            });
        }
    });
}
