var quickSearchFetched = false;
var quickcontactFetched = false;

// fired from quick search button in header
function showHideQuickSearch()	{

	if ($("#quick-search").css('display') == 'none')	{

		// show venue search
		$("#quick-search").slideDown('slow');

		if (!quickSearchFetched)		{

			// Grab the form with ajax rather than load on every page
			$.get(
				"/content/quickSearch.asp", 
				 { 	},
				 function(data){	
					$("#quick-search").html(data);
					quickSearchFetched = true;

					// Quality slider
					//sliDelegate = $('select#qualityMapMin, select#qualityMapMax').selectToUISlider({	labels: 0,	steps: 5, max: 5, min: 1	}).next();
					//$('#qualityMapMin').css('display', 'none');
					//$('#qualityMapMax').css('display', 'none');

					// Deligate slider
					sliDelegate = $('select#delegateMin, select#delegateMax').selectToUISlider(	{	labels: 0,	steps: 50, max: 10000, min: 0 }).next();
					$('#delegateMin').css('display', 'none');
					$('#delegateMax').css('display', 'none');
					

				}
			);
		}
	}	else	{
		// Hide quick search
		$("#quick-search").slideUp('slow');

	}
}

function showHideContact()	{
	if ($("#quick-contact").css('display') == 'none')	{

		// show venue search
		$("#quick-contact").slideDown('slow');

		if (!quickcontactFetched)		{

			// Grab the form with ajax rather than load on every page
			$.get(
				"/content/quickContact.asp", 
				 { 	},
				 function(data){	
					$("#quick-contact").html(data);
					quickcontactFetched = true;

					// Quality slider
					
					//sliDelegate = $('select#qualityMapMin, select#qualityMapMax').selectToUISlider({	labels: 0,	steps: 5, max: 5, min: 1	}).next();
					//$('#qualityMapMin').css('display', 'none');
					//$('#qualityMapMax').css('display', 'none');

					// Deligate slider
					sliDelegate = $('select#delegateMin, select#delegateMax').selectToUISlider(	{	labels: 0,	steps: 50, max: 10000, min: 0 }).next();
					$('#delegateMin').css('display', 'none');
					$('#delegateMax').css('display', 'none');
					

				}
			);
		}
	}	else	{
		// Hide quick search
		$("#quick-contact").slideUp('slow');

	}
}
function doFormSearch(toPage)	{

	$('.loading').css('display', 'block');
	jQuery("#search-results").html(' ');

	currentPage = toPage;

	keyWords = jsEscape($('#txt-keywords').val(), false);
	venueTypes = jsEscape(getCheckArray('chk-venueTypes'), false);
	roomLayout = jsEscape(getCheckArray('chk-roomLayout'), false);
	facilities = jsEscape(getCheckArray('chk-facilities'), false);

	bedroomsMin = $('#bedroomsMin').val();
	bedroomsMax = $('#bedroomsMax').val();

	delegateMin = $('#delegateMin').val();
	delegateMax = $('#delegateMax').val();

	capacityMin = $('#capacityMin').val();
	capacityMax = $('#capacityMax').val();

	//qualityMin = $('#qualityMin').val();
	//qualityMax = $('#qualityMax').val();

	region = $("input[name='chk-Regions']:checked").val();

	searchParams = {	region: region,cpage: currentPage, kWord: keyWords, vType: venueTypes, vLayout: roomLayout, vFacilities: facilities, brMin: bedroomsMin, brMax: bedroomsMax, deMin: delegateMin, deMax: delegateMax, caMin: capacityMin, caMax: capacityMax		};			//, quaMin: qualityMin, quaMax: qualityMax

	fetchRecords(searchParams);
}


