/**
 * @author Giulio Chiarenza
 * 
 */

$(document).ready(function(){
	
	// Ajax pagination
	
		$("#pagination_friends a").live("click", function() {
			$("#pagination_friends").html("More friends loading...");
			$.get(this.href + "&paginating_friends=true", null, null, "script");
			return false;
		});
		
		$("#pagination_messages a").live("click", function() {
			$("#pagination_messages").html("More messages loading...");
			$.get(this.href + "&paginating_messages=true", null, null, "script");
			return false;
		});
		
		$("#pagination_gallery a").live("click", function() {
			$("#pagination_gallery").html("More Home Stars loading...");
			$.get(this.href + "&paginating_gallery=true", null, null, "script");
			return false;
		});
		
	// Ajax friend sorting
	
		$("a.friend_sort").live("click", function() {
			$("#pagination_friends").html("Sorting...");
			$.get(this.href, null, null, "script");
			return false;
		});	
		
	// Remove friend
	
		$("a.remove_friend_link").live("click", function() {
			//$($(this)).replaceWith("<img src='/images/indicator.gif' />");
			$.get($(this).attr("href"), $(this).serialize(), null, "script");
			return false;
		});	
	
	// Wall messages
	
		$(".submit_message_form").live("submit", function() {
			$.post(this.action, $(this).serialize(), null, "script");
			return false;
		})
	
	// Wall message replies
	
		$(".reply_textarea").Watermark("Reply to message...");
		
		// slide reply textarea up and down when you click in or out of it  
		$(".reply_textarea").focus(function(){
			$(this).animate({"height": "54px",}, "fast" );
			//$(".new_message_reply #button_block").slideDown("fast");
			return false;
		});
		$(".reply_textarea").blur(function(){
			$(".reply_textarea").animate({"height": "18px",}, "fast" );
			return false;
		});
		
		$(".submit_reply_form").live("submit", function() {
			$.post(this.action, $(this).serialize(), null, "script");
			return false;
		})
			
	// Dashboard change character image on hover
	
		$("#userpic_small").mouseover(function() {
    		img_src = $(this).find("img").attr("src");
    		$(this).find("img").attr("src","/images/ctw3/new_badge_change_char.png");
  		}).mouseout(function(){
    		$(this).find("img").attr("src",img_src);
  		});

	
	// switch to another user's home star page
	
		$("a.switch_to_friend").live("click", function() {
			$(".ctw3_switchable").slideToggle("fast");
			$("<h3 class=\"switch_to_friend_loading\">Loading...</h3>").insertAfter(".ctw3_switchable");
			$.get($(this).attr("href"), $(this).serialize(), null, "script");
			return false;
		});	
		
	// travel to user homestar in the flash app
		
		$(".travel_to_homestar").live("click", function() {
			$.get($(this).attr("href"), $(this).serialize(), null, "script");
			return false;
		});
		
	// change character
	
		//$(".change_character").live("click", function() {
		//	$.get($(this).attr("href"), $(this).serialize(), null, "script");
		//	return false;
		//});
		
	// check for key rewards
	
		$(".check_for_key_rewards a").live("click", function() {
			$(".check_for_key_rewards").html("<img src='/images/indicator.gif' />");
			$.get($(this).attr("href"), $(this).serialize(), null, "script");
			return false;
		});
	
	// slides out the "buy the game" links on the home page and my world page
		$("a.show-buy-links").click(function(){
			$("#buy-links").slideToggle("normal");
			return false;
		});
		
	// replace check key rewards button with loading spinner
		$("#key_rewards_button a").click(function(event) {
			//console.log($("#key_rewards_button").html());
			$("#key_rewards_button").html("<img src='/images/check_key_rewards_load.gif' />");
			return true;
		});
		
	// report abuse (Message Wall)
		$(".message_options a.utility").live("click", function() {
			//console.log($(this));
			//$($(this)).replaceWith("<img src='/images/indicator.gif' />");
			$.get($(this).attr("href"), $(this).serialize(), null, "script");
			return false;
		});	
		
	/*******************************************
	 * PLUGIN USAGE BELOW...
	 *******************************************/		
	
	// tabs
		
		$("#tabs").tabs();
		$("#tabs2").tabs();
		
	// rounded corners
	
		$(".rounded").corners();
		
	// accordion lists
		
		$("#accordion").accordion();
		$("#accordion2").accordion();
		
	// coda slider
		
		$("#coda-slider-1").codaSlider({
			dynamicArrows: false,
			dynamicTabs: false,
			autoSlide: true,
			autoSlideInterval: 4000,
			autoSlideStopWhenClicked: true
		});
		
	// auto-tab in form fields	
		
		$('#tmail1').autotab({ target: 'tmail2', format: 'numeric' });
		$('#tmail2').autotab({ target: 'tmail3', format: 'numeric', previous: 'tmail1' });
		$('#tmail3').autotab({ target: 'tmail4', format: 'numeric', previous: 'tmail2' });
		$('#tmail4').autotab({ format: 'numeric', previous: 'tmail3' });
		
		$('#friendcode1').autotab({ target: 'friendcode2', format: 'numeric' });
		$('#friendcode2').autotab({ target: 'friendcode3', format: 'numeric', previous: 'friendcode1' });
		$('#friendcode3').autotab({ format: 'numeric', previous: 'friendcode2' });
		

	// Tooltips using JQuery QTip plugin
		$(".fb-tooltip").qtip({
	   		content: 'NEW! Your Facebook friends will be able to take a look at your World and listen to your Jukebox, ' +  
				'even if they are not members of Club Treasure World!',
			position: {
		      	corner: {
		        	target: 'topMiddle',
		         	tooltip: 'bottomLeft'
		      	}
		   	},
			style: {
	      		padding: 5,
		      	textAlign: 'center',
				fontWeight: 'bold',	
				fontSize: '14px',
	      		tip: 'bottomLeft',
	      		name: 'light' // Inherit the rest of the attributes from the preset style
	   		},
	
	   		show: 'mouseover',
	   		hide: 'mouseout'
		})
		
		$("#key_rewards_button img").qtip({
	   		content: 'Check for new Key Rewards often!',
			position: {
		      	corner: {
		        	target: 'topMiddle',
		         	tooltip: 'bottomMiddle'
		      	}
		   	},
			style: {
	      		padding: 5,
		      	textAlign: 'center',
				fontWeight: 'bold',	
				fontSize: '14px',
	      		tip: 'bottomMiddle',
	      		name: 'light' // Inherit the rest of the attributes from the preset style
	   		},
	
	   		show: 'mouseover',
	   		hide: 'mouseout'
		})
		
					
	
	// Colorbox

	//Examples of how to assign the ColorBox event to elements.
		$("a[rel='gamescreens']").colorbox({transition:"elastic", contentCurrent:"{current} / {total}", title:"", previous:"", next:"", current:""});
		$("#flash").colorbox({href:"/images/fpo_screen.jpg"});
		
		// launch character creator in colorbox
		$(".colorbox-link").colorbox({
			scrolling:false, 
			overlayClose:false, 
			opacity:.7, 
			close:"", 
			title:"",
			//onClosed:function(){ location.reload(); }
		});	
		
		$(".launch_char_creator").colorbox({
			onOpen:function(){ $(".flash_game_container").html(""); },
			scrolling:false, 
			overlayClose:false, 
			opacity:.7, 
			close:"", 
			title:"",
			onClosed:function(){ location.reload(); }
		});
		
		$("a.user_preview").colorbox({
			scrolling:false, 
			overlayClose:false, 
			opacity:.7, 
			close:"Close", 
			title:""
		});	
		
});

/******************************************
 * CUSTOM FUNCTIONS
 ******************************************/



jQuery.fn.submitWithAjax = function() {
	this.unbind('submit', false);
	this.submit(function() {
		//$(".add_tags_form .buttons").replaceWith("<img src='/images/ajax_loader.gif' />");
		$.post(this.action, $(this).serialize(), null, "script");
		return false;
	}) 
	return this;
};
  
  

