// JavaScript Document
function siteRel(){
	switch(window.location.hostname){
 
	case 'dev.digitalhive.com.au':
		siteRel = '/player_rater/website/dev/';
	break;
	
	case 'clients2.digitalhive.com.au':
		siteRel = '/player_rater/';
	break;
	
	default:
		siteRel = '/';	
	}
	return siteRel;
}

function getFixture($rnd){
	  $('#fixturepanel').load('fixture.ajax/'+$rnd);
	  return false;
}

function getHomeFixture($rnd){
	  $('#fixture_bar').load('homefixture.ajax/'+$rnd);
	  return false;
}

function selectType(){
	if($('#select_type').val()=="preseason"){
		$('#preseason').show();
		$('#finals').hide();
		$('#premiership').hide();
		getFixture($('#pre_select_round').val());
	} else if($('#select_type').val()=="finals"){
		$('#preseason').hide();
		$('#finals').show();
		$('#premiership').hide();
		getFixture($('#f_select_round').val());
	} else {
		$('#preseason').hide();
		$('#finals').hide();
		$('#premiership').show();
		getFixture($('#ps_select_round').val());
	}

	return false;
}

function gotoPlayer($player){
	siteRel = siteRel();
	window.location.href = siteRel + "player/" + $player;
}

function showLogin(){
	$('#loginbox').show(); 
	$('#username').focus();
}
function showSignup(){
	$('#signupbox').show(); 
}
function toggleResponse(){
	$('#response').slideToggle();
}
function showResponse(){
	$('#response').slideDown();
}
function postResponse(username){
	$('#response').slideDown('slow', function(){
		$('#comment').focus();
		$('#comment').html('@'+username+' ');
		$('#comment').setCursorPosition(99);
	  });
}


