function getCookie(c_name){
var i,x,y,ARRcookies=document.cookie.split(";");
for (i=0;i<ARRcookies.length;i++)
{
  x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
  y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
  x=x.replace(/^\s+|\s+$/g,"");
  if (x==c_name)
    {
    return unescape(y);
    }
  }
}

if( curMemberID==null ){
	var curMemberID = getCookie('memberID');
}

function getNotifications(){
	jQuery.post('/notification.jqserver.php', {proc: 'getNotification'}, function(data){
		jQuery("#notification").html(data);
		setTimeout("getNotifications()",300000);
	});
}
function clearNotification(){
	jQuery("#notification").html('0');
}

if (typeof j == 'undefined') { var j = jQuery.noConflict(); }
j(document).ready(function(){
	v2_win_refresh();

	jQuery(".notif-btn").show();
	jQuery(".notif-btn").colorbox({ opacity: "0.7", close: " ", scrolling: false });
	jQuery(".pop").colorbox({ opacity: "0.7", close: "Close", overlayClose: false });
	setMainHeight();
	j(window).resize(function() {
		setMainHeight();					  
	});

	j(".mainLogo").hover(function(){
		j(this).attr("src","media/images/logo_hover.png");
	 },
	 function(){
		j(this).attr("src","media/images/logo.png");	  
	 });
	 
	 update_online_status();
	
	if( parseInt(curMemberID)>0 ){
		getNotifications();
		jQuery(".apps-btn").show();
		jQuery(".apps-btn").colorbox({ opacity: "0.7", close: " ", scrolling: false });
		//checkMailNotification();
		//checkProfileNotification();
	}
	
	// css fixes
	j("#pro_pic").css({ opacity: 0 });
	j("#comments_wrapper").css("overflow-y","auto");
	j(".comments_content").css("overflow-y","auto");
	//j(".profile-links a").css("text-shadow","0px 1px 0px #cecece");
	//j(".profile-links a.profile-links-selected").css("text-shadow","0px -1px 0px #333");
	j(".thumbnail_block, .thumbnail_block img").css("-moz-border-radius","5px").css("-webkit-border-radius","5px");
	j(".friend-icon-holder-selected").css("-moz-border-radius","10px").css("-webkit-border-radius","10px");
	j(".friend-icon-holder-disabled").css({ opacity: 0.5 });
	j(".action-item").css("-webkit-border-radius","10px").css("-moz-border-radius","10px");
	j(".remove-confirmation").css("-moz-border-radius","15px").css("-webkit-border-radius","15px");
	j(".black_button").css("text-shadow","1px 1px 0px #CCC");
	j("#curchat_list").css("overflow-x","hidden");
	j("#curchat_userlist_moveleft, #curchat_userlist_moveright, #curchat_close_button, .chatuser_item  .cui_status,.cui_message_header  .cui_headerstatus, .chatuser_item .cui_message_bottom_minimize, .chatuser_item .cui_message_bottom_close, .chatuser_item .cui_name").css("cursor","hand");
	j(".chatuser_item .cui_name_text").css("text-shadow","1px 1px 0px #CCC");
	j(".chatuser_item .cui_message_header_text").css("overflow-y","1px 1px 0px #000");
	j(".cui_message_scroll_inner").css("-moz-border-radius","5px").css("-webkit-border-radius","5px");
	j(".chat-ol-row .thumbnail_block, .chat-ol-row .thumbnail_block img, .chat-ol-row .online_pr_photo img").css("overflow-y","auto");
	j(".app_row:last-child").css("border-bottom","none");
	
	j(".v2_header_mainnav a.mainnav_item").css("text-shadow","0px 1px 0px #cecece").css("text-shadow","#666666");
	
	j(".v2_header_mainnav a.mainnav_item").hover(
	  function () {
		j(this).css("text-shadow","0px -1px 0px #333");
	  }, 
	  function () {
		j(this).css("text-shadow","");
	  }
	);

	j(".v2_header_mainnav a.mainnav_item_current").css("text-shadow","0px 2px 0px #333");

	
/*	j(".profile-links a").hover(
	function () {
		j(this).css("text-shadow","0px -1px 0px #333");
	}, 
	function () {
		j(this).css("text-shadow","");
	});
*/	
	//getApplicationList();
	//getBookmarkList();
	//getActivityList();
	//getFriendList();
	//getChatFriendsList();
	//checkNotificationUpdate();
	
	j("#b_bookmarks .black_button").click(function(){
		j(".appication-list").hide();
		j(".notification-list").hide();
		j(".menu-friend-list").hide();
		j(".chat-list").hide();
		j(".activity-list").hide();
		j(".chat-list").hide();									
		
		j(".bookmark-list").show();
		
		return false;
	});
	
	
	j("#b_application .black_button").click(function(){
		j(".bookmark-list").hide();
		j(".notification-list").hide();
		j(".menu-friend-list").hide();

		j(".activity-list").hide();

		j(".appication-list").show();
		
	});
	
	j("#b_notification .black_button").click(function(){
		j(".bookmark-list").hide();
		j(".appication-list").hide();
		j(".menu-friend-list").hide();
		j(".chat-list").hide();
		j(".activity-list").hide();
		
		j(".notification-list").show();
		j.ajax({
		  type: "GET",
		  url: "/notification.jqserver.php",
		  data: "proc=notificationNavUpdate&memberID="+curMemberID,
		  cache: false,
		  success: function (data){
			  j("#notifications").removeClass('red_button');
			  j("#notifications").text('Notifications');
		  }
		});
		
	});
	j("#b_activity .black_button").click(function(){
		j(".bookmark-list").hide();
		j(".appication-list").hide();
		j(".notification-list").hide();
		j(".menu-friend-list").hide();
		j(".chat-list").hide();
		
		j(".activity-list").show();
	});
	
	j("#b_friends .black_button").click(function(){
		j(".bookmark-list").hide();
		j(".appication-list").hide();
		j(".notification-list").hide();
		j(".chat-list").hide();
		j(".activity-list").hide();
		
		j(".menu-friend-list").show();
	});
	
});

