$(document).ready(function(){
	getusertimezone();
	opengan = 0;
	opengantype = 99;
	$(".img").imgLiquid();
	$(".imgnofill").imgLiquid({
        fill: false,
        horizontalAlign: "center",
        verticalAlign: "50%"
    });
	$('#username').bind('keyup',checkUsername);
	if($("#content").length){
		var topdiv = $('#content').offset().top;
		var leftdiv = $('#content').offset().left;
	}
	$('.emoticons a').click(function () {
	   var smiley = $(this).attr('title');
	   ins2pos(smiley, 'post');
	});
	if($(".gift").length){
		$(".gift").each(function(){
			$(this).draggable({
			   containment: 'parent',
			   cursor: 'pointer',
			   stop: function(event, ui) {
				var giftid = $(this).attr('title');
				var offset = $(this).offset();
				var xPos = parseInt(offset.top)-parseInt(topdiv);
				var yPos = parseInt(offset.left)-parseInt(leftdiv);
				$.ajax({
					url: 'ajax/giftposition',
					async: true,
					type: "POST",
					data: "ajax=1&giftid="+giftid+"&posx="+xPos+"&posy="+yPos,
					success: function(_2){},
					error: function (_6) {
					}
				});
			  }
			})
		});
		$("#draggable").draggable({});
	}
});

function changepostauthor(type) {
	if(type==1) {
		$('.byuser').addClass('none');
		$('.byanon').removeClass('none');
	}
	else {
		$('.byanon').addClass('none');
		$('.byuser').removeClass('none');
	}
}

function toogle_search() {
	$( ".navsearch" ).toggle();
}

function getusertimezone() {
	var timezone = jstz.determine();
	var tzname = timezone.name(); 
	$.ajax({
		url: 'ajax/tz',
		async: true,
		type: "POST",
		data: "tzname="+tzname+"&ajax=1",
		success: function(_2){},
		error: function (_6) {}
    });
}

function validationError(el, _0) {
    var _1 = $("#" + el + "Status");
    if (!_1) {
        return;
    }
    $(_1).html(_0);
    $(_1).removeClass("validationOK");
    $(_1).addClass("validationError");
    return false;
}

function validationOK(el) {
    var _1 = $("#" + el + "Status");
    if (!_1) {
        return;
    }
    $(_1).html("");
    $(_1).removeClass("validationError");
    $(_1).addClass("validationOK");
    return true;
}

function checkUsername() {
    var _0 = $("#username").val();
    $.ajax({
		url: 'ajax/checkusername',
		async: true,
		type: "POST",
		data: {
            ajax: 1,
            username: _0
        },
        success: function (_1) {
		var _2 = JSON.parse(_1);
            if (_2.error) {
                validationError("username", _2.error);
            } else {
                validationOK("username");
            }
        },
        error: function(){
			
		}
    });
}

function IsEmail(sEmail) {
    var filter = /^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
    if (filter.test(sEmail)) {
        return true;
    }
    else {
        return false;
    }
}

function checkProfileSettings() {
	obj = [];
    _0 = 0;
	_1 = $("#username");
	_2 = $("#name");
	_3 = $("#email");
	obj.push(_1);
	obj.push(_2);
	obj.push(_3);
	$.each(obj, function() {
		this.removeClass("highlightRed");
		if(!this.val()) {
			this.addClass("highlightRed");
			_0++;
		}
	});
    if ($("#usernameStatus").hasClass("validationError")) {
        $("#username").addClass("highlightRed");
        _0++;
    }
    if ($("#password") && $("#password").is(":visible")) {
        if ($("#password").val() == "" || $("#password2").val() == "") {
            $("#password").addClass("highlightRed");
            $("#password2").addClass("highlightRed");
            _0++;
        } 
		else {
            if ($("#password").val() != $("#password2").val()) {
                $("#password").addClass("highlightRed");
                validationError("password", "The passwords do not match");
                _0++;
            } 
			else {
                validationOK("password");
                $("#password").removeClass("highlightRed");
                validationOK("password2");
                $("#password2").removeClass("highlightRed");
            }
        }
    }
    if ($("#email")) {
		emailval = $("#email").val();
        if (!IsEmail(emailval)) {
            $("email").addClass("highlightRed");
            validationError("email", "The email address does not appear to be valid");
            _0++;
        } 
		else {
            validationOK("email");
        }
    }
    return _0;
}

function setupComplete() {
    var _0 = 0;
    _0 += checkProfileSettings();
    if (!$('#terms').is(':checked')) {
        $("#terms").addClass("highlightRed");
        $("#termsLabel").addClass("highlightRed");
        _0++;
    } else {
        $("#terms").removeClass("highlightRed");
        $("#termsLabel").removeClass("highlightRed");
    }
    if (!_0) {
        return;
    } 
	else {
        return false;
    }
}

