var curDropdown;
var curDropdownParent;
var createMessageSendTo;


function vIE(){return (navigator.appName=='Microsoft Internet Explorer')?parseFloat((new RegExp("MSIE ([0-9]{1,}[.0-9]{0,})")).exec(navigator.userAgent)[1]):-1;}

$(document).ready(function() {
	$('input[type=radio]').prettyCheckboxes({display:"inline"});

	if (vIE() != 6) { //No internet exploder 6!
		$('input[type=checkbox]').prettyCheckboxes({display:"inline"});
	}
	
	$("#mlfEmail, #mlfPass").focus(function(){
		if ($(this).attr("id") == "mlfEmail") var defaultVal = "email";
		if ($(this).attr("id") == "mlfPass") { var defaultVal = "password"; }
		
		if ($(this).val() == defaultVal) {
			$(this).val("");
		}
	});
	
	$("#mlfEmail, #mlfPass").blur(function(){
		if ($(this).attr("id") == "mlfEmail") var defaultVal = "email";
		if ($(this).attr("id") == "mlfPass") var defaultVal = "password";
		
		if ($(this).val() == "") {
			$(this).val(defaultVal);
		}
	});
	
	$(".displayTable tr").mouseenter(function(){
		$(this).children("td").children(".displayTableRO").show();
	});
	$(".displayTable tr").mouseleave(function(){
		$(this).children("td").children(".displayTableRO").hide();
	});
	
	$(".countrydropdown").change(function() {
		var selected = $(".countrydropdown option:selected").val();
		if (selected == "GB") {
			$(".countydropdown").show();
		} else {
			$(".countydropdown").hide();
		}
	});
	
	var selected = $(".countrydropdown option:selected").val();
	if (selected == "GB") {
		$(".countydropdown").show();
	} else {
		$(".countydropdown").hide();
	}
});

function showEdit(thisObj) {
	$(thisObj).parent().parent().children(".profileEdit").show();
	$(thisObj).parent().parent().children(".profileDisplay").hide();
}

function showEditAll() {
	$(".profileDisplay").each(function(){
		$(this).parent().children(".profileEdit").show();
		$(this).parent().children(".profileDisplay").hide();
	});
}


function saveProfile(section) {
	if (section == "general") {
		var postData =
			"&forename=" + encodeURIComponent($("#UserForename").val()) +
			"&middlename=" + encodeURIComponent($("#UserMiddlename").val()) +
			"&surname=" + encodeURIComponent($("#UserSurname").val()) + 
			"&businessAs=" + encodeURIComponent($("#UserBusinessAs").val()) +
			"&gender=" + encodeURIComponent($("[name='data[User][gender]']:checked").val()) +
			"&dobD=" + encodeURIComponent($("#UserDateOfBirthDay").val()) + 
			"&dobM=" + encodeURIComponent($("#UserDateOfBirthMonth").val()) +
			"&dobY=" + encodeURIComponent($("#UserDateOfBirthYear").val()) +
			"&country=" + encodeURIComponent($("#UserCountry").val()) +
			"&county=" + encodeURIComponent($("#UserCounty").val()) +
			"&creativeField_1=" + encodeURIComponent($("#UserCreativeField1").val()) +
			"&creativeField_2=" + encodeURIComponent($("#UserCreativeField2").val()) +
			"&creativeField_3=" + encodeURIComponent($("#UserCreativeField3").val()) +
			"&nameShow=" + $("[name='data[User][nameShow]']:checked").val() +
			"&genderShow=" + $("[name='data[User][genderShow]']:checked").val() +
			"&businessAsShow=" + $("[name='data[User][businessAsShow]']:checked").val() +
			"&dateOfBirthShow=" + $("[name='data[User][dateOfBirthShow]']:checked").val() +
			"&locationShow=" + $("[name='data[User][locationShow]']:checked").val()
			;
	} else if (section == "education") {
		var postData = 
			"&edu=" + encodeURIComponent($("#UserEdu").val()) +
			"&eduCourse=" + encodeURIComponent($("#UserEduCourse").val()) +
			"&eduQualification=" + encodeURIComponent($("#UserEduQualification").val()) +
			"&eduShow=" + $("[name='data[User][eduShow]']:checked").val() +
			"&eduCourseShow=" + $("[name='data[User][eduCourseShow]']:checked").val() +
			"&eduQualificationShow=" + $("[name='data[User][eduQualificationShow]']:checked").val()
	} else if (section == "contact") {
		var postData = 
			"&phone=" + encodeURIComponent($("#UserPhone").val()) +
			"&website=" + encodeURIComponent($("#UserWebsite").val()) +
			"&phoneShow=" + $("[name='data[User][phoneShow]']:checked").val() +
			"&emailShow=" + $("[name='data[User][emailShow]']:checked").val() +
			"&websiteShow=" + $("[name='data[User][websiteShow]']:checked").val()
	} else if (section == "about") {
		var postData = 
			"&about=" + encodeURIComponent($("#UserAbout").val()) +
			"&aboutShow=" + $("[name='data[User][aboutShow]']:checked").val()	
	}
	$.ajax({
		type: "POST",
		url: "/ajax/user/saveprofile/",
		data: "section=" + section + postData,
		success: function(msg) {
			var data = eval("(" + msg + ")");

			if (data['view']) {
				$("#profileEdit_" + section).hide();
				$("#profileDisplay_" + section).html(data['view']);
				$("#profileDisplay_" + section).show();
			} 
			if (data['form']) {
				$("#profileEdit_" + section).html(data['form']);
				$('#profileEdit_' + section + ' input[type=radio]').prettyCheckboxes({display:"inline"});
			}
		}
	});
	
	return false;
}

