// basic functions for use in all pages
var url;
var date=new Date();
var user;
var ie=0;

if (!Array.indexOf)
{
	Array.prototype.indexOf=function(obj)
	{
		for(var i=0; i<this.length; i++)
		{
			if(this[i]==obj)
			{
				return i;
			}
		}
		return -1;
	}
}

/* if (!Array.remove)
{
	Array.prototype.remove=function(from,to)
	{
		var rest = this.slice((to || from) + 1 || this.length);
		this.length = from < 0 ? this.length + from : from;
		return this.push.apply(this, rest);
	};
} */

function dump(arr,level)
{
	var dumped_text="";
	if (!level) level=0;
	//The padding given at the beginning of the line.
	var level_padding="";
	for (var j=0;j<level+1;j++) level_padding+="    ";
	if (typeof(arr)=='object')
	{ //Array/Hashes/Objects
		for (var item in arr)
		{
			var value=arr[item];
			if (typeof(value)=='object')
			{ //If it is an array,
				dumped_text+=level_padding + "'" + item + "' ...\n";
				dumped_text+=dump(value,level+1);
			}
			else
			{
				dumped_text+=level_padding+"'"+item+"' => \""+value+"\"\n";
			}
		}
	}
	else
	{ //Stings/Chars/Numbers etc.
		dumped_text="===>"+arr+"<===("+typeof(arr)+")";
	}
	return dumped_text;
}

// javascript displays

function miniHouse(house,year)
{
	var html;
	if (!house.img) house.img='images/gallery_thumbs/house.png';
	html='<div class="mini">';
	html+='<div class="img">';
	if (house.icon=='let') html+='<div class="overlay"></div>';
	html+='<a href="house/'+house.id+'" title="View the house profile"><img src="'+url+house.img+'" alt="No photo"/></a></div>';
	html+='<div class="details">';
	html+='<h4><a href="house/'+house.id+'" title="'+house.title+'">'+house.cut+'</a></h4>';
	html+='<p class="shortlist">';
	if (house.list==1) html+='<a href="'+url+'db_delete.php?type=bookmark&amp;id='+house.id+'" class="ico list" title="Remove this house from your shortlist" rel="'+house.id+'">Remove</a>';
	else html+='<a href="'+url+'db_add.php?type=bookmark&amp;id='+house.id+'" class="ico list add" title="Add this house to your shortlist, so you can easily find it in future" rel="'+house.id+'">Shortlist</a>';
	html+='</p></div>';
	html+='<div class="details">';
	html+='<p class="area">'+house.area+'</p>';
	html+='<p class="letting"><span title="This house is available from '+house.let+'" class="ico '+house.icon+'">'+house.result+'</span></p>';
	html+='</div>';
	html+='<div class="details">';
	html+='<p class="beds">'+house.beds+' beds</p>';
	html+='<p class="price" title="&#163;'+house.price+' per person per week">&#163;'+house.price+' pw</p>';
	if (house.bills==1) html+='<p class="ico bills">Bills included</p>';
	html+='</div></div>';
	return html;
}

function miniBusiness(bus)
{
	var html;
	html='<div class="mini">';
	html+='<div class="img"><a href="'+url+'business.php?id='+bus.id+'" title="view this business profile"><img src="'+url+bus.img+'" alt="No photo"/></a></div>';
	html+='<div class="details">';
	html+='<h4><a href="'+url+'business.php?id='+bus.id+'" title="view this business profile">'+bus.title+'</a>';
	if (bus.postcode) html+=', '+bus.postcode;
	html+='</h4></div>';
	html+='<div class="details">';
	if (bus.phone) html+='<p><span class="phone ico">'+bus.phone+'</span></p>';
	html+='<p class="interact">';
	if (bus.email) html+='<a href="mailto:'+bus.email+'" class="ico email" title="email this business">send email</a> ';
	if (bus.website) html+='<a href="'+url+'business.php?id='+bus.id+'" class="ico website" title="view this business profile">view profile</a>';
	html+='</p></div></div>';
	return html;
}

function checkUser()
{
	$.get(url+"ajax_user.php",function(data){
		user=unescape(data);
	});
}

function shortList(link)
{
	if (link.hasClass('add'))
	{
		$.get(url+"ajax_shortlist.php",{type:'add',id:link.attr('rel')},function(response){
			response=unescape(response);
			response=response.split('|');
			if (response[0]!='success')
			{
				alert(response[1]);
			}
		});
		link.fadeOut('fast',function(){
			if (text.length>9) text='Remove from shortlist';
			else text='Remove';
			$(link).text(text).removeClass('add').fadeIn('slow');
		});
	}
	else
	{
		$.get(url+"ajax_shortlist.php",{type:'delete',id:link.attr('rel')},function(response){
			response=unescape(response);
			response=response.split('|');
			if (response[0]!='success')
			{
				alert(response[1]);
			}
		});
		link.fadeOut('fast',function(){
			if (text.length>6) text='Add to shortlist';
			else text='Shortlist';
			$(link).text(text).addClass('add').fadeIn('slow');
		});
	}
}