function fetchRecords(jsOptions)	{

	//alert(jsOptions);

	 $.getJSON(
		"/content/venueJSON.asp", 
		 jsOptions,
		 function(venuesObj){

			//console.log(venuesObj.vSQL);

			venueList = venuesObj.Venues;
			pages = venuesObj.pages;
			tRecords = venuesObj.records;
					
			jQuery("#resultCount").html(tRecords.toString());

			pages = doPages(currentPage, pages)
			jQuery("#search-pages").html( pages );
			jQuery("#search-pages2").html( pages );

			jQuery("#search-results").html( listVenues(venueList) );
			
			$('.loading').css('display', 'none');
		 }
	);
}

function fetchPCORecords(jsOptions)	{

	//alert('here');
	$('#venues .loading').css('display', 'block');
	

	 $.getJSON(
		"/content/pcoJSON.asp", 
		 jsOptions,
		 function(venuesObj){

			//alert('got data :');
		
			venueList = venuesObj.Venues;
			pages = venuesObj.pages;
			tRecords = venuesObj.records;
			catTitle = venuesObj.catTitle;
				
			jQuery("#resultCount").html( tRecords.toString() );
			jQuery("div.pager h2").html( catTitle );

			pages = doPages(currentPage, pages)
			jQuery("#search-pages").html( pages );
			jQuery("#search-pages2").html( pages );

			jQuery("#search-results").html( listVenues(venueList) );

			$('#venues .loading').css('display', 'none');

		 }
	);
}


function closePanel()	{
	$("#listing-container").slideUp("slow");
	$("#listing-container").queue(function () {
		jQuery("#listing-container").remove();
		$("#listing-container").dequeue();
		$("#venue_" + currentVenue).css("display", "block");
		$("#venue_" + currentVenue).slideDown("slow");
		currentVenue = 0;
		lastVenue = 0;
	});
}

function showNext(venueID)	{
	//jQuery("#txt-keywords").attr('value','Show Next');

	$.get(
		"/content/venueDetails.asp", 
		 {	venueID: venueID	},
		 function(data){	

			jQuery("#venue_" + currentVenue).slideUp("slow");
								
			// google tracking
			var title = data.match(/<h3.*>(.*)<\/h3>/);
			//alert('/venueDetails/' + title[1]);
			pageTracker._trackPageview('/venueDetails/' + title[1])


			jQuery("#venue_" + currentVenue).after( data );
			jQuery("#listing-container").css('display', 'none');
			$("#listing-container").slideDown("slow");
			$("#listing-container").dequeue();

		}
	)
}

function fetchDetails(venueID)	{
	if (currentVenue != 0)	{	lastVenue = currentVenue;	}
	currentVenue = venueID;

	// get rid of any open detail pages and redraw the list item
	if (lastVenue != 0)		{

		$("#listing-container").slideUp("slow");
		$("#listing-container").queue(function () {
			$("#listing-container").dequeue();
			$("#venue_" + lastVenue).css("display", "block");
			$("#venue_" + lastVenue).slideDown("slow");
			jQuery("#listing-container").remove();
			showNext(venueID);
		});

	}	else	{
		//alert('==0');
		showNext(venueID);
	}
}


function changeCart(venueId, action)	{
	 $.getJSON(
		"/content/venueCart.asp", 
		 {	venueId: venueId, act: action	},
		 function(venuesObj){
			cartVenues = venuesObj.CartVenues;
			allRows = '';
			if (cartVenues.length == 0)					{
				allRows = '<div class="cart-help"><p>No items in cart</p></div>';
				$("#cartButtons").hide();
			}	else	{
				$("#cartButtons").show();
				$.each(cartVenues, 
					function(i, venue){
						rowTpl = cartItemTpl.replace('%%venueTitle%%', venue.title);
						rowTpl = rowTpl.replace(/%%venueID%%/gi, venue.id);			// regexp as we need to replace all instances of the venueID

						allRows += rowTpl;
					}
				);
			}

			$('#cartItemList').html(allRows);
		 }
	);
}


// Get a set of records
function changePage(toPage, toId)	{
	currentPage = toPage;
	fetchRecords({	cpage: toPage,	cRecord: toId	});
}



