var isBrowsing = true;

function Set_Cookie(name, value)
{
    document.cookie = name + "=" + escape(value);
}

function EnableQuickMode(val)
{
	StateJSON.quickrate = val ? 1 : 0;
	window.location = BuildURL();
}

function parse_str(str, array){
	// http://kevin.vanzonneveld.net
	// +   original by: Cagri Ekin
	// +   improved by: Michael White (http://crestidg.com)
	// *     example 1: parse_str('first=foo&second=bar');
	// *     returns 1: { first: 'foo', second: 'bar' }
	// *     example 2: parse_str('str_a=Jack+and+Jill+didn%27t+see+the+well.');
	// *     returns 2: { str_a: "Jack and Jill didn't see the well." }

	var glue1 = '=';
	var glue2 = '&';

	var array2 = str.split(glue2);
	var array3 = [];
	for (var x1 = 0; x1 < array2.length; x1++) {
		var tmp = array2[x1].split(glue1);
		if (tmp[1] != null)
			array3[unescape(tmp[0])] = unescape(tmp[1]).replace(/[+]/g, ' ');
	}

	return array3;
}



function ShowSubCategories()
{
	if ($('ulSubCategories') != null)
	{
		new Effect.BlindDown($('ulSubCategories'));
		new Effect.BlindUp($('ulCategories'));
	}
}


function BrowseFilters(search_input)
{
	if (typeof(search_input) == 'string')
		StateJSON.search = $('#' + search_input).val();
	else
		StateJSON.search = $('#search_input').val();

	StateJSON.offer 		= ($('#price_show').get(0).checked) ? 1 : 0;
	StateJSON.menu 			= ($('#price_menu').get(0).checked) ? 1 : 0;
	StateJSON.booking 		= ($('#price_booking').get(0).checked) ? 1 : 0;
	StateJSON.privatehire 	= ($('#price_privatehire').get(0).checked) ? 1 : 0;

	var arr = GetPrice(document.getElementsByName('price_price'));
	StateJSON.lowcost 	= arr[0];
	StateJSON.highcost 	= arr[1];
	StateJSON.page 		= 1;
	if($('#price_friends').length)
		StateJSON.friends = $('#price_friends').get(0).checked ? 1 : 0;
	StateJSON.user 	= $('#price_member').val();
	if($('#postcode_postcode').length > 0 && String($('#postcode_postcode').val()).trim() != '') {
		StateJSON.halfpostcode = $('#postcode_postcode').val();
		StateJSON.city = '';
		StateJSON.district = '';
	}
	var hr = BuildURL();

	window.location.href = 'http://' + window.location.host + hr;
	return false;
}

function GetPrice(radios)
{
	var low = 0;
	var high = 0;
	for (var i = 0; i < radios.length; i++)
	{
		if (radios[i].checked)
		{
			return radios[i].value.split(',');
		}
	}
}


