$(document).ready(function(){

	$(".loadingImg").hide();
	$(".buzzButton").show();
	
	// focus on the first text input field in the first field on the page
  $("input[type='text']:first", document.forms[0]).focus();
				
	$(".buzzField").counter(); // Show the counter

	$(".linkSendInvite").click(function(){
		$(".tabSendInvite").show();
		$(".tabImportInvites").hide();
		$(".supportedWebmail").hide();
		$(".linkSendInvite").addClass('tabsActive');
		$(".linkSendInvite").removeClass('tabs');
		$(".linkImportInvites").removeClass('tabsActive');
		$(".linkImportInvites").addClass('tabs');
	});
	$(".linkImportInvites").click(function(){
		$(".tabSendInvite").hide();
		$(".tabImportInvites").show();	
		$(".linkImportInvites").addClass('tabsActive');
		$(".linkImportInvites").removeClass('tabs');
		$(".linkSendInvite").removeClass('tabsActive');
		$(".linkSendInvite").addClass('tabs');
	});		
	$(".linkImportInvitesNoTab").click(function(){
		$(".tabSendInvite").hide();
		$(".tabImportInvites").show();	
		$(".linkImportInvites").addClass('tabsActive');
		$(".linkImportInvites").removeClass('tabs');
		$(".linkSendInvite").removeClass('tabsActive');
		$(".linkSendInvite").addClass('tabs');
	});		
	
	
$(".searchBtn1").hover(
      function () {
        $(".searchTxt1").addClass('hover');			
		$(".searchTxt1").removeClass('noHover');	
      }, 
      function () {
        $(".searchTxt1").addClass('noHover');	
		$(".searchTxt1").removeClass('hover');
      }
    );	
$(".searchBtn2").hover(
      function () {
        $(".searchTxt2").addClass('hover');			
		$(".searchTxt2").removeClass('noHover');	
      }, 
      function () {
        $(".searchTxt2").addClass('noHover');	
		$(".searchTxt2").removeClass('hover');
      }
    );	
	// START LOADING GIF //
	// Preload the GIF
	$(".loadingGifOnSubmit").parents("form").after('<div class="loadingGifImage hide"><img src="/style/img/loader_swirl.gif" alt="loading" /> Finding addres book ....</div>');
	
	$(".loadingGifOnSubmit").click(function(){
		$(this).parents("form").hide();
		$(".loadingGifImage").show();
	});

	// END LOADING GIF //
	
	$('.checkboxAll').toggle(function(){
		$(this).html('un-check all');
		$(this).parents("form").find("input[@type=checkbox]").attr('checked', 'checked');
	}, function(){	
		$(this).html('check all');
		$(this).parents("form").find("input[@type=checkbox]").removeAttr('checked');
	});

    $("#supportedWebmail").click(function () {
     	$('.supportedWebmail').toggle();
    });


  // Show poke panel
	$(".pokeBtn").toggle(function(){
		$("#followPanel").slideUp(function(){$("#pokePanel").show();});
	},function(){
	  $("#pokePanel").hide();
	});	

  // Show dbuzz panel
	$(".dBuzzBtn").toggle(function(){
		$("#followPanel").slideUp(function(){$("#dBuzzPanel").show();});
	},function(){
	  $("#dBuzzPanel").hide();
	});		
	
	
	
	// Show follow panel
	$(".followBtn").click(function(){

	}); 		
	
	// Cancel a poke or follow panel
	$(".cancelBtn").click(function(){
		$(".cancelBtn").parent().slideUp();
	});   
	 
	// FOLLOW PANEL
	$(".followBtn").toggle(function(){
		$("#pokePanel").slideUp(function(){$("#followPanel").show();});
	},function(){
	  $("#followPanel").hide();
	});	
	
	
	
	// POKE AND FOLLOW BUTTONS MULTI 
	// added by AL: 29/06/2009
  	// Show poke panel MULTI, this is used if you have more than one poke panel per page
	$(".pokeBtnMulti").toggle(function(){			
		var i = $(this).attr('id');		
		$("#followPanel"+i).slideUp(function(){$("#pokePanel"+i).slideDown();});
	},function(){
	  var i = $(this).attr('id');
	  $("#pokePanel"+i).hide();
	});	
	
	// Cancel a poke or follow panel MULTI
	$(".cancelBtnMulti").click(function(){
		var i = $(this).attr('id');
		$(".cancelBtn"+i).parent().slideUp();
	});   
	 
	// FOLLOW PANEL MULTI
	$(".followBtnMulti").toggle(function(){			
		var i = $(this).attr('id');		
		$("#pokePanel"+i).slideUp(function(){$("#followPanel"+i).show();});
	},function(){
	  var i = $(this).attr('id');
	  $("#followPanel"+i).hide();
	});	
	
	//FOLLOW BUTTON MULTI
	$(".followPanel > .acceptBtn").click(function(){
		var i = $(this).attr('id');
		var currentLocation = $(this).parent().attr('id');
		$("#"+currentLocation).fadeOut(function(){
			$("#"+currentLocation).text('you are now following '+ i +'. You will see their buzzes in your timeline');
			$("#"+currentLocation).fadeIn();
			window.location.href = '/buzz/recentWithFriends';
		});
	})	 
	 
	//PROD BUTTON MULTI
	$(".pokePanel > .acceptBtn").click(function(){
		var i = $(this).attr('id');
		var currentLocation = $(this).parent().attr('id');
		$("#"+currentLocation).fadeOut(function(){
			pokeUser(i, false);
			$("#"+currentLocation).slideUp();
			$("#"+currentLocation).text('your prod has been sent');
			$("#"+currentLocation).fadeIn();
		});
	})	
	
	 
	$("a.message-link").click(function(){	 
		$("#eventBox").slideUp(function(){
			$("#photoArea").slideUp(function(){
				$("#rssArea").slideUp(function(){
					$("#buzzArea").slideDown();
					
					$("#wordCounter").hide(); // Hide any other showing counter
					
					$(".buzzField").counter(); // Show the counter				
								
					e = $(".event-link");
					e.css("color","#000000");
					e.css("background-color","transparent");
					e.css("border-width","0");
						
					p = $(".photo-link");
					p.css("color","#000000");
					p.css("background-color","transparent");
					p.css("border-width","0");	
					
					r = $(".rss-link");
					r.css("color","#000000");
					r.css("background-color","transparent");
					r.css("border-width","0");
							
					m = $(".message-link");
					m.css("color","#D9006B");
					m.css("background-color","white");
					m.css("border-color","#D9006B");
					m.css("border-style","solid");
					m.css("border-width","2px 0 0 0");						
				});
			});
		});  
	});   /* END $(".message-link").click() */
   
	 
	$("a.event-link").click(function(){	 
		$("#buzzArea").slideUp(function(){
			$("#photoArea").slideUp(function(){
				$("#rssArea").slideUp(function(){
					$("#eventBox").slideDown();
	
					$("#wordCounter").hide(); // Hide any other showing counter
								
					m = $(".message-link");
					m.css("color","#000000");
					m.css("background-color","transparent");
					m.css("border-width","0");
		
					p = $(".photo-link");
					p.css("color","#000000");
					p.css("background-color","transparent");
					p.css("border-width","0");
					
					r = $(".rss-link");
					r.css("color","#000000");
					r.css("background-color","transparent");
					r.css("border-width","0");	
					
					e = $(".event-link");
					e.css("color","#D9006B");
					e.css("background-color","white");
					e.css("border-color","#D9006B");
					e.css("border-style","solid");
					e.css("border-width","2px 0 0 0");	
				});					
			});
		});  
	});   /* END $(".message-link").click(function(){*/		
	 
	 
	$("a.photo-link").click(function(){	 
		$("#buzzArea").slideUp(function(){
			$("#eventBox").slideUp(function(){
				$("#rssArea").slideUp(function(){	
					$("#photoArea").slideDown();
					
					$("#wordCounter").hide(); // Hide any other showing counter
					
					$(".imageNote").counter(); // Show the counter
					
					m = $(".message-link");
					m.css("color","#000000");
					m.css("background-color","transparent");
					m.css("border-width","0");
		
					e = $(".event-link");
					e.css("color","#000000");
					e.css("background-color","transparent");
					e.css("border-width","0");
					
					r = $(".rss-link");
					r.css("color","#000000");
					r.css("background-color","transparent");
					r.css("border-width","0");
					
					p = $(".photo-link");
					p.css("color","#D9006B");
					p.css("background-color","white");
					p.css("border-color","#D9006B");
					p.css("border-style","solid");
					p.css("border-width","2px 0 0 0");
				});							
			});
		});  
	});   /* END $(".photo-link").click(function(){*/		
 
   $("a.rss-link").click(function(){
		$("#eventBox").slideUp(function(){
			$("#photoArea").slideUp(function(){
				$("#buzzArea").slideUp(function(){
					$("#rssArea").slideDown();
					
					$("#wordCounter").hide(); // Hide any other showing counter
					
					$(".buzzField").counter(); // Show the counter				
								
					e = $(".event-link");
					e.css("color","#000000");
					e.css("background-color","transparent");
					e.css("border-width","0");
						
					p = $(".photo-link");
					p.css("color","#000000");
					p.css("background-color","transparent");
					p.css("border-width","0");
					
					r = $(".rss-link");
					r.css("color","#D9006B");
					r.css("background-color","white");
					r.css("border-color","#D9006B");
					r.css("border-style","solid");
					r.css("border-width","2px 0 0 0");	
							
					m = $(".message-link");
					m.css("color","#000000");
					m.css("background-color","transparent");
					m.css("border-width","0");						
				});
			});
		});  
	});   /* END $(".rss-link").click() */
	 
	 
	 
	 
	 
	 
	/*side content for friend requests*/
	$(".leaveAsFan").click(function(){
		thisTagId = $(this).attr('id');
  	$("#"+thisTagId).parent().parent().slideUp();
		$.post('/buzz/buzz.deleteUserBuzz', {
			   buzzid: buzzid
			}); // END $.post			
   }); 		 
	
	
	$(".saveFriend").click(function(){
		friendId = $(this).attr('id');		
		$(this).parent().parent().parent().slideUp()
	});
	
	
	
// Hide and delete buzz
$(".btnDelBuzz").click(function(){	
	// buzzid = $(this).children("img").attr("alt");
	buzzid = $(this).attr('title');
	$(this).parent().parent().slideUp();	// Slide up the buzz
	$.post('/buzz/buzz.deleteUserBuzz', {
		   buzzid: buzzid
		}); // END $.post		
	showDropDownMessage('Your buzz has been deleted')		
 }); 	

// Hide and delete direct buzz
$(".btnDelSentDBuzz").click(function(){	
	buzzid = $(this).children("img").attr("class");
	$(this).parent().parent().slideUp();	// Slide up the buzz
	$.post('/buzz/buzz.deleteUserBuzz', {
		   buzzid: buzzid,
			 d:'s'
		}); // END $.post			
 }); 		 
	 
	 
	 
	 
// Hide and delete buzz confirmation
$('.btnDelBuzz').confirm({
  msg:'delete buzz? ',
  timeout:3000
});

// Hide and delete buzz
$(".stopFollowing").click(function(){	
	friendid = $(this).attr("id");
	$.get('/buzz/buzz.stopFollowingUser/u/' + friendid); // END $.post			
	window.location.href = "/buzz/recentWithFriends";
}); 	
	 
	 
// Hide and delete buzz confirmation
	$('.stopFollowing').confirm({
	msg:'are you sure? ',
	timeout:3000
});


	
	// Hide and cancel Poke
	$("#cancelPoke").click(function(){	
		theUsername = $(".pokeBack").attr("id");
		$(this).parent().parent().slideUp();	
		$.post('/buzz/buzz.deleteUserPoke/id/' + theUsername); // END $.post			
   });   	
	 
	// Poke back!
	$(".pokeBack").click(function(){
		theUsername = $(".pokeBack").attr("id");
		$(this).parent().parent().slideUp();	
		$(this).parent().parent().remove();
		pokeUser(theUsername,true);
	});
	 
	 
	 

	$("#followPanel > .acceptBtn").click(function(){
		$("#followPanel").fadeOut(function(){
			/*
$.post('/buzz/buzz.followUser', {
			   friendId: $("#followPanel > .acceptBtn").attr('id')
			}); // END $.post
*/			
			$("#followPanel").text('you are now following '+$("#followPanel > .acceptBtn").attr('id')+'. You will see their buzzes in your timeline');
			$("#followPanel").fadeIn();
			window.location.href = '/buzz/recentWithFriends';
		});
	})	 
	 
	$("#pokePanel > .acceptBtn").click(function(){
		$("#followPanel").fadeOut(function(){
			pokeUser($("#pokePanel > .acceptBtn").attr('id'), false);
			$("#pokePanel").slideUp();
			$("#followPanel").text('your prod has been sent');
			$("#followPanel").fadeIn();
		});
	})		 
   
/*
   // remote update
	$(".quickBuzzButton").click(function(){		
		$.post('/buzz/buzz.remoteSave', {
		   username: $("#qusername").attr("value"),
		   password: $("#qpassword").attr("value"),
		   buzzText: $("#buzzArea").attr("value")
		}, function(){alert('Quick post done')}); // END $.post('/buzz/buzz.remoteSave'
		
		$("#buzzArea").val("");
		
   });   // END $(".quickBuzzButton").click()

*/


	$(".ajaxPost").click(function(){	
		var buzz 		= $(".buzzField").attr('value');
		var buzzTo 	= $(".buzzTo").attr('value');

		// check that the buzz is defined
		if (buzz != undefined && buzz != 'Whats your buzz?') {
			// Hide the button to prevent multiple submits
			$(".buzzButton").hide();
			// Show a spinny loading image
			$(".loadingImg").show();
			
			$.ajax({
				url: "/index.cfm?buzz=buzz.save",
				processData: false,
				data: "buzzText=" + escape(buzz) +"&buzzTo="+buzzTo,
				//dataType:"xml",
				error: function(xml,textStatus, errorThrown){
					//alert('there has been an error posting your buzz');
					var x = xml;
					var t = textStatus;
					var e = errorThrown;
					alert('You appear to be logged out. Please log back in to post again');
					window.location.href = '/';
				},
				success: function(xml){
					
					// Appends the buzz to the current list
					$("#messagesContainer").prepend('<div class="messageSent">' + xml + '</div>');
					$(".messageSent:first > .buzz").css("margin-left","30px");
					
					$(".messageSent:first > a > img").css("padding-top","10px");
					$(".messageSent:first > a > img").css("padding-left","10px");
					
					// ADD SOME CSS to the last buzz in the side column					
					// Populates the last buzz in the side column
					$("#latestBuzz").html(xml);
					$("#latestBuzz > .buzz").css("width","200px");
					$("#latestBuzz > .buzz").css("font-weight","normal");
					$("#latestBuzz > .messageOptions").css("display","none");
					$("#latestBuzz > img").css("display","none");			
					$(".messageSent > .pbuzz").css("margin-left", "80px");
					
					
					if ( $(xml).hasClass("pbuzz") ){
						var newCnt = parseInt($("#privateBuzzCountSent").html())+1;
						$("#privateBuzzCountSent").html(newCnt);
					}
					else{
						var newCnt = parseInt($("#liveBuzzCount").html())+1;
						$("#liveBuzzCount").html(newCnt);						
						
					}
				},
				complete: function(xml){
					// Bring back the loading image
					$(".loadingImg").hide();
					$(".buzzButton").show();
					resetBuzz();
					}
			});
		}
	});	
	
	
	
	
	
  /* Get user search results
	$("#searchfield").keypress(function(){
		searchVal = $("#searchfield").val();
		searchLength = $("#searchfield").val().length;
		
		if(searchLength > 2){
			$.ajax({
				url: "/buzz/searchUser",
				processData: false,
				data: "search=" + escape(searchVal),
				//dataType:"xml",
				error: function(xml){
					alert('There is an error with your search');
				},
				success: function(xml){
					$("div#searchResults").html(xml);
				},
				complete: function(){}
			});
		}
	});  */ 



	//replys and forwards
	
	
	







   
});// END  $(document).ready(function()