function socialComplete() {
    var _0 = 0;
    if(!$("#username").val()) {
		$("#username").addClass("highlightRed");
		validationError("username", "Error");
		_0++;
	}
	else {
		$("#username").removeClass("highlightRed");
	}
	if(!$("#email").val()) {
		$("#email").addClass("highlightRed");
		_0++;
	}
	else {
		$("#email").removeClass("highlightRed");
	}
	if ($("#email")) {
		emailval = $("#email").val();
        if (!IsEmail(emailval)) {
            $("email").addClass("highlightRed");
            validationError("email", "Error");
            _0++;
        } 
		else {
            validationOK("email");
        }
    }
    if (!_0) {
        return;
    } 
	else {
        return false;
    }
}

function checkAccountSettings() {
    var _0 = 0;
	if(!$("#name").val()) {
		$("#name").addClass("highlightRed");
		_0++;
	}
	else {
		$("#name").removeClass("highlightRed");
	}
	if(!$("#email").val()) {
		$("#email").addClass("highlightRed");
		_0++;
	}
	else {
		$("#email").removeClass("highlightRed");
	}
    if ($("#password") && $("#password").is(":visible")) {
        if ($("#password2").val() == "") {
            $("#password").addClass("highlightRed");
            $("#password2").addClass("highlightRed");
            _0++;
        } 
		else {
                validationOK("password");
                $("#password").removeClass("highlightRed");
                validationOK("password2");
                $("#password2").removeClass("highlightRed");
        }
    }
    if ($("#email")) {
		emailval = $("#email").val();
        if (!IsEmail(emailval)) {
            $("email").addClass("highlightRed");
            validationError("email", "The email address does not appear to be valid");
            _0++;
        } 
		else {
            validationOK("email");
        }
    }
    return _0;
}

function changePassword() {
    $("#changePasswordLink").hide();
    $("#changePassword").show();
    $("#password").show();
    $("#changePassword2").show();
    $("#password2").show();
}

function checkWebsite() {
    if ($("#website")) {
         if ($("#website").val().match(/^http/)) {
            $.noop()
        }
        else {
            var cur_val = $("#website").val(); 
            $("#website").val('http://' + cur_val);
        } 
    }
    $("#saveProfileButton").attr('disabled', 'disabled');
    $("#saveProfileIndicator").show();
    $("#profileForm").submit();
}

function deletePhoto() {
    $("#photoPreview").hide();
    $("#photoUpload").show();
    $("#deletephoto").val('1');
}

function deleteCover() {
    $("#coverPreview").hide();
    $("#coverUpload").show();
    $("#deletecover").val('1');
}

function selectTheme(_0) {
    $(".selectedTheme").removeClass("selectedTheme");
	$('link[title*="theme_"]').each(function(){
		if($(this).attr('title') === 'theme_' + _0) {
			$(this).removeAttr('disabled')
		}
		else {
			$(this).attr('disabled', 'disabled');
		}
    });
    $("#theme_" + _0).addClass("selectedTheme");
    $("#theme").val(_0);
}

function selectGift(_0) {
    $(".selectedgift").removeClass("selectedgift");
	$(".gift"+_0).addClass("selectedgift");
	$('#selectedgif').val(_0);
	$("#submitgift").css("display","block");
	$('body').delay(1000).animate({
        'scrollTop': $(document).height()
    }, 300); 
}

function deleteBackground() {
    $("backgroundPreview").hide();
    $("backgroundUpload").show();
    $("deletebackground").val('1');
}

function saveSettings() {
    var _0 = checkAccountSettings();
    if (_0) {
        return true;
    }
    $("#saveSettingsButton").attr('disabled', 'disabled');
    $("#saveSettingsIndicator").show();
    $("#settingsForm").submit();
}

function like(_0) {
	$.ajax({
		url: 'ajax/like',
		async: true,
		type: "POST",
		data: "id="+_0+"&ajax=1",
		success: function(_2){
			var _01 = JSON.stringify(_2);
			var _00 = JSON.parse(_01);
			var _3 = $(".countlike"+_0).html();
			var _4 = $(".likestotal").html();
			if(_00.type == 'delete') 
			{ _3--; _4--; $("#post"+_0+" .liked").switchClass( "liked", "like", 1000, "easeInOutQuad" ); }		
			else
			{ _3++; _4++; $("#post"+_0+" .like").switchClass( "like", "liked", 1000, "easeInOutQuad" ); }		
			$(".countlike"+_0).html(_3);
			$(".likestotal").html(_4);
		},
		error: function (_6) {
			location.reload();
        }
    });
}

