var popupStatus = 0;
function addToProfile(value,type) {
	//var nvalue = $('<div />').text(value).html();
	//alert(nvalue);
	var dataSend = "value="+value+"&t="+type;
	var mesg = $.ajax(
	{
		type:"POST",
		url:"/profile/add",
		data:dataSend,
		async:false
	}).responseText;
	if (mesg < 0) {
		displayLogin(value,type);
		//document.location = '/login/';
	}
	else if( mesg == 0) {
		alert("ERROR");
	}
	else {
		centerPopup();
		loadPopup();
	}
	
}
function displayLogin(value,type,action) {
	var dataSend = "value="+value+"&t="+type+"&action=addToProfile";
        var mesg = $.ajax(
        {
                type:"GET",
                url:"/login/small",
                data:dataSend,
                async:false
        }).responseText;
	$("#loginBox").html(mesg);
	centerLogin();
	loadLogin();
}
function removeConfirm(value , type , id) {
	centerConfirm();
	if (type == 'gallery') {
		$("#pName").html(' this gallery ');
	} else {
		$("#pName").html(value);
	}
	$("#value").val(value);
	$("#i").val(id);
	$("#type").val(type);
	$("#backgroundPopup").css({
		"opacity": "0.7"
	});
	$("#backgroundPopup").fadeIn('slow');
	$("#popupConfirm").fadeIn('slow');
	popupStatus = 1;
}
function removeItem(ans) {
	var v = $("#value").val();
	var t = $("#type").val();
	var i = $("#i").val();
	var idName = i;


	if (ans == 'yes') {
		var dataSend = "value="+v+"&t="+t;
		var mesg = $.ajax({
			type:"POST",
			url:"/profile/remove",
			data:dataSend,
			async:false
		}).responseText;

		if (mesg == 1) {
			closeBox();
			if ($("#"+idName).is(':hidden')) {
				$("#"+idName).slideDown("slow");
			}
			else if ($("#"+idName).is(':visible')) {
				$("#"+idName).slideUp("slow");
			}
		} else {
			alert($("#popupConfim").html());
		}
	} else {
		closeBox();
	}
}

function removeLink(type , value) {
	var dataSend = "value="+value+"&t="+type;
	var mesg = $.ajax(
	{
		type:"POST",
		url:"/profile/remove",
		data:dataSend,
		async:false
	}).responseText;
	if (mesg > 0) {
		alert("Link Has Been Removed");
		window.location.reload();
	}
	else if (mesg == 0) {
		alert("ERROR");
	}
	else {
		document.location = '/login/';
	}
}


function reportAsBad(type , value) {
	var dataSend = "value="+value+"&t="+type;
	var mesg = $.ajax(
	{
		type:"POST",
		url:"/profile/bad",
		data:dataSend,
		async:false
	}).responseText;
	if (mesg > 0 ) {
		alert("REPORTED LINK TO WEBMASTER");
	}
	else if (mesg < 0 ) { 
		document.location = '/login/';
	}
	else {
		alert(mesg);
	}
		
}
function changeClass(row , newClass , linkId , linkClass) {
	row.className = newClass;
}





function rate(name , type , value) {
	var dataSend = "name="+name+"&t="+type+"&value="+value;
	var mesg = $.ajax(
	{
		type:"POST",
		url:"/profile/rate",
		data:dataSend,
		async:false
	}).responseText;
	$("#ratingText").html(mesg);
}

function gotoURLWin(url) {
	window.open(url);
}


