var page = 'home';
var page_before_gal = '';
var html5_capable = true;

function validateData(name, phone, mail, date_arrival, date_departure, couples, children, infants, male_single_adults, female_single_adults, message){
    var mail_reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
    phone = phone.replace(/\s/g, "");
    
    if(!name)
        return 'invalid name';
    
    if(!phone || isNaN(phone))
        return 'invalid phone number';
        
    if(!mail_reg.test(mail))
        return 'invalid email';
    
    if(!date_arrival)
        return 'invalid arrival date';
        
    if(!date_departure)
        return 'invalid departure date';
    
    if(!couples)
        return 'invalid couples';

    if(!children)
        return 'invalid children';
    
    if(!infants)
        return 'invalid infants';
    
    if(!male_single_adults)
        return 'invalid male single adults';
    
    if(!female_single_adults)
        return 'invalid female single adults';
    
    if(!message)
        return 'invalid message';

    return true;
}

function sendEnquiry(){
    var name = $('#enq_name').val();
    var phone = $('#enq_phone').val();
    var mail = $('#enq_mail').val();
    var date_arrival = $('#enq_arrival_date').val();
    var date_departure = $('#enq_departure_date').val();
    var couples = $('#enq_couples option:selected').text();
    var children = $('#enq_children option:selected').text();
    var infants = $('#enq_infants option:selected').text();
    var male_single_adults = $('#enq_male_single_adults option:selected').text();
    var female_single_adults = $('#enq_female_single_adults option:selected').text();
    var message = $('#enq_message').val();
    var copy = $('#enq_copy').is(':checked');
    var dataIsValid = validateData(name, phone, mail, date_arrival, date_departure, couples, children, infants, male_single_adults, female_single_adults, message);
    if(dataIsValid != true){
        $('#enq_status').html(dataIsValid);
    }else{
        $('#enq_status').html('sending...');
        var request = new XMLHttpRequest();  
        var request_url = 'mailer.php?name='+name+'&phone='+phone+'&mail='+mail+'&date_arrival='+date_arrival+'&date_departure='+date_departure+'&couples='+couples+
        '&children='+children+'&infants='+infants+'&male_single_adults='+male_single_adults+'&female_single_adults='+female_single_adults+'&copy='+copy+'&message='+message;
        request.open('GET', request_url);
        request.onreadystatechange = function (aEvt) {  
        if (request.readyState == 4) {  
            if(request.status == 200) {    
                var obj = eval("(" + request.responseText + ")");
                if(obj.status=="success"){
                    $('#enq_status').html(obj.msg);
                }
                else if(obj.status=="error"){
                    $('#enq_status').html(obj.msg);
                }
             }else {
               $('#enq_status').html('technical error 1.');
             }
          }  
        };  
        request.send(null);
    }
}

function createFlashVideo(){
    // The video to load.
    var videoID = "Y7T6-YkCMWE"
    // Lets Flash from another domain call JavaScript
    var params = { allowScriptAccess: "always" , allowFullScreen: "true" };
    // The element id of the Flash embed
    var atts = { id: "ytPlayer" };
    // All of the magic handled by SWFObject (http://code.google.com/p/swfobject/)
    //swfobject.embedSWF("http://www.youtube.com/v/" + videoID + "?version=3&enablejsapi=1&playerapiid=player1", 
    //                   "videoDiv", "590", "360", "9", null, null, params, atts);
                       
    //$('#video_holder').css('width','590px');
}

