﻿/***************************/
//@Author: Adrian "yEnS" Mato Gondelle
//@website: www.yensdesign.com
//@email: yensamg@gmail.com
//@license: Feel free to use it, but keep this credits please!					
/***************************/

//SETTING UP OUR POPUP
//0 means disabled; 1 means enabled;
var popupStatus = 0;
function checkHtmlTag(inputvalue) {
    var input = $(".txtAddFriend").val();
    if(input.indexOf("<") != -1)
    {
        alert("Please do not enter Html/script tag into the textbox")
        return false;
    }
    else
    {
        return true;
    }
}
//loading popup with jQuery magic!
function loadPopup(){
	//loads popup only if it is disabled
	if(popupStatus==0){
		$("#backgroundPopup").css({
			"opacity": "0.7"
		});
		$("#backgroundPopup").fadeIn("slow");
		$("#popupContact").fadeIn("slow");
		popupStatus = 1;
	}
}

//disabling popup with jQuery magic!
function disablePopup(){
	//disables popup only if it is enabled
	if(popupStatus==1){
		$("#backgroundPopup").fadeOut("slow");
		$("#popupContact").fadeOut("slow");
		popupStatus = 0;
	}
}

//centering popup
function centerPopup(key,obj){
	//request data for centering
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = $("#popupContact").height();
	var popupWidth = $("#popupContact").width();
	var popupTop = $(obj).offset().top;
	var popupRight = $(obj).offset().left;
	
	var picUrl = $("#img" + (key)).attr('src');
	var FirstName = $("#firstname" + (key)).html();
	var Counrtry = $("#country" + (key)).html();
	var DutDB = $("#dutBD" + (key)).html();
	$("#img1").attr({ 
          src: picUrl,
          title: FirstName
        });
    $("#popupFirstName").html(FirstName);
    $("#popupCountry").html(Counrtry);
    $("#popupDutDB").html(DutDB);
	
	//centering
	$("#popupContact").css({
		"position": "absolute",
		"top": popupTop,
		"left": popupRight - popupWidth -10
	});
	//only need force for IE6
	
	$("#backgroundPopup").css({
		"height": windowHeight
	});

}
function getRecentlyMember() {
//alert('recentlyMember');
    $('#RecentMemberAreaId').Loading({zindex:10000,autostop:0});
    //var targetCombo = 'Moodselect';
    //document.getElementById(targetCombo).options.length = 0;
    var domain=window.location.href.match(/:\/\/(.[^/]+)/)[1];
    //alert("http://" + domain + "/base/AsyMemberHelper/GetRecentlyMember.aspx");
    $.post("http://" + domain + "/base/AsyMemberHelper/GetRecentlyMember.aspx", null,
    function(data) {
    //alert("done");
        if (data.IsException == true) {
            //alert(data.ExceptionDetail);
        }
        else {
            binding("",data);
        }
        $('#RecentMemberAreaId').EndLoading();
        $('#RecentMemberCenterID').show();
    }, "json");
    //document.getElementById(targetCombo).selectedIndex = 0;
    //if (document.getElementById(targetCombo).refresh != undefined)
    //    document.getElementById(targetCombo).refresh();
}
function getMyfriend() {

    //var targetCombo = 'Moodselect';
    //document.getElementById(targetCombo).options.length = 0;
	
    var domain=window.location.href.match(/:\/\/(.[^/]+)/)[1];
    //alert("http://" + domain + "/base/AsyMemberHelper/GetMyFriendOnline.aspx");
    $.post("http://" + domain + "/base/AsyMemberHelper/GetMyFriendOnline.aspx", null,
    function(data) {
    //alert("done");
        if (data.IsException == true) {
            //alert(data.ExceptionDetail);
        }
        else {
            bindingMyFriend("",data);
        }
    //    $('#MyfriendAreaID').EndLoading();
    }, "json");
    
    //document.getElementById(targetCombo).selectedIndex = 0;
    //if (document.getElementById(targetCombo).refresh != undefined)
    //    document.getElementById(targetCombo).refresh();
}
function getCountMyfriend() {
    //var targetCombo = 'Moodselect';
    //document.getElementById(targetCombo).options.length = 0;
    var domain=window.location.href.match(/:\/\/(.[^/]+)/)[1];
    //alert("http://" + domain + "/base/AsyMemberHelper/GetCountMyFriendOnline.aspx");
    $.post("http://" + domain + "/base/AsyMemberHelper/GetCountMyFriendOnline.aspx", null,
    function(data) {
    //alert("done");
        if (data.IsException == true) {
            //alert(data.ExceptionDetail);
        }
        else {
        //alert(data.ExceptionDetail);
            $("#friendonlineDiv").html(data.ExceptionDetail);
        }
        
    }, "json");
    //document.getElementById(targetCombo).selectedIndex = 0;
    //if (document.getElementById(targetCombo).refresh != undefined)
    //    document.getElementById(targetCombo).refresh();
}

