var $j = jQuery.noConflict();
$j(document).ready(function() {

Shadowbox.init({
overlayOpacity: 0
});


$j(".searchelementcontent").show();
//$j(".specialoffer[title]").tooltip({tip: '#tooltiptwo', effect: 'slide', direction: 'left', bounce: 'true', lazy: 'false'});
$j(".resetbutton[title]").tooltip({tip: '#tooltiptwo', effect: 'slide', direction: 'left', bounce: 'true', lazy: 'false'});
$j("#search_submit, #signup_submit, #datestrip").hide();
// ajax advanced search form submission
$j("#searchbutton.advanced, #searchgo").click(function() {
var searchvalues = $j("#bfbsearch").serialize();
//console.log(searchvalues);
if(searchvalues=="search_propertyname=&search_country=&search_region=&search_arrivaldate=&search_numbernights=&search_sleeps=") {
$j.facebox("<p class='alert'>Form Error</p><p class='dialogtext'>You haven't selected any search options yet.<br>Try selecting a country to start your search...</p>");
return false;
}
var arrival = $j("#search_arrivaldate").val();
var nights = $j("#search_numbernights").val();
if(arrival !="" && (nights<1 || nights=="")) {
$j.facebox("<p class='alert'>Form Error</p><p class='dialogtext'>You've selected an Arrival Date for your search but left the Number of Nights field empty. <br>Please select the Number of Nights before submitting your search.</p>");
return false;
} else if(nights!="" && arrival=="") {
$j.facebox("<p class='alert'>Form Error</p><p class='dialogtext'>You've selected the Number of Nights for your search but left the Arrival Date field empty. <br>Please select the Arrival Date before submitting your search.</p>");
return false;
}
$j("#pages").empty();
$j("#loading").loading({img: 'elements/images/loader.gif'});
var sorttype = $j('#sorttype').val();
searchvalues+= "&search_orderby="+sorttype;
$j.ajax({
type: "POST",
url: "scripts/xhr_get_search.php",
cache: false,
data: searchvalues,
success: function(html){
location.href="#listingtopstrip";
$j("#loading").loading(false).empty();
var newhtml = html.split("---");
var results = newhtml[2].split("###");
$j("#searchreturned").fadeOut(200).empty().html(newhtml[0]).fadeIn(250).show();
$j("#listing_displayresults").empty().html(newhtml[1]).fadeIn(400).show();
$j(".listingblock").effect("highlight", {}, 1500);
$j("#pages, #pages_bottom").empty().html(newhtml[4]).show();
$j("#results_checkin").empty().html(results[0]).show();
$j("#results_nights").empty().html(results[1]).show();
$j("#results_guests").empty().html(results[2]).show();
//$j("#bfbnocache").val(newhtml[5]);
//console.log(newhtml[5]);
$j("#datestrip").show();
}
});
return false;
});
// end ajax form search
//
// paging triggers search
$j('ul.paging li a').live("click",function() {
$j("#pages").empty();
$j("#loading").loading({img: 'elements/images/loader.gif'});
var searchvalues = $j("#bfbsearch").serialize();
var pagenumber = $j(this).html();
searchvalues+= "&showpage="+pagenumber;
var sorttype = $j('#sorttype').val();
searchvalues+= "&search_orderby="+sorttype;
$j.ajax({
type: "POST",
url: "scripts/xhr_get_search.php",
cache: false,
data: searchvalues,
success: function(html){
location.href="#listingtopstrip";
$j("#loading").loading(false).empty();
var newhtml = html.split("---");
var results = newhtml[2].split("###");
$j("#searchreturned").fadeOut(200).empty().html(newhtml[0]).fadeIn(250).show();
$j("#listing_displayresults").empty().html(newhtml[1]).fadeIn(400).show()
$j(".listingblock").effect("highlight", {}, 1500);
$j("#pages, #pages_bottom").empty().html(newhtml[4]).show();
$j("#results_checkin").empty().html(results[0]).show();
$j("#results_nights").empty().html(results[1]).show();
$j("#results_guests").empty().html(results[2]).show();
//$j("#bfbnocache").val(newhtml[5]);
//console.log(newhtml[5]);
$j("#datestrip").show();
}
});
return false;
});
// sort
$j("#sorttype").change( function() {
$j("#pages").empty();
$j("#loading").loading({img: 'elements/images/loader.gif'});
var searchvalues = $j("#bfbsearch").serialize();
var sorttype = $j('#sorttype').val();
searchvalues+= "&search_orderby="+sorttype;
//var param_continent = $j('#param_continent').html();
//console.log(param_continent)
//alert (getparams);
/*if(param_continent!="") {
searchvalues+= "&search_continent="+param_continent;
}*/
$j.ajax({
type: "POST",
url: "scripts/xhr_get_search.php",
cache: false,
data: searchvalues,
success: function(html){
location.href="#listingtopstrip";
$j("#loading").loading(false).empty();
var newhtml = html.split("---");
var results = newhtml[2].split("###");
$j("#searchreturned").fadeOut(200).empty().html(newhtml[0]).fadeIn(250).show();
$j("#listing_displayresults").empty().html(newhtml[1]).fadeIn(400).show()
$j(".listingblock").effect("highlight", {}, 1500);
$j("#pages, #pages_bottom").empty().html(newhtml[4]).show();
$j("#results_checkin").empty().html(results[0]).show();
$j("#results_nights").empty().html(results[1]).show();
$j("#results_guests").empty().html(results[2]).show();
//$j("#bfbnocache").val(newhtml[5]);
//console.log(newhtml[5]);
$j("#datestrip").show();
}
});
return false;
});
// compare
$j('a#comparebutton').live("click",function() {
var comparelist = "";
$j(".propertytick :checked").each(function() {
comparelist+= $j(this).attr("id") + ",";
});
var searchvalues = "comparelist="+comparelist;
if(comparelist!="") {
$j.ajax({
type: "POST",
url: "scripts/xhr_get_search.php",
cache: false,
data: searchvalues,
success: function(html){
location.href="#listingtopstrip";
var newhtml = html.split("---");
var results = newhtml[2].split("###");
$j("#searchreturned").fadeOut(200).empty().html(newhtml[0]).fadeIn(250).show();
$j("#listing_displayresults").empty().html(newhtml[1]).fadeIn(400).show();
$j("#pages, #pages_bottom").empty().html(newhtml[4]).fadeIn(400).show();
$j("#results_checkin").empty().html(results[0]).fadeIn(260).show();
$j("#results_nights").empty().html(results[1]).fadeIn(280).show();
$j("#results_guests").empty().html(results[2]).fadeIn(300).show();
}
});
}
return false;
});
// get regions when country changes
$j("#search_country").change( function() {
countrycode = $j('#search_country').val();
//alert(countrycode);
$j.ajax({
type: "GET",
url: "scripts/xhr_get_locations.php",
cache: false,
data: "countrycode=" + countrycode,
success: function(html){
$j('#search_region').empty();
$j('#search_region').html("<option value=\"\">any region</option>\n" + html);
$j("#search_region").css({
border: 'Solid #eaed7e 1px',
color: '#f4f5b8'
      });
}
});
});
$j(".resetbutton").click(function() {
$j('#bfbsearch')[0].reset();
$j("#searchbox").effect("highlight", "#b768a9", 1000);
});
// toggling
$j(".elementheader").click(function() {
$j(this).siblings(".searchelementcontent").toggle();
});
$j('#bfbcomments').cycle({fx: 'fade', timeout: 7500});

// end of document ready
});