function openPage(new_page){
    if(new_page == page)
        return;
        
    document.title = $('#title_'+new_page).html();
    removeEventListeners();
    
    if(new_page!='home')
        $('#close_bt_home').show();
    else
        $('#close_bt_home').hide();
            
    
           
    //if(page == 'vide' && html5_capable)
    //    document.getElementById('videoPlayer').pause();
    //else 
    
    if(page == 'vide'){
        $('#playerDiv').empty();
    }
    
    //if(new_page == 'vide')
       //document.getElementById('videoPlayer').playVideo();    
       
        
    if(page == 'gall'){
        $('#bt_gall').removeClass('menu_bt_in_page');
        $('#bt_home').addClass('menu_bt_in_page');
        $('#social_container').fadeIn(1000);
    }
        
    if(new_page=='gall'){
        galFormat();
        $('#social_container').fadeOut(1000);
        $('#page_'+page).fadeOut(1000);
        $('#page_'+new_page).fadeIn(1000, function(){
            page_before_gal = page;
            page = new_page;
            galInit();
            createEventListeners();    
        });
    }else{                
        $('#page_'+page).fadeOut(1000, function(){
            $('#page_'+new_page).fadeIn(1000, function(){
                setFooterPosition();
                setBgSize();
                if(new_page == 'vide')
                    $('#playerDiv').html('<iframe title="YouTube video player" width="640" height="360" src="http://www.youtube.com/embed/Y7T6-YkCMWE?version=3&enablejsapi=1&playerapiid=player1" scrolling="no" frameborder="0" allowfullscreen></iframe> ');  
                else if(new_page == 'loca')
                    initialize();
                
                // page opened                             
                page = new_page;
                createEventListeners();
            });  
        });
        
        
        $('#bg_'+page).fadeOut(1000, function(){
            ;
        });
        $('#bg_'+page_before_gal).fadeOut(1000, function(){
            ;
        });              
        $('#bg_'+new_page).fadeIn(1000, function(){
            ;
        });
    }                      
    $('#bt_'+page).removeClass('menu_bt_in_page');
    $('#bt_'+new_page).addClass('menu_bt_in_page');
}
function setBgSize(){
    var imgs = $('#backgrounds').children();
    var window_width = $(window).width();      
    var window_height = $(window).width();      
    if(window_height <= window_width){
        $(imgs).each(function(){
            $(this).attr('width',window_width+'px');
        });
    }else{                                   
        $(imgs).each(function(){
            $(this).attr('height',window_height+'px');
        });   
    }
}
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}
function createEventListeners(){
    $('body').delegate('#bt_home', 'click', function(){ openPage('home'); });
    $('body').delegate('#bt_desc', 'click', function(){ openPage('desc'); });
    $('body').delegate('#bt_pric', 'click', function(){ openPage('pric'); });
    $('body').delegate('#bt_avai', 'click', function(){ openPage('avai'); });
    $('body').delegate('#bt_loca', 'click', function(){ openPage('loca'); });
    $('body').delegate('#bt_gall', 'click', function(){ openPage('gall'); });
    $('body').delegate('#bt_vide', 'click', function(){ openPage('vide'); });
    $('body').delegate('#bt_cont', 'click', function(){ openPage('cont'); });
    $('body').delegate('#bt_enqu', 'click', function(){ openPage('enqu'); });
    $('body').delegate('#close_bt_home', 'click', function(){ openPage('home'); });
}
function removeEventListeners(){
    $('body').undelegate('#bt_home','click');
    $('body').undelegate('#bt_desc','click');
    $('body').undelegate('#bt_pric','click');
    $('body').undelegate('#bt_avai','click');
    $('body').undelegate('#bt_loca','click');
    $('body').undelegate('#bt_gall','click');
    $('body').undelegate('#bt_vide','click');
    $('body').undelegate('#bt_cont','click');
    $('body').undelegate('#bt_enqu','click');
    $('body').undelegate('#close_bt_home','click');
}
function setFooterPosition(){
    var window_height = $(window).height();
    if(page=='home')
        var margin_top_small = $('#topbar').height() + 400 + 100;
    else
        var margin_top_small = $('#topbar').height() + $('#site').height() + 65;
    var margin_top_big = $(window).height() - 60;
    
    if(margin_top_small > window_height)
        $('#footer').css('margin-top',margin_top_small);
    else
        $('#footer').css('margin-top',margin_top_big);
}
$(window).bind('resize', function(){
    setBgSize();
    setFooterPosition();
});

//test html5 capacity
var test_audio= document.createElement("audio") //try and create sample audio element
var test_video= document.createElement("video") //try and create sample video element
var mediasupport={audio: (test_audio.play)? true : false, video: (test_video.play)? true : false}
if(!mediasupport.video)
    html5_capable = false;