function notifications() {
	$.ajax({
		url: 'ajax/notify',
		async: true,
		type: "POST",
		data: "ajax=1&ntype=0",
		success: function(_2){
			var _01 = JSON.stringify(_2);
			var _00 = JSON.parse(_01);
			if(_00.answers>0) $(".nanswers").html("<span class='notifyanswerst'>"+_00.answers+"</span>");
			if(_00.likes>0) $(".nlikes").html("<span class='notifylikest'>"+_00.likes+"</span>");
			if(_00.posts>0) $(".nposts").html("<span class='notifypostst'>"+_00.posts+"</span>");
		},
		error: function (_6) {
		}
	});
	window.setInterval(function(){
		$.ajax({
			url: 'ajax/notify',
			async: true,
			type: "POST",
			data: "ajax=1&ntype=0",
			success: function(_2){
				var _01 = JSON.stringify(_2);
				var _00 = JSON.parse(_01);
				if(_00.answers>0) $(".nanswers").html("<span class='notifyanswerst'>"+_00.answers+"</span>");
				if(_00.likes>0) $(".nlikes").html("<span class='notifylikest'>"+_00.likes+"</span>");
				if(_00.posts>0) $(".nposts").html("<span class='notifypostst'>"+_00.posts+"</span>");
			},
			error: function (_6) {
			}
		});
	}, 30000);
}

function opennotification(_0) {
	var position = $('.not'+_0).position();
	$("#wrapper").before("<div id='notifybox'></div>");
	if(opengan==0 || opengantype != _0)
	{
		$.ajax({
			url: 'ajax/notify',
			async: true,
			type: "POST",
			data: "ajax=1&ntype="+_0,
			success: function(_2){
				$("#notifybox").show();
				var _01 = JSON.stringify(_2);
				var _00 = JSON.parse(_01);
				$("#notifybox").html(_00.response);
				var elheight = $('#notifybox').height();
				var bottom = $(window).height() - position.top + 20;
				bottom = bottom - (elheight/2);
				if($('#notifybox').height()>$(window).height()/2) {
					bottom = bottom - $('#notifybox').height()/4;
					$('#notifybox').css('bottom',bottom+'px');
				}
				else {
					$('#notifybox').css('bottom',bottom+'px');
				}
				opengan = 1;
				opengantype = _0;
				if(_0 == '1') $(".nlikes").html("");
				if(_0 == '2') $(".nposts").html("");
				if(_0 == '3') $(".nanswers").html("");
				$('#wrapper').click(function() {
					$('#notifybox').hide();
					opengan = 0;
					opengantype = _0;
				});
			},
			error: function (_6) {
			}
		});
	}
	else
	{
		$('#notifybox').hide();
		opengan = 0;
		opengantype = _0;
	}
}

function addComment(_0) {
    if(!$("#comment").val()) {
        $("#comment").addClass("highlightRed").focus();
        return;
    }
    $("#comment").removeClass("highlightRed");
    $("#sendButton").attr("disabled", "disabled");
    $("#sendIndicator").show();
	var _1 = $("#commentForm").serialize();
	$.ajax({
		url: 'ajax/comment',
		async: true,
		type: "POST",
		data: _1 + "&id="+_0+"&ajax=1",
		success: function(_2){
			var _01 = JSON.parse(_2);
			if (_01.error == 'error') {
				$("#sendButton").removeAttr('disabled');
                $("#sendIndicator").hide();
			}
			else {
				$(".nocomm").hide();
				$("#commentslist").append(_01.html).show('slow');
				$("html, body").animate({ scrollTop: $(document).height() }, "slow");
				var _3 = $("#commentCount").html(); _3++;
				$("#commentForm").hide();
				$("#commentCount").text(_3);
                $("#commentSuccess").show();
                $("#sendButton").removeAttr('disabled');
                $("#sendIndicator").hide();
            }
		},
		error: function (_6) {
			alert('Error: '+_6);
            $("#sendButton").removeAttr('disabled');
            $("#sendIndicator").hide();
        }
    });
}

function commentAgain() {
    location.reload();
}

function checkLoginForm() {
	if(!$("#inputUsernameEmail").val()) {
        $("#inputUsernameEmail").addClass("highlightRed").focus();
        ok = false;
    }
	else { var ok = true; }
    if(!$("#inputPassword").val()) {
        $("#inputPassword").addClass("highlightRed").focus();
        ok = false;
    }
	else { var ok = true; }
    return ok;
}

function setc(value) {
	var date = new Date();
	date.setTime(date.getTime()+(360*24*60*60*1000));
	var expires = "; expires="+date.toGMTString();
	document.cookie = "lang"+"="+value+expires+"; path=/";
	$.ajax({
		url: 'ajax/changelang',
		async: true,
		type: "POST",
		data: "ajax=1",
		success: function(){
			location.reload(); 
		},
		error: function(){
		}
	});
}

