$(document).ready(function(){ 
	
	$("#typeSelectorRow").hide();
	
	validateForms();
	toggleCompany();
	$("a.terms[rel]").overlay({
				  start:
				  { 
				  		top: "center",
				  		left: "center" 
				  },
    			  finish:
				  { 
				  		top: "center",
				  		left: "center"
				  }	
	});
	
	
	$('body').click(function(event)
	{	
		if ($(event.target).is(".beta"))
		{		
			event.preventDefault();
			alert("We are currently in beta, please check back later");
		}
		if ($(event.target).is("#displayFilter, #thumbFilter"))
		{		
			event.preventDefault();
			var filter = $(event.target).attr("rel");
			setDisplayFilter(filter);
		}		
	});
	
    $("#accType").change(function() 
    { 
       toggleCompany(); 
    }); 
        
    $("#displayFilterMenu, #thumbsFilterMenu").hover(function ()
    {
    	$(this).find("ul").show();    	
    }, function ()
    {
        $(this).find("ul").hide(); 
    });
    
    $("table#clipsSmall tr").hover(function ()
    {
    	$(this).css("background-color","rgb(240,240,240)");    	
    }, function ()
    {
        $(this).css("background-color","white"); 
    });
	$('input#contactName, input#contactMail, input#contactSubject, textarea#contactMessage, input#searchTop, input.search_field').addClass("idleField");
	$('input#contactName, input#contactMail, input#contactSubject, textarea#contactMessage, input#searchTop, input.search_field').focus(function()
	{  	
			$(this).removeClass("idleField").addClass("focusField");
			if (this.value == this.defaultValue)
			{  
				this.value = '';  
			}  
			if(this.value != this.defaultValue){  
				this.select();  
			} 
	});  
	$('input#contactName, input#contactMail, input#contactSubject, textarea#contactMessage, input#searchTop, input.search_field').blur(function()
	{  
			if ($.trim(this.value) == ''){  
				this.value = this.defaultValue;
				$(this).removeClass("focusField").addClass("idleField"); 
			} 			
	}); 
	
	$.validator.addMethod("defaultInvalid", function(value, element)
	{
		return value != element.defaultValue;
	}, "This field is required!");
	
 	$.validator.addMethod("url", function( value, element ) {
 			var result = this.optional(element) || value.length >= 5 && /^[a-zA-Z0-9_]+$/i.test(value);
			if (!result) {
				//element.value = "";
			}
			return result;
	}, "This must be at least 5 characters long and must not contain other character than a-z, 0-9 and _ (underscore).");

});


$(function(){
    $('input#searchTop').keydown(function(e){
        if (e.keyCode == 13) {
            $(this).parents('form').submit();
            return false;
        }
    });
});

function validateForms()
{	
	$("#registerForm, #loginForm, #resetPassword, #contactForm").validate(
	{
		errorElement: "div",
		//errorLabelContainer: $("#warning"),
		errorPlacement: function(error, element)
		{
			//element.after(error);
			error.appendTo( element.next("div") );
		},
		rules:
		{
			usrName:
			{
				required: true,
				minlength: 3				
			},
			usrSurname:
			{
				required: true,
				minlength: 3				
			},			
			usrMail:
			{
				required: true,
				email: true
			},
			usrHandle:
			{
				//defaultInvalid: true,
				required: true,
				//minlength: 5
				url: true
			},			
			usrPwd:
			{
				required: true,
				minlength: 5
			},
			usrPwdCon:
			{
      			equalTo: "#usrPwd"
			},
			usrMsg:
			{
				defaultInvalid: true,
				required: true,
				minlength: 5				
			},
			about:
			{
				defaultInvalid: true,
				required: true			
			},
			priority:
			{
				defaultInvalid: true,
				required: true			
			},
			terms:
			{
				required: true			
			}			
		},
		messages:
		{
			usrName:
			{
				required: "Name is required!",
				minlength: "This must be at least 3 characters long!"
			}
			,
			usrSurname:
			{
				required: "Surname is required!",
				minlength: "This must be at least 3 characters long!"
			}
			,
			usrMail:
			{
				required: "Mail is required!",
				email: "This must be a valid email-address!"
			}
			,
			usrHandle:
			{
				required: "This is required!",
				minlength: "This must be at least 5 characters long!"
			},
			usrPwd:
			{
				required: "Password is required!",
				minlength: "This must be at least 5 characters long!"
			},
			usrPwdCon:
			{
				equalTo: "Does not match password!"
			},
			terms:
			{
				required: "This is required!"
			}
		}
	});		
}
function setDisplayFilter(filter)
{
	$.post("/connectors/ajax/setDisplayFilter.php", { filter: filter }, function(data){
	  	window.location.reload(true);
	});	
}
function toggleCompany()
{
	var type = $("#accType").val();
	//alert(type);
	if(type == "freelanceA" || type == "freelanceB" || type == "trial")
	{
		$("#companyName").hide();
		if(type == "trial")
		{
			$(".trialHide").hide();
			$(".paypal").css('background-position', '0 10000px');
		}else{
			$(".trialHide").show();
			$(".paypal").css('background-position', '14px 8px');
		}
	}else{
		$("#companyName").show();
		$(".trialHide").show();
		$(".paypal").css('background-position', '14px 8px');		
	}
}
function loadThumbs()
{
	var clipLinks = $('.reelLogo');
	var featuredLinks = $('.fThumb');
	
	$.each(clipLinks,function(i,item)
	{	
		var thumb = $(item).attr('rel');
		//alert(thumb);
		var img = new Image();
	  	// wrap our new image in jQuery, then:
		$(img).load(function ()
	    {
	    	var m = 0;
	    	 h = img.height;
	    	 w = img.width;
	    	 r = (h/w) * 10;
	    	if(h < 103)
	    	{
	    		m = 1 + (103 - h) / 2;	
	    	}
	    	if(r > 10)
	    	{
	    		m = 0;
	    	}
	    	//alert(m);
			$(this).hide();
			
	    	$(item).html(this);
	    	
	    	//$(item).find('img').attr('width', '100px');
	    	//$(item).find('img').attr('style', 'margin-top:'+m+'px;');
			$(this).fadeIn();
			$(this).attr('style', 'margin-top:'+m+'px;');
	    })
		.error(function ()
		{
	      // notify the user that the image could not be loaded
	    })
	    .attr('src', thumb).attr('width', '155');
	    
	});

	$.each(featuredLinks,function(i,item)
	{	
		var thumb = $(item).attr('rel');
		//alert(thumb);
		var img = new Image();
	  	// wrap our new image in jQuery, then:
		$(img).load(function ()
	    {
	    	var m = 15;
	    	var h = img.height;
	    	if(h < 103)
	    	{
	    		m = 1 + (103 - h) / 2;	
	    	}
	    	//alert(m);
			$(this).hide();
			
	    	$(item).html(this);
	    	
	    	//$(item).find('img').attr('width', '100px');
	    	//$(item).find('img').attr('style', 'margin-top:'+m+'px;');
			$(this).show();
			$(this).attr('style', 'margin:'+m+'px 8px;');
	    })
		.error(function ()
		{
	      // notify the user that the image could not be loaded
	    })
	    .attr('src', thumb).attr('width', '140');
	    
	});	

}
