$(document).ready(function(){
/*	Add Hover classes to buttons for all browsers ---------- */
	$("button").hover(function() {
		$(this).addClass("hover");
	}, function() {
			$(this).removeClass("hover");
	});

/*	Add Hover class to the tweets for IE6 *cough* fuck you ie6 *cough* ---------- */
	$("ul.tweet li").hover(function() {
		$(this).addClass("hover");
	}, function() {
		$(this).removeClass("hover");
	});


/*	Extra fun for tweet box on focus ---------- */
	$('#tweet-text').focus(function(){
		$(this).parent("div#tweetBox").addClass("focus");
	});
	$('#tweet-text').blur(function(){
		$(this).parent("div#tweetBox").removeClass("focus");
	});

    /*
      REMOVED BY PB
	$('input#uploadfile').change(function(){
		$('#upload').addClass('uploading');
		$.post('/twitgoo.com/twitter/pics2.html', { image: $(this).val() }, function(data){
			setTimeout(function(){
				$('#tweet-text').insertAtCaret('http://p.twitter.com/37cn3');
				$('#uploading').html('<span id="uploaded">Uploaded: <a href="../../goo/index_files/1338317376.html" target="_blank">http://p.twitter.com/12345</a></span>');
			}, 1500);
		});
		return false;
	});
    */
	
	$.fn.insertAtCaret = function (myValue) {
	    return this.each(function(){
	        //IE support
	        if (document.selection) {
	            this.focus();
	            sel = document.selection.createRange();
	            sel.text = myValue;
	            this.focus();
	        }
	        //MOZILLA/NETSCAPE support
	        else if (this.selectionStart || this.selectionStart == '0') {
	            var startPos = this.selectionStart;
	            var endPos = this.selectionEnd;
	            var scrollTop = this.scrollTop;
	            this.value = this.value.substring(0, startPos)
	                + myValue
	                + this.value.substring(endPos,
	                this.value.length);
	            this.focus();
	            this.selectionStart = startPos + myValue.length;
	            this.selectionEnd = startPos + myValue.length;
	            this.scrollTop = scrollTop;
	        } else {
	            this.value += myValue;
	            this.focus();
	        }
	    });
	
	};	



/*	Show and hide the rotate actions on hover ---------- */
	$("div.media span").hover(function() {
		$("div.media span a.rotate").show();
	}, function() {
		$("div.media span a.rotate").hide();
	});

    /*
	$("div.media span a.rotate").click(function() {
		if ($(this).is("#rotateLeft")) {
			$("#tehimage").rotateLeft(90);
		} else {
			$("#tehimage").rotateRight(90);
		}
		return false;
	});
    */


/*	Faving toggle ---------- */
	$("ul li.fav a").click(function() {
		if ($(this).parent("li").hasClass("faved")) {
			$(this).parent("li").removeClass("faved");
		} else {
			$(this).parent("li").addClass("faved");
		}
		return false;
	});

	$("div.media li a.on-page").click(function() {
		$(this).parent("li").toggleClass("active");

		if ($(this).parent().hasClass("share")) {
			if ($("div#reply").is(":visible")) {
				$("div#reply").slideToggle("fast");
				$("li.reply a").parent().toggleClass("active");
			}
			if ($("div#retweet").is(":visible")) {
				$("div#retweet").slideToggle("fast");
				$("li.retweet a").parent().toggleClass("active");
			}
			$("div#share").slideToggle("fast");
			
		} else if ($(this).parent().hasClass("reply")) {
			if ($("div#retweet").is(":visible")) {
				$("div#retweet").slideToggle("fast");
				$("li.retweet a").parent().toggleClass("active");
			}
			if ($("div#share").is(":visible")) {
				$("div#share").slideToggle("fast");
				$("li.share a").parent().toggleClass("active");
			}
			$("div#reply").slideToggle("fast");
			
		} else if ($(this).parent().hasClass("retweet")) {
			if ($("div#reply").is(":visible")) {
				$("div#reply").slideToggle("fast");
				$("li.reply a").parent().toggleClass("active");
			}
			if ($("div#share").is(":visible")) {
				$("div#share").slideToggle("fast");
				$("li.share a").parent().toggleClass("active");
			}
			$("div#retweet").slideToggle("fast");
		}
		return false;
	});

	$("div.hiddenSection a.cancel").click(function() {
		if ($(this).parent("div.hiddenSection").is("div#reply")) {
			$("div#reply").slideToggle("fast");
			$("div.media li.reply a").parent("li").toggleClass("active");
			
		} else if ($(this).parent("div.hiddenSection").is("div#retweet")) {
			$("div#retweet").slideToggle("fast");
			$("div.media li.retweet a").parent("li").toggleClass("active");
		}
	});

	$("div#share a.cancel").click(function() {
		$("div#share").slideToggle("fast");
		$("div.media li.share a").parent("li").toggleClass("active");
	});
		

/*	Toggle Notes ---------- */
	$("div#notes strong#demoNotes").click(function() {
		$(this).parent("div#notes").toggleClass("open");
	});


/*	Close Messages ---------- */
	$("div.message a#closeMsg").click(function() {
		$(this).parent("div.message").hide();
		return false;
	});

/*	Clear sign in fields on click ----- */
	$("#twusername").focus(function() { 
		if ( $(this).val() == "Twitter Username") {
			$(this).val("");
		}
	});
	$("#twusername").blur(function() { 
		if ( $(this).val() == "") {
			$(this).val("Twitter Username");
		}
	});
    /*
	$("#twassword").focus(function() { 
		if ( $(this).val() == "Password") {
			$(this).val("");
		}
	});
	$("#twassword").blur(function() { 
		if ( $(this).val() == "") {
			$(this).val("Password");
		}
	});
    */

});