$(document).ready(function(){

	// optinal info toggle, user registration
	// fancy scrollbars, see jScrollPane.js
	$('div.scroll').jScrollPane();
	
	// profile summary animation
	$(".profile_summary_recent_tab").toggle(
		function() {
			$(".profile_summary_recent_wrap,.profile_summary_recent").show("fast");
			$(this).addClass("current");
		},
		function() {
			$(".profile_summary_recent_wrap,.profile_summary_recent").hide("fast");
			$(this).removeClass("current");
		}
	);
	
	// user rating vote show animation
	$("#rate_user").toggle(
		function() {
			$("#rate_user_container").show("fast");
			$(this).addClass("current");
		},
		function() {
			$("#rate_user_container").hide("fast");
			$(this).removeClass("current");
		}
	);

// TOOL TIPS
	// get position of cursor
   $("span.tool_tip").mousemove(function(e){
   }); 
   
	$("span.tool_tip_inside").each(function() {
		$(this).html('<span class="btn"><i></i><span><span></span><i></i>'+ $(this).html() +'</span></span>');
	});
	
	$("span.tool_tip").hover(
		function(e) {
		   var x = e.pageX - this.offsetLeft;
		   var y = e.pageY - this.offsetTop;
		   $(this).children("span.tool_tip_inside").css({visibility: "visible", left: x+"px", top: y+"px"});
		   $('#status').html(x +', '+ y);
		},
		function() {
			$(this).children("span.tool_tip_inside").css({visibility: "hidden"});
		}
	);

	// Registration field toggle (gamertags)
	$("div.gamertag").hide();	
	$("input[@type='checkbox']").click(
	  function() {
	   $(this).siblings("div.gamertag").toggle();
	  }
	);
	
	$("#month").focus(function(){
		if(this.value == '00'){
			this.value = '';
		}
	});
	
	$("#month").blur(function(){
		if(this.value == ''){
			this.value = '00';
		}
	});
	
	$("#day").focus(function(){
		if(this.value == '00'){
			this.value = '';
		}
	});
	
	$("#day").blur(function(){
		if(this.value == ''){
			this.value = '00';
		}
	});
	
	$("#year").focus(function(){
		if(this.value == '0000'){
			this.value = '';
		}
	});
	
	$("#year").blur(function(){
		if(this.value == ''){
			this.value = '0000';
		}
	});
	
	$("#about_me").keyup(function(){
		var max = parseInt(this.getAttribute('maxlength'));
		var val = this.value;

		if(val.length > max){
			this.value = this.value.substr(0, max);
		}

		var left = max - this.value.length;
		$('#max_chars_txt').text(left);
	});	

	if($.tooltip){
		$('.tooltip').tooltip({
			track: true,
			delay: 0,
			showURL: false,
			opacity: .75,
			fixPNG: true,
			bodyHandler: function(){
				if(this.tooltipText){
					return this.tooltipText;
				}else{
					return this.innerHTML;
				}
			}
		});
	}

});

var do_scroll = true;
var stop_scrolling = false;
function scrollItemList(dir){
	var menu = document.getElementById('item_scroll_list');

	if(!stop_scrolling && menu.scrollTop != menu.scrollHeight){
		var amt = 29;
		if(dir == 'down'){
			menu.scrollTop += amt;
		}else{
			menu.scrollTop -= amt;
		}
		scroll_interval = setTimeout("scrollItemList('"+ dir +"');", 100);
	}else{
		stop_scrolling = false;
	}
}
function stopItemScroll(){
	stop_scrolling = true;
}

function favorite_toggle(topic_id, session_id){
	$.ajax({
		url:'/ajax_add_favorite_thread',
		data: {
			topic_id:topic_id, 
			action:"AddFavorite",
			session_id: session_id
		},
		cache:false,
		error: function() {
			alert("there was an error");
		},
		success: function(html) {
			if (html != "max_reached") {
				$('#favorite_'+topic_id).html(html);
			} else {
				GB_show('', '/favorite_limit/', 230, 515, 'favorite_limit');
			}
		}
	});
}

var has_rated = false;
function submitRating(thread_id, rating, session_id){
		$.ajax({
		url:'/ajax_user_rating',
		data: {
			rated_id:thread_id, 
			rating:rating,
			action:'UpdateRating',
			session_id: session_id
		},
		cache:false,
		error: function() {
			alert("there was an error");
		},
		success: function(json) {
			var json_value = $.parseJSON(json, true);
			if (json_value.result) {
				has_rated = true;
				$('#rating_' + thread_id).html(json_value.result);
			} else {
				GB_show('', '/rating_message?text='+json_value.error,245,515,'confirm_leave');
			}
			//initTooltips(thread_id);
			//$('.tooltip .tt_content').css({width:'300px'});
		}
	});
}

	
function initTooltips(thread_id) {
	if($.tooltip){
		//$('#rating_' + thread_id + ' .tooltip').tooltip({
		$('.tooltip').tooltip({
			track: true,
			delay: 0,
			showURL: false,
			opacity: .75,
			fixPNG: true,
			bodyHandler: function(){
				if(this.tooltipText){
					return this.tooltipText;
				}else{
					return this.innerHTML;
				}
			}
		});
	}
}