function binding(targetDiv,memberData) {
    $.each(memberData, function(key, state) {
    var imgID = "#img" + (key +2);
    var firstname = "#firstname" + (key +2);
    var country = "#country" + (key+2);
    var dutBD2 = "#dutBD" + (key +2);
    //alert(imgID);
        $(imgID).attr({ 
          src: state.picAvatar,
          title: state.FirstName,
          alt: state.Email
        });
        $(firstname).text(state.FirstName);
        $(country).text(state.CountryFullName);
        $(dutBD2).text(state.Baby1DateofBirthStr);
        //document.getElementById(targetCombo).options[key] = new Option(state.ImageNameEn, state.MoodID);
        //    document.getElementById(targetCombo).options[key].title = state.ImageUrl;
            //alert(state.ImageUrl);
        });
}

function bindingMyFriend(targetDiv,memberData) {
    $.each(memberData, function(key, state) {
    var imgID = "#img" + (key +8);
    var firstname = "#firstname" + (key +8);
    var country = "#country" + (key+8);
    var dutBD2 = "#dutBD" + (key +8);
    var myfriendDiv = "#MyfriendItem" + (key +8);
    //alert(state.LinkProfile);
    $(myfriendDiv).show();
    //alert(imgID);
        $(imgID).attr({ 
          src: state.picAvatar,
          title: state.FirstName,
          alt: state.Email
        }).click(function(){window.open(state.LinkProfile)});
        $(firstname).text(state.FirstName);
        $(country).text(state.CountryFullName);
        $(dutBD2).text(state.DateofBirthStr);
        //document.getElementById(targetCombo).options[key] = new Option(state.ImageNameEn, state.MoodID);
        //    document.getElementById(targetCombo).options[key].title = state.ImageUrl;
            //alert(state.ImageUrl);
        });
}


function showMyFriends()
{
    $("[id$='_panelRecentMember']").hide();
    $("[id$='_panelMyfriends']").show();
    $('#recentMember').hide();
    $('#myFriends').show();
    $('#myFriends').Loading({zindex:10000,autostop:0});
    getCountMyfriend();
    getMyfriend();
    $('#myFriends').EndLoading();
}

function showRecentMember()
{
    
    $("[id$='_panelRecentMember']").show();
    $("[id$='_panelMyfriends']").hide();
    $("#recentMember").show();
    $("#myFriends").hide();
    getRecentlyMember();
    $(".RecentMemberItem").click(function() {
		//centering with css
		centerPopup($(this).attr('sid'),$(this));
		//load popup
		loadPopup();
	});
	
    
    
}
function GetCookies(cookieData, cookiesName) {
    try
	{
	var cookies = cookieData.split('&');
    for (var i = 0; i < cookies.length; i++) {
        var item = jQuery.trim(cookies[i]);
        var pitem = item.indexOf('=');
        if (item.substring(0, pitem) == cookiesName) {
            return (item.substring(item.indexOf('=') + 1, item.length));
        }
    }
	}catch(e){}
}

function showHideMyFriend(isShowMyfriends)
{   
        
}
//CONTROLLING EVENTS IN jQuery
$(document).ready(function(){
    //$("#myFriends").hide();
    $(document).bind('LoggedIn',function(event, element)
    {
        //alert(element);
        //$("#myFriends").show();
        //alert($('#myFriends').size());
        showMyFriends();
    });
	   
	var authen = ($.cookie('HuggiesMemberLogon'));
	var email = GetCookies($.cookie('HuggiesMemberLogon'), 'Email');
	    //alert(email);
    if (authen != null && email != "") {
        //showMyFriends();
    }
	else{
	//$('#RecentMemberCenterID').hide();
	    showRecentMember();
	}
	
				
	//CLOSING POPUP
	//Click the x event!
	$("#popupContactClose").click(function(){
		disablePopup();
	});
	//Click out event!
	$("#backgroundPopup").click(function(){
		disablePopup();
	});
	//Press Escape event!
	$(document).keypress(function(e){
		if(e.keyCode==27 && popupStatus==1){
			disablePopup();
		}
	});

});
