
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 : 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);
	}
	,
	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(){
	
	$('#user-email, #user-password').each(function(){
		$(this).data('def', $(this).val());
	
		
		$(this).focus(function()
		{ 
			$(this).val(''); 
			
		});
		$(this).blur(function(){
			if($(this).val().length	== 0) $(this).val($(this).data('def'));
			
		});
	});
	
	$('#languages img').each(function(){
		$(this).data('on', $(this).attr('alt'));
		$(this).data('off', $(this).attr('src'));
		$(this).hover(
		function(){ $(this).attr('src', $(this).data('on')); }, 
		function(){ $(this).attr('src', $(this).data('off')); });	
	});
	$('#content img').each(function(){
		var float = $(this).css('float');
		switch(float){
		case 'left': $(this).addClass('img-left'); break;	
		case 'right': $(this).addClass('img-right'); break;	
		}
	});
	
	$(function()
	{
       $('.scroll-pane').jScrollPane();
	});
	
	
	$('div.content-box img').each(function(){
		var html = '';
		html += '<div class="content-image" style="width:'+$(this).attr('width')+'px;'+$(this).attr('style')+' background-color:transparent;">';
		html += '<img src="'+$(this).attr('src')+'" alt="'+$(this).attr('alt')+'" title="'+$(this).attr('title')+'" width="'+$(this).attr('width')+'px" />';
		html += '<div class="caption"><p>'+$(this).attr('title')+'</p></div>';
		html += '</div>';
		$(this).replaceWith(html);
	});
	
	$('div#margin p img').each(function(){
		var html = '';
		html += '<div class="content-image1" style="width:'+$(this).attr('width')+'px;'+$(this).attr('style')+';">';
		html += '<img src="'+$(this).attr('src')+'" alt="'+$(this).attr('alt')+'" title="'+$(this).attr('title')+'" width="'+$(this).attr('width')+'px" style=" background-color:#eeeeee;" />';
		html += '<div class="caption1"><p>'+$(this).attr('title')+'</p></div>';
		html += '</div>';
		$(this).replaceWith(html);
	});
	
});

