var Spell_Win_Name;
var Work_Form_Name;
var Work_Field_Name;

function SpellCheck(form_name,field_name) 
{

  
  var textform = self.document[form_name][field_name].value;
  
    if ( ! Spell_Win_Name || Spell_Win_Name.closed || (Work_Form_Name != form_name || Work_Field_Name != field_name))
  {
  Spell_Win_Name = win_pop('');
  if (Spell_Win_Name.focus) { Spell_Win_Name.focus(); }
  
  self.document.hidden_form.form_name.value = form_name; 	//w skritata forma se izpolzwa ime na pole form_name i field_name
  self.document.hidden_form.field_name.value = field_name;  	// koito nqmat nishto obshto s promrnliwite w tazi funkciq
  self.document.hidden_form.first_time_text.value = textform;
  self.document.hidden_form.submit();
  
  Work_Form_Name=form_name;
  Work_Field_Name=field_name;
  }
  else
  {
  if (Spell_Win_Name.focus) { Spell_Win_Name.focus(); }
  }

}

function win_pop(URL)
{	
	winname=window.open(URL,'WIN','width=350,height=380,left=210,top=210,resizable=yes,scrollbars=yes,status=yes'); 
	return winname;
}


function stripSlashes(str)
{
return str.replace(/\\/g, '');
}


var message = ""

