/**
 * Scripts to be merged later. Adding 'cd' to all functions in case of function name conflicts.
 */

/**
 * For the Live Hotel Search text box.
 * Does an ajax request based on value passed in to return a list of all matching
 * hotels beneath the search box.
 *
 * @params string searchTxt
 *
 */
 function cdHotelSearch(searchTxt)
 {
    //Continue only if 3 or more characters have been entered AND we are in the hotel location search
    if( searchTxt.length >= 3 && typeof($('#hotelSearchTab').attr('class')) != "undefined" )
    {
        // Do ajax call to get hotels based on search text, use data to populate suggestions div
        $.getJSON('/search/hotels_search.php',{a: 'liveHotel', search: searchTxt}, function(data){
            var list = '<ul>';

            var tmp = '';
            if( data.length == 0 )
                list += '<li>No Hotels Found</li>';
            else{
                //this is the template for the list item
                var lineTpl = '<li><a onClick="hotelsNrMap.openHotelFloatingWindow([[id]])" class="suggestions">[[hotelName]]</a></li>'
                for( var i = 0; i < data.length; i++ )
                {
                    tmp = lineTpl;
                    tmp = tmp.replace("[[id]]", data[i].HotelID);
                    tmp = tmp.replace("[[lat]]", data[i].latitude);
                    tmp = tmp.replace("[[lng]]", data[i].longitude);
                    tmp = tmp.replace("[[hotelName]]", data[i].name);
                    //list += '<li><a onclick="hotelsNrMap.showHotelFromSidebar({"HotelID"'+data[i]+')" class="suggestions">'+data[i].name+'</a></li>';
                    list += tmp;                    
                }
            }
        
            list += '</ul>';
            $('#hotelSuggest').html(list);
        })
        //unhide suggestions box
        $('#hotelSuggest').fadeIn(400);
    }
    else{
        //hide suggestions box
        $('#hotelSuggest').fadeOut(400);
    }
 }

/**
 * Populates the recently viewed box with the last 5 viewed hotels, based on cookies set.
 *
 * @dependancies jQuery, jQuery Cookie plugin
 *
 */
 function cdShowRecent()
 {
    // Variables for storing hotel id number, default to 0 for when cookie does not exist
    var dat = new Array(0,0,0,0,0);

    //Set the vars
    for ( var i = 0; i < 5; i++ )
    {
        var hotNum = i+1;
        if( $.cookie('hotel'+hotNum) != undefined )
            dat[i] = $.cookie('hotel'+hotNum);
    }

    if( $.cookie('hotel1') ) // If there is at least 1 hotel cookie set
    {

        $.getJSON('/search/hotels_search.php', {a: 'getHotelRecent', id1: dat[0], id2: dat[1], id3: dat[2], id4: dat[3], id5: dat[4]}, function(data)
        {
                var list = '';

                for( var i = 0; i < data.length; i++ )
                {
                        if( i == 0 )//First item in list has a different design
                        {
                            list += '<li id="featured">\n\
                                    <img id="featuredImage" src="'+data[i].URL+'"/>\n\
                                    <div class="sideTextFeat">\n\
                                        <h3><a id="featuredTitle" href="#" onClick="hotelsNrMap.openHotelFloatingWindow('+data[i].HotelID+')">'+data[i].name+'</a></h3>\n\
                                        <div id="starBoxFeatured">';
                            //if there is no rating e.g. rating = 0
                            if( data[i].Rating == 0 )
                                list += '<p class="noRating">No Rating Available</p>';
                            else
                                list += '<img class="starRating" src="/images/'+data[i].Rating+'stars.png" alt="hotels rating" />';

                            list += '</div>\n\
                                        <p id="featuredp"><sup id="featuredsup">from</sup> <span id="featuredspan">&pound;'+data[i].price+'</span><span id="featuredsub"> per night</span></p>\n\
                                        <a style="float: right;" class="orangeBtnSide" href="/hotels/'+data[i].name.toLowerCase().replace(/\s/g, '-').replace(/---/g, '-')+'/'+data[i].id+'/">Find Out More</a>\n\
                                    </div>\n\
                                </li>';
                        }
                        else{
                            list += '<li>\n\
                                        <img src="'+data[i].URL+'"/>\n\
                                        <div class="sideText">\n\
                                            <h3><a href="#" onClick="hotelsNrMap.openHotelFloatingWindow('+data[i].HotelID+')">'+data[i].name+'</a></h3>\n\
                                            <div id="starBox">';
                            //if there is no rating e.g. rating = 0
                            if( data[i].Rating == 0 )
                                list += '<p class="noRating">No Rating Available</p>';
                            else
                                list += '<img class="starRating" src="/images/'+data[i].Rating+'stars.png" alt="hotels rating" />';

                                list += '</div>\n\
                                            <p><sup>from</sup> <span>&pound;'+data[i].price+'</span> <sub>per night</sub></p>\n\
                                            <a class="textLink" href="/hotels/'+data[i].name.replace(/\s/g, '-').toLowerCase().replace(/---/g, '-')+'/'+data[i].id+'/">Find Out More</a>\n\
                                        </div>\n\
                                    </li>';
                        }
                 }
                 $('#recent').html(list);
          });
       }
       else{
            //No recently viewed
            $('#recent').append('<li><p class="noViewed">No Recently Viewed Hotels</p></li>');
       }

 }

/**
 * Shows/Hides all attraction icons based on the category passed in.
 * @parameter string cat The category
 *
 * @dependencies jQuery
 * @author CD
 */
 function filterAttractionByCat(category)
 {
    // Show/Hide all the icons currently on the map
    for( var i = 0; i < attractions.length; i++ )
    {
        //special case for tourist attraction - itsnot one word...
        if( attractions[i].cat == 'Tourist Attraction' && category == 'tourist' )
        {
            if( categoryArray['tourist'] == 'hidden' )
                attractionMarkers[attractions[i].id].show();
            else
                attractionMarkers[attractions[i].id].hide();
        }
        else if( attractions[i].cat.toLowerCase() == category )
        {
            if( categoryArray[category] == 'hidden' )
                attractionMarkers[attractions[i].id].show();
            else
                attractionMarkers[attractions[i].id].hide();
        }
    }
    //change the show state of the category.
    if( categoryArray[category] == 'hidden')
    {
        categoryArray[category] = 'show';
        $('#'+category).attr('src', '/images/'+category+'-small.png'); //change the pic to ON version
    }
    else{
        categoryArray[category] = 'hidden';
        $('#'+category).attr('src', '/images/'+category+'-small-off.png'); //change the pic to OFF version
    }
 }


/**
 * Shows/Hides all hotel icons.
 *
 * @dependencies jQuery
 * @author CD
 */
 function filterHotels()
 {
    // Show/Hide all the icons currently on the map
    for( var i = 0; i < hotels.length; i++ )
    {
        if( showHotels == false )
            hotel_markers[hotels[i].id].show();
        else
            hotel_markers[hotels[i].id].hide();
    }
    //change the show state of the category.
    if( showHotels == false )
    {
        showHotels = true;
        $('#hotels').attr('src', '/images/hotel-small.png'); //change the pic to ON version
    }
    else{
        showHotels = false;
        $('#hotels').attr('src', '/images/hotel-small-off.png'); //change the pic to OFF version
    }
 }

// Bind the onkeyup event onto the textbox via jQuery
$(document).ready(function(){
    $('.hotelSearchText').keyup(function(){
        cdHotelSearch($(this).val());
    });
    
    // If user clicks anywhere outside text box, hide the suggestions box
    $(document).click(function(){
        $('#hotelSuggest').fadeOut(400);
    });

    // Make sure clicking on the text box itself doesnt hide the suggestions box
    $('.hotelSearchText').click(function(e){
        e.stopPropagation();
    });

    // Populate recently viewed
    cdShowRecent();

    // Attach onfocus event on all <a> links so that there is none of that horrible marquee business around the links
    $('a').focus(function(){(this).blur()});

    $('#map_canvas').append('<img id="loadImg" src="/images/load.gif" />');

    $('.afIcon img').tooltip('#tooltip');

    $('.afIcon img').click(function(){
            //remove the
    });
});
