// JavaScript Document $Id: rctSearch.js,v 1.9 2011/10/26 08:44:44 andy.p Exp $

var sortOutResults = '';

function pullRSDown()
{
	pullRSDown('');
}

function pullRSDown(data)
{
	rsUrl=Global.url+'recentSearches?sid='+Math.random();
	$.ajax({
	    url: rsUrl,
	    type: 'GET',
	    dataType: 'html',
	    data: data,
	    timeout: 4000,
	    error: function(request, status, error){
	        alert('Error loading XML document'+"\n"+'-----------------------------'+"\n"+rsUrl+"\n"+'-----------------------------'+"\n\n"+'request.status: '+request.status+"\n"+'request.error: '+request.error+"\n"+'request.responseText: '+request.responseText);
	    },
	    success: function(html){
			populateRSBox(html);
	    }
	});
}

function populateRSBox(html)
{
	var newData=$('#rsContent', html);
	$('#rsAjaxUpdate').slideUp('slow', function() {
		$('#rsAjaxUpdate').html(newData);
		$('#rsAjaxUpdate').slideDown('slow');
	});
	if(typeof window.sortOutResults == 'function')
	{
		sortOutResults();
	}
}

function deleteRS(squidForm,deleId)
{
	rsUrl=Global.url+'recentSearches?sid='+Math.random();
	$('#'+squidForm).slideUp('slow');
	data='delete=Delete&recentSearchId='+deleId;

	$.ajax({
	    url: rsUrl,
	    type: 'POST',
	    dataType: 'html',
	    data: data,
	    timeout: 4000,
	    error: function(request, status, error){
	        alert('Error posting data to RS '+"\n"+'-----------------------------'+"\n"+rsUrl+"\n"+'-----------------------------'+"\n\n"+'request.status: '+request.status+"\n"+'request.error: '+request.error+"\n"+'request.responseText: '+request.responseText);
	    },
	    success: function(html){
	    	//post success, refresh on page data...
	    	pullRSDown('refresh=true');
    		var tmpJig = $('#rctSearch ul li').html();
    		tmpJig=(((tmpJig.substring(9,tmpJig.indexOf('<')))*1)-1);
    		if(tmpJig=='0'||deleId=='All')
    		{
	    		$('#rctSearch').slideUp('slow');
    			$('#rsContent').css('display','block');
				$('#rctSrchDelAll').slideUp().remove();
				$('#rsContent').append("<p class=\"tac\">You have deleted all your recent searches</p>");
	    	}

    		else if(tmpJig>1)
			{
   				$('#rctSearch ul li').html('You have '+tmpJig+' <a id="rsLink" href="'+Global.url+'recentSearches">Recent Searches</a>');
   				recentSrch.rcntSrchTrigger();
			}
    		else
			{
    			$('#rctSearch ul li').html('You have '+tmpJig+' <a id="rsLink" href="'+Global.url+'recentSearches">Recent Search</a>');
    			recentSrch.rcntSrchTrigger();
			}
	    }
	});
}

recentSrch ={
		init : function(){
			$('body').append("<div id=\"recentSrchContainer\" class=\"hide\"><div id=\"recentSrchReceiver\"></div></div>");
			$('#recentSrchContainer').append("<a id=\"recentSrchFBLink\" href=\"#recentSrchReceiver\">Display Recent Searches</a>");
			recentSrch.rcntSrchTrigger();
		},
		rcntSrchTrigger: function(){
			$('a#rsLink').click(function(e){
				e.preventDefault();
				recentSrch.rcntSrchRetreiveSearches($(this));
			});
		},
		rcntSrchRetreiveSearches: function(el){
			//What to pull in via AJAX
			var toLoad = el.attr('href')+' #rsContent';

			//Show fancy box Waiting...
			$.fancybox.showActivity();

			//Put content into hidden container for use...
			$('#recentSrchReceiver').load(toLoad,'',function(){

				$('#recentSrchFBLink').fancybox({
					'centerOnScroll' : true,
					'padding' : 4,
					'autoDimensions' : false,
					'width' : 602,
					'height': 400,
					'overlayColor' : 	'#000',
					'overlayOpacity': 	0.8,
					'titlePosition' 	: 'inside',
					'titleFormat'		: function(title, currentArray, currentIndex, currentOpts) {
						return '<p id="fancybox-disclaimer-under"><span>*</span> Prices may change due to availability at time of booking</p>';
						}


				}).trigger('click');
				recentSrch.rctSrchDelAllListener();
			});
		},
		rctCloseFB: function(){
			$.fancybox.close();
		},
		rctSrchDelAllListener: function(){
			$("form#rctSrchDelAll").bind("submit", function(e) {
				e.preventDefault();
				var $tgtForm = $(e.target);
				tgtAction = $tgtForm.attr("action");
				var dataString =  'deleteAll=true';
				$.ajax({
					type: "POST",
					url: tgtAction,
					data: dataString,
					success: function(html) {
						// hide the one you click on
						$('#rsContent div.rctSrchRSItem').remove();
						$('#rctSearch').slideUp().remove();
						$('#rctSrchDelAll').slideUp().remove();
						$('#rsContent').append("<p class=\"tac\">You have deleted all your recent searches</p>");
					}

				});

			});
		}
};