function trim(stringToTrim) {
	return stringToTrim.replace( /[\=\^\s+|\s+$\.\"\~\@\^\&\<\>\`\*\+\|\}\{\+,;!#\$\%\/:\?'\(\)\[\]_\-\\]/g, "");
}


function showresult() {		
if(document.getElementById('username')) {
//message=document.forms[0].elements[0].value

message=document.signupForm.username.value.toLowerCase()
message=trim(message)
firstfont.innerHTML='<small><b>URL:</b></small><font color=#000066>&nbsp;http://</font><font color=silver>' + message + '</font><font color=#000066>.spoink.com</font>'


}
var timer=setTimeout("showresult()",300) 
}

function display(obj,id1,id2) {
txt = obj.options[obj.selectedIndex].value;
document.getElementById(id1).style.display = 'none';
document.getElementById(id2).style.display = 'none';
if ( txt.match(id1) ) {
document.getElementById(id1).style.display = 'block';
}
if ( txt.match(id2) ) {
document.getElementById(id2).style.display = 'block';
}
}



function messages(){
	var html = $.ajax({
	url: "ajax/messages.php",
	async: false
	}).responseText;
	$("div#messages_all").empty().append(html);
}

function set_notify_mode(stuff){
	var result = $.ajax({
		type: "GET",
		url: "ajax/set_notify_way.php",
		data: "stuff=" + stuff,
		async: false
	}).responseText;
	$("#ncmr").empty().append(result);
}

function set_msg_notification(stuff){
	var result = $.ajax({
		type: "GET",
		url: "ajax/message_notification.php",
		data: "stuff=" + stuff,
		async: false
	}).responseText;
	$("#mnr").empty().append(result);
}

function make_form_submit(n, id)
{
	var x = $("<form action='' method='post'></form>").append("<input type=hidden name='" + n + "' value='" + id + "'>").hide().appendTo(document.body).get(0);
	setTimeout(function() {
		x.submit(); return;
	}, 100);
}

function edit_my_phone(id) {
	make_form_submit('edit_id', id);
}

function play_video(uid)
{
	var w = document.body.offsetWidth;
	var h = document.body.offsetHeight;
	var pw = 300, ph = 200;
	var t = 300 + document.documentElement.scrollTop || 0;
	var l = ((w - pw) / 2) + document.documentElement.scrollLeft || 0;

	if(!window.backLayout)
		window.backLayout = $("<div class='x-layout-back'>").prependTo(document.body);
	if(!window.backMask)
	{
		window.backMask = $("<div class='x-layout-mask'>").appendTo(window.backLayout)
			.css({width: w + "px", height: h + "px"})
			.click(stop_play)
	}
	if(!window.playerWindow)
	{
		window.playerWindow = $("<div class='x-layout-player'>").appendTo(window.backMask)
			.css({left: l +"px", top: t + "px", width: pw + "px", height: ph + "px"})
	}
	$.post("ajax/movie.php", {uid:uid}, function(rsp) {
		$(document.body).find("embed").css("visibility", "hidden")
		window.playerWindow
			.html(rsp)
			.css({left: l +"px", top: t + "px", width: pw + "px", height: ph + "px"})
			.show()
		window.backMask.show()
			.css("opacity", 0.75)			
		window.backMask.find("embed").css("visibility", "visible");
	});
}
function stop_play()
{
       	$(this).hide();
       	window.playerWindow.html('');
	$(document.body).find("embed").css("visibility", "visible");
}

function checkbox_form_submit(n, c) {
	make_form_submit(n, c.checked ? 'true' : 'false');
}

function remove_my_phone(id) {
	if(!confirm("Sure ?")) return;
	make_form_submit('rm_id', id);
}

function my_channel_subscribe(id) {
	make_form_submit('sid', id);
}
function view_channel(id) {
	make_form_submit('cid', id);
}

function set_desc_popups(container, desc)
{
	var c = $(container);
	c.each(function() {
	       	$(this).hover(function(){
	       		$("body").append("<div id='last_msg_tooltip'>" + $(this).find(desc).get(0).innerHTML + "</div>");
	       	}, function(){
       			$("#last_msg_tooltip").remove();
	       	});
       	});
}


$(document).ready(function(){
	$("#cloud_tags a.tags_more").click(function() {
		$(this).hide().find( "span.tags_more" ).show();
		var r = $(this).attr("rel");
		$(this.parentNode).find("span[@rel=" + r + "]").show();
		return false;
	});
	$("#note").keyup(function(){
		$("#additional_note").empty().append($(this).val());
	});

	$("#choose_photo").hide();
	$("#upload").addClass("current");

	$("#upload").click(function(){
		$("#choose_photo").hide();
		$("#upload_photo").fadeIn("slow");
		$(this).addClass("current");
		$("#photo_set").removeClass("current");
		return false;
	});

	$("#photo_set").click(function(){
		$("#upload_photo").hide();
		$("#choose_photo").fadeIn("slow");
		$(this).addClass("current");
		$("#upload").removeClass("current");
		return false;
	});

	$("img.avatar.choose").click(function(){
		$("img.avatar.choose").removeClass("chosen");
		$(this).addClass("chosen");
		$("#chosen_photo").val($(this).attr("alt"));
		return false;
	});

	$("#message").keyup(function(){
		var chars;
		chars = 500 - $(this).val().length;
		if(chars == 0 || chars < 0) $(this).val($(this).val().substr(0, 499))
		$("#chars_left").html(chars);
	});
	$("#bio, #interests").keyup(function(){
		var chars;
		chars = 200 - $(this).val().length;
		if(chars == 0 || chars < 0) $(this).val($(this).val().substr(0, 199));
	});

//login/register tabs switching
	if($("#show_register_form").val() == 1){
		$("#login_stuff").hide();
		$("#link_register").addClass("current");
	} else {
		$("#register_stuff").hide();
		$("#link_login").addClass("current");
	}

	$("#link_register").click(function(){
		$("#login_stuff").hide();
		$("#register_stuff").fadeIn("slow");
		$(this).addClass("current");
		$("#link_login").removeClass("current");
		return false;
	});
	$("#link_login").click(function(){
		$("#register_stuff").hide();
		$("#login_stuff").fadeIn("slow");
		$(this).addClass("current");
		$("#link_register").removeClass("current");
		return false;
	});

//direct messages
	$("#dm_outbox").hide();
	$("#link_inbox").addClass("current");
	$("#link_inbox").click(function(){
		$("#dm_outbox").hide();
		$("#dm_inbox").fadeIn("slow");
		$(this).addClass("current");
		$("#link_outbox").removeClass("current");
		return false;
	});
	$("#link_outbox").click(function(){
		$("#dm_inbox").hide();
		$("#dm_outbox").fadeIn("slow");
		$(this).addClass("current");
		$("#link_inbox").removeClass("current");
		return false;
	});

//most popular and most	recent users
	$("#most_popular_stuff").hide();
	$("#link_recent").addClass("current");
	var recent = $.ajax({
		url: "ajax/most_recent.php",
		async: false
	}).responseText;
	$("#most_recent_stuff").html(recent);

	$("#link_popular").click(function(){
		$("#most_recent_stuff").hide();
		$("#most_popular_stuff").fadeIn("slow");
		$(this).addClass("current");
		$("#link_recent").removeClass("current");
		var popular = $.ajax({
			url: "ajax/most_popular.php",
			async: false
		}).responseText;
		$("#most_popular_stuff").html(popular);
		recent_popular_enable();
		return false;
	});

	$("#link_recent").click(function(){
		$("#most_popular_stuff").hide();
		$("#most_recent_stuff").fadeIn("slow");
		$(this).addClass("current");
		$("#link_popular").removeClass("current");
		var recent = $.ajax({
			url: "ajax/most_recent.php",
			async: false
		}).responseText;
		$("#most_recent_stuff").html(recent);
		recent_popular_enable();
		return false;
	});


	$("#link_popular").click(function(){
		$("#most_recent_stuff").hide();
		$("#most_popular_stuff").fadeIn("slow");
		$(this).addClass("current");
		$("#link_recent").removeClass("current");
		var popular = $.ajax({
			url: "ajax/most_popular.php",
			async: false
		}).responseText;
		$("#most_popular_stuff").html(popular);
		recent_popular_enable();
		return false;
	});

//most popular and most	recent users home page
	$("#most_popular_stuff_home").hide();
	$("#link_recent_home").addClass("current");
	var recent = $.ajax({
		url: "ajax/most_recent_home.php",
		async: false
	}).responseText;
	$("#most_recent_stuff_home").html(recent);

	$("#link_popular_home").click(function(){
		$("#most_recent_stuff_home").hide();
		$("#most_popular_stuff_home").fadeIn("slow");
		$(this).addClass("current");
		$("#link_recent_home").removeClass("current");
		var popular = $.ajax({
			url: "ajax/most_popular_home.php",
			async: false
		}).responseText;
		$("#most_popular_stuff_home").html(popular);
		recent_popular_enable();
		return false;
	});

	$("#link_recent_home").click(function(){
		$("#most_popular_stuff_home").hide();
		$("#most_recent_stuff_home").fadeIn("slow");
		$(this).addClass("current");
		$("#link_popular_home").removeClass("current");
		var recent = $.ajax({
			url: "ajax/most_recent_home.php",
			async: false
		}).responseText;
		$("#most_recent_stuff_home").html(recent);
		recent_popular_enable();
		return false;
	});


	$("#link_popular_home").click(function(){
		$("#most_recent_stuff_home").hide();
		$("#most_popular_stuff_home").fadeIn("slow");
		$(this).addClass("current");
		$("#link_recent_home").removeClass("current");
		var popular = $.ajax({
			url: "ajax/most_popular_home.php",
			async: false
		}).responseText;
		$("#most_popular_stuff_home").html(popular);
		recent_popular_enable();
		return false;
	});


	$("#link_my_channels").click(function() {
		$(this).addClass("current");
		$(this).siblings("a").removeClass("current");
		$("#all_channels_stuff").hide();
		$("#user_channels_stuff").hide();
		$("#my_channels_stuff").fadeIn("slow");
		return false;
	});

	$("#link_all_channels").click(function() {
		$(this).addClass("current");
		$(this).siblings("a").removeClass("current");
		if(window._all_channels_stuff)
		{
			$("#my_channels_stuff").hide();
			$("#all_channels_stuff").fadeIn("slow");
			return false;
		}
		$.get("ajax/get_channels.php", function(rsp) {
			$("#my_channels_stuff").hide();
			$("#all_channels_stuff").html(rsp).fadeIn("slow");
			window._all_channels_stuff = true;
		});
		return false;
	});

	$("#link_user_channels").click(function() {
		$(this).addClass("current");
		$(this).siblings("a").removeClass("current");
		if(window._user_channels_stuff)
		{
			$("#my_channels_stuff").hide();
			$("#user_channels_stuff").fadeIn("slow");
			return false;
		}
		var post = {user: $(this).attr("rel")};
		$.post("ajax/get_channels.php", post, function(rsp) {
			$("#my_channels_stuff").hide();
			$("#user_channels_stuff").html(rsp).fadeIn("slow");
			window._user_channels_stuff = true;
		});
		return false;
	});

// most popular groups
	$("#link_popular_groups").addClass("current");
	var popular_g = $.ajax({
		url: "ajax/most_popular_groups.php",
		async: false
	}).responseText;
	$("#most_popular_groups_stuff").html(popular_g);

	$("#link_popular_groups").click(function(){
		$("#link_popular_groups").addClass("current");
		$("#most_new_groups_stuff").hide();
		$("#most_popular_groups_stuff").fadeIn("slow");
		$(this).addClass("current");
		$("#link_new_groups").removeClass("current");
		var popular_g = $.ajax({
			url: "ajax/most_popular_groups.php",
			async: false
		}).responseText;
		$("#most_popular_groups_stuff").html(popular_g);
//		recent_popular_enable();
		return false;
	});

// most new groups
	$("#link_new_groups").click(function(){
		$("#link_new_groups").addClass("current");
		$("#most_popular_groups_stuff").hide();
		$("#most_new_groups_stuff").fadeIn("slow");
		$(this).addClass("current");
		$("#link_popular_groups").removeClass("current");
		var new_g = $.ajax({
			url: "ajax/most_popular_groups.php?act=new",
			async: false
		}).responseText;
		$("#most_new_groups_stuff").html(new_g);
//		recent_popular_enable();
		return false;
	});


/// psychodelic
	$("#channel_messages").click(function(){
		if(window.channel_stuff == $(this).attr("rel") && !window.loc_filter_flag) return false;
		$(this).addClass("current");
		$(this).siblings("a").removeClass("current");
		load_channel_messages(this);
		return false;
	}).trigger("click");

	function load_channel_messages(el)
	{
		$("#tab_content").html("<p>Loading...</p>");
		var q = $(el).attr("rel");
		$.get("ajax/channel_messages.php?stuff=" + q, function(rsp) {
			$("#tab_content").html(rsp);
			$("#tab_content").find("a.pagination").click( function() {
				load_channel_messages(this);
				return false;
			});
			window.channel_stuff = q;
			window.loc_filter_flag = null;
			_thickbox();

		});
	};

	function load_cart_messages(el)
	{
		$("#tab_content").html("<p>Loading...</p>");
		var q = $(el).attr("rel");
		$.get("ajax/cart.php?stuff=" + q, function(rsp) {
			$("#tab_content").html(rsp);
			$("#tab_content").find("a.pagination").click( function() {
				load_cart_messages(this);
				return false;
			});
			window.cart_stuff = q;
			window.loc_filter_flag = null;
			_thickbox();

		});
	};

	var exx = document.getElementById("loc_filters");
	if( exx )
	{
		var loc_filters = $(exx).blur(function() {
			if(this.value != loc_filters) {
				var v = this.value;
				$.post( "ajax/set_post_filter.php", {f: v}, function(rsp) {
					loc_filters = v;
					window.loc_filter_flag = true;
					$("#channel_messages").trigger("click");
				});
			}
		}).click(function() {
			this.select();
		}).val();
	}

	function _thickbox() {
		
		tb_init('a.thickbox, area.thickbox, input.thickbox');//pass where to apply thickbox
		imgLoader = new Image();// preload image
		imgLoader.src = tb_pathToImage;
	};

	$("#channel_contacts").click(function(){
		if(window.channel_stuff == $(this).attr("rel")) return false;
		$(this).addClass("current");
		$(this).siblings("a").removeClass("current");
		load_channel_users(this);
		return false;
	});

	$("#channel_users").click(function(){
		if(window.channel_stuff == $(this).attr("rel")) return false;
		$(this).addClass("current");
		$(this).siblings("a").removeClass("current");
		load_channel_users(this);
		return false;
	});

	function load_channel_users(el)
	{
		$("#tab_content").html("<p>Loading...</p>");
		var q = $(el).attr("rel");
		$.get("ajax/channel_users.php?stuff=" + q, function(rsp) {
			var c = $("#tab_content").html(rsp).fadeIn("slow").find("div.ch_user_info").hover(function(){
				var info = $(this.parentNode).find("div.info").get(0);
				if(!info) return;
				$("body").append("<div id='last_msg_tooltip'>" + info.innerHTML +"</div>");
			}, function(){
				$("#last_msg_tooltip").remove();
			});

			$("#tab_content").find("a.pagination").click( function() {
				load_channel_users(this);
				return false;
			});
			window.channel_stuff = q;
		});
	};

//latest message tooltip
	var winH = $("body").height();
	if(winH < window.innerHeight) winH = window.innerHeight;
	var winW = window.innerWidth;
	var tooltip_w, tooltip_h;
	function eventMouseX(e, w) {
		if (e.pageX) xval = e.pageX;
		//if(xval + w > winW) xval = winW - w - 30;
		return xval;
	}
	function eventMouseY(e, h) {
		if (e.pageY) yval = e.pageY;
		//if(yval + h > winH) yval = winH - h - 30;
		return yval;
	}
	$("body").mousemove(function(e){
		var mouse_x = eventMouseX(e, 200)-100;
		var mouse_y = eventMouseY(e, tooltip_h)+10;
		$("#last_msg_tooltip").css({
			left: mouse_x + 'px',
			top: mouse_y + 'px'
		});
		tooltip_h = $("#last_msg_tooltip").height;
	});
	$(".show_msg_tooltip").hover(function(){
		var markup = $.ajax({
			type: "GET",
			url: "ajax/user_last_message.php",
			data: "user=" + $(this).attr("alt"),
			async: false
		}).responseText;
		$("body").append("<div id='last_msg_tooltip'><strong>" + $(this).attr("alt") + "</strong>: " + stripSlashes(markup) + "</div>");
		//$("body").append($(this).attr("alt") + "</strong>: " + stripSlashes(markup));

	}, function(){
		$("#last_msg_tooltip").remove();
	});
	$(".show_msg_tooltip").mouseout(function(){
		$("#last_msg_tooltip").remove();
	});

	function recent_popular_enable(){
		$(".show_msg_tooltip").hover(function(){
			var markup = $.ajax({
				type: "GET",
				url: "ajax/user_last_message.php",
				data: "user=" + $(this).attr("alt"),
				async: false
			}).responseText;
			$("body").append("<div id='last_msg_tooltip'><strong>" + $(this).attr("alt") + "</strong>: " + stripSlashes(markup) + "</div>");
		}, function(){
			$("#last_msg_tooltip").remove();
		});
	}
//favorites ajax stuff

	function enable_add(){
		$(".fav_add").click(function(){
			var result = $.ajax({
				type: "GET",
				url: "ajax/add_favorite.php",
				data: "stuff=" + $(this).attr("alt"),
				async: false
			}).responseText;
			if(result != "OK") alert("Error adding this message to favorites");
			if(result == "OK") {
				$(this).parent().append("<img src='grafika/heart_delete.png' class='fav_del' width='16' height='16' alt='"+ $(this).attr("alt") +"' title='Remove from favorites' />");
				enable_del();
				$(this).remove();
			}
		});
	}

	function enable_del(){
		$(".fav_del").click(function(){
			var result = $.ajax({
				type: "GET",
				url: "ajax/del_favorite.php",
				data: "stuff=" + $(this).attr("alt"),
				async: false
			}).responseText;
			if(result != "OK") alert("Error removing this message from favorites");
			if(result == "OK") {
				$(this).parent().append("<img src='grafika/heart_add.png' class='fav_add' width='16' height='16' alt='"+ $(this).attr("alt") +"' title='Add to favorites' />");
				enable_add();
				$(this).remove();
			}
		});
	}

	$(".fav_add").click(function(){
		var result = $.ajax({
			type: "GET",
			url: "ajax/add_favorite.php",
			data: "stuff=" + $(this).attr("alt"),
			async: false
		}).responseText;
		if(result != "OK") alert("Error adding this message to favorites");
		if(result == "OK") {
			$(this).parent().append("<img src='grafika/heart_delete.png' class='fav_del' width='16' height='16' alt='"+ $(this).attr("alt") +"' title='Remove from favorites' />");
			enable_del();
			$(this).remove();
		}
	});

	$(".fav_del").click(function(){
		var result = $.ajax({
			type: "GET",
			url: "ajax/del_favorite.php",
			data: "stuff=" + $(this).attr("alt"),
			async: false
		}).responseText;
		if(result != "OK") alert("Error removing this message from favorites");
		if(result == "OK") {
			$(this).parent().append("<img src='grafika/heart_add.png' class='fav_add' width='16' height='16' alt='"+ $(this).attr("alt") +"' title='Add to favorites' />");
			enable_add();
			$(this).remove();
		}
	});

//scripts to copy and paste
	$("#paste_code").click(function(){
		this.select();
	});
	$("#js_my_status").click(function(){
		this.select();
	});
	$("#js_my_friends").click(function(){
		this.select();
	});

//notification mode setting
	$("#notify_mode1").click(function(){
		set_notify_mode($(this).val());
	});
	$("#notify_mode2").click(function(){
		set_notify_mode($(this).val());
	});
	$("#notify_mode3").click(function(){
		set_notify_mode($(this).val());
	});
	$("#notify_mode4").click(function(){
		set_notify_mode($(this).val());
	});

//message notification
	$("#notify_direct1").click(function(){
		set_msg_notification($(this).val());
	});
	$("#notify_direct2").click(function(){
		set_msg_notification($(this).val());
	});

//autoselect of	api key
	$("#api_key").click(function(){
		this.select();
	});

//autofill of the mail address in the invite page
	$("#check_user").val("@hotmail.com");
	$("#check_type").change(function(){
		$("#check_user").val("@"+ $(this).val() +".com");
	});

//password for account deletion
	$("#del_pass_form").hide();
	$("#del_pass").click(function(){
		$(this).hide();
		$("#del_pass_form").show();
	});


// ==========================
// tabs	in user's profile
// ==========================

// opening the right tab
	switch($("#current_tab").val()){
		case "group_messages":
			$("#profile_my_msg").addClass("current");
			$("#profile_my_msg").siblings("a").removeClass("current");
			$("#tab_content").html("<p>Loading...</p>");
			var stuff = $.ajax({
			        type: "GET",
				url: "ajax/messages_group.php",
				data: 'group_id=' + $("#current_group").val(),
				async: false
			}).responseText;
			$("#tab_content").html(stuff).fadeIn("slow");
			//enable_mine();
			//enable_add();
			//enable_del();
			break;
		case "mine":
			$("#profile_my_msg").addClass("current");
			$("#profile_my_msg").siblings("a").removeClass("current");
			$("#tab_content").html("<p>Loading...</p>");
			var stuff = $.ajax({
				type: "GET",
				url: "ajax/messages_mine.php",
				data: "stuff=" + $("#profile_my_msg").attr("rel"),
				async: false
			}).responseText;
			$("#tab_content").html(stuff).fadeIn("slow");
			_thickbox();
			enable_mine();
			enable_add();
			enable_del();
			break;
		case "with_friends":
			$("#profile_friends").addClass("current");
			$("#profile_friends").siblings("a").removeClass("current");
			$("#tab_content").html("<p>Loading...</p>");
			var stuff = $.ajax({
				type: "GET",
				url: "ajax/messages_with_friends.php",
				data: "stuff=" + $("#profile_friends").attr("rel"),
				async: false
			}).responseText;
			$("#tab_content").html(stuff).fadeIn("slow");
			_thickbox();
			enable_add();
			enable_del();
			enable_friends();
			break;
		case "replys":
			$("#profile_replys").addClass("current");
			$("#profile_replys").siblings("a").removeClass("current");
			$("#tab_content").html("<p>Loading...</p>");
			var stuff = $.ajax({
				type: "GET",
				url: "ajax/messages_replys.php",
				data: "stuff=" + $("#profile_replys").attr("rel"),
				async: false
			}).responseText;
			$("#tab_content").html(stuff).fadeIn("slow");
			_thickbox();
			enable_add();
			enable_del();
			enable_replys();
			break;
		case "customize":
			$("#profile_customize").addClass("current");
			$("#profile_customize").siblings("a").removeClass("current");
			var stuff = $.ajax({
				type: "GET",
				url: "ajax/profile_customize.php",
				data: "user=" + $("#current_user").val(),
				async: false
			}).responseText;
			$("#tab_content").html(stuff).fadeIn("slow");
			enable_customize();
			break;
		case "cart":
			$("#profile_customize").addClass("current");
			$("#profile_customize").siblings("a").removeClass("current");
			var stuff = $.ajax({
				type: "GET",
				url: "ajax/cart.php",
				data: "user=" + $("#current_user").val(),
				async: false
			}).responseText;
			$("#tab_content").html(stuff).fadeIn("slow");
			enable_cart();
			break;
		default:
			$("#profile_my_msg").addClass("current");
			$("#profile_my_msg").siblings("a").removeClass("current");
			$("#tab_content").html("<p>Loading...</p>");
			var stuff = $.ajax({
				type: "GET",
				url: "ajax/messages_mine.php",
				data: "stuff=" + $("#profile_my_msg").attr("rel"),
				async: false
			}).responseText;
			$("#tab_content").html(stuff).fadeIn("slow");
			_thickbox();
			enable_mine();
			enable_add();
			enable_del();
			break;
	}
// when	user clicks on a tab...
	$("#profile_my_msg").click(function(){
		window.channel_stuff = '';
		$(this).addClass("current");
		$(this).siblings("a").removeClass("current");
		$("#tab_content").html("<p>Loading...</p>");
		var stuff = $.ajax({
			type: "GET",
			url: "ajax/messages_mine.php",
			data: "stuff=" + $(this).attr("rel"),
			async: false
		}).responseText;
		$("#tab_content").html(stuff).fadeIn("slow");
//		enable_mine();
		return false;
	});

	$("#profile_friends").click(function(){
		window.channel_stuff = '';
		$(this).addClass("current");
		$(this).siblings("a").removeClass("current");
		$("#tab_content").html("<p>Loading...</p>");
		var stuff = $.ajax({
			type: "GET",
			url: "ajax/messages_with_friends.php",
			data: "stuff=" + $(this).attr("rel"),
			async: false
		}).responseText;
		$("#tab_content").html(stuff).fadeIn("slow");
		enable_add();
		enable_del();
		enable_friends();
		return false;
	});
	$("#profile_replys").click(function(){

		window.channel_stuff = '';
		$(this).addClass("current");
		$(this).siblings("a").removeClass("current");
		$("#tab_content").html("<p>Loading...</p>");
		var stuff = $.ajax({
			type: "GET",
			url: "ajax/messages_replys.php",
			data: "stuff=" + $(this).attr("rel"),
			async: false
		}).responseText;
		$("#tab_content").html(stuff).fadeIn("slow");
		enable_add();
		enable_del();
		enable_replys();
		return false;
	});

	$("#profile_customize").click(function(){
		window.channel_stuff = 'customize';
		$(this).addClass("current");
		$(this).siblings("a").removeClass("current");
		var stuff = $.ajax({
			type: "GET",
			url: "ajax/profile_customize.php",
			data: "user=" + $(this).attr("rel"),
			async: false
		}).responseText;
  		$("#tab_content").html(stuff).fadeIn("slow");
		enable_customize();
		return false;
	});

// pagination enable
	function enable_mine(){
		$("a.pagination.mine").click(function(){
			$("#tab_content").empty().html("<p>Loading...</p>");
			var stuff = $.ajax({
				type: "GET",
				url: "ajax/messages_mine.php",
				data: "stuff=" + $(this).attr("rel"),
				async: false
			}).responseText;
			$("#tab_content").hide().empty().html(stuff).fadeIn("slow");
			enable_mine();
			return false;
		});
	}

	function enable_friends(){
		$("a.pagination.friends").click(function(){
			$("#tab_content").empty().html("<p>Loading...</p>");
			var stuff = $.ajax({
				type: "GET",
				url: "ajax/messages_with_friends.php",
				data: "stuff=" + $(this).attr("rel"),
				async: false
			}).responseText;
			$("#tab_content").html(stuff).fadeIn("slow");
			enable_add();
			enable_del();
			enable_friends();
			return false;
		});
	}

	function enable_replys(){
		$("a.pagination.replys").click(function(){
			$("#tab_content").empty().html("<p>Loading...</p>");
			var stuff = $.ajax({
				type: "GET",
				url: "ajax/messages_replys.php",
				data: "stuff=" + $(this).attr("rel"),
				async: false
			}).responseText;
			$("#tab_content").hide().empty().html(stuff).fadeIn("slow");
			enable_add();
			enable_del();
			enable_replys();
			return false;
		});
	}

	//color picker stuff

	function add_picker(current_element){
		var move_picker = true;
		current_element.parent().prepend("<div id='colorpicker'><div id='wheel'></div><a id='cmClose' href='#'>Apply color</a></div>");
		$("#wheel").farbtastic(current_element);
		$("#colorpicker").show();
		current_element.parent().click(function(e){
			var mouse_x = eventMouseX(e, 200)-100;
			var mouse_y = eventMouseY(e, $("#colorpicker").height())+10;
			if(move_picker == true){
				$("#colorpicker").css({
					left: mouse_x + 'px',
					top: mouse_y + 'px'
				});
				move_picker = false;
			}
		});
		$("#cmClose").click(function(){
			if(current_element.val().substr(0, 1) == "#") current_element.val(current_element.val().substr(1));
			current_element.css("background", "#"+current_element.val());
			switch(current_element.attr("id")){
				case "back_color":
					$("html").css("background-color", "#" + current_element.val());
					break;
				case "side_fill_color":
					$(".side_stuff").css("background", "#" + current_element.val());
					break;
				case "side_border_color":
					$(".decorative_bar").css("background", "#"+current_element.val());
					break;
				case "text_color":
					$("*, h1, h2, h3, h4, #footer, #copyright").not("a").css("color", "#"+current_element.val());
					break;
				case "link_color":
					$("a, a:visited, #footer a, #footer a:visited, .side_middle a, .side_middle a:visited, .username, .username:visited").css("color", "#"+current_element.val());
					$("input.submit").css("background-color", "#"+current_element.val());
					break;
				case "top_area_color":
					$("#profile_header").css("background", "#" + current_element.val());
					break;
			}
			$(this).parent().remove();
			return false;
		});
		return false;
	}

	var current_element;
	$("#colorpicker").hide();
	if($("#current_tab").val() != "customize") enable_customize();

	function bglib_enable(){
		$("#backs").hide();
		$(".back_lib").click(function(){
			var x = false;
			if(x==false){
				$("#backs").show("slow");
				x = true;
			} else {
				$("#backs").fadeOut("slow");
				x = false;
			}
			return false;
		});
		$("a.bglib_link").click(function(){
			$("a.bglib_link").not($(this)).children("img").removeClass("bglib_pic_current").addClass("bglib_pic");
			$(this).children("img").removeClass("bglib_pic").addClass("bglib_pic_current");
			$("#background_name").val($(this).attr("rel"));
			$("#use_image").attr("checked", "checked");
			return false;
		});
	}

	function enable_customize(){
		bglib_enable();
		$("#sticker_color").css("background", "#"+$("#sticker_color").val());
		$("#sticker_color").click(function(){add_picker($(this));});

		$("#back_color").css("background", "#"+$("#back_color").val());
		$("#back_color").click(function(){add_picker($(this));});

		$("#text_color").css("background", "#"+$("#text_color").val());
		$("#text_color").click(function(){add_picker($(this));});

		$("#link_color").css("background", "#"+$("#link_color").val());
		$("#link_color").click(function(){add_picker($(this));});

		$("#bubble_fill_color").css("background", "#"+$("#bubble_fill_color").val());
		$("#bubble_fill_color").click(function(){add_picker($(this));});

		$("#bubble_text_color").css("background", "#"+$("#bubble_text_color").val());
		$("#bubble_text_color").click(function(){add_picker($(this));});

		$("#side_border_color").css("background", "#"+$("#side_border_color").val());
		$("#side_border_color").click(function(){add_picker($(this));});

		$("#side_fill_color").css("background", "#"+$("#side_fill_color").val());
		$("#side_fill_color").click(function(){add_picker($(this));});

		$("#top_area_color").css("background", "#"+$("#top_area_color").val());
		$("#top_area_color").click(function(){add_picker($(this));});
	}

//invitations check all	and uncheck all
	$("#check_all").click(function(){
		$("table input").attr("checked", "checked");
		return false;
	});
	$("#uncheck_all").click(function(){
		$("table input").removeAttr("checked");
		return false;
	});

	//blocking confirmation
	$(".block_user").click(function(){
		return confirm("Are you sure you want to block this user? He will not be able to read your updates or add you as a friend.");
	});

	$("#search_words").click(function(){
		$(this).val("");
	}).blur(function(){
		if($(this).val() == "") $(this).val("Search for a Friend or Group");
	});

	$("#user").click(function(){
		$(this).val("");
	}).blur(function(){
		if($(this).val() == "") $(this).val("User Name");
	});

	var autoComplete = {
		'test': 'testt',
		minSearchLen: 2,
		cachable: true
	}
	$("#search_loc").click(function(){
		if(!autoComplete.changed) $(this).val("");
	}).blur(function(){
		if($(this).val() == "") {
			$(this).val("City or Zip");
			autoComplete.changed = false;
		}
	}).autoComplete(autoComplete);

//sms credit paypal form
	if($("#item_name").val() != "") $("#item_name").parent().submit();
});

jQuery.autoComplete = {
	cache: {},
	build: function(o) {
		o.minSearchLen = o.minSearchLen || 3;
		return this.each(function() {
			jQuery.autoComplete.make(this, o);
		});
	},
	make: function(el, o) {
		if(!jQuery.autoComplete.layout)
		{
			$(document.body).append("<div id='ac-layout' style='position:absolute; left: 0px; top: 0px;width: 1px; height: 1px;'>");
			jQuery.autoComplete.layout = $("#ac-layout");
		}
		var box = $("<div class='jq-context-menu' style='display: none; position: absolute; left:0px; top: 0px;'>");
		box.appendTo(jQuery.autoComplete.layout);
		o.box = box;
		o.element = el;

		$(el).bind("keydown", o, jQuery.autoComplete.onKeyDown)
			.bind("keyup", o, jQuery.autoComplete.onKeyUp)
			.bind("blur", o, jQuery.autoComplete.onBlur);
	},

	moveCursor: function(o, dir) {
		if(!o.items) return;
		if(o.current == null) {
			if(dir > 0) o.current = o.items.length - 1;
			else o.current = 0;
		}
		while(true){
			o.current += dir;
			if(o.current < 0) o.current = o.items.length - 1;
			else if(o.current > o.items.length - 1) o.current = 0;
			if(o.items[o.current].type != 'separator') break;
		}
		o.box.find("div").removeClass("current").get(o.current).className += " current";
	},

	onKeyDown: function(e) {
		var o = e.data;
		if( !o ) return true;
		switch(e.keyCode) {
			case 38: // up
				jQuery.autoComplete.moveCursor(o, -1);
				return false;
			case 40: // down
				jQuery.autoComplete.moveCursor(o, +1);
				return false;
		}
		return true;
	},

	onKeyUp: function(e) {
		var o = e.data;
		if( !o ) return false;
		var el = e.target, q = el.value;
		if($.trim(q).length < o.minSearchLen ) {
			o.box.hide();
			jQuery.autoComplete.onBlur();
			return false;
		}
		if(e.keyCode == 27)
		{
			o.box.hide();
			jQuery.autoComplete.onBlur();
			return false;
		}
		if(e.keyCode == 13 && o.current != null) {
			var item = o.items[o.current]
			el.value = item.title;
			o.box.hide();
			jQuery.autoComplete.onBlur();
			return false;
		}
		var skip = /^(13|37|38|39|40|33|34|9|27|36|35|39|12|45|44|145|19|20|16|17|91|18|144|)$/
			.test(e.keyCode.toString()) || e.keyCode > 111 && e.keyCode < 124;
		if(skip) return false;

		if(o.lastQuery == q) return false;

		jQuery.autoComplete.load(o, q);
		return false;
	},

	load: function(o, q, v) {
		if(!v && o.cachable && jQuery.autoComplete.cache[q])
			v = jQuery.autoComplete.cache[q];
		if(!v)
		{
			$.post( "ajax/sac.php", {q: q}, function(rsp) {
				jQuery.autoComplete.load(o, q, rsp);
				if(o.cachable) jQuery.autoComplete.cache[q] = rsp;
			});
			return false;
		}
		var el = o.element;
		o.changed = true;
		if(v && o.cachable) {
			jQuery.autoComplete.cache[q] = v;
		}
		v = v || jQuery.autoComplete.cache[q];
		try {
			eval( "var x = {" + v + "}" );
			if(x.status != 'OK') {
				jQuery.autoComplete.onBlur();
				o.box.hide();
				return;
			}
			o.box.html('');
			for(var i=0; i<x.list.length; i++) {
				if(x.list[i].type == 'separator')
					o.box.append("<div class='separator'></div>");
				else {
					$("<div class='cMenuItem" + (i==0 ? ' current' : '') + "' xindex='" + i + "'>" + x.list[i].title + "</div>")
					.appendTo(o.box).hover(
						function() {
							$(this.parentNode).find("div").removeClass("current");
							o.current = parseInt($(this).addClass('current').attr("xindex"));
						},
						function() {
							$(this).removeClass('current');
							o.current = null;
						}
					).click(function() {
						el.value = this.innerHTML;
						o.box.hide();
						jQuery.autoComplete.onBlur();
					});
				}
			}
			o.items = x.list;
			o.current = 0;
			var x = el.offsetLeft - 1, y = el.offsetTop + el.offsetHeight;
			o.box.css({"left": x +"px", "top": y+ "px"});
			o.box.show();
			$(document.body).bind("click", o, jQuery.autoComplete.onBlur);

		} catch(e) {
			o.box.hide();
			jQuery.autoComplete.onBlur();
		};
	},

	onBlur: function(e) {
		if(e && e.target == e.data.element) return false;
		if(e && e.data) e.data.box.hide()
		$(document.body).unbind("click", jQuery.autoComplete.onBodyClick);
	}
}

jQuery.fn.extend({ autoComplete: jQuery.autoComplete.build} );

var consoleMsg = 0;
window.d = function(v,h) {
	if(!$('#consoler').length) {
		if(h== null) h = 120
		var e = document.createElement("DIV");
		e.style.position = "absolute";
		e.style.left = 0+"px";
		e.style.top= 0+"px";
		e.style.color = "white";
		e.style.width = 700+"px";
		e.style.height = h +"px";
		e.style.backgroundColor = "#2D3D4D";
		e.style.zIndex = 3000;
		e.style.border = '1px'
		e.style.borderColor = 'white'
		e.style.borderStyle = 'solid'
		document.body.appendChild(e);
		$(e).css("opacity", 0.5);

		var e = document.createElement("DIV");
		e.style.position = "absolute";
		e.style.color = "white";
		e.style.left = 0 + "px";
		e.style.top=  0 + "px";
		e.style.width = 700+"px";
		e.style.height = h+"px";
		e.style.zIndex = 3001;
		e.style.overflow = 'auto';
		document.body.appendChild(e);
		e.id = 'consoler';
	}
	consoleMsg ++;
	$('#consoler')[0].innerHTML += "<pre style='margin:2px; padding: 0px'>"+consoleMsg + ": " +  v + "</pre>";
	$('#consoler')[0].scrollTop = 50000;
}