function resetBuzz(){
	// Clears the textarea 
	$(".buzzField").val('');
	//Resets the counter
	$("#wordCounter").text('150');
}

 jQuery.fn.counter = function() {
  $(this).each(function() {
    var max = 150;
    var val = $(this).attr('value');
    var cur = 0;
    if(val) // value="", or no value at all will cause an error
      cur = val.length;
    var left = max-cur;
	
	
    $("#buzzActions>.clear").after("<div id='wordCounter'>"
      + left.toString()+"</div>");
    // You can use something like this to align the
    // counter to the right of the input field.
    var c = $("#buzzActions>.clear").next("#wordCounter");
    c.css("color","#D9006B");
		//c.width(40);
    //c.css("float","right");
		//c.css("top",-$(this).height()-25);
	  //c.css("left",$(this).width()+8);
	  //c.css("background","#FFF");
	  //c.css("color","#333");
		//c.css("font-size", "50px");
		//c.css("font-family", "georgia");
		//c.css("margin-left", "5px");
 
    $(this).keyup(function(i) {
      var max = 150;
      var val = $(this).attr('value');
      var cur = 0;
      if(val)
        cur = val.length;
      	var left = max-cur;
		if(cur >= max){
			$(this).val(val.substring(0, 150));
			$(this).next("#wordCounter").text(0);
		}	
		else{
			$("#buzzActions>.clear").next("#wordCounter").text(left.toString());
		}
		return this;
	});
  });
  return this;
}