function setMainHeight(){
	var html_height = j("html").height();
	var less;
	less = j('.top_black').height() + j('#v2_footer').height();
		
	var main_height = parseInt(html_height)-parseInt(less);
	
	if( j('#vp2_content').length !=0 ){
		$('body').css('overflow','hidden');
		
		var window_height = j(window).height();
		var header_height = j('.top_black').height() + j('#vp2_header').height();
		var footer_height = j('#v2_footer').height();
		
		var newheight = window_height - (header_height+footer_height);
		
		if( newheight<120 ){
			newheight = 120;
		}

		j("#vp2_content").css('overflow','auto');
		j("#vp2_content").css('height',newheight+'px');
	}else{
		j(".main").height(main_height);
	}
}

function checkProfileNotification(){
	if( parseInt(curMemberID)<=0 ) return false;
		
	j.ajax({
	  type: "GET",
	  url: "/notification.jqserver.php",
	  data: "proc=checkProfileNotification&memberID="+curMemberID,
	  cache: false,
	  success: function (data){
		  if(data<=0){
			  j("#top_nav_profile").text('My Profile');
		  }else{
			  j("#top_nav_profile").text('My Profile (' + data + ')');
		  }
		  
		setTimeout("checkProfileNotification()", 300000);
	  }
	});													   
	j.ajax({
	  type: "GET",
	  url: "/notification.jqserver.php",
	  data: "proc=checkFriendRequest&memberID="+curMemberID,
	  cache: false,
	  success: function (data){
		  if(data<=0){
			  j("#top_nav_home").text('Home');
		  }else{
			  j("#top_nav_home").text('Home (' + data + ')');
		  }
	  }
	});													   
}
function checkMailNotification(){
	if( parseInt(curMemberID)<=0 ) return false;
		
	j.ajax({
	  type: "GET",
	  url: "/notification.jqserver.php",
	  data: "proc=checkMailNotification&memberID="+curMemberID,
	  cache: false,
	  success: function (data){
		  if(data<=0){
			  j("#top_nav_messages").text('Messages');
		  }else{
			  j("#top_nav_messages").text('Messages (' + data + ')');
		  }
		  
			setTimeout("checkMailNotification()", 300000);
	  }
	});													   
}

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);
}

function getApplicationList(){
	if( parseInt(curMemberID)<=0 ) return false;
	j.ajax({
	  type: "GET",
	  url: "/feed.jqserver.php",
	  data: "proc=applicationList&memberID="+curMemberID,
	  cache: false,
	  success: function (data){
		  j(".appication-list").html(data);
	  }
	});
}
function getBookmarkList(){
	if( parseInt(curMemberID)<=0 ) return false;
	j.ajax({
	  type: "GET",
	  url: "/feed.jqserver.php",
	  data: "proc=bookmarkList&memberID="+curMemberID,
	  cache: false,
	  success: function (data){
		  j(".bookmark-list").html(data);
	  }
	});
}
function getActivityList(){
	if( parseInt(curMemberID)<=0 ) return false;
	j.ajax({
	  type: "GET",
	  url: "/feed.jqserver.php",
	  data: "proc=activityList&memberID="+curMemberID,
	  cache: false,
	  success: function (data){
		  j(".activity-list").html(data);
	  }
	});
}
function getFriendList(){
	if( parseInt(curMemberID)<=0 ) return false;
	j.ajax({
	  type: "GET",
	  url: "/feed.jqserver.php",
	  data: "proc=friendList&memberID="+curMemberID,
	  cache: false,
	  success: function (data){
		  j(".menu-friend-list").html(data);
		  //setTimeout("getFriendList()",30000);
	  }
	});
}


