$(document).ready(function(){


$('#gallery a').lightBox();

var msie = ((navigator.appVersion.indexOf("msie")))? true : false;
	msie = false;
	

 $("#dropblip").click(function () {
	 	 $("#blipform").hide();
		$("#bliploading").fadeIn("slow");
    });

 $("#statusbar").focus(function () {
	 	if($("#statusbar").val() == $("#hiddenstatus").val()){
			$("#statusbar").val("");
		}   
    });
$("#statusbar").blur(function () {
	str = jQuery.trim($("#statusbar").val());
	 	if(str == ""){
			$("#statusbar").val($("#hiddenstatus").val());
			$('#charlimitinfo').val("");
		}   
    });


function limitChars(textid, limit, infodiv)
{
	var text = $('#'+textid).val();	
	var textlength = text.length;
	if(textlength > limit)
	{
		$('#' + infodiv).html('You cannot write more than '+limit+' characters!');
		$('#'+textid).val(text.substr(0,limit));
		return false;
	}
	else
	{
		$('#' + infodiv).html('You have '+ (limit - textlength) +' characters left');
		return true;
	}
}

$(function(){
 	$('#statusbar').keyup(function(){
 		limitChars('statusbar', 120, 'charlimitinfo');
 	})
});




//====================================================================
// Qtip
//--------------------------------------------------------------------
// By suppling no content attribute, the library uses each elements title attribute by default
   
   if (msie == false) {
   	//alert("Not MSIE");
   	$('.userlist img').each(function(){
   		$(this).qtip({
   		
   			content: {
   				text: $(this).attr('title') // Use each elements title attribute
						},
						show: 'mouseover',
						hide: 'mouseout',
						
						style: {
							name: 'cream',
							border: {
								width: 1,
								radius: 4
							},
							tip: {
								corner: 'bottomMiddle', // We declare our corner within the object using the corner sub-option
								size: {
									x: 10, // Be careful that the x and y values refer to coordinates on screen, not height or width.
									y: 5 // Depending on which corner your tooltip is at, x and y could mean either height or width!
								}
							}
						},
						
						
						position: {
							corner: {
								target: 'topMiddle',
								tooltip: 'bottomMiddle'
							}
						}
					
					});
				});


			}


//====================================================================
// Chat session
//--------------------------------------------------------------------
  
	
	$("#chathistory").click(function () {
		$("#chatmessages").load("/ajax/chat/history/" + $("#buddy").attr('value'));
		
	});  
	
	 $("#sendmessage").click(function () {
		button = this;
		button.value = "Sending...";
		 $("#sendmessage").attr('disabled', 'true');
		$.post("/ajax/chat/send/"+ $("#buddy").attr('value'), { message: $("#message").attr('value') }, function(data){
		 //alert("Data Loaded: " + data);
		
		button.value = "Send";
		 $("#sendmessage").removeAttr('disabled');
		});
		$("#message").val("");
		$("#chatmessages").load("/ajax/chat/history/" + $("#buddy").attr('value'));
	});
	




//====================================================================
// Top Menu Hovers
//--------------------------------------------------------------------
  $("#topmenu > li").addClass("ui-state-default ui-corner-all");
  $("#topmenu > li").hover(
      function () {
        $(this).addClass("ui-state-highlight");
     },
      function () {
       $(this).removeClass("ui-state-highlight");
      }

	 );
	  

  
//====================================================================
// Tabs
//--------------------------------------------------------------------
  if(typeof $('#tabs').tabs == 'function') { 
	$('#tabs').tabs();
	}
if(typeof $('#blipupload').tabs == 'function') { 
	$('#blipupload').tabs();
	}
//====================================================================
// Status Update ajax call
//--------------------------------------------------------------------
   
    $("#update_status").click(function () {
		button = this;
		button.value = "Updating...";
		$.post("/ajax/status/update", { message: $("#statusbar").val() }, function(data){
		 //alert("Data Loaded: " + data);
		button.value = "Update";
		});
		
	});
	
	
//====================================================================
// Map marker transparency
//--------------------------------------------------------------------	
    $(".seebehind").click(function () {
	if($("#blip").css('opacity') == 1) {	
      $("#blip").fadeTo("slow", 0.33);
	  } else {
	  	 $("#blip").fadeTo("slow", 1);
	  }
    });




//==================================================================================
//Manual locate search
//==================================================================================

 $("#addsearch").click(function () {
		button = this;
		button.value = "Searching...";
		$.post("/ajax/addsearch", { s: $("#addsearchfield").val() }, function(data){
		$("#searchresults").html(data);
		button.value = "Search";
		});
		
	});

timeout = null;
lastKeyPressCode = 0;
$("#addsearchfield").keydown(function(e) {
		// track last key pressed
		lastKeyPressCode = e.keyCode;
		//alert(lastKeyPressCode);
		s = $("#addsearchfield").val();
		
		if(lastKeyPressCode == 13)
		{
			//alert("pressed enter");
			$("#addsearch").click();
		}

		if( lastKeyPressCode == 46 || (lastKeyPressCode > 8 && lastKeyPressCode < 32)){
		} else {
			if(s.length > 5) {
				if (timeout) clearTimeout(timeout);
				timeout = setTimeout(function(){$("#addsearch").click();}, 1000);
			}
		}
	
});

//================================================================================







  });
  