function readc() {
	var nameEQ = "lang" + "=";
	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;
	location.reload();
}

function unsetc() {
	createCookie("lang","",-1);
	location.reload();
}

function addf(_0) {
	$.ajax({
		url: 'ajax/addfriend',
		async: true,
		type: "POST",
		data: "username="+_0+"&ajax=1",
		success: function(){
			location.reload();
		},
		error: function () {
        }
    });
}

function removef(_0) {
    $.ajax({
		url: 'ajax/removefriend',
		async: true,
		type: "POST",
		data: "username="+_0+"&ajax=1",
		success: function(){
			location.reload();
		},
		error: function () {
        }
    });
}

function lock(_0) {
	$.ajax({
		url: 'ajax/blockuser',
		async: true,
		type: "POST",
		data: "username="+_0+"&ajax=1",
		success: function(){
			location.reload();
		},
		error: function () {
        }
    });
}

function unlock(_0) {
    $.ajax({
		url: 'ajax/unblockuser',
		async: true,
		type: "POST",
		data: "username="+_0+"&ajax=1",
		success: function(){
			location.reload();
		},
		error: function () {
        }
    });
}

function stream(type) {
	$.ajax({
		url: 'ajax/stream',
		async: true,
		type: "POST",
		data: "ajax=1&streamtype="+type,
		success: function(_2){
			var _01 = JSON.stringify(_2);
			var _00 = JSON.parse(_01);
			$("#stream").html(_00.response);
			$(".loadingstream").css("display","none");
			$(".img").imgLiquid();
			if(type=='friends' && _00.response=='')
			{
				$('.nofollowing').show();
			}
		},
		error: function (_6) {
		}
	});
}

function verified(username,type) {
	if(type==0 || type==1)
	{
		var id = $("."+username+"").val();
		$.ajax({
			url: 'ajax/sticker',
			async: true,
			type: "POST",
			data: "ajax=1&username="+username+"&verified="+type,
			success: function(_2){
			},
			error: function (_6) {
			}
		});
	}
	else
	{
		alert("Error. Value should be 0 for disabling or 1 for enabling!");
	}
}

function ins2pos(str, id) {
   var TextArea = document.getElementById(id);
   var val = TextArea.value;
   var before = val.substring(0, TextArea.selectionStart);
   var after = val.substring(TextArea.selectionEnd, val.length);
   TextArea.value = before + str + after;
   setCursor(TextArea, before.length + str.length);
   
}

function setCursor(elem, pos) {
   if (elem.setSelectionRange) {
      elem.focus();
      elem.setSelectionRange(pos, pos);
   } else if (elem.createTextRange) {
      var range = elem.createTextRange();
      range.collapse(true);
      range.moveEnd('character', pos);
      range.moveStart('character', pos);
      range.select();
   }
}

function emoticons() {
	$( ".emoticons" ).toggle();
}

function toppeople(type) {
	var loader = '<div class="loadingtop"><img src="../media/images/wait.gif" style="margin-bottom:50px;margin-top:50px"/></div>';
	$("#topcontainer").html(loader);
	$.ajax({
		url: 'ajax/toppeople',
		async: true,
		type: "POST",
		data: "ajax=1&optiontype="+type,
		success: function(_2){
			var _01 = JSON.stringify(_2);
			var _00 = JSON.parse(_01);
			$("#topcontainer").html(_00.response);
			$(".img").imgLiquid();
		},
		error: function (_6) {
		}
	});
}

function addfr(_0) {
	$.ajax({
		url: 'ajax/addfriend',
		async: true,
		type: "POST",
		data: "username="+_0+"&ajax=1",
		success: function(){
			$(".followb"+_0).html("<img src='../media/images/verified.png' style='width:30px;height:30px;margin-left:30px;' />");
		},
		error: function () {
        }
    });
}

function removefr(_0) {
    $.ajax({
		url: 'ajax/removefriend',
		async: true,
		type: "POST",
		data: "username="+_0+"&ajax=1",
		success: function(){
			$(".followb"+_0).html("<img src='../media/images/verified.png' style='width:30px;height:30px;margin-left:30px;' />");
		},
		error: function () {
        }
    });
}

function closelikes() {
	$("#likespopup").remove();
	$(".blackpopup").remove();
}

function loadlikes(id) {
	closelikes();
	$("body").before("<div class='blackpopup'></div>");
	$("#wrapper").before("<div id='likespopup'></div>");
	$.ajax({
		url: 'ajax/likes',
		async: true,
		type: "POST",
		data: "ajax=1&id="+id,
		success: function(_2){
			var _01 = JSON.stringify(_2);
			var _00 = JSON.parse(_01);
			$("#likespopup").css("display","block");
			$("#likespopup").html(_00.response);
		},
		error: function (_6) {
		}
	});
}