var fn = { 
	url:'',
	page: 0,
	area: 0,
	town:0,
	type:0,
	category:0,
	special:0
 }

var ds = '/';
var ext = '/';
var hy = 'http://www.hello-yorkshire.co.uk';
var media = hy+'/media/';
var working = '/media/images/working.gif';
var handle = 'handle';
var tt = 'http://www.hello-gb.co.uk/tourism/templates/';
var ti = 'http://www.hello-gb.co.uk/tourism/media/images/';
var tl = 'http://www.hello-gb.co.uk/tourism/lib/';

fn.check_form = function(elem)
{
	var elem_id = $(elem).attr('id');
	
	var errors = "";
	
	$("form[id='"+$(elem).attr('id')+"'] input[accept='true'], form[id='"+$(elem).attr('id')+"'] textarea[accept='true']").each(function(){
		$(this).removeClass('error');
		var mask = $(this).attr('mask');
		
		switch(mask)
		{
		case "email":
			if(!fn.is_email($(this).val())) 
			{
				errors += $(this).attr('alt')+"\n";
				$(this).addClass('error');
				$(this).focus(function(){$(this).removeClass('error');});
			}	
		break; 
		default:
			if($(this).val().length < 1) 
			{
				errors += $(this).attr('alt')+"\n";
				$(this).addClass('error');
				$(this).focus(function(){$(this).removeClass('error');});
			}	
		break;
		}
		
	});
	
	
	if(errors.length < 1) return true;
	alert("Please amend the following before continuing:\n"+errors);
	
	return false;
}

fn.is_email = function(email)
{
	//var email = email.value;
	//var email = document.forms[target].elements[field].value;
	var atSym = email.indexOf('@');
	var dot = email.lastIndexOf('.');
	var space = email.indexOf(' ');
	var len = email.length;
	if (atSym < 1 || dot < atSym || len - dot <= 2 || space != -1) {
		return false;
	}
	else { 
		return true; 
	}
}

fn.encode = function(str) {
		var result = "";
		for (i = 0; i < str.length; i++) {
			if (str.charAt(i) == " ") {
				result += "+";
			}
			else {
				result += str.charAt(i);
			}
		}
		return escape(result);
}

fn.decode = function(str) {
		var result = str.replace(/\+/g, " ");
		return unescape(result);
}

fn.mem = function(mem)
{
	$('div#mem').fadeIn();
	$('div#mem').html(mem);
	//fn.flash('div#mem');
	setTimeout("$('div#mem').fadeOut()", 3000);	
}

fn.flash = function(elem)
{
	$(elem).fadeIn(100).fadeOut(100).fadeIn(100).fadeOut(100).fadeIn(100);	
}

fn.confirm = function(conf, url)
{
	if(confirm(conf)) location.href = url;
}

fn.random = function()
{
	var random_no = Math.floor(Math.random()*1000000);
	return random_no;
}

fn.media = function()
{
	/*
	var $advert = $('#advert-media');
	var $comment = $('#advert-media-comment');
	var $border = $('#advert-media-border');
	$('div.advert-media-thumbs>a>img').each(function(){
		var src = $(this).parent().attr('alt');	
		var title = $(this).attr('title');
		$(this).parent().attr('href', '#');
		$(this).parent().click(function(){
			$('div.advert-media-thumbs>a').removeClass('active');
			$(this).addClass('active');
			$advert.attr('src', working);
			$advert.attr('src', hy+'/'+src);
			$comment.css({width:$advert.attr('width')-20});
			$comment.hide();
			$comment.html(title);
			$comment.fadeIn();
			$border.css({width:$advert.attr('width')});
			return false;
		});
		$('div.advert-media-thumbs>a>img:first').click();
	});
	*/
	var $advert = $('#advert-media');
	var $comment = $('#advert-media-comment');
	var $border = $('#advert-media-border');
	var c = 0;
	$('div.advert-media-thumbs>a>img').each(function(){
		$(this).parent().attr('href', '#');
		var img = $(this).attr('src');
		var title = $(this).attr('title');
		$(this).parent().click(function(){
			$('div.advert-media-thumbs>a').removeClass('active');
			$(this).addClass('active');	
			$advert.attr('src', working);
			$advert.attr('src', img);
			$comment.css({width:$advert.attr('width')-20});
			$comment.hide();
			$comment.html(title);
			$comment.fadeIn();
			$border.css({width:$advert.attr('width')});
			return false;
		});
		c++;
		
	});
	$('div.advert-media-thumbs>a>img:first').click();
}

fn.pages = function(page)
{
	this.page = page;
	if(this.page == 0) var $pages = $('#pages');
	else var $pages = $('#pages'+this.page);
	var $more = $('#more');
	if(this.page != 0)	
	{
		$pages.html('<img src="'+working+'" /> working...');
		$more.hide();
		var params = { p: this.page, in_url:this.url }
		$.get(ext+"handle/adverts", params, function(data){ 
			var html = data; 
			html += '<div id="pages'+(fn.page+1)+'">&nbsp;</div>';
			$pages.html(html); 
			$more.html('<a href="javascript:fn.pages('+(fn.page+1)+');void(0);" class="pink-btn-results">Show next page of results...</a>');
			$more.fadeIn();
			$('p.address').hide();
		});
	}
	else
	{
		$pages.html('<div id="pages1">&nbsp;</div>');
		$more.html('<a href="javascript:fn.pages(1);void(0);" class="pink-btn-results">Show next page of results...</a>');
	}
}

$(document).ready(function(){
	fn.pages(0);
	//$('p.address').hide();
	$('#q').focus(function(){
		$(this).val(' ');
	});
	$('#qs-form-btn').click(function(){
		$('#qs-form').submit(); return false;
	});
	//$('div#header').css({background:'#ffffff url(/media/images/homepage/banner.jpg) bottom no-repeat'});
});