// inline shortlist function
$.fn.shortList=function(){
	text=this.text();
	that=this;
	if (user!=1)
	{
		var form=loginForm();
		$.blockUI({
			message:form,
			css:{width:'70%',left:'15%',padding:'0 0 5px 10px',color:'#666'}
		}); 
		$('.blockOverlay').css('cursor','pointer').attr('title','Click to cancel login').click($.unblockUI);
		$('form[name="login"]').submit(function(){
			$.post(url+"ajax_login.php",$(this).serialize(),function(response){
				response=unescape(response);
				response=response.split('|');
				if (response[0]!='success')
				{
					alert(response[1]);
				}
				else
				{
					checkUser();
					$('<li><a href="'+url+'account"><span></span>account</a></li>').appendTo('#nav  ul');
					$('#nav p').html('<a href="'+url+'login.php?log=out" class="logout">Log out</a>');
					$.unblockUI();
					//alert('Weve managed to log you in, but something went wrong saving to your shortlist. Please click the "shortlist" link again.');
					shortList(that);
				}
			});
			return false;
		});
	}
	else shortList(this);
	return this;
};

function loginForm()
{
	login='<form action="'+url+'login.php" method="post" name="login">';
	login+='<fieldset class="login align">';
	login+='<label>Email address</label><input type="text" name="email"/><br/>';
	login+='<label>Password<br/><span>(case sensitive)</span></label><input type="password" name="pass"/><br/>';
	login+='<label>Save on this computer?</label><input type="checkbox" name="remember" value="yes"/><br/>';
	login+='<input type="submit" value="log in" class="submit"/>';
	login+='</fieldset>';
	login+='<p class="center"><a href="'+url+'user_forgotten.php" title="click here to recover your password via email">forgotten your password?</a></p>';
	login+='</form>';
	reg='<form action="'+url+'db_add.php" method="post" name="reg"><input type="hidden" name="type" value="register"/>';
	reg+='<p>Enter your email address and we will create your account.</p><p>Your password will be emailed to you to activate your account.</p>';
	reg+='<fieldset class="register align">';
	reg+='<label>Email address</label><input type="text" name="email"/><br/>';
	reg+='<label for="terms">you agree to the <a href="'+url+'legal" title="This will open in a new window" target="_blank">Terms & Conditions</a></label><input type="checkbox" name="terms" value="1"/><br/>';
	reg+='<input type="submit" value="create account" class="submit"/>';
	reg+='</fieldset></form>';
	form='<div class="box login"><div class="box-h"><div class="box-h-r"><h4>Login</h4><br class="clear"/></div></div><div class="box-r">';
	form+=login;
	form+='</div><div class="box-b"><span></span></div></div>';
	form+='<div class="box register"><div class="box-h"><div class="box-h-r"><h4>Register</h4><br class="clear"/></div></div><div class="box-r">';
	form+=reg;
	form+='</div><div class="box-b"><span></span></div></div>';
	return form;
}

// block login box
$.fn.login=function(){
	form=loginForm();
	$.blockUI({
		message:form,
		css:{width:'70%',left:'15%',padding:'0 0 5px 10px',color:'#666'}
	});
    $('.blockOverlay').css('cursor','pointer').attr('title','Click to cancel login').click($.unblockUI);
	return this;
};

$(document).ready(function(){
	$('h1 > a').each(function(){url=$(this).attr('href');});
	checkUser();
	
	$("#tabs > ul").tabs();
	
	$('#noscript').hide();
	if (ie<1) $('.noscript-ie').hide();
	$('.noscript').hide();
	
	$.blockUI.defaults.css.cursor='';
	
	$('ul.check li label').click(function(e){
		$(this).prev('input').check();
		$(this).toggleClass('selected');
	}).addClass('pointer').each(function(){
		if ($(this).prev('input').attr('checked') == true){
			$(this).addClass('selected');
		}
	});
	
	$('a.list').click(function(e){
		$(this).shortList();
		e.preventDefault();
	});
	
	$('input.uppercase').change(function(){
		$(this).val($(this).val().toUpperCase());
	});
	
	$('a.jslogin').click(function(e){
		$(this).login();
		e.preventDefault();
	});
	
	$("input[type='checkbox']").addClass('small');
	$("input[type='radio']").addClass('small');
});