// JavaScript Document

// -------------- Global values --------------- //
var browser = new Browser();
var online_friends = new Array();
var chats = new Array();
var bm_right_open = '';
var chat_update_interval;
var open_chat_id;
var oldTitle;
var PhotoTags = new Object;
var moveObj = new Object;
var imgTag = new Object;

var idleTimer = null;
var idleState = false;
var idleWait = 300000;
// -------------------------------------------- //

jQuery(document).ready(function() {
	jQuery('.top_menu_start_btn').click(function(event){
		event.stopPropagation();
	});
	jQuery('#wmn_menu').click(function(event){
		event.stopPropagation();
	});
});
$(document).ready(function () {
	$(this).bind('mousemove keydown scroll', function () {
		clearTimeout(idleTimer);

		if (idleState == true) { 
			// Reactivated event
			update_status('Online');
		}

		idleState = false;

		idleTimer = setTimeout(function () { 
			// Idle Event
			update_status('Idle');
			
			idleState = true; }, idleWait);
	});

	$("body").trigger("mousemove");
});
// -------------- Browser/Crypt --------------- //
function Browser()
{
	var ua, s, i;
  
	this.isIE    = false;
	this.isNS    = false;
	this.version = null;
  
	ua = navigator.userAgent;
  
	s = "MSIE";
	if ((i = ua.indexOf(s)) >= 0) {
	  this.isIE = true;
	  this.version = parseFloat(ua.substr(i + s.length));
	  return;
	}
  
	s = "Netscape6/";
	if ((i = ua.indexOf(s)) >= 0) {
	  this.isNS = true;
	  this.version = parseFloat(ua.substr(i + s.length));
	  return;
	}
  
	// Treat any other "Gecko" browser as NS 6.1.
  
	s = "Gecko";
	if ((i = ua.indexOf(s)) >= 0) {
	  this.isNS = true;
	  this.version = 6.1;
	  return;
	}
}
function encrypt(input, output)
{
	var inId = document.getElementById(input);
	var ouId = document.getElementById(output);
	var inp_value = inId.value;
	inId.value = '';
	ouId.value = md5(inp_value);
}
// -------------------------------------------- //

// --------------- Saved tabs ----------------- //
function saved_tabs(obj,tab_id,target,target_id)
{
	if(obj.className == 'ico-pin')
		action = 'pin';
	else
		action = 'close';
	
	jQuery.ajax({
	 type: "GET",
	 queue: true,
	 url: "ajax/saved_tabs.php",
	 data: "action="+ action +"&target="+ target +"&target_id="+ target_id,
	 success: function(html){
		if(html == true) {
			if(action == 'pin') {
				obj.className = 'ico-remove';
			}
			else {
				obj.className = 'ico-pin';
				var tab = document.getElementById('tab_'+ tab_id);
				if(tab.className == 'top_menu_btn')
					tab.style.display = 'none';
			}
		}
		else
			alert(html);
	 }
	});
}
// -------------------------------------------- //

// --------------- Bottom bar ----------------- //
function open_bm_right(eId,action)
{
	var menuId = document.getElementById('bm_rightside_open_menu');
	
	if(bm_right_open == eId)
	{
		eId.className = 'bm_rightside_btn';
		menuId.style.visibility = 'hidden';
		menuId.innerHTML = '';
		bm_right_open = '';
	}
	else
	{
		if(bm_right_open != '')
		{
			bm_right_open.className = 'bm_rightside_btn';
		}
		
		eId.className = 'bm_rightside_btn_open';
		
		if(action == 'notifications')
		{
			jQuery.ajax({
			 type: "POST",
			 queue: true,
			 url: "ajax/notification_list.php",
			 success: function(html){
			   document.getElementById('bm_rightside_open_menu').innerHTML=html;
			 }
			});
		}
		else if(action == 'online_friends')
		{
			var html = "";
			jQuery.each(online_friends, function(i,item)
			{
				userid = item.userid;
				name = item.name;
				onlineStatus = item.onlineStatus;
				thumb = item.thumb;
				onClick = 'chat_new_window(\'' + userid + '\');';
				
				if(name.length > 27)
					shortName = name.substring(0,26)+"...";
				else
					shortName = name;
				
				html = html + '<span class="bm_nf_box" onclick="' + onClick + ' open_bm_right(document.getElementById(\'bm_of_btn\'),\'online_friends\');">' +
				'<div class="thumb_30_status_image" style="margin-top:2px; margin-bottom:2px;" title="'+ name +'"><img src="'+ thumb +'">'+
				'<span rel="'+ userid +'" class="thumb_status_Offline"></span></div>'+
				'<span class="bm_chat_name" title="' + name + '">'+ shortName + '</span> ' +
				'<span class="chat_status_' + onlineStatus + '">' + onlineStatus + '</span></span>';
			});
			if(online_friends.length == 0)
			{
				html = '<table cellpadding="2px" cellspacing="0px" style="margin:5px; width:230px;">' +
					'<tr height="25px;"><td align="center" style="color:#999;">No friends online</td></tr></table>';
			}
			document.getElementById('bm_rightside_open_menu').innerHTML = html;
			update_online_status();
		}
		
		menuId.style.visibility = 'visible';
		bm_right_open = eId;
	}
}
// -------------------------------------------- //