function updateNotificationList(){
	if( parseInt(curMemberID)<=0 ) return false;
	j.ajax({
	  type: "GET",
	  url: "/notification.jqserver.php",
	  data: "proc=notificationList&memberID="+curMemberID,
	  cache: false,
	  success: function (data){
		  j(".notification-list").html(data);
	  }
	});
}

function checkNotificationUpdate(){
	if( parseInt(curMemberID)<=0 ) return false;
		
	j.ajax({
	  type: "GET",
	  url: "/notification.jqserver.php",
	  data: "proc=checkNotificationUpdate&memberID="+curMemberID,
	  cache: false,
	  success: function (data){
		  if(data<=0){
			  j("#notifications").removeClass('red_button');
			  j("#notifications").text('Notifications');
		  }else if(!j('#notifications').hasClass('red_button')){		  
			  j("#notifications").addClass('red_button');
			  j("#notifications").text('Notifications '+data);
			  updateNotificationList();
		  }else{
			  j("#notifications").text('Notifications '+data);
			  updateNotificationList();
		  }
		  
		  setTimeout("checkNotificationUpdate()", 300000);
	  }
	});													   
}


function notificationRemove( memberID, type, dateposted ){
	j.ajax({
	  type: "GET",
	  url: "/notification.jqserver.php",
	  data: ("proc=removeNotification&memberID="+curMemberID+"&type="+type+"&dateposted="+dateposted),
	  cache: false,
	  success: function (data){
		  j('#notification_item_'+type+"_"+dateposted).hide();
	  }
	});			
}



var chat_last_id = new Array();
var chat_scstate = new Array();
var chat_timercount = new Array();

// bottom butons
j(document).ready(function(){
	
	j(".bottom_button_item").hover(
	function () {
		j(this).addClass('bottom_button_item_hover');
		// compact
		j(this).parent().find('.bottom_button_item_compact:eq('+(j(this).index('.bottom_button_item'))+')').addClass('bottom_button_item_compact_hover');
		var t_offset = j(this).position();

		j(this).find('.bottom_button_listparent').css('left',(t_offset.left-12)+'px');						
		footer_setchatwidth();
	}, 
	function () {
		j(this).removeClass('bottom_button_item_hover');
		// expand
		j(this).parent().find('.bottom_button_item_compact:eq('+(j(this).index('.bottom_button_item'))+')').removeClass('bottom_button_item_compact_hover');
		footer_setchatwidth();
	});
	
	//window.setInterval( 'footer_chat_updatestatus()', 5000);
	//footer_updateonlinestatus();
	//window.setInterval( 'footer_updateonlinestatus()', 240000);
});

function footer_updateonlinestatus(){
	j.get("last_nav_time_update.php", {profile_id:curMemberID}, function(data){ });
}

function footer_buttons_compact(){
	j(".bottom_button_item").addClass('bottom_button_item_compact');
	footer_setchatwidth();
}
function footer_buttons_expand(){
	j(".bottom_button_item").removeClass('bottom_button_item_compact');
	footer_setchatwidth();
}

function footer_showchat( userid ){
/*
	footer_buttons_compact();
	//show
	footer_setchatwidth();
	j('#curchat_list').show();
	j('.chat-list').hide();
	footer_chat_load(userid);*/
}

function footer_setchatwidth(){
	/*var mb_w = j('#bottom_mainbuttons').width();
	j('#curchat_list').css('width',(980-mb_w)+'px');
	
	footer_chatusercroller_resize();*/
}

function footer_chat_close(){
	/*j('#curchat_list').hide();
	footer_buttons_expand();
	footer_setchatwidth();*/
}


function isArray(obj){ 
	
	return jQuery.isArray(obj);

}


function update_online_status(){
	var profileID = readCookie("memberID");
}

j(window).resize(function() {
  v2_win_refresh();	
  if(typeof setMainHeight == 'function') {
	setMainHeight();					  
  }
});



function v2_win_refresh(){
	
	j('body').css('overflow','hidden');	
	var window_height = j(window).height();
	var header_height = j('.v2_header').height();
	var footer_height = j('#v2_footer').height();
	var midwcontent_inner_height  = j('#v2_mid_content_inner').height();
	
	var midwcontent_height = window_height-(header_height+footer_height);


	if( midwcontent_height<=168){
		midwcontent_height=168;
	}
	j('#v2_mid_content').css('height',midwcontent_height+'px');
	j('#v2_mid_content').css('*height',midwcontent_height+'px');
	j('#v2_mid_content').css('_height',midwcontent_height+'px');
}
