
fn = {}

fn.message = function(str, cla)
{	$message = $('#message');
	if(!cla) cla = 'default';
	$message.hide();	
	$message.html('<div class="message"><div class="'+cla+'">'+str+'</div></div>');
	$message.fadeIn();
	setTimeout('$message.fadeOut();', '8000');
}

fn.check_form = function(elem, mem)
{
	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;
	var str = "Please amend the following before continuing:\n"+errors;
	if(mem) fn.mem('<div class="error">'+str+'</div>');
	else alert(str);
	
	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; 
	}
}

jQuery.fn.extend({
	tabs_old : function(clk)
	{
		var group = "#"+this.attr('id');
		$(group+" div.tab").hide();
		$(group+" div.tabs>h1>a").click(function(){
			$(group+" div.tabs>h1>a").removeClass('active');
			var href = $(this).attr('href');	
			$('a[href="'+href+'"]').addClass('active');
			$(group+" div.tab").hide();
			$(group+" div#"+href).show();
			return false;
		});
		if(clk) $(group+" div.tabs>h1>a[href='"+clk+"']").click();
		else $(group+" div.tabs>h1>a:first").click();
		return $(this);
	}
	,
	tabs : function()
	{
		return this.each(function(i, elem){
			$('.tab').hide();
			$(this).find('.tabs a').click(function(){
				$('.tab').hide();
				$('.tabs a').removeClass('active');
				var href = $(this).attr('href');
				var ps = href.split('#');
				if(ps.length==1) href = ps[0];
				else href = ps[1];
				$(this).addClass('active');
				$('#'+href).show();
				return false;
			});	
		});
		
	}
	,
	checkboxes : function()
	{
		var parent = "#"+this.attr('id');
		$(parent+' a[class="checkbox"]').each(function(){
		$(this).click(function(){
			var elem_id = '#'+$(this).attr('title');
			if($(elem_id).attr('checked')) $(elem_id).attr('checked', false);
			else $(elem_id).attr('checked', true);
			return false;
		});
		});	
		return $(this);
	}
	,
	buttons : function()
	{
		var parent = "#"+this.attr('id');
		$(parent+' a[class="button"], a[class="button-small"]').each(function(){
		$(this).prepend('<img src="'+fn.ext+fn.media+'icons/'+$(this).attr('title')+'.png" /> ');	
		if($(this).attr('href').length < 2) { $(this).click(function(){ return false; }); $(this).css('cursor', 'default'); }	
		});	
		return this;
	}
	,
	radios : function()
	{
		var parent = "#"+this.attr('id');
		$(parent+' a[class="radio"]').each(function(){
		$(this).click(function(){
			var elem_id = '#'+$(this).attr('title');
			if($(elem_id).attr('checked')) $(elem_id).attr('checked', false);
			else $(elem_id).attr('checked', true);
			return false;
		});
		});	
		return $(this);
	}	
});

$(document).ready(function(){
	$('#intro1 img, #accommodation1 img').each(function(){
		var src = $(this).attr('src');
		var title = $(this).attr('title');	
		var width = ($(this).css('width'));
		width = parseInt(width.replace(/px$/, ''));
		width = parseInt(width - 22);
		var html = '<div class="img-container"><img src="'+src+'" /><div class="img-title" style="width:'+width+'px">'+title+'</div></div>';
		$(this).replaceWith(html);
	});
	
	$('#intro img, #accommodation img').each(function(){
		var title = $(this).attr('title');
		var src = $(this).attr('src');
		var width = ($(this).css('width'));
		var left = ($(this).css('left'));
		width = parseInt(width.replace(/px$/, ''));
		left = parseInt(left.replace(/px$/, ''));
		var html = '<img src="'+src+'" /><div style="position:absolute; top:10px; left:'+left+'px; border:1px solid red; width:100px; height:100px; z-index:100">'+title+'</div>';
		$(this).replaceWith(html);
	});	
});