// --------------- Dialog box ----------------- //
function open_dialog(title,url,height,width)
{
	document.getElementById('dialog_bg').style.display='block';
	document.getElementById('dialog_header').innerHTML=title;
	if(width > 300)
		document.getElementById('dialog_border').style.width = width + 'px';
	
/*	if(height > 150)
		document.getElementById('dialog_border').style.height = height + 'px';
	else
		document.getElementById('dialog_border').style.height = '140px';
*/	
	jQuery.ajax({
	 type: "GET",
	 queue: true,
	 url: url,
	 success: function(html){
		document.getElementById('dialog_content').innerHTML = html;
	 }
	});
}
function close_dialog()
{
	document.getElementById('dialog_bg').style.display='none';
	document.getElementById('dialog_header').innerHTML='';
	document.getElementById('dialog_content').innerHTML='';
}
function dialog_bg_click(e)
{
	var event = e || window.event;
	if( ! event.target ) {
		event.target = event.srcElement
	}
	
	if (event.target.id == 'dialog_bg') {
		if(confirm('Are you shure you want to close this dialog box?')) 
			close_dialog()
	}
}
function friend_dialog_action(fid,action)
{
	jQuery.ajax({
	 type: "GET",
	 queue: true,
	 url: "ajax/friend_rr_action.php",
	 data: "fid="+ fid +"&action="+ action,
	 success: function(html){
		document.getElementById('dialog_content').innerHTML=html;
	 }
	});
	
	if(action == 'invite')
		setTimeout('close_dialog()',200);
	else
		setTimeout('window.location.reload(true)',200);
}
function group_dialog_action(group,action)
{
	jQuery.ajax({
	 type: "GET",
	 queue: true,
	 url: "ajax/group_action.php",
	 data: "group="+ group +"&action="+ action,
	 success: function(html){
		document.getElementById('dialog_content').innerHTML=html;
	 }
	});
	
	if(action == 'request')
		setTimeout('close_dialog()',200);
	else
		setTimeout('window.location.reload(true)',200);
}
function event_dialog_action(event_id,action)
{
	for (var i=0; i < document.rsvpForm.rsvp.length; i++)
	{
		if (document.rsvpForm.rsvp[i].checked)
		{
			var rsvp = document.rsvpForm.rsvp[i].value;
		}
	}
	
	var notes = document.getElementById('rsvp_notes').value;
	
	jQuery.ajax({
	 type: "GET",
	 queue: true,
	 url: "ajax/rsvp_event.php",
	 data: "event="+ event_id +"&action="+ action +"&rsvp="+ rsvp +"&notes="+ notes,
	 success: function(html){
		document.getElementById('dialog_content').innerHTML=html;
	 }
	});
	
	if(action == 'request')
		setTimeout('close_dialog()',200);
	else
		setTimeout('window.location.reload(true)',200);
}
// -------------------------------------------- //

// ----------------- Objects ------------------ //
function online_friend(userid,onlineStatus,name,thumb)
{
	this.userid = userid;
	this.onlineStatus = onlineStatus;
	this.name = name;
	this.thumb = thumb;
}
function chat(chat_id,to_userid,chat_status,chat_name,user_writes,unread_msgs)
{
	this.chat_id = chat_id;
	this.to_userid = to_userid;
	this.chat_status = chat_status;
	this.chat_name = chat_name;
	this.user_writes = user_writes;
	this.unread_msgs = unread_msgs;
}
function pTag(x,y,size,Tag)
{
	this.x = x;
	this.y = y;
	this.s = size;
	this.t = Tag;
}
// -------------------------------------------- //

