$(document).ready(function(){
	if ($("#divOverlay").length) {
		$("form").find("input:text,textarea,select").each(function() {	
			$(this).hideLabels();
			$("#divForm ul li span.text input").width("360px");
		});
		
		$("form").find("input:radio,input:checkbox").each(function() {
			$(this).customInput();
		});
		
		$("#divIam span.title").css("line-height","28px");
		
		$('#divOverlay a.goback').click(function(e) {
			if (parent) {
				e.preventDefault();
				parent.closeBox();
			}	
		});
		
		$('#divOverlay a.redirect').click(function(e) {
			if (parent) {
				e.preventDefault();
				parent.redirect($(this).attr("href"));
			}	
		});
		
		$("#divOverlay.register a").click(function(e){
			if (self != top) {				
				e.preventDefault();
				parent.openWindow($(this).attr("href"));
			}
		});
		
		if ($('#divOverlay').hasClass("thanks")) {
			createCookie("cs-box-reg", "1", 30);
		}
		
		if ($("#divOverlay form").length) {		
			$.extend(jQuery.validator.messages, {
				required: "Fill the required field",
				noLabelTxt: "Fill the required field"
			});
			
			jQuery.validator.addMethod("noLabelTxt", function(value, element) {	
				var label = $(element).parent().parent().parent().parent().find("label");
				return  this.optional(element) || (value != label.html());
			});
			
			$("#divOverlay form").validate({
				event: "keyup",
				messages:{
					role: "Please select a role",
					email: "Please enter a valid email address"
				},
				rules: {
					role: {required: true},
					email: {noLabelTxt: true, email: true}			
				},
				errorPlacement: function(error, element) {
					$(error).attr("title",$(error).text());
					$("#divOverlay form .errorWrapper").append(error);
				},
				submitHandler: function(form) {		
					$("#divContent form input:submit").attr('disabled', 'disabled').val("Sending...");
					if ($("input[name=role]:checked").val() == "jobseeker") {
						//$(form).attr("action","redirect.html");
						redirect("redirect.html");
					}
					else {
						form.submit();
					}					
				},
				success: function(label) {
					label.remove();
				}
			});
			
			$("#nomore").click(function(){
				if ($(this).is(':checked')) {
					createCookie("cs-box-reg", "1", 30);
				}
			});
		}
		
		$('a[rel="_blank"],a.-blank').click(function(e) {
			this.target = "_blank";
		});
	}
	else 
	if (!readCookie("cs-box-reg")) {
		setTimeout("openBox()",1000);
	}
	
});

function openWindow(url){
	window.open(url, "_blank");
}

function openBox() {
	var auxurl = "form.html";
	$.fn.colorbox({'href':auxurl, open:true, iframe:true, height:'500px',width:'920px',opacity:'0.5'});
}

function closeBox() {
	$.colorbox.close();
}

function redirect(url) {
	location.href = url;
}

function createCookie(name, value, days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
		var expires = "; expires=" + date.toGMTString();
	}
	else var expires = "";
	document.cookie = name + "=" + value + expires + "; path=/";
};

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for (var i = 0; i < ca.length; i++) {
		var c = ca[i];
		while (c.charAt(0) == ' ') c = c.substring(1, c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
	}
	return null;
};

function eraseCookie(name) {
	createCookie(name, "", -1);
};

jQuery.fn.hideLabels = function() {
	var input = $(this);
	var label = input.parent().parent().find("label");
	label.hide();
	input.val(label.html());

	if (!input.is("select")) {
		input.focus(function(){
			if (input.val() == label.html()) input.val("");
		}).blur(function(){
			if (input.val() == "") input.val(label.html());
		});
	}
	else {
		input.prepend("<option value=''>"+label.html()+"</option>").get(0).selectedIndex = 0;
	}	
	return input;
};
