﻿/* GLOBAL VARIABLES
--------------------- */
var slideshow_promos;
var slideshow_position = 0;


/* LoadSlideShow
--------------------------------------------------------------------------------- */
function LoadSlideShow(raw) {
    
    // data format: 
    // 20,,Villa in St. Martin,,All Villas Anguilla,,1,,Winter Leaves123.jpg,,I got no Beach and im Small,,0||
    // 21,,4 Beachfront Villa in Anguilla,,All Villas FWI,,4,,1600123.JPG,,Sex on the Beach Anyone?,,1||
    // 22,,Tett,,All Villas DWI,,4,,Strawberry_Feelings-1600x1200123.jpg,,One is not like the other,,0
    // get it started
    raw += '||';
    
    // add 'em
    raw += '0,,Sovereign,,120 ft, 12 Passengers, 6 Staterooms,,0,,bve-Sovereign-docked2.jpg,,Still Relaxing...,,0||';
    raw += '0,,Argyll,,153 feet, 10 Passengers, 5 Staterooms,,0,,bve-ARGYLL-Running3.jpg,,Enjoying the blues...,,0||';
    raw += '0,,The Lady J,,105 feet, 8 Passengers, 4 Staterooms,,0,,bve-ladyJ4.jpg,,Sea for yourself...,,0';
    
    // split into villas
    slideshow_promos = raw.split("||");
    
    // randomize
    slideshow_position=Math.floor(Math.random()*slideshow_promos.length);
    
    // preload images (TODO: is this implemented correctly?)
    for(var i=0;i<slideshow_promos.length;i++)
    {
        var Pre = new Image();
        Pre.src = 'listings/photos/'+ slideshow_promos[i].split(',,')[4];
    }
    
    // assume:
    // -------------- 
    // 0- villa ID
    // 1- title
    // 2- location
    // 3- bedrooms
    // 4- main photo
    // 5- fancy 
    // 6- is_beachwater
    
    // set timeout function
    SlideshowFunction();
    
}

/* SlideshowFunction
--------------------------------------------------------------------------------- */
function SlideshowFunction()
{
    // slide
    Slide(1);
    
    // timeout
    setTimeout("SlideshowFunction()", 5000);
}


/* VerifyLogin
--------------------------------------------------------------------------------- */
function VerifyLogin() {

    // grab form
    var xml = GrabForm('formLogin');

    // TODO: later add code for grabbing ReturnURL...

    // validate
    var valForm = new Validation('formLogin', { useTitles: true });
    if (valForm.validate()) {
        // loading
        // loading();

        // make the ajax call
        new Ajax.Request('ajax/public-ajax-processor.aspx', {
            parameters: { xml: xml, action: 'verifylogin' },
            onSuccess: function(transport) {

                // redirect
                location.href = 'back/' + transport.responseText;

            },
            onFailure: function(transport) {
                alert("There was an error logging you in, please try again!");
                $('tarDebug').update(transport.responseText);
            }
        });
        
        
        
    } // end if validated
}



/* ForgotPassword
--------------------------------------------------------------------------------- */
function ForgotPassword() {
    // validate
    var valForm = new Validation('form01', { useTitles: true });
    if (valForm.validate()) {
        // loading
        // loading();

        // grab email
        var email = $('tbxForgotEmail').value;

        // make the ajax call
        new Ajax.Request('ajax/public-ajax-processor.aspx', {
            parameters: { email: email, action: 'forgotpassword' },
            onSuccess: function(transport) {

                // send to google checkout... or confirmation page
                $('divConfirmation').show();
                $('form01').hide();

            },
            onFailure: function(transport) {
                alert("No account was found with that email, please try again!");
                $('tarDebug').update(transport.responseText);
            }
        });
    }
}
 

/* InquireAboutVilla
--------------------------------------------------------------------------------- */
function InquireAboutVilla() {
    // validate
    var valForm = new Validation('form01', { useTitles: true });
    if (valForm.validate()) {
        
        // loading
        $('btnSubmit').update('Please wait...');
        $('btnSubmit').disabled = true;

        // grab form
        var xml = GrabForm('form01');

        // make the ajax call
	    new Ajax.Request('ajax/custom-public-ajax-processor.aspx', {
            parameters: { method: 'InquireAboutVilla', xml:xml, email : $('inquireEmail').value },
            onSuccess: function(transport) {

                 // show success message / hide form
                 $('formContact').hide();
                 $('inquireMessage').show();
                 
                 // clear all fields
                 $$('textarea', 'input').invoke('clear');
                 $$('input.chx').each(function(s){ s.checked = false; });
                 
                 // settimeout close modal
                 // here, only fade message out if not on main contact.aspx page
                 if (location.href.toLowerCase().indexOf('contact.aspx') == -1) {
                    new Effect.Fade('inquireMessage', {duration:5});                 
                    setTimeout("HideFacebox();", 4000);
                 }   
                 
                 // loading
                $('btnSubmit').update('Submit Inquiry');
                $('btnSubmit').disabled = false; 
            },
            onFailure: function(transport) {
                alert("We had some trouble sending your message:\r\n\r\n" + transport.responseText);  
                
                // loading
                $('btnSubmit').update('Submit Inquiry');
                $('btnSubmit').disabled = false;          
            }
        });  	
    }
}