function BuildURL(){
	var url = '/places/'+StateJSON.country;
	var hasall = false;
	if (StateJSON.country == 'us' || StateJSON.country == 'canada' || StateJSON.country == 'new-zealand' || StateJSON.country == 'australia') {
		if (StateJSON.state == '' || StateJSON.state == 'all')
			hasall = true;
		url += '/' + ((hasall) ? 'all' : StateJSON.state);
	}

	if (StateJSON.country == 'uk' && StateJSON.halfpostcode != '')
		url += '/' + encodeURIComponent(StateJSON.halfpostcode);
	else {
	if (!hasall) {
			url += '/' + ((StateJSON.city != null && StateJSON.city != 'all' && StateJSON.city != '') ? StateJSON.city : 'all');
	}
	if (StateJSON.country == 'uk' && StateJSON.city == 'london') {
			url += '/' + ((StateJSON.district != 'all' && StateJSON.district != '' && StateJSON.district != null) ? StateJSON.district : 'all');
	}
	}

	if (StateJSON.category != '')
		url += '/' + StateJSON.category;

	if (StateJSON.subcategory != '')
		url += '/' + StateJSON.subcategory;

	var str_params = '';

	if (StateJSON.sort1 != '' && StateJSON.sort1 != null && StateJSON.sort1 != 'rating')
		str_params += (str_params.indexOf('?') > -1 ? '&' : '?') + 'sort1=' + StateJSON.sort1;
	if (StateJSON.page != '' && StateJSON.page != null && StateJSON.page != 1)
		str_params += (str_params.indexOf('?') > -1 ? '&' : '?') + 'page=' + StateJSON.page;
	if (StateJSON.search != '' && StateJSON.search != null)
		str_params += (str_params.indexOf('?') > -1 ? '&' : '?') + 'search=' + encodeURIComponent(StateJSON.search);
	if (StateJSON.tag != '' && StateJSON.tag != null)
		str_params += (str_params.indexOf('?') > -1 ? '&' : '?') + 'tag=' + encodeURIComponent(StateJSON.tag);
	if (StateJSON.offer != '0' && StateJSON.offer != null)
		str_params += (str_params.indexOf('?') > -1 ? '&' : '?') + 'offer=' + StateJSON.offer;
	if (StateJSON.menu != '0' && StateJSON.menu != null)
		str_params += (str_params.indexOf('?') > -1 ? '&' : '?') + 'menu=' + StateJSON.menu;
	if (StateJSON.booking != '0' && StateJSON.booking != null)
		str_params += (str_params.indexOf('?') > -1 ? '&' : '?') + 'booking=' + StateJSON.booking;
	if (StateJSON.privatehire != '0' && StateJSON.privatehire != null)
		str_params += (str_params.indexOf('?') > -1 ? '&' : '?') + 'privatehire=' + StateJSON.privatehire;
	if (StateJSON.lowcost != '0' && StateJSON.lowcost != null)
		str_params += (str_params.indexOf('?') > -1 ? '&' : '?') + 'lowcost=' + StateJSON.lowcost;
	if (StateJSON.highcost != '0' && StateJSON.highcost != null)
		str_params += (str_params.indexOf('?') > -1 ? '&' : '?') + 'highcost=' + StateJSON.highcost;
	if (StateJSON.friends != '0' && StateJSON.friends != null)
		str_params += (str_params.indexOf('?') > -1 ? '&' : '?') + 'friends=' + StateJSON.friends;
	if (StateJSON.user != '' && StateJSON.user != null)
		str_params += (str_params.indexOf('?') > -1 ? '&' : '?') + 'user=' + encodeURIComponent(StateJSON.user);
	if (StateJSON.quickrate != '0' && StateJSON.quickrate != null)
		str_params += (str_params.indexOf('?') > -1 ? '&' : '?') + 'quickrate=' + encodeURIComponent(StateJSON.quickrate);

	if (str_params != '')
		url += str_params;
	return url;
}


function AddAreaWikiLink()
{
	tAreaWikiLinkCount = $("#useful_links").get(0).childNodes.length;
	t_id = "useful_link_" + tAreaWikiLinkCount;

    var newDiv = document.createElement("div");
	newDiv.innerHTML +=
		"<div id='" + t_id + "'>" +
		"Name: <input type='text' name='link_name[]' size='20' /><br/> " +
		"URL:  <input type='text' name='link_url[]'  size='30' /> " +
		"<button onclick=\"RemoveAreaWikiLink('" + t_id + "')\">x</button>" +
		"</div>";
    $("#useful_links").get(0).appendChild(newDiv);

}

function RemoveAreaWikiLink(id)
{
	$('#'+id).html('');
}

function MapToolTip()
{
	return toolTip2.tipOver($('previewmap'));
}

function BuildMap(img_url)
{
	var imgMap = document.createElement('img');
	imgMap.setAttribute('src', img_url);
	imgMap.setAttribute('width', 300);
	imgMap.setAttribute('height', 372);
	imgMap.setAttribute('id', 'previewmap');
	$('mapsmall').innerHTML = '';
	$('mapsmall').appendChild(imgMap);
}

function SetQuickRate()
{
	if (StateJSON.quickrate == 1 && $('hiddenQuickRate') != null)
		$('hiddenQuickRate').value = 1;
}