$(document).click(function(e) {
	var target = (e && e.target) || (event && event.srcElement); 
	var jTarget = $(target);
	if (
		jTarget.attr("class") != "menuDropdownContainer" &&
		jTarget.attr("class") != "menuDropdownMain"
		) {
		if (curDropdown) {
			$("#main_dropdown_" + curDropdown).css("display", "none");
			$(curDropdownParent).removeClass("mainMenuActive");
			$(curDropdownParent).addClass("mainMenuInactive");
			curDropdown = 0;
		}
	}
});

function showMenu(thisObj, menuID) {	
	if (curDropdown) {
		$("#main_dropdown_" + curDropdown).css("display", "none");
		$(curDropdownParent).removeClass("mainMenuActive");
		$(curDropdownParent).addClass("mainMenuInactive");
	}
	
	if (menuID != curDropdown) {
		var pos = $(thisObj).position();
		pos.top = pos.top+38;
		if (menuID == "howitworks") {
			$("#main_dropdown_" + menuID).css("left", pos.left - 42);
		} else {
			$("#main_dropdown_" + menuID).css("left", pos.left);
		}
		$("#main_dropdown_" + menuID).css("top", pos.top+1);
		$("#main_dropdown_" + menuID).css("display", "block");
	
		$(thisObj).parent().removeClass("mainMenuInactive");
		$(thisObj).parent().addClass("mainMenuActive");
	
		curDropdownParent = $(thisObj).parent();
	
		curDropdown = menuID;
	} else {
		curDropdown = "";
	}
	
	return false;
}

function openSendMessage() {
	$("#MessageSubject").val("");
	$("#MessageMessage").val("");
	$("#createmessage_status").html("");
	
	$("#createmessage_contacts").show();
	$("#createmessage_form_stage").hide();
}

function openReplyMessage(msg) {
	openSendMessage();

	var id		= msg["sender_id"];
	var name	= msg["sender"];
	var subject	= msg["subject"];
	
	$("#createmessage_form_tolist").html(name);
	$("#MessageSendto").val(id);
	$("#MessageSubject").val("RE: " + subject);
	$("#createmessage_contacts").hide();
	$("#createmessage_form_stage").show();
}

function showSendMessageForm(contactsData) {
	createMessageSendTo = ""; //Reset this incase something goes wrong.
	var namesBuffer = ""
	var numNames = 0;
	$(".sendto:checked").each(function(){
		var userID = $(this).attr("id");
		userID = userID.split("_");
		userID = userID[1];
		namesBuffer = namesBuffer + contactsData[userID] + ", ";
		createMessageSendTo = createMessageSendTo + userID + "|";
		numNames = numNames + 1;
	});
	namesBuffer = namesBuffer.substring(0, namesBuffer.length - 2);
	createMessageSendTo = createMessageSendTo.substring(0, createMessageSendTo.length - 1);
	if (numNames == 0) {
		$("#createmessage_contacts_status").html("Please select at least one contact!");
	} else {
		$("#createmessage_form_tolist").html(namesBuffer);
		$("#MessageSendto").val(createMessageSendTo);
		$("#createmessage_contacts").hide();
		$("#createmessage_form_stage").show();
	}
}

function appreciate(id) {
	$.ajax({
		type: "POST",
		url: "/portfolio/appreciate/" + id,
		success: function(msg) {
			var msgSplit = msg.split("|");

			if (msgSplit[0] == "success") {
				var parent = $("#appreciated_" + id).parent();
				parent.html("Appreciated " + msgSplit[1]);
				parent.removeClass("portfolio_appreciate");
				$("#appreciation_dynamic").html(parseInt($("#appreciation_dynamic").html()) + 1);
			} else {
				alert("There was a problem appreciating this. Please try again later.");
			}
		}
	});
}

function addToContacts(id, clickObj) {
	$.ajax({
		type: "GET",
		url: "/ajax/contacts/addcontact/" + id + "/",
		success: function(msg) {
			if (msg == "success") {
				$(clickObj).html("Friend request sent");
				$(clickObj).removeAttr("href");
				$(clickObj).removeAttr("onclick");
			} else {
				$(clickObj).html("Could not send friend request");
			}
		}
	});
}

function contest_extend(id) {
	$.ajax({
		type: "GET",
		url: "/ajax/contest/extend/" + id + "/",
		success: function(msg) {
			tb_remove();
			window.location = window.location;
		}
	});
}

function contest_refund(id) {
	$.ajax({
		type: "GET",
		url: "/ajax/contest/refund/" + id + "/",
		success: function(msg) {
			tb_remove();
			window.location = window.location;
		}
	});
}