/* Send2Friend
--------------------------------------------------------------------------------- */
function Send2Friend(id_villa) {

    // validate
    var valForm = new Validation('form01', { useTitles: true });
    if (valForm.validate()) {
        
        // loading
        $('btnSend2Friend').update('Please wait...');
        $('btnSend2Friend').disabled = true;

        // grab form
        var xml = GrabForm('form01');

        // make the ajax call
	    new Ajax.Request('ajax/custom-public-ajax-processor.aspx', {
            parameters: { 
                method: 'Send2Friend', 
                xml:xml, 
                senderemail : $('sendEmail').value, 
                friendemail : $('sendFriendsEmail').value, 
                id_villa:id_villa 
            },
            onSuccess: function(transport) {

                 // show success message / hide form
                 $('formSend2Friend').hide();
                 $('sendMessage').show();
                 
                 // clear and reset
                 $$('textarea', 'input').invoke('clear');
                 $('btnSend2Friend').update('Send');
                 $('btnSend2Friend').disabled = false;  
                 
                 // settimeout close modal
                 new Effect.Fade('sendMessage', {duration:5});
                 setTimeout("HideFacebox();", 4000);
            },
            onFailure: function(transport) {
                alert("We had some trouble sending your message:\r\n\r\n" + transport.responseText);  
                
                // loading
                $('btnSend2Friend').update('Send');
                $('btnSend2Friend').disabled = false;          
            }
        });  	
    }
}



/* Slide
--------------------------------------------------------------------------------- */
function Slide(increment) {
     
      
    // debugger;
    // left = -1
    // right = 1

    // alert('START slideshow_position: ' + slideshow_position);
    
    // move current position
    slideshow_position += increment;
    
    // are we at an end point
    if (slideshow_position > (slideshow_promos.length - 1))
    {
        // flip back to beginning
        slideshow_position = 0;
    }
    else if (slideshow_position == -1)
    {
        // flip to end
        slideshow_position = slideshow_promos.length - 1;
    }
    
    // hide everything
    $('promo-fancy').hide();
    $('hyp-promo-image').hide();
    $('hyp-promo-title').hide();
    $('promo-subtitle').hide();
    
    // get values
    var this_villa = slideshow_promos[slideshow_position].split(',,');
    
    // assume:
    // -------------- 
    // 0- villa ID
    // 1- title
    // 2- location
    // 3- bedrooms // NOT USING THIS ANYMORE
    // 4- main photo
    // 5- fancy 
    // 6- is_beachwater
    
    // location
    var villalocation = this_villa[2];
    
    // build subtitle
    var subtitle;
    if (this_villa[0] != '0')
    {
        // change manually (for non-regions) - villas only!
        if (villalocation == 'All Villas FWI') villalocation = 'St. Martin FWI';
        else if (villalocation == 'All Villas DWI') villalocation = 'St. Maarten DWI';
        else if (villalocation == 'All Villas Anguilla') villalocation = 'Anguilla BWI';
        else villalocation += ', St. Martin FWI';    
        
        // villa subtitle
        var bed = (this_villa[3] == '1') ? ' bedroom' : ' bedrooms';
        // var beachwater = (this_villa[6] == '1') ? ', Beachfront/Waterfront' : '';
        subtitle = villalocation + ', ' + this_villa[3] + bed; // + beachwater;
    }
    else
        subtitle = villalocation;
    
    // build url
    var url;
    if (this_villa[0] == '0') // it's a yacht!
        url = 'yachts.aspx';
    else
        url = 'villa.aspx?id_villa='+this_villa[0];
    
    // load values    
    $('hyp-promo-title').writeAttribute('href', url);
    $('hyp-promo-image').writeAttribute('href', url);
    $('hyp-promo-title').update(this_villa[1]);    
    $('promo-subtitle').update(subtitle);
    var imagepath = 'url(listings/photos/' + urlencode(this_villa[4]) + ')';
    $('promo-image').setStyle({ backgroundImage : imagepath });
    $('promo-fancy').update(this_villa[5]); 
    
    // re-cufon
    Cufon.replace('#promo-fancy');
    Cufon.now();    
    
    // show everything
    $('promo-fancy').show(); // IE bug on appear, so just show it...
    $('hyp-promo-image').show();
    new Effect.Parallel([
//        new Effect.Appear('hyp-promo-image') ,
        new Effect.Appear('hyp-promo-title'),
        new Effect.Appear('promo-subtitle')                 
        ], {duration: 2});     
}

 
/* SearchAll
--------------------------------------------------------------------------------- */
function SearchAll() {
    // clear all parameters
    $('selSearchLocation').value = '0';
    $('selSearchBedrooms').value = '0';
    $('selSearchBeachWater').value = '0';
    
    // submit
    $('frmSearchBox').submit();

}
