//////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////jQUERY on load////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////// 
$(document).ready(function() {
    buffer = []; //global
    // CSS3
    $('.main P:has(CODE)').css({ clear:'both', display:'table', width:'100%', marginBottom:0 });
    $('#index .main DIV:last').css({ borderRight:'none', paddingRight:0 });    
    // --
    
    // Image enlargement
    $("A.gal").live('click', function(){
        $(this).lightBox().trigger('click');
        return false;        
    });
    // --
    
    // CODE REPLACEMENT
    $('CODE').each(function() {
        switch ($(this).text()) {
            case '[google map]': {
                var html = '<h4>Map</h4><div id="gmap" style="border: 1px solid rgb(255, 255, 255); background-color: rgb(255, 252, 233); width:100%; height:325px;">&nbsp;</div>';
                $(this).text('').prepend(html);
                //GoogleMap API
                //http://maps.google.com/maps?f=q&source=s_q&hl=en&geocode=&q=North+Petherton,+South+of+Bridgwater,+Somerset+TA6+6QA&sll=37.0625,-95.677068&sspn=46.092115,79.013672&ie=UTF8&hq=&hnear=Bridgwater,+Somerset+TA6+6QA,+United+Kingdom&ll=51.179343,-2.8125&spn=173.863131,272.109375&t=h&z=1
                //http://maps.google.com/maps/place?ftid=0x487209ae1bc8086f:0x22914f1a9011f9a4&q=North+Petherton,+South+of+Bridgwater,+Somerset+TA6+6QA&hl=en&ei=GSCOTLrfJ9SQjAex7-2xDQ&dtab=0&sll=51.0924,-3.012613&sspn=0.006295,0.006295&ie=UTF8&ll=51.273515,-3.191528&spn=0,0&t=h&z=11&iwloc=A
                var map = new GMap2(document.getElementById("gmap"));
                var point = new GLatLng(51.0924,-3.012613); //ll
                map.setCenter(point, 12); // This method MUST BE CALLED FIRST!!!
                map.addControl(new GLargeMapControl());
                var marker = new GMarker(point);
                map.addOverlay(marker);
                GEvent.addListener(marker, "click", function() {
                    marker.openInfoWindowHtml("North Petherton,<br /> Somerset,<br />TA6 6QA, UK");
                });
                //--
            } break;
            case '[home image 1]': {
                var html = '<img src="images/code/home_image_1.jpg" alt="home_image_1.jpg" title="Welcome" />';
                $(this).text('').prepend(html);
            } break;
            case '[home image 2]': {
                var html = '<img src="images/code/home_image_2.jpg" alt="home_image_2.jpg" title="BUSINESS" />';
                $(this).text('').prepend(html);
            } break;
            case '[home image 3]': {
                var html = '<img src="images/code/home_image_3.jpg" alt="home_image_3.jpg" title="PLEASURE" />';
                $(this).text('').prepend(html);
            } break;
            
            
            case '[accommodation image 1-1]': {
                var img    = 'walnut-tree_david-griffen-photography_1401-3363.jpg';
                var img_sm = 'walnut-tree_david-griffen-photography_1401-3363_sm.jpg';
                var title  = 'Master Suites';
                var html = '<a class="gal left" rel="lightbox[MasterSuites]" href="images/code/'+img+'" title="'+title+'"><img src="images/code/'+img_sm+'" alt="'+img_sm+'" title="Click to enlarge" /></a>';
                $(this).html(html);
            } break;
            case '[accommodation image 1-2]': {
                var img    = 'walnut-tree_david-griffen-photography_1401-3413.jpg';
                var img_sm = 'walnut-tree_david-griffen-photography_1401-3413_sm.jpg';
                var title  = 'Master Suites';
                var html = '<a class="gal left" rel="lightbox[MasterSuites]" href="images/code/'+img+'" title="'+title+'"><img src="images/code/'+img_sm+'" alt="'+img_sm+'" title="Click to enlarge" /></a>';
                $(this).html(html);
            } break;
            case '[accommodation image 2-1]': {
                var img    = 'walnut-tree_david-griffen-photography_1401-3512.jpg';
                var img_sm = 'walnut-tree_david-griffen-photography_1401-3512_sm.jpg';
                var title  = 'Junior Suites';
                var html = '<a class="gal left" href="images/code/'+img+'" title="'+title+'"><img src="images/code/'+img_sm+'" alt="'+img_sm+'" title="Click to enlarge" /></a>';
                $(this).html(html);
            } break;
            case '[accommodation image 3-1]': {
                var img    = 'walnut-tree_david-griffen-photography_1401-3360.jpg';
                var img_sm = 'walnut-tree_david-griffen-photography_1401-3360_sm.jpg';
                var title  = 'Executive Rooms';
                var html = '<a class="gal left" rel="lightbox[ExecutiveRooms]"  href="images/code/'+img+'" title="'+title+'"><img src="images/code/'+img_sm+'" alt="'+img_sm+'" title="Click to enlarge" /></a>';
                $(this).html(html);
            } break;
            case '[accommodation image 3-2]': {
                var img    = 'walnut-tree_david-griffen-photography_1401-3470.jpg';
                var img_sm = 'walnut-tree_david-griffen-photography_1401-3470_sm.jpg';
                var title  = 'Executive Rooms';
                var html = '<a class="gal left" rel="lightbox[ExecutiveRooms]"  href="images/code/'+img+'" title="'+title+'"><img src="images/code/'+img_sm+'" alt="'+img_sm+'" title="Click to enlarge" /></a>';
                $(this).html(html);
            } break;
            case '[accommodation image 4-1]': {
                var img    = 'walnut-tree_david-griffen-photography_1401-3485.jpg';
                var img_sm = 'walnut-tree_david-griffen-photography_1401-3485_sm.jpg';
                var title  = 'Standard Rooms';
                var html = '<a class="gal left" href="images/code/'+img+'" title="'+title+'"><img src="images/code/'+img_sm+'" alt="'+img_sm+'" title="Click to enlarge" /></a>';
                $(this).html(html);
            } break;
            
            case '[conference_rooms image 1]': {
                var img    = 'walnut-tree_david-griffen-photography_1401-3570.jpg';
                var img_sm = 'walnut-tree_david-griffen-photography_1401-3570_sm.jpg';
                var title  = 'Chaucer Room';
                var html = '<a class="gal" href="images/code/'+img+'" title="'+title+'"><img src="images/code/'+img_sm+'" alt="'+img_sm+'" title="Click to enlarge" /></a>';
                $(this).html(html);
            } break;
            case '[conference_rooms image 2]': {
                var img    = 'walnut-tree_david-griffen-photography_1401-3561.jpg';
                var img_sm = 'walnut-tree_david-griffen-photography_1401-3561_sm.jpg';
                var title  = 'Avon Room';
                var html = '<a onClick="return false;" class="gal" href="images/code/'+img+'" title="'+title+'"><img src="images/code/'+img_sm+'" alt="'+img_sm+'" title="Click to enlarge" /></a>';
                $(this).html(html);
            } break;
            case '[conference_rooms image 3]': {
                var img    = 'walnut-tree_david-griffen-photography_1401-3567.jpg';
                var img_sm = 'walnut-tree_david-griffen-photography_1401-3567_sm.jpg';
                var title  = 'Somerset Room';
                var html = '<a class="gal" href="images/code/'+img+'" title="'+title+'"><img src="images/code/'+img_sm+'" alt="'+img_sm+'" title="Click to enlarge" /></a>';
                $(this).html(html);
            } break;
            case '[conference_rooms image 4]': {
                var img    = 'walnut-tree_david-griffen-photography_1401-3529.jpg';
                var img_sm = 'walnut-tree_david-griffen-photography_1401-3529_sm.jpg';
                var title  = 'Polden Room';
                var html = '<a class="gal" href="images/code/'+img+'" title="'+title+'"><img src="images/code/'+img_sm+'" alt="'+img_sm+'" title="Click to enlarge" /></a>';
                $(this).html(html);
            } break;
            case '[conference_rooms image 5]': {
                var img    = 'walnut-tree_david-griffen-photography_1401-3576.jpg';
                var img_sm = 'walnut-tree_david-griffen-photography_1401-3576_sm.jpg';
                var title  = 'Cedar Room';
                var html = '<a class="gal" href="images/code/'+img+'" title="'+title+'"><img src="images/code/'+img_sm+'" alt="'+img_sm+'" title="Click to enlarge" /></a>';
                $(this).html(html);
            } break;
            case '[conference_rooms image 6]': {
                var img    = 'walnut-tree_david-griffen-photography_1401-3545.jpg';
                var img_sm = 'walnut-tree_david-griffen-photography_1401-3545_sm.jpg';
                var title  = 'Mendip Room';
                var html = '<a class="gal" href="images/code/'+img+'" title="'+title+'"><img src="images/code/'+img_sm+'" alt="'+img_sm+'" title="Click to enlarge" /></a>';
                $(this).html(html);
            } break;
            
            case '[bar_and_restaurant image 1]': {
                var html = '<img src="images/code/bar-and-restaurant_restaurant.jpg" alt="bar-and-restaurant_restaurant.jpg" title="The Lemon Tree Restaurant" />';
                $(this).text('').prepend(html);
            } break;
            case '[bar_and_restaurant image 2]': {
                var html = '<img src="images/code/bar-and-restaurant_bistro.jpg" alt="bar-and-restaurant_bistro.jpg" title="The Bistro" />';
                $(this).text('').prepend(html);
            } break;
            case '[bar_and_restaurant image 3]': {
                var html = '<img src="images/code/bar-and-restaurant_bar.jpg" alt="bar-and-restaurant_bar.jpg" title="The Bar" />';
                $(this).text('').prepend(html);
            } break;
            case '[bar_and_restaurant image 5]': {
                var html = '<img src="/upl_images/pg_e_9.jpg" alt="bar-and-restaurant_bar.jpg" title="The Bar" />';
                $(this).text('').prepend(html);
            } break;
            default: {
                //--0x0000
            }            
        }    
    });
    
    //BLOCKS functionallity
    $('DD.link A', this).hide();
    $("#content .sub .block").hover(
        function() {
            var alink = $('DD.link A', this); 
            $(this).addClass('hover').attr('title', alink.attr('title'));
            $(this).css({ cursor:'pointer' });
            $(this).click(function() { 
                document.location.href = alink.attr('href'); 
            });
        },
        function() { $(this).removeClass('hover'); } 
    );

    //Newsletter AJAX form
    // prepare the form when the DOM is ready 
    //pre-submit Ajax form settings
    var options = { 
        target:     '#Newsletter',
        url:        'ar',
        type:       'post',
        dataType:   'json',
        data:       { action:"newsletter" },
        // $.ajax options can be used here too
        beforeSubmit: function(formData, jqForm, options) {
        //pre-submit callback
            form = $("FORM", this.target);
            //checking 'yourname' field
            if ($('INPUT[name=yourname]', form).val() == '') {
                alert('Please, Enter your name.');
                $('INPUT[name=yourname]', form).focus();
                return false;
            }
            //checking 'email' field
            var filter = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;
            if (!filter.test($('INPUT[name=youremail]', form).val())) {
                alert('Please, Enter the valid e-mail address.');
                $('INPUT[name=youremail]', form).focus();
                return false;
            }
            $("#Newsletter .submit").addClass('af_load');
            //var queryString = $.param(formData); 
            //alert('About to submit: \n\n' + queryString); 
            return true; 
        },
        success: function(responseText, statusText, xhr, $form) { 
        //post-submit callback
            var tpl = '<b>{text}</b><p>{post}</p><a href="#" id="new_newsletter" title="Subscribe another email"><i>Anew</i></a>';
            var res = tpl.supplant(responseText);
            $('#Newsletter DD').hide().html(res).fadeIn('slow');
            //$('#Newsletter').html(responseText).fadeIn('slow');
            $("#Newsletter .submit").removeClass('af_load');            
            //alert('status: ' + statusText + '\n\nresponseText: \n' + responseText + '\n\nThe output div should have already been updated with the responseText.'); 
        }
    }; 
    $('#Newsletter FORM[name=newsletter] INPUT[type=submit]').live('click', function() { 
        $('#Newsletter FORM[name=newsletter]').ajaxSubmit(options); 
        return false; 
    });
    var newsletter = $("#Newsletter FORM").clone();
    $('#new_newsletter').live('click', function() {
        $("#Newsletter DD").hide().html(newsletter).fadeIn('slow');
        return false;        
    });
    // -- newsletter
    
    
    // Conatct AJAX form
    var contact_options = { 
        target:     '#contact_form',
        url:        'ar',
        type:       'post',
        dataType:   'json',
        data:       { action:"contact" },
        beforeSubmit: function(formData, jqForm, options) {
            form = $(this.target);
            if ($('INPUT[name=name]', form).val() == '') {
                alert('Please, Enter your name.');
                $('INPUT[name=name]', form).focus();
                return false;
            }
            var filter = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;
            if (!filter.test($('INPUT[name=email_address]', form).val())) {
                alert('Please, Enter the valid e-mail address.');
                $('INPUT[name=email_address]', form).focus();
                return false;
            }
            return true; 
        },
        success: function(responseText, statusText, xhr, $form) { 
            var tpl = '<b>{text}</b><p>{post}</p><a href="#" id="new_form" title="Use the form again"><i>Click here to use the form again</i></a>';
            var res = tpl.supplant(responseText);
            $('#contact .main .form').hide().html(res).fadeIn('slow');
        }
    }; 
    $('#contact_form INPUT[type=submit]').live('click', function() { 
        $('#contact_form').ajaxSubmit(contact_options); 
        return false; 
    });
    var new_form = $("#contact_form").clone();
    $('#new_form').live('click', function() {
        $('#contact .main .form').hide().html(new_form).fadeIn('slow');
        return false;        
    });
    // -- contact-form
    
}); // -- onload

/////////////////////////////////////////////////////////////////////////////////////
/////////////////////////// Additional functions ////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////
/**
*  Javascript trim, ltrim, rtrim
*  http://www.webtoolkit.info/
**/

function trim(str, chars) {
    return ltrim(rtrim(str, chars), chars);
}
 
function ltrim(str, chars) {
    chars = chars || "\\s";
    return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}
 
function rtrim(str, chars) {
    chars = chars || "\\s";
    return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}