function pokeUser(id,isPokeBack){
	var i = id; //The username
	var p = isPokeBack; // This tells us if its a poke back
	/*
$.post('/buzz/buzz.saveUsersPoke', {
		id: i,
		ipb:p
	}); // END $.post
*/
	$.ajax({
		url: "index.cfm?buzz=buzz.saveUsersPoke",
		processData: false,
		data: "id=" + escape(i) +"&ipd="+p,
		//dataType:"xml",
		error: function(xml,textStatus, errorThrown){
			alert('You appear to be logged out. Please log back in to poke again');
			window.location.href = '/';
		},
		success: function(xml){
		},
		complete: function(){
		}
	});
}


function checkNoneBoxes(cboxes){
	var i = 0;
	
	for (i=0; i < cboxes.length;i++) {
		cboxes[i].checked = false;
	}
}

function checkAllBoxes(cboxes){
	var i = 0;
	
	for (i=0; i < cboxes.length;i++) {
		cboxes[i].checked = true;
	}
}

function showDropDownMessage(txt){
	if(txt.length > 0){
		$("#dropDownMessage").html(txt)
	}
	$('html, body').animate({scrollTop:0}, 'fast', dropDownMessageSlideDownSlideUp());
	
}

function dropDownMessageSlideDownSlideUp(){
	$("#dropDownMessage").slideDown(1000).slideUp(1500);
	return true
}


function loadingGif(){
	$(".photoPost").hide();
	$(".loadingImg").show();
	return true;
}