function loadPopup() {
	if (popupStatus == 0) {
		$("#backgroundPopup").css({
			"opacity": "0.7"
		});
		$("#backgroundPopup").fadeIn('slow');
		$("#popupAdd").fadeIn('slow');
		popupStatus = 1;
	}
}
function loadLogin() {
	if (popupStatus == 0) {
		$("#backgroundPopup").css({
			"opacity": "0.7"
		});
		$("#backgroundPopup").fadeIn('slow');
		$("#loginBox").fadeIn('slow');
		popupStatus = 1;
	}
}
function centerPopup() {
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = $("#popupAdd").height();
	var popupWidth = $("#popupAdd").width();
	$("#popupAdd").css({
		"position": "absolute",
		"top": windowHeight/2-popupHeight/2,
		"left": windowWidth/2-popupWidth/2
	});
}
function centerLogin() {
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = $("#loginBox").height();
	var popupWidth = $("#loginBox").width();
	$("#loginBox").css({
		"position": "absolute",
		"top": windowHeight/2-popupHeight/2,
		"left": windowWidth/2-popupWidth/2
	});
}
function centerConfirm() {
	var windowWidth = document.body.clientWidth;
	var windowHeight = document.body.clientHeight;
	var popupHeight = $("#popupConfirm").height();
	var popupWidth = $("#popupConfirm").width();
	$("#popupConfirm").css({
		"position": "absolute",
		"top": windowHeight/3-popupHeight/2,
		"left": windowWidth/2-popupWidth/2
	});
}
function doLogin(myform) {
	var u = $("#username").val();
	var p = $("#password").val();
	var t = $("#t").val();
	var v = $("#value").val();
	var a = $("#action").val();
	var dataSend = "username="+u+"&password="+p+"&t="+t+"&value="+v+"&action="+a;
	var url = "/login/ajaxlogin?" + dataSend ;
	var jsonResponse = $.getJSON( "/login/ajaxlogin" , dataSend , function(j) {
				if (j.success) {
					popupStatus = 0;
					$("#loginBox").fadeOut('slow');
					addToProfile(j.value,j.t);
				}
				else {
					$("fieldset.loginform").css( {
						"height":"120px"
					});
					$("#errors").css( {
						"color": "red"
					});
					$("#errors").html(j.error);
				}
			});
	return false;
}
function closeBox() {
		if (popupStatus == 1) {
			$("#backgroundPopup").fadeOut("slow");
			$("#popupAdd").fadeOut("slow");
			$("#popupConfirm").fadeOut("slow");
			$("#loginBox").fadeOut("slow");
			popupStatus = 0;
		}

}

function submitComment() {
	var category = $("#commentCategory").val();
	var comment  = $("#textBox").val();
	
	var dataSend = "Category="+category+"&comment="+comment;
	var mesg = $.ajax(
	{
		type:"POST",
		url:"/profile/addcomment",
		data:dataSend,
		async:false
	}).responseText;
	mesgObj = $.evalJSON(mesg);
	if (mesgObj.value < 0) {
		displayLogin(category,comment);
		//document.location = '/login/';
	}
	else if( mesgObj.value == 0) {
		alert("ERROR");
	}
	else {
		var newComment = jQuery(document.createElement('div'));
		newComment.attr("class" , "cBox");

		var commentBox = jQuery(document.createElement('div'));
		commentBox.attr('class' , 'cBoxComment');
		commentBox.html(mesgObj.comment);

		var userBox = jQuery(document.createElement('div'));
		userBox.attr('class' , 'cBoxUser');
		userBox.html(mesgObj.username + '<br />');

		var dateSpan = jQuery(document.createElement('span'));
		dateSpan.css( {
			'font-size': '10px',
			'color': '#555'
			});
		dateSpan.html(mesgObj.date_added);
		userBox.append(dateSpan);

		newComment.append(userBox);
		newComment.append(commentBox);

		$("#comments2").prepend(newComment);
		//centerPopup();
		//loadPopup();
	}
	return false;
}

$(document).ready(function(){
	$("#backgroundPopup").click(function() {
		if (popupStatus == 1) {
			$("#backgroundPopup").fadeOut("slow");
			$("#popupAdd").fadeOut("slow");
			$("#popupConfirm").fadeOut("slow");
			$("#loginBox").fadeOut("slow");
			popupStatus = 0;
		}
	});
	$("#commentsShow").click(function() {
		if ($("#comments").is(':hidden')) {
			$("#comments").slideDown("slow");
		}
		else if ($("#comments").is(':visible')) {
			$("#comments").slideUp("slow");
		}
		
	});
});