// ----------------- Channel ------------------ //
function bm_right_pulse()
{
	channel();
}
function channel()
{
	jQuery.getJSON('ajax/channel.php', function(data) {
		var of = document.getElementById('bm_num_of');
		var nf = document.getElementById('bm_num_nf');
		online_friends = new Array();
		if(data.online_friends != null)
		{
			jQuery.each(data.online_friends, function(i,item)
			{
				userid = item.userid;
				name = item.name;
				onlineStatus = item.status;
				thumb = item.thumb;
				online_friends[i] = new online_friend(userid,onlineStatus,name,thumb);
			});
			num_online = online_friends.length;
			update_online_status();
		}
		else
		{
			num_online = 0;
			update_online_status();
		}
		if(data.chats != null)
		{
			jQuery.each(data.chats, function(i,item)
			{
				chat_id = item.chat_id;
				to_userid = item.to_userid;
				chat_status = item.chat_status;
				chat_name = item.chat_name;
				user_writes = item.user_writes;
				unread_msgs = item.unread_msgs;
				chats[i] = new chat(chat_id,to_userid,chat_status,chat_name,user_writes,unread_msgs);
				if(!document.getElementById('chat_container_'+chat_id))
					chat_receive_window(chats[i]);
				if(unread_msgs > 0)
					chat_notify(chat_id,'start');
			});
		}
		// if(num_online == 0) of.innerHTML = ''; else 
		of.innerHTML = '('+num_online+')';
		if(data.nf == 0) nf.innerHTML = ''; else nf.innerHTML = '('+data.nf+')';
	});
	
	setTimeout('channel()',3000);
}
// -------------------------------------------- //

// ----------------- Search ------------------- //
function search_text(obj, text)
{
	if(obj.value == text)
	{
		obj.className = 'sc_search_field_focus';
		obj.value = '';
		document.getElementById('search_btn').disabled = false;
	}
	else if(obj.value == '')
	{
		obj.className = 'sc_search_field';
		obj.value = text;
		document.getElementById('search_btn').disabled = true;
	}
}
function live_search(obj,target)
{
	var q = obj.value;
	
	jQuery.ajax({
	 type: "GET",
	 queue: true,
	 url: "ajax/live_search.php",
	 data: "q="+ q +"&target="+ target,
	 success: function(html){
		var resultBox = document.getElementById(target);
		if(html != '' && obj.value != '')
			resultBox.style.display = 'inline';
		else
			resultBox.style.display = 'none';
		resultBox.innerHTML=html;
	 }
	});
}
// -------------------------------------------- //

// ------------------ Posts ------------------- //
function active_textarea(eid,action,str,class_name)
{
	if(action == 'focus' && eid.value == str)
	{
		eid.value='';
		eid.className=class_name+'_focus';
		eid.rows = 3;
		if(class_name == 'profile_message_textbox')
			document.getElementById('profile_message_btn').style.display='block';
	}
	else if(action == 'blur' && eid.value == '')
	{
		eid.value=str;
		eid.className=class_name;
		if(class_name == 'profile_message_textbox')
			document.getElementById('profile_message_btn').style.display='none';
	}
}
function show_hide_comment_btn(eId,str,boxId)
{
	var eId = document.getElementById(eId);
	if(eId.style.display == 'none')
	{
		eId.style.display='block';
	}
	else
	{
		if(boxId.value == '' || boxId.value == str)
			eId.style.display='none';
	}
}
function show_comment_form(itemId)
{
	document.getElementById('nf_comment_form_'+itemId).style.display = 'block';
	document.getElementById('comment_box_'+itemId).focus();
}
function like(eId,item_id,tbl_name)
{
	var like;
	if(eId.innerHTML == likeName)
	{ like = 1; eId.innerHTML = unlikeName; }
	else
	{ like = 0; eId.innerHTML = likeName; }
	
	jQuery.ajax({
	 type: "GET",
	 queue: true,
	 url: "ajax/like_action.php",
	 data: "item_id="+ item_id +"&tbl_name="+ tbl_name +"&like="+ like,
	 success: function(html){
		document.getElementById('profile_msg_like_'+item_id).innerHTML=html;
	 }
	});
}
function post_comment(eId,item_id,tbl_name)
{
	var comment;
	comment = document.getElementById(eId).value;
	
	jQuery.ajax({
	 type: "POST",
	 queue: true,
	 url: "ajax/comment_action.php",
	 data: "item_id="+ item_id +"&tbl_name="+ tbl_name +"&comment="+ comment,
	 success: function(html){
		document.getElementById('profile_msg_comment_'+item_id).innerHTML=html;
		update_online_status();
	 }
	});
}
function post_message(eId,tread_id,msg)
{
	var message;
	textarea = document.getElementById(eId);
	message = textarea.value;
	userid = document.getElementById('msg_to_userid').value;
	
	if(tread_id == 0)
		data = "message="+ message +"&userid="+ userid;
	else
		data = "message="+ message +"&tread_id="+ tread_id;
	
	jQuery.ajax({
	 type: "POST",
	 queue: true,
	 url: "ajax/msg_post_msg.php",
	 data: data,
	 success: function(html){
		if(tread_id == 0) {
			document.getElementById('dialog_content').innerHTML = msg;
			setTimeout('close_dialog()',400);
		}
		else {
			document.getElementById('msg-tread_msg-msgs').innerHTML+=html;
			textarea.value = msg;
			textarea.className = 'msg-tread_msg-field';
			document.getElementById('msg-tread_msg-btn').style.display = 'none';
		}
	 }
	});
}
// -------------------------------------------- //

