﻿/* GLOBAL VARIABLES
--------------------- */
var slideshow_promos;
var slideshow_position = 0;


/* LoadSlideShow
--------------------------------------------------------------------------------- */
function LoadSlideShow(raw) {
    
    // get it started
    raw += '||';
    
    // add 'em
    raw += '0,,Argyll,,153 feet, 10 Passengers, 5 Staterooms,,0,,bve-ARGYLL-Running3.jpg,,Enjoying the blues...,,0,,http://www.charterindexonline.com/rys?m=EBrochure&yachtKey=3735&charterKey=1364&charterLocKey=1124&charterPerKey=&page=index.html||';
    raw += '0,,The Lady J,,105 feet, 8 Passengers, 4 Staterooms,,0,,bve-ladyJ5.jpg,,Sea for yourself...,,0,,http://www.charterindexonline.com/rys?m=EBrochure&yachtKey=4172&charterKey=1364&charterLocKey=1124&charterPerKey=&page=index.html||';
    // new yachts
    raw += '0,,Destination Fox Harb&#146;r Too,,161 feet, 11 Passengers, 5 Staterooms,,0,,bve-DFHT.jpg,,Cool Running...,,0,,http://www.charterindexonline.com/rys?m=EBrochure&yachtKey=4273&charterKey=1364&charterLocKey=1124&charterPerKey=&page=index.html||';
    // mystique
    raw += '0,,Mystique ,,166 feet, 10 Passengers, 5 Staterooms,,0,,bve-mystique-final.jpg,,Still Relaxing...,,0,,http://www.charterindexonline.com/rys?m=EBrochure&yachtKey=4423&charterKey=1364&charterLocKey=1124&charterPerKey=&page=index.html';
    
    
    
    
    // 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() {
    
     // if mypath is blank or null, then make it empty (b/c this method is re-used on contact.aspx, which doesn't rewrite url)
    if (typeof mypath == 'undefined')
        mypath = '';
        
     // 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(mypath + 'ajax/custom-public-ajax-processor.aspx', {
            parameters: { 
                method: 'InquireAboutVilla', 
                xml:xml, 
                email : $('inquireEmail').value,
                link : location.href  },
            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, please try again:\r\n\r\n" + transport.responseText);  
                
                // loading
                $('btnSubmit').update('Submit Inquiry');
                $('btnSubmit').disabled = false;          
                
                // try again
                //InquireAboutVilla();
            }
        });  	
    }
}

/* 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(mypath + 'ajax/custom-public-ajax-processor.aspx', {
            parameters: { 
                method: 'Send2Friend', 
                xml:xml, 
                senderemail : $('sendEmail').value, 
                friendemail : $('sendFriendsEmail').value, 
                link : location.href                 
            },
            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, please try again:\r\n\r\n" + transport.responseText);  
                
                // loading
                $('btnSend2Friend').update('Send');
                $('btnSend2Friend').disabled = false;          
                
                // try again
                //Send2Friend(id_villa);
            }
        });  	
    }
}



/* 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 
    // 4- main photo
    // 5- fancy 
    // 6- is_beachwater
    // 7 - island // NEW - only for villas
    
    // location
    var villalocation = this_villa[2];
    
    // build subtitle
    var subtitle;
    if (this_villa[0] != '0')
    {
        // island
        var island = this_villa[7];
        
        // rewrite
        if (island == 'SXMDWI') island = 'St. Maarten DWI';
        else if (island == 'SXMFWI') island = 'St. Martin FWI';
        else if (island == 'SBFWI') island = 'St. Barts FWI';
        else if (island == 'ABWI') island = 'Anguilla BWI';
        
        // change manually (for non-regions) - villas only!
        if (villalocation.indexOf('No Preference') != -1)
            villalocation = island;
         else
            villalocation += ', ' + island; 
        
        // 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!
    {
        // extra url info
        url = this_villa[7];
        
        // open in new window
        $('hyp-promo-title').writeAttribute('target', '_blank');
        $('hyp-promo-image').writeAttribute('target', '_blank');
        
    }
    else {
        // NEW: villa-rentals.aspx/St-Barts/86/Aurève
        // OLD: url = 'villa.aspx?id_villa='+this_villa[0];
        
        // get justname for island first (but last 4 characters and replace spaces with hyphen)
        var justname = island.substring(0, island.length - 4).replace('.', '').replace(' ', '-');
        url = 'villa-rentals.aspx/'+justname+'/'+this_villa[0]+'/'+this_villa[1].replace(' ', '-');
    }
    
    // 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
    $('selSearchIsland').value = '0';
    $('selSearchLocation').value = '0';
    $('selSearchBedrooms').value = '0';
    $('selSearchBeachWater').value = '0';
    
    // submit
    $('frmSearchBox').submit();

}


/* GetLocations
--------------------------- */
function GetLocations(island)
{
    
    // if mypath is blank or null, then make it empty (b/c this method is re-used on contact.aspx, which doesn't rewrite url)
    if (typeof mypath == 'undefined')
        mypath = '';
    
    // make the ajax call
    new Ajax.Request(mypath + 'ajax/custom-public-ajax-processor.aspx', {
        parameters: { method: 'GetLocations', island:island, mode:'front'},
        onSuccess: function(transport) {

            
            // grab what we need
            var res = transport.responseText;
            
            // replace -- Choose a Location --
            res = res.replace(/-- Choose a Location --/gi, 'No Preference');
	        
	        // returned XHTML, full select box
	        $('selSearchLocation').update(res); 
	        
	        // effect
	        new Effect.Highlight('selSearchLocation');
        },
        onFailure: function(transport) {
            alert("There was an error:" + transport.responseText);
        }
    });  	

}

/* ActivateFaceboxes
--------------------------- */
function ActivateFaceboxes()
{
    // run thru and remove
    $$('a.viewdetails, a.send2friend, a#top-newsletter').each(function(s){
        // remove onclick handler
        s.onclick = null;
    });
}