/*
function rate(params){
	alert($(this).attr('title'));
	var aurl = '../ajax/rate/'+params;
	$.ajax({
		   url: aurl,
		   success: function(data){
			   if(data > 0){
				   alert(scopre.attr('title'));
			   } else {
				   alert('An error has occured saving your rating - please try again');
			   }
		   }
		   });
	return false;
}
*/
$(document).ready(function(){
		$('.rate_btn').bind('click', function(){
            
			var aurl = '/ajax/rate/'+$(this).attr('rel');
			$.ajax({
			   url: aurl,
			   success: function(data){
				   if(data > 0){
//					   alert(scopre.attr('title'));
				   } else {
					   alert('An error has occured saving your rating - please try again');
					   return false;
				   }
			   }
			   });
			$(this).parent().parent().children().children('a').removeClass('selected');
			$(this).addClass('selected');
			return false;
		});

		$('.frmrate a').bind('click', function(){
			var aurl = '/ajax/frmrate/'+$(this).attr('rel');
			var aprnt = $(this).parent().attr('id');
			$.ajax({
			   url: aurl,
			   success: function(data){
					$('#'+aprnt).html(data);
			   }
			   });
			return false;
	   });

		$('#game_rater tr').hover(
		   function()
		   {
			$(this).addClass('highlight');
		   },
		   function()
		   {
			$(this).removeClass('highlight');
		   }
		)


		/* Style Select List */
		//$("#select_player").sSelect({ddMaxHeight: '300px'});
		
	
		/* Help Toolips */
		$("#help_rating_index").tooltip({      
			tip: "#rating_index_tooltip",          
			effect: "slide",
			direction: "down",
			slideOffset: 2,
			events: {def:"click,mouseout"}         
		}).dynamic( { 
			bottom: { 
				direction: "down", 
				bounce: true 
			} 
		});
		
		$("#help_stats").tooltip({      
			tip: "#help_stats_tooltip",          
			effect: "slide",
			direction: "down",
			slideOffset: 2,
			events: {def:"click,mouseout"}   
		}).dynamic( { 
			bottom: { 
				direction: "down", 
				bounce: true 
			} 
		}); 
		
		 $("#help_graph").tooltip({      
			tip: "#help_graph_tooltip",          
			effect: "slide",
			direction: "down",
			slideOffset: 2,
			events: {def:"click,mouseout"}   
		}).dynamic( { 
			bottom: { 
				direction: "down", 
				bounce: true 
			} 
		}); 
		
		$("#help_response").tooltip({      
			tip: "#help_response_tooltip",          
			effect: "slide",
			direction: "down",
			slideOffset: 2,
			events: {def:"click,mouseout"}         
		}).dynamic( { 
			bottom: { 
				direction: "down", 
				bounce: true 
			} 
		}); 
		
		$("#help_watchlist").tooltip({      
			tip: "#help_watchlist_tooltip",          
			effect: "slide",
			direction: "down",
			slideOffset: 2,
			events: {def:"click,mouseout"}         
		}).dynamic( { 
			bottom: { 
				direction: "down", 
				bounce: true 
			} 
		});
		
		$("#help_lboard").tooltip({      
			tip: "#help_lboard_tooltip",          
			effect: "slide",
			direction: "down",
			slideOffset: 2,
			events: {def:"click,mouseout"}         
		}).dynamic( { 
			bottom: { 
				direction: "down", 
				bounce: true 
			} 
		});
		
		$("#help_ratings").tooltip({      
			tip: "#help_ratings_tooltip",          
			effect: "slide",
			direction: "down",
			slideOffset: 2,
			events: {def:"click,mouseout"}         
		}).dynamic( { 
			bottom: { 
				direction: "down", 
				bounce: true 
			} 
		});
		
		$("#help_rating_index").click(function() {
			return false;
		});
	
	
	
	
		// show/hide advanced options	
		$("a#more_filters").click(function() {
			$("#advanced_filters").slideToggle(300);
			if ($("input[name='rating_type']:checked").val() == "All Ratings")
			   $("#select_group_container").slideDown(300);
			else
			   $("#select_group_container").hide();
			return false;
		});
		
		$("#all").click(function()				
			{
				var checked_status = this.checked;
				$(".suppgroup").each(function()
				{
					this.checked = checked_status;
				});
				$("#none").attr("checked", false);
			});	
		
		$("#none").click(function()				
			{
				var checked_status = this.unchecked;
				$(".suppgroup").each(function()
				{
					this.checked = checked_status;
				});
				$("#all").attr("checked", false);
	
			});	
			
			$('#search_item').autofill({
				value: 'Search for players, teams, coaches...',
				defaultTextColor: '#a2a2a2',
				activeTextColor: '#404040'
			});
			$('#blackout,#close').bind('click', function() {
				$('#loginbox,#signupbox').hide(); 
			});

		$("#searchpieces").bind("mouseenter",function(){
	//	don't initiate unless they've mouseovered first
			}).bind("mouseleave",function(){
						$('#predictive').slideUp('5000');
		});

		$('#search_item').bind("mouseenter",function(){
				if ($('#search_item').val()){
						$('#predictive').slideDown('5000');
				}
		});
			
		$.fn.setCursorPosition = function(pos) {
			if ($(this).get(0).setSelectionRange) {
			  $(this).get(0).setSelectionRange(pos, pos);
			} else if ($(this).get(0).createTextRange) {
			  var range = $(this).get(0).createTextRange();
			  range.collapse(true);
			  range.moveEnd('character', pos);
			  range.moveStart('character', pos);
			  range.select();
			}
		  }
});

		function predict(){
			if($('#search_item').val().length > 1 && $('#search_item').val() != 'Search for players, teams, coaches...'){
				$.post('/psearch', $("#searchform").serialize(), function(data) {
					$('#predictive').html(data);
				});
			} else {
				$('#predictive').html('');
			}
		}
		
		function predictSearch(term){
			$('#search_item').val(term);
			$('#predictive').slideUp('5000');
			$('.search_button').click();
		}


