$(document).ready(function() {
	function megaHoverOver(){
		$(this).find(".sub").stop().fadeTo('fast', 1).show();
		(function($) { 
			jQuery.fn.calcSubWidth = function() {
				rowWidth = 0;
				//Calculate row
				$(this).find("ul").each(function() {					
					rowWidth += $(this).width(); 
				});	
			};
		})(jQuery); 
		
		if ( $(this).find(".row").length > 0 ) { //If row exists...
			var biggestRow = 0;	
			//Calculate each row
			$(this).find(".row").each(function() {							   
				$(this).calcSubWidth();
				//Find biggest row
				if(rowWidth > biggestRow) {
					biggestRow = rowWidth;
				}
			});
			//Set width
			$(this).find(".sub").css({'width' :biggestRow});
			$(this).find(".row:last").css({'margin':'0'});
			
		} else { //If row does not exist...
			
			$(this).calcSubWidth();
			//Set Width
			$(this).find(".sub").css({'width' : rowWidth});
			
		}
	}
	
	function megaHoverOut(){ 

	  $(this).find(".sub").stop().fadeTo('fast', 0, function() {
		  $(this).hide(); 
	  });
	}




	$("ul.nav li .sub").css({'opacity':'0'});
	$("ul.nav li .sub").corner();
//	$("#menu").corner();
	$("#main_container").corner();
	$(".inner").corner("round 8px").parent().css('padding', '2px').corner("round 10px")
	$(".prod_message").corner();

	$("ul.nav li").hover(megaHoverOver,megaHoverOut);
	
	$(".nav").lavaLamp({ fx: "backout", speed: 0 })
	$("a[rel=login]").colorbox({
		onComplete:initLogin,
		width:"700px", height:"430px", opacity:0.5});
	
	$("body select").msDropDown();	
	$(".blank").attr("target","_blank");
	$("a[rel='modal']").colorbox();
	$("a[rel='gallery']").colorbox({transition:"fade"});/**/

	
	
});
function initAccount(){
	$("a[rel=info]").colorbox({
		onComplete:initInfo,
		width:"330px", height:"400px", opacity:0.5});
	$("a[rel=pass]").colorbox({
		onComplete:initPass,
		width:"330px", height:"300px", opacity:0.5});
	$("a[rel=pass]").colorbox({
		onComplete:initPass,
		width:"330px", height:"300px", opacity:0.5});
		 
}
function initInfo(){
	$("a[rel=account]").colorbox({
		onComplete:initAccount,
		width:"500px", height:"332px", opacity:0.5});
		var options = { 
				type: 'post',
				success: showProfileResponse,
				dataType:'json'
		};
		$("#edit_frm").validate({
			errorElement: "em",
			rules :{
				firstname: "required",
				lastname: "required",		
				email: {
					required: true,
					email: true
				},
				telephone:{
					required:true,
					minlength:10
				}
			},
			submitHandler: function(form) {
					$(form).ajaxSubmit(options);
			}
	})		
}
function initPass(){
	$("a[rel=account]").colorbox({
		onComplete:initAccount,
		width:"500px", height:"332px", opacity:0.5});
		var options = { 
				type: 'post',
				success: showProfileResponse,
				dataType:'json'
		};
		
}
function initLogin(){			
		$("a[rel=forgotten]").click(function(){
			$(this).colorbox({onComplete:initForggotten,width:"340px",height:"280px"});	
		});
		var options = { 
			type: 'post',
			success: showLoginResponse,
			dataType:'json'
		};
		$("#login_frm").validate({
			errorElement: "em",
			rules :{			
				email: {
					required: true,
					email: true
				},
				password: {
					required:true,
					minlength:5
				}

			},		
			submitHandler: function(form) {
				$(form).ajaxSubmit(options);
			}
		});
		var accountOptions = { 
			type: 'post',
			success: showCreateAccountResponse,
			dataType:'json'
		};
	 
		$("#account").validate({
			errorElement: "em",
			rules :{
				firstname: "required",
				lastname: "required",
				username: {
					required: true,
					minlength: 2
				},
				password: {
					required: true,
					minlength: 5
				},
				confirm_password: {
					required: true,
					minlength: 5,
					equalTo: "#password"
				},
				email: {
					required: true,
					email: true
				},
				telephone:{
					required:true,
					minlength:10
				}
			},
			submitHandler: function(form) {
				$(form).ajaxSubmit(accountOptions);
			}
		})
		
}
function initForggotten(){
	var options = { 
		type: 'post',
		success: showProfileResponse,
		dataType:'json'
	}
	$("a[rel=login]").colorbox({
		onComplete:initLogin,
		width:"700px", height:"430px", opacity:0.5});
	
	$("#forgotten_frm").validate({
		errorElement: "em",
		rules :{			
			email: {
				required: true,
				email: true
			}
		},		
		submitHandler: function(form) {
			$(form).ajaxSubmit(options);
		}
	})
} 


function showProfileResponse(responseText, statusText, xhr, $form) { 
	$(".message").removeClass("warning");
	$(".message").removeClass("success");
	if(String(responseText.error) != "undefined" ){
		$(".message").html(responseText.error);
		$(".message").addClass("warning");
	} else {
		$(".message").html(responseText.success);
		$(".message").addClass("success");
		//parent.document.location.reload();
	}	
	$(".message").css("visibility","visible");
}

function showLoginResponse(responseText, statusText, xhr, $form)  { 
	var status = parseInt(responseText);
	if(String(responseText.error) != "undefined" ){
		$(".warning").html(responseText.error);
		$(".warning").css("visibility","visible");
	} else {
		document.location.href = responseText.href;
	}	
}
function showCreateAccountResponse(responseText, statusText, xhr, $form)  { 
	var status = parseInt(responseText);
	if(String(responseText.error) != "undefined" ){
		$(".warningCreate").html(responseText.error);
		$(".warningCreate").css("visibility","visible");
	} else {
		document.location.href = responseText.href;
	}	
}