// -------------- Validation ------------------ //
function msg_form_validation()
{
	subject = document.getElementById('subject').value;
	message = document.getElementById('msg-tread_msg-field').value;
	
	if(subject != '' && subject != 'Subject' && message != '')
		return true;
	else
		return false;
}
// -------------------------------------------- //

// ----------------- Chat --------------------- //
function chat_window_status(obj,chat_id)
{
	if(open_chat_id != null && open_chat_id != chat_id)
	{
		var old_chat_btn = document.getElementById('chat_btn_'+open_chat_id);
		var old_chat_box = document.getElementById('chat_box_'+open_chat_id);
		if(old_chat_btn.className == 'bm_leftside_btn_open')
		{
			old_chat_btn.className = 'bm_leftside_btn';
			old_chat_box.style.visibility = 'hidden';
		}
	}
	var chat_box = document.getElementById('chat_box_'+chat_id);
	open_chat_id = chat_id;
	if(obj.className == 'bm_leftside_btn')
	{
		var status = 1;
		jQuery.ajax({
		 type: "POST",
		 queue: true,
		 url: "ajax/chat.php",
		 data: ({event : 'show', id : chat_id}),
		 success: function(html){
			 chat_box = document.getElementById('chat_box_'+ chat_id);
			 chat_box.innerHTML = html;
		 }
		});
		chat_update_interval = setInterval(chat_new_msgs,3000);
	}
	else
	{
		var status = 2;
		clearInterval(chat_update_interval);
	}
	
	jQuery.ajax({
	 type: "POST",
	 queue: true,
	 url: "ajax/chat.php",
	 data: ({event : 'status', id : chat_id, ms : status}),
	 success: function(html){
		if(status == 2)
		{
			obj.className = 'bm_leftside_btn';
			chat_box.style.visibility = 'hidden';
		}
		else
		{
			obj.className = 'bm_leftside_btn_open';
			chat_box.style.visibility = 'visible';
			var chat_msgs = document.getElementById('chat_msgs_'+chat_id);
			chat_msgs.scrollTop = chat_msgs.scrollHeight;
			var chat_input = document.getElementById('chat_input_'+chat_id);
			chat_input.focus();
		}
	 }
	});
}
function chat_notify(chat_id,action)
{
	var btn = document.getElementById('chat_btn_'+chat_id);
	
	if(btn.className == 'bm_leftside_btn_unread')
	{
		btn.className = 'bm_leftside_btn';
		document.title = oldTitle;
	}
	else if(btn.className != 'bm_leftside_btn_open')
	{
		btn.className = 'bm_leftside_btn_unread';
		if(document.title != 'New message in chat!')
			oldTitle = document.title;
		document.title = 'New message in chat!';
	}
}
function chat_window_close(chat_id)
{
	var chat_container = document.getElementById('chat_container_'+chat_id);
	
	clearInterval(chat_update_interval);
	if(open_chat_id == chat_id)
		open_chat_id = null;
	
	jQuery.ajax({
	 type: "POST",
	 queue: true,
	 url: "ajax/chat.php",
	 data: ({event : 'close', id : chat_id}),
	 success: function(html){
		 var d = document.getElementById('bottom_menubar');
		 d.removeChild(chat_container);
	 }
	});
}
function chat_new_window(userid)
{
	jQuery.post('ajax/chat.php', {event : 'new', id : userid}, function(data) {
		chat_name = data.chat_name;
		chat_id = data.chat_id;
		chat_status = data.chat_status;
		
		if(chat_name != 'exist')
		{
			var newdiv = document.createElement('div');
			newdiv.setAttribute('id',"chat_container_"+ chat_id);
			newdiv.className = 'bm_chat_container';
			
			html = '<div id="chat_box_'+ chat_id +'" class="bm_chat_open_box"></div>'+
			'<div id="chat_btn_'+ chat_id +'" class="bm_leftside_btn" onclick="chat_window_status(this,\''+ chat_id +'\');">'+
			'<span class="thumb_30_status_image" style="margin:0px; width:0px;"><span rel="'+ userid +'" class="thumb_status_Offline" style="width:8px; padding:0px;"></span></span>'+
			chat_name +'</div>';
			
			newdiv.innerHTML = html;
			document.getElementById('bottom_menubar').appendChild(newdiv);
		}
		if(chat_status != 1)
			chat_window_status(document.getElementById('chat_btn_' + chat_id),chat_id)
		else
			document.getElementById('chat_input_' + chat_id).focus();
		update_online_status();
	}, "json");
}
function chat_receive_window(chat)
{
	chat_name = chat.chat_name;
	chat_id = chat.chat_id;
	chat_status = chat.chat_status;
	userid = chat.to_userid;
	
	if(chat_name != 'exist')
	{
		var newdiv = document.createElement('div');
		newdiv.setAttribute('id',"chat_container_"+ chat_id);
		newdiv.className = 'bm_chat_container';
		
		html = '<div id="chat_box_'+ chat_id +'" class="bm_chat_open_box"></div>'+
		'<div id="chat_btn_'+ chat_id +'" class="bm_leftside_btn" onclick="chat_window_status(this,\''+ chat_id +'\');">'+
		'<span class="thumb_30_status_image" style="margin:0px; width:0px;"><span rel="'+ userid +'" class="thumb_status_Offline" style="width:8px; padding:0px;"></span></span>'+
		chat_name +'</div>';
		
		newdiv.innerHTML = html;
		document.getElementById('bottom_menubar').appendChild(newdiv);
	}
	update_online_status();
}
function runScript(e,obj,userid,chat_id)
{
    if (e.keyCode == 13) {
      	chat_send_msg(obj,userid,chat_id);
        return false;
    }
}
function chat_send_msg(obj,userid,chat_id)
{
	var message = obj.value;
	obj.value = '';
	
	jQuery.ajax({
	 type: "POST",
	 queue: true,
	 url: "ajax/chat.php",
	 data: ({event : 'send', id : userid, ms : message}),
	 success: function(html){
		var chat_msgs = document.getElementById('chat_msgs_'+chat_id);
		chat_msgs.innerHTML+=html;
		chat_msgs.scrollTop = chat_msgs.scrollHeight;
	 }
	});
}
function chat_new_msgs()
{
	var chat_id = open_chat_id;
	jQuery.ajax({
	 type: "POST",
	 queue: true,
	 url: "ajax/chat.php",
	 data: ({event : 'msgs', id : chat_id}),
	 success: function(html){
		var chat_msgs = document.getElementById('chat_msgs_'+chat_id);
		if(html != '')
		{
			chat_msgs.innerHTML+=html;
			chat_msgs.scrollTop = chat_msgs.scrollHeight;
		}
	 }
	});
}
function update_status(new_status)
{
	jQuery.ajax({
	 type: "GET",
	 queue: true,
	 url: "ajax/update_status.php",
	 data: "status="+ new_status,
	 success: function(html){
		if(new_status == 'Idle')
			new_status = 'Away';
		iUserStatus = new_status;
		update_online_status();
	 }
	});
}
// -------------------------------------------- //

