/*
// MAFOCUS
// common.js
*/

function doPopups()
{
if (!document.getElementsByTagName) return false;
	var anchors = document.getElementsByTagName('a');
	for (var i=0; i<anchors.length; i++){
		var anchor = anchors[i];
		var relAttribute = String(anchor.getAttribute('rel'));
		if (anchor.getAttribute('href') && (relAttribute.toLowerCase().match('popup'))){
			anchor.onclick = function () {window.open(this.href); return false;}
		}
	}
}

$(document).ready(function() {
						   
	function validate(form) {
		
		var text = '';
		var passwordvalue = '';
		var value;
		var id;
		var validationStatus = 0;
		
		$(form).children().each(function() {
			value = $(this).children('input').val();
			id = $(this).children('input').attr('id');
			
			if(value == '') {
				if(id=='username') {
					text += 'Please enter a username<br />';
				}
				else if(id=='fullname') {
					text += 'Please enter your full name<br />';
				}
			}
			
			// Email input specific checks			
			if(id=='email') {
				var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
				
				if(value == '') {
					text += 'Please enter your email address<br />';
				}
				else if(value != '' && !pattern.test(value)) {
					text += 'Please enter a valid email address<br />';	
				}
			}
			
			
			// Email input specific checks	
			if(id=='password') {
				passwordvalue = value;
				if(value == '') {
					text += 'Please enter a password<br />';
				}
				else if(value != '' && value.length < 6) {
					text += 'The password is too short<br />';					
				}
			}
			
			if(id=='confirmpassword') {
				if(value == '') {
					text += 'Please confirm your password<br />';
				}
				else if(value != '' && value != passwordvalue) {
					text += 'The password fields do not match<br />';	
				}
			}			
			
		});
		
		$('.validation').html(text);
		
		//return false;
	}
	
	//Launch links in new window where rel="popup"
	doPopups();
					   			   
	//$('#register').hide();					   
	//$('.registerlink').append('<a href="javascript:;">Register</a>');
	//$('.signinlink').append('<a href="javascript:;">Sign In</a>');
	
	//$('.registerlink a').click(function() {
	//	$('#signin').fadeOut('fast', function() {
	//		$('#register').fadeIn('fast');									
	//											});
	//	$('.signinheading').html("Register");
	//	Cufon.replace('.signinheading');
	//});
	
	$('form').submit(function() {
		return validate(this);
	});
	
	//$('.signinlink a').click(function() {
	//	$('#register').fadeOut('fast', function() {
	//		$('#signin').fadeIn('fast');									
	//											});
	//	$('.signinheading').html("Sign In");
	//	Cufon.replace('.signinheading');
	//});

	if($('#imagelist .preview').length != 0) {
		$('#imagelist .preview').lightBox();
	}
	
	if($('#advertlist .preview').length != 0) {
		$('#advertlist .preview').lightBox();
	}

	if($('.latest-pr').length != 0) {
		$(".latest-pr").accordion();
	}

	$("ul.menu").superfish({ 
		pathClass:'current',
		autoArrows:false
	});
	
	$("#ajaxSearch_input").focus(function() {
		if( this.value == this.defaultValue ) {
			this.value = "";
		}
    }).blur(function() {
		if( !this.value.length ) {
			this.value = this.defaultValue;
		}
	});
	
	if ($('.product-listing').length > 0) {
		//Load colour plug-in for highlight fade
		$.getScript("assets/templates/mafocus/js/jquery.color.js");
		$('.product-listing .product-thumb').css({opacity: 0.5});
		
		var bodyid=$('body').attr('id');
		
		if (bodyid == 'products') {
			
			$('.product-listing li a').hover(
				function () {
					$(this).next('div').fadeTo("normal", 1);
					$(this).children('strong').animate({color:'#eee'}, 600);
				},
				function () {
		  			$(this).next('div').fadeTo("normal", 0.5);
					$(this).children('strong').animate({color:'#ddd'}, 600);
			});
		} else {
			$('.product-listing li a').hover(
				function () {
					$(this).next('div').fadeTo("normal", 1);
					$(this).children('strong').animate({color:'#000'}, 600);
				},
				function () {
		  			$(this).next('div').fadeTo("normal", 0.5);
					$(this).children('strong').animate({color:'#666'}, 600);
			});
		}
	}
	
	$('a.preview').css({'opacity': '0.0', 'top':'30px'});
	$('a.download').css({'opacity': '0.0', 'bottom':'30px'});
	$('.image-overlay').css({'display' : 'block', 'opacity': '0.0'});

	$('#imagelist li').hover(function () {
		$(this).css({cursor : 'pointer'});
		$('a.preview').animate({'top': '50px', 'opacity': '0.8'}, 200);
		$('a.download').animate({'bottom': '50px', 'opacity': '0.8'}, 200);
		$(this).children('p').children('.image-overlay').animate({'opacity': '0.4'}, 200);
	},
	function () {
		$('a.preview').css({'top': '30px', 'opacity': '0.0'});
		$('a.download').css({'bottom': '30px', 'opacity': '0.0'});
		$(this).children('p').children('.image-overlay').animate({'opacity': '0.0'}, 200);
	});
	
	
	$('#advertlist li').hover(function () {
		$(this).css({cursor : 'pointer'});
		$('a.preview').animate({'top': '50px', 'opacity': '0.8'}, 200);
		$('a.download').animate({'bottom': '50px', 'opacity': '0.8'}, 200);
		$('a.downloadb').animate({'bottom': '72px', 'opacity': '0.8'}, 200);
		$(this).children('p').children('.image-overlay').animate({'opacity': '0.4'}, 200);
	},
	function () {
		$('a.preview').css({'top': '20px', 'opacity': '0.0'});
		$('a.download').css({'bottom': '20px', 'opacity': '0.0'});
		$('a.downloadb').css({'bottom': '42px', 'opacity': '0.0'});
		$(this).children('p').children('.image-overlay').animate({'opacity': '0.0'}, 200);
	});

});