// this builds up the pages list
function doPages(current, totalPages)	{
	pl = '';

	// Start position
	if (current > 4)	{
		//alert((current - 4));
		startPage = (current - 4);
	}	else	{
		startPage = 1;
	}

	// End position
	if ((startPage + 4) > totalPages)	{
		endPage = totalPages;
	}	else	{
		endPage = (startPage + 9);
	}

	if (endPage >= totalPages)	{	endPage = totalPages;	}



	for (pages = startPage ; pages <= endPage ; pages++ )		{

		if (pages == current)	{
			isOn = 'class="selected"';
		}	else	{
			isOn = '';
		}

		pl += '<li><a href="javascript:doFormSearch(' +pages+ ');" ' +isOn+ ' >' +pages+ '</a></li>';
	}

	return pageListTpl.replace('%%pages%%', pl);
}


function getStars(noOfStars)	{
	stars = '';
	noOfStars = parseInt(noOfStars);
	if (isNaN(noOfStars))	{	noOfStars = 0;		}
	if(noOfStars != 0)	{	
		for (i=1 ; i<=5 ; i++)	{
			if (i <= noOfStars)	{
				stars += '<img src="images/star_on.gif" class="noPadding" alt="Star Rating" />';
			}	else	{
				stars += '<img src="images/star_off.gif" class="noPadding" alt="Star Rating" />';
			}
		}
		stars += ' &nbsp;VisitScotland Quality Assurance';
	}
	return stars;
}

// Displays the list of venues
function listVenues(venueList)	{

	lastVenue = 0;
	currentVenue = 0;
	vPremium = 0;

	allRows = '';

	$.each(venueList, 
		function(i,venue){
			rowTpl = venueListTpl.replace('%%venueName%%', venue.vName);
			rowTpl = rowTpl.replace('%%vAddress%%', venue.vAddress);
			rowTpl = rowTpl.replace('%%venueDesc%%', venue.vSyn);
			rowTpl = rowTpl.replace(/%%venueID%%/gi, venue.vID);			// regexp as we need to replace all instances of the venueID

			vPremium = venue.vPremium

			// read more and stars
			if (vPremium == 1)	{
				rowTpl = rowTpl.replace('%%readmore%%', '<li><a href="javascript:fetchDetails(' + venue.vID + ');">Read more</a></li>');
				//alert(venue.stars)
				rowTpl = rowTpl.replace('%%stars%%', getStars(venue.stars));
			}	else	{
				rowTpl = rowTpl.replace('%%readmore%%', '<li><span style="width: 85px; display: block;height: 10px;">&nbsp;</span></li>');
				rowTpl = rowTpl.replace('%%stars%%','');
			}
			
			// Cart add - Only for venues
			if (venue.vService == 0)	{
				rowTpl = rowTpl.replace('%%addToCart%%','<li><a href="javascript:changeCart('+ venue.vID + ', \'add\');">Add to My Selected Venues</a></li>'); 
			}	else	{
				rowTpl = rowTpl.replace('%%addToCart%%','<li>&nbsp;</li>'); 
			}


			// Thumbnail if exists
			if ((venue.vImg != '') && (vPremium == 1))	{
				rowTpl = rowTpl.replace('%%imgThumb%%', '<img src="'+ imgPath + 'thumbs/medium_'+ venue.vImg +'" alt="'+ venue.vImgAlt +'" width="135" height="100"  />');
			}	else	{
				rowTpl = rowTpl.replace('%%imgThumb%%', '');
			}

								
			allRows += rowTpl;
		}
	);

	// if only one venue returned open the details - Only if premium
	if (venueList.length == 1)	{
		if (vPremium == 1)	{
			fetchDetails(venueList[0].vID);
		}

	}


	return allRows;
}


function clearEmailBox(ele)
{
	if(ele.value=="enter email"){
		ele.value = "";
	}	
}

