var cookiename = 'mybritain';

// add an id/value pair to a cookie
// and update the shortlist
function addMyBritain(userid, theid, thename, thetype) {
//    if($.cookie(cookiename)) {
//        var current = $.cookie(cookiename);
//        // check this item isn't already in the shortlist
//        var parts = $.cookie(cookiename).split('*****');
//        var already = 0;
//        for(var i=0; i<parts.length; i++) {
//            if(parts[i].indexOf(theid + "%",0)==0) already = 1;
//        }
//        if(already==0) { // it's not already been shortlisted
//            $.cookie(cookiename,current + "*****" + theid + "%%%%%" + thename + "%%%%%" + thetype, { path: '/' });
//            //var content = '<p><a href="' + path + 'property-details/' + thetype + '/' + theid + '/" title="' + thename + '">' + thename + '</a> [<a href="javascript:removeFromShortlist(\'' + theid + '\');">x</a>]</p>';
//            //var newline = $('#shortlist div').append(content).hide();
//            //$(newline).fadeIn('slow');
//        }
//    } else {
//        $.cookie(cookiename,theid + "%%%%%" + thename + "%%%%%" + thetype, { path: '/' });
//        //var content = '<p><a href="' + path + 'property-details/' + thetype + '/' + theid + '/" title="' + thename + '">' + thename + '</a> [<a href="javascript:removeFromShortlist(\'' + theid + '\');">x</a>]</p>';
//        //var newline = $('#shortlist div').append(content).hide();
//        //$(newline).fadeIn('slow');
//    }

     var url = path + "ajax/myopenbritain.php?action=add&userid=" + userid + "&listingid=" + theid;
        $.getJSON(url,
        function(data){
        });
        // update the database
   	 alert("The property has been added to your MyOpenBritain shortlist.");
}

// remove an id/value pair from the cookie
function removeFromShortlist(userid,theid, thetype, theel) {
//    var pairs = $.cookie(cookiename).split('*****');
//    var content = "";
//    for(var i=1; i<pairs.length; i++) {
//        var parts = pairs[i].split('%%%%%');
//        if(parts[0]==theid&&parts[2]==thetype) {
//        }else {
//            content = content + "*****" + parts[0] + "%%%%%" + parts[1] + "%%%%%" + parts[2] ;
//        }
//    }
//    $.cookie(cookiename, content, { path: '/' });
	var url = path + "ajax/myopenbritain.php?action=delete&userid=" + userid + "&listingid=" + theid;
        $.getJSON(url,
        function(data){
        });     
    refreshShortlist2(theel);
}

function refreshShortlist2(el) {
    $(el).parent().parent().css("display","none");
    //$(el).css("display","none");
}

function refreshShortlist() {
    $('#shortlist div').html('');
    if($.cookie(cookiename)) {
        var pairs = $.cookie(cookiename).split('*****');
        var thestring = "";
        for(var i=0; i<pairs.length; i++) {
            var parts = pairs[i].split('%%%%%');
            if(parts[0]) thestring = thestring + '<p><a href="' + path + 'property-details/' + parts[2] + '/' + + parts[0] + '/" title="' + parts[1] + '/' + parts[2] + '">' + parts[1] + '</a> [<a href="javascript:removeFromShortlist(\'' + parts[0] + '\');">x</a>]</p>';
            //thestring = thestring + '<a href="/viewproperty/' + parts[0] + '" title="' + parts[1] + '">' + parts[1] + '</a><br />';
        }
        $('#shortlist div').html(thestring);
    }
}

function clearCookies() {
    $.cookie(cookiename, null);
    refreshShortlist();
}

function showCookies() {
    alert($.cookie(cookiename));
}

function add_review() {
    $('#current_reviews').hide();
    $('#new_review').show();
}

function cancel_review() {
    $('#comment').val("");
    $('#new_review').hide();
    $('#current_reviews').show();
}

function add_travelreview() {
    $('#current_travelreviews').hide();
    $('#new_travelreview').show();
}

function cancel_travelreview() {
    $('#comment').val("");
    $('#new_travelreview').hide();
    $('#current_travelreviews').show();
}

function report_review(theid) {
    if(confirm("Would you like to report this review for questionable content?")) {
        var url = path + "ajax/reportreview.php?reviewid=" + theid;
        $.getJSON(url,
        function(data){
        });
        // update the database
        alert("Thank you.\nThis entry has been marked for review by an administrator.")
    }
}
function report_travelreview(theid) {
    if(confirm("Would you like to report this travel review for questionable content?")) {
        var url = path + "ajax/reporttravelreview.php?travelreviewid=" + theid;
        $.getJSON(url,
        function(data){
        });
        // update the database
        alert("Thank you.\nThis entry has been marked for review by an administrator.")
    }
}

function report_listing(theid, thetype) {
    if(confirm("Would you like to report this entry for questionable content?")) {
        var url = path + "ajax/reportlisting.php?listingid=" + theid + "&listingtype=" + thetype;
        $.getJSON(url,
        function(data){
        });
        // update the database
        alert("Thank you.\nThis entry has been marked for review by an administrator.")
    }
}