// --------------- wmn menu ------------------- //
function display_wmn_menu()
{
	if(jQuery("#wmn_menu").css('visibility') == 'visible') {
		jQuery("#wmn_menu").css('visibility','hidden');
		jQuery("body").unbind('click');
	}
	else {
		jQuery("#wmn_menu").css('visibility','visible');
	
		jQuery("body").click(function() {
			jQuery("#wmn_menu").css('visibility','hidden');
			jQuery("body").unbind('click');
		});
	}
}
// -------------------------------------------- //

// ---------------- Profile ------------------- //
function clear_profile_status()
{
	jQuery.ajax({
	 type: "GET",
	 queue: true,
	 url: "ajax/clear_profilestatus.php",
	 success: function(html){
		document.getElementById('profile_status').innerHTML='';
	 }
	});
}
function relationship_change(obj)
{
	var rel_status = obj.options[obj.selectedIndex].value;
	var rel_text;
	
	if(rel_status == 1)
	{
		rel_text = '';
	}
	else if(rel_status == 3 || rel_status == 4)
	{
		rel_text = '..to:';
	}
	else
	{
		rel_text = '..with:';
	}
	
	if(rel_status > 1)
	{
		document.getElementById('rel_text').innerHTML=rel_text;
		document.getElementById('rel_opt').style.display='table-row';
	}
	else
	{
		document.getElementById('rel_opt').style.display='none';
	}
}
function moveThumb(e)
{
	if (e == null) 
		var e = window.event;
	
	var CropMe = document.getElementById('CropMe');
	var thumb_from = document.getElementById('thumb_from');
	var pLeft = document.getElementById('pLeft');
	var pTop = document.getElementById('pTop');
	
	if(e.type == 'mousedown' || e.type == 'onmousedown') {
		
		oldLeft = CropMe.style.left;
		oldTop = CropMe.style.top;
		
		moveObj.oldX = e.clientX;
		moveObj.oldY = e.clientY;
		moveObj.oldLeft = Number(oldLeft.replace(/px/i,""));
		moveObj.oldTop = Number(oldTop.replace(/px/i,""));
		
		if (browser.isIE) {
		  document.attachEvent("onmousemove", moveThumb);
		  document.attachEvent("onmouseup",   moveThumb);
		  window.event.cancelBubble = true;
		  window.event.returnValue = false;
		  e.preventDefault();
		}
		if (browser.isNS) {
		  document.addEventListener("mousemove", moveThumb,   true);
		  document.addEventListener("mouseup",   moveThumb, true);
		  e.preventDefault();
		}
	}
	else if(e.type == 'mouseup' || e.type == 'onmouseup') {
		if (browser.isIE) {
		  document.detachEvent("onmousemove", moveThumb);
		  document.detachEvent("onmouseup",   moveThumb);
		}
		if (browser.isNS) {
		  document.removeEventListener("mousemove", moveThumb,   true);
		  document.removeEventListener("mouseup",   moveThumb, true);
		}
	}
	else {
		var newLeft = ((e.clientX - moveObj.oldX) + moveObj.oldLeft);
		var newTop = ((e.clientY - moveObj.oldY) + moveObj.oldTop);
		var imgWidth = CropMe.width;
		var imgHeight = CropMe.height;
		
		if(newLeft > 0)
			newLeft = 0;
		else if((50 - imgWidth) > newLeft)
			newLeft = (50-imgWidth);
		
		if(newTop > 0)
			newTop = 0;
		else if((50 - imgHeight) > newTop)
			newTop = (50-imgHeight);
			
		CropMe.style.left = newLeft + "px";
		CropMe.style.top = newTop + "px";
		
		pLeft.value = newLeft;
		pTop.value = newTop;
	}
}
function drawAlbumThumb(eId,src1,x1,y1,src2,x2,y2,src3,x3,y3)
{
	var ctx = document.getElementById(eId).getContext('2d');
	
	var new_x1 = ((180-x1)/2)-5;
	var new_y1 = ((150-y1)/2);
	var new_x2 = ((180-x2)/2)+5;
	var new_y2 = ((150-y2)/2)-10;
	var new_x3 = ((180-x3)/2)-15;
	var new_y3 = ((150-y3)/2)+15;
	
	var img3 = new Image();
	var img2 = new Image();
	var img1 = new Image();
	img3.onload = function(){  
	  ctx.save();
	  ctx.rotate(-10 * Math.PI / 180);
	  ctx.fillStyle = "rgb(255,255,255)";
	  ctx.strokeStyle = "rgb(0,0,0)";
	  ctx.lineWidth = 0.5;
	  ctx.fillRect (new_x3-5, new_y3-5, x3+10, y3+10);
	  ctx.save();
	  ctx.restore();
	  ctx.drawImage(img3,new_x3,new_y3);
	  //ctx.strokeRect (new_x3+1, new_y3+1, x3-2, y3-2);
	  ctx.strokeRect (new_x3-5, new_y3-5, x3+10, y3+10);
	  ctx.restore();
	}
	img2.onload = function(){  
	  ctx.save();
	  ctx.rotate(10 * Math.PI / 180);
	  ctx.fillStyle = "rgb(255,255,255)";
	  ctx.strokeStyle = "rgb(0,0,0)";
	  ctx.lineWidth = 0.5;
	  ctx.fillRect (new_x2-5, new_y2-5, x2+10, y2+10);
	  ctx.save();
	  ctx.restore();
	  ctx.drawImage(img2,new_x2,new_y2);
	  //ctx.strokeRect (new_x2+1, new_y2+1, x2-2, y2-2);
	  ctx.strokeRect (new_x2-5, new_y2-5, x2+10, y2+10);
	  ctx.restore();
	}
	img1.onload = function(){  
	  ctx.save();
	  ctx.fillStyle = "rgb(255,255,255)";
	  ctx.strokeStyle = "rgb(0,0,0)";
	  ctx.lineWidth = 0.5;
	  ctx.fillRect (new_x1-5, new_y1-5, x1+10, y1+10);
	  ctx.save();
	  ctx.restore();
	  ctx.drawImage(img1,new_x1,new_y1);
	  //ctx.strokeRect (new_x1+1, new_y1+1, x1-2, y1-2);
	  ctx.strokeRect (new_x1-5, new_y1-5, x1+10, y1+10);
	  ctx.restore();
	}
	img3.src = src3;
	img2.src = src2;
	img1.src = src1;
}
function open_profilepicture_menu()
{
	if (browser.isIE) {
	  document.attachEvent("onclick", close_profilepicture_menu);
	  window.event.cancelBubble = true;
	  window.event.returnValue = false;
	}
	if (browser.isNS) {
	  document.addEventListener("click", close_profilepicture_menu,   true);
	}
	document.getElementById('profile_picture_menu').style.display='block';
}
function close_profilepicture_menu()
{
	document.getElementById('profile_picture_menu').style.display='none';
	document.getElementById('change_profilepicture_btn').style.display='none';
	if (browser.isIE) {
	  document.detachEvent("onclick", close_profilepicture_menu);
	}
	if (browser.isNS) {
	  document.removeEventListener("click", close_profilepicture_menu,   true);
	}
}
function place_image_tag(obj,e)
{
	if (e == null) 
		var e = window.event;
	
	imgTag.x = (e.pageX - (obj.offsetLeft));
	imgTag.y = (e.pageY - (obj.offsetTop));
	
	imgWidth = obj.width;
	imgHeight = obj.height;
	
	eId = document.getElementById('image_tag_box');
	selectorId = document.getElementById('image_tag_selector');
	size = 160;
	
	if(imgTag.x > (imgWidth - 86)) {
		if(imgTag.x < (imgWidth - 50))
			size = ((imgWidth - imgTag.x) * 2);
		else
			size = 100;
		eId.style.left = ((obj.offsetLeft) + (imgWidth - size))-8 + "px";
	}
	else if(imgTag.x <= 80) {
		if(imgTag.x > 50)
			size = (imgTag.x * 2);
		else
			size = 100;
		eId.style.left = (e.pageX - imgTag.x) + "px";
	}
	
	if(imgTag.y > (imgHeight - 86)) {
		if(imgTag.y < (imgHeight - 50) && size > (imgTag.y * 2))
			size = ((imgHeight - imgTag.y) * 2);
		else if(size > 100)
			size = 100;
		eId.style.top = ((obj.offsetTop) + (imgHeight - size))-8 + "px";
	}
	else if(imgTag.y <= 80) {
		if(imgTag.y > 50 && size > (imgTag.y * 2))
			size = (imgTag.y * 2);
		else if(size > 100)
			size = 100;
		eId.style.top = (e.pageY - imgTag.y) + "px";
	}
	
	if(imgTag.x > 80 && imgTag.x < (imgWidth - 86))
		eId.style.left = (e.pageX - (size/2))-3 + "px";
	
	if(imgTag.y > 80 && imgTag.y < (imgHeight - 86))
		eId.style.top = (e.pageY - (size/2))-3 + "px";
	
	eId.style.height = size + "px";
	eId.style.width = size + "px";
	
	if(imgTag.x < 80)
		selectorId.style.left = (e.pageX - imgTag.x + size + 15) + "px";
	else if(imgTag.x > (imgWidth - 86))
		selectorId.style.left = ((obj.offsetLeft) + (imgWidth + 15))-8 + "px";
	else
		selectorId.style.left = (e.pageX + (size/2) + 15) + "px";
	selectorId.style.top = eId.style.top;
	
	imgTag.size = size;
	tag_friend_search();
	
	if(eId.style.display == 'none')
		eId.style.display = 'block';
	if(selectorId.style.display == 'none')
		selectorId.style.display = 'block';
	
	document.getElementById('tag_input').value = '';
	tag_friend_search();
	document.getElementById('tag_input').focus();
	//alert(imgTag.x + ":" + imgTag.y);
	//alert(imgWidth + ":" + imgHeight);
}
function cancel_image_tag()
{
	eId = document.getElementById('image_tag_box');
	selectorId = document.getElementById('image_tag_selector');
	if(eId.style.display == 'block')
		eId.style.display = 'none';
	if(selectorId.style.display == 'block')
		selectorId.style.display = 'none';
}
function tag_friend_search()
{
	query = document.getElementById('tag_input').value;
	
	jQuery.ajax({
	 type: "GET",
	 queue: true,
	 url: "ajax/image_friend_tag.php",
	 data: "q="+ query +"&filter="+ photoId,
	 success: function(html){
		document.getElementById('tag_friends_list').innerHTML = html;
	 }
	});
}
function set_image_tag(userid)
{
	tag = document.getElementById('tag_input').value;
	document.getElementById('tag_input').value = '';
	if(userid == null)
		userid = jQuery("input:checkbox[name=userid]:checked").value;
	
	if(userid == undefined)
		userid = 0;
	
	jQuery.post("ajax/save_image_tag.php", { left: imgTag.x, top: imgTag.y, size: imgTag.size, userid: userid, tag: tag, photo_id: photoId },
		function(data) {
			if(data != '')
				tag = data;
			id = 1;
			jQuery.each(PhotoTags, function(k, tag){
				id++;
			});
			PhotoTag(id,imgTag.x,imgTag.y,imgTag.size,tag);
		}
	);
	cancel_image_tag();
}
function PhotoTag(id,x,y,size,Tag)
{
	PhotoTags[id] = new pTag(x,y,size,Tag);
}
function PhotoTagLabels(e)
{
	if (e == null) 
		var e = window.event;
	
	offset = jQuery(".tagEnabledPhoto").offset();
	
	x = (e.pageX - offset.left);
	y = (e.pageY - offset.top);
	show = null;
	
	jQuery.each(PhotoTags, function(k, tag){
		pleft = tag.x - (tag.s/2);
		pright = tag.x + (tag.s/2);
		ptop = tag.y - (tag.s/2);
		pbottom = tag.y + (tag.s/2);
		
		if((x > pleft && x < pright) && (y > ptop && y < pbottom)) {
			if(show != null) {
				lastshow = PhotoTags[show]
				if(lastshow.x > x) {	lastx = (lastshow.x - x); }
				else {					lastx = (x - lastshow.x); }
				if(lastshow.y > y) {	lasty = (lastshow.y - y); }
				else {					lasty = (y - lastshow.y); }
				if(tag.x > x) {	tagx = (tag.x - x); }
				else {					tagx = (x - tag.x); }
				if(tag.y > y) {	tagy = (tag.y - y); }
				else {					tagy = (y - tag.y); }
				
				//alert(tagx+">"+lastx+" - "+tagy+">"+lasty);
				if((tagx+tagy) < (lastx+lasty))
					show = k;
			}
			else
				show = k;
		}
	});
	
	if(show == null || jQuery(".phototaglabel").length != 0)
		RemoveTagLabel();
	
	if(show != null && jQuery(".phototaglabel").length == 0) {
		tag = PhotoTags[show];
		pleft = (tag.x + offset.left);
		pbottom = (tag.y + (tag.s/3) + offset.top);
		CreateTagLabel(show,pleft,pbottom,tag.t);
	}
}
function CreateTagLabel(id,pleft,ptop,label)
{
	var newdiv = document.createElement('div');
	newdiv.setAttribute('id',"tagLabel_" + id);
	newdiv.className = "phototaglabel";
	newdiv.innerHTML = "<span>"+label+"</span>";
	document.body.appendChild(newdiv);
	width = jQuery("#tagLabel_" + id).width();
	newLeft = (pleft - (width/2));
	newdiv.style.left = newLeft +"px";
	newdiv.style.top = ptop +"px";
}
function RemoveTagLabel()
{
	jQuery(".phototaglabel").remove();
}
function update_online_status()
{
	jQuery(".thumb_status_Online").removeClass().addClass('thumb_status_Offline');
	jQuery(".thumb_status_Away").removeClass().addClass('thumb_status_Offline');
	jQuery(".thumb_status_Busy").removeClass().addClass('thumb_status_Offline');
	
	jQuery("span[rel='"+ iUserid +"']").removeClass().addClass('thumb_status_'+iUserStatus);
	
	jQuery.each(online_friends, function(i,item)
	{
		userid = item.userid;
		onlineStatus = item.onlineStatus;
		
		jQuery("span[rel='"+ userid +"']").removeClass().addClass('thumb_status_'+onlineStatus);
	});
}
function start_message_tread()
{
	
}
// -------------------------------------------- //

// --------------- Calendar ------------------- //
function scroll_week()
{
	document.getElementById('calendar_pane').scrollTop = 320;
}
// -------------------------------------------- //