function showReviewResponse(data)  {
    $('#review_result').html(data.message);
    $('#review_result').fadeIn('slow');
    if(data.success=="True") {
        // flash a success message and hide the 'add review' form
        $('#new_review').css('display', 'none');
        $('#review_result').css('marginBottom','5px');
        $('p.numreviews').hide();
        
        // now show the review
        var thehtml = $('#current_reviews').html();
        var thereview = "<p class='numreviews'>There are " + data.total + " reviews for this property.</p>";
        thereview += '<p class="bluetext">';
        thereview += data.stars + data.comment;
        thereview += '</p>';
        $('#current_reviews').html(thereview + thehtml);
        $('#current_reviews').show();
        $('.reviewstar').rating();
    }
}

$(document).ready(function(){

        $('A[rel="external"]').click( function() {
            window.open( $(this).attr('href') );
            return false;
        });

        $('.hideinfo').click(function() {
            $(this).parent().next().toggle();
            if($(this).parent().css("marginBottom")=="8px") {
                $(this).html('Hide');
                $(this).parent().css("marginBottom","0px");
            } else {
                $(this).html('Show');
                $(this).parent().css("marginBottom","8px");
            }
        });
        
        $('.attractionhideinfo').click(function() {
            $(this).parent().next().toggle();
            if($(this).parent().css("marginBottom")=="8px") {
                $(this).html('Hide');
                $(this).parent().css("marginBottom","0px");
            } else {
                $(this).html('Show');
                $(this).parent().css("marginBottom","8px");
            }
        });
        
        if(window.opera) {  
            if ($("a.jqbookmark").attr("rel") != ""){ // don't overwrite the rel attrib if already set  
               $("a.jqbookmark").attr("rel","sidebar");  
           }  
        }  
        
        $("a.jqbookmark").click(function(event){
            // prevent the anchor tag from sending the user off to the link  
            event.preventDefault(); 
            var url = this.href;  
            var title = "OpenBritain";
            if (window.sidebar) { // Mozilla Firefox Bookmark  
                window.sidebar.addPanel(title, url,"");  
            } else if( window.external ) { // IE Favorite  
                window.external.AddFavorite(url,title);  
            } else if(window.opera) { // Opera 7+  
                return false; // do nothing - the rel="sidebar" should do the trick  
            } else { // for Safari, Konq etc - browsers who do not support bookmarking scripts (that i could find anyway)  
                alert('Unfortunately, this browser does not support the requested action, please bookmark this page manually.');  
            }
            return false;
        });
        
        refreshShortlist();
        
        $('#nav li:first').attr("id","home");
        $('.topbar li:last').attr("id","access");
        $('#subnav li:first').attr("class","removeback");
        $('#footer li:last').css("border","0");
        
});  

// control the display of the advanced search options
// on the property search page
function adv_options(el) {
    // see which ones are checked
    var els = new Array('whatisthisid1', 'whatisthisid2', 'whatisthisid3');
    var checkeditems = new Array();
    for(var i=0;i<els.length;i++) {
        if( $('#'+els[i]).attr('checked')==true) {
            checkeditems[i] = $('#'+els[i]).attr('value');
        } else {
            checkeditems[i] = "";
        }
    }
    var url = path + "ajax/advsearchfields.php?selected=1&array1=" + checkeditems[0] + "&array2=" + checkeditems[1] + "&array3=" + checkeditems[2];
        $.getJSON(url,
        function(data){
          $.each(data.hide, function(i,item){
            // hide the parent element
            $('#' + item).parent().css("display", "none");
            $('#' + item).parent().prev().css("display", "none");
            $('#' + item).parent().next().css("display", "none");
            // and it's previous sibling
            //$('#' + item)
            // and it's next sibling
            
          });
          $.each(data.show, function(i,item){
            //alert(item);
            $('#' + item).parent().css("display", "inline");
            $('#' + item).parent().prev().css("display", "inline");
            $('#' + item).parent().next().css("display", "inline");
          });
          // set the title of the rooms/units heading if theyve selected 'self-catering'
          //if(data.selfcatering=="1") {
          //  $('#roomsunitshead').html('<strong>Units</strong>');
          //} else {
          //  $('#roomsunitshead').html('<strong>Rooms/Units</strong>');
          //}
        });
}

numtags = 0;

function newtag() {
    numtags = parseInt($('#numtags').val());
    var thenewtag = $('#embed_tags div textarea:first').clone().appendTo('#embed_tags div').val('');
    var thelinebreak = $('#embed_tags div br:first').clone().appendTo('#embed_tags div');
    var thenewlink = $('#embed_tags div a:first').clone().appendTo('#embed_tags div');
    var anotherlinebreak = $('#embed_tags div br:first').clone().appendTo('#embed_tags div');
    numtags = numtags+1;
    $(thenewtag).attr('id','tag_' + parseInt(numtags));
    $(thenewtag).attr('name','tag_' + parseInt(numtags));
    $(thenewlink).attr('id','taglink_' + parseInt(numtags));
    $('#numtags').val(parseInt(numtags));
}

function deletetag(el) {
    $(el).prev().prev().val('').css('display','none').next().css('display','none');
    $(el).css('display','none').next().css('display','none');
}