$(document).ready(
	function()
	{

		// open/close tournament info
		$('.tournamentSummary').click(function()
			{
			   	if($(this).next('.tournamentDetails').css('display') == 'block')
			   	{
					$(this).next('.tournamentDetails').slideUp();
					$(this).find('.open_summary').hide();
					$(this).find('.summary').show();
			   	}
			   	else
			   	{
					$(this).next('.tournamentDetails').slideDown();
					$(this).find('.summary').hide();
					$(this).find('.open_summary').show();
			   	}
			 return false;  	
			}
		);
				// open/close tournament info
		$('.hu_nav2 li a').click(function()
			{
				var id = $(this).attr('id');
				var tabNum = $(this).attr('class');
				var tourId = $(this).attr('href');
				var perTab = 57;
				var margin = ((tabNum -4) * perTab) * -1;
				if (margin > 0) {
					margin = 0;
				}
		//			$('#f_leaderboard').slideUp(function() {
						xajax_switchLeaderboards(id);
		//				$('#f_leaderboard').slideDown();
		//			});
				  
				  $(this).parent().parent().animate({ 
			        marginLeft: margin+"px"
			      }, 500 );
			 return false;  	
			}
		);
		
		// open/close tournament info
		$('.hu_nav3 li a').click(function()
			{
				var tid = $(this).attr('id');
				var tabNum = $(this).attr('class');
				var tourId = $(this).attr('href');
				var perTab = 57;
				var margin = ((tabNum -4) * perTab) * -1;
				if (margin > 0) {
					margin = 0;
				}
//					$('.f_leaderboard_'+tourId).slideUp(function() {
						xajax_switchSingleLeaderboards(tid);
//						$('.f_leaderboard_'+tourId).slideDown();
//					});
					
			      $(this).parent().parent().animate({ 
			        marginLeft: margin+"px"
			      }, 500 );
			 return false;  	
			}
		);
		
		// slide prev
		$('.prev a').click(function()
			{
				var currentMargin = $(this).parent().parent().find("ul").css("marginLeft");
				out = "px"; // replace this
				add = ""; // with this
				temp = "" + currentMargin; // temporary holder
				
				while (temp.indexOf(out)>-1) {
				pos= temp.indexOf(out);
				temp = "" + (temp.substring(0, pos) + add + 
				temp.substring((pos + out.length), temp.length));
				}
				currentMargin = temp;
				var margin = currentMargin - -114;
				if (margin > 0) {
					margin = 0;
				}
			      $(this).parent().parent().find("ul").animate({ 
			        marginLeft: margin+"px"
			      }, 500 );
				return false;
			}
		);
		
		// slide next
		$('.next a').click(function()
			{
				var currentMargin = $(this).parent().parent().find("ul").css("marginLeft");
				out = "px"; // replace this
				add = ""; // with this
				temp = "" + currentMargin; // temporary holder
				
				while (temp.indexOf(out)>-1) {
				pos= temp.indexOf(out);
				temp = "" + (temp.substring(0, pos) + add + 
				temp.substring((pos + out.length), temp.length));
				}
				currentMargin = temp;
				var margin = currentMargin - 114;
				
			      $(this).parent().parent().find("ul").animate({ 
			        marginLeft: margin+"px"
			      }, 500 );
				return false;
			}
		);
		
		//LANGUAGE SELECTION BOX
		$("a.lang_expand").click(function()	{
		   	if($(this).parent('li').find('ul.sub').css('display') == 'block')
		   	{
				$(this).parent('li').find('ul.sub').fadeOut();
				//xajax_updateNav($(this).parent('li').find('ul.sub').attr('id'),'close');
				return false;
		   	}
		   	else if($(this).parent('li').find('ul.sub').css('display') == 'none')
		   	{
		   		var list_pos = findPosY(document.getElementById('dd_container'));
		   		
				  var scrOfX = 0, scrOfY = 0;
				  if( typeof( window.pageYOffset ) == 'number' ) {
				    //Netscape compliant
				    scrOfY = window.pageYOffset;
				    scrOfX = window.pageXOffset;
				  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
				    //DOM compliant
				    scrOfY = document.body.scrollTop;
				    scrOfX = document.body.scrollLeft;
				  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
				    //IE6 standards compliant mode
				    scrOfY = document.documentElement.scrollTop;
				    scrOfX = document.documentElement.scrollLeft;
				  }
				  
		   		var window_start = scrOfY;
		   		
		   		  var myWidth = 0, myHeight = 0;
				  if( typeof( window.innerWidth ) == 'number' ) {
				    //Non-IE
				    myWidth = window.innerWidth;
				    myHeight = window.innerHeight;
				  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
				    //IE 6+ in 'standards compliant mode'
				    myWidth = document.documentElement.clientWidth;
				    myHeight = document.documentElement.clientHeight;
				  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
				    //IE 4 compatible
				    myWidth = document.body.clientWidth;
				    myHeight = document.body.clientHeight;
				  }
		   		var window_height = myHeight;
		   		
		   		var pos_top = list_pos - window_start;
		   		var pos_bottom = window_height - pos_top - 20;
		   		
		   		var direction;
		   		if (pos_bottom < 240) {
		   			direction = 'up';
		   		} else if (pos_bottom >- 240) {
		   			direction = 'down';
		   		}
		   		
		   		if (pos_bottom >= pos_top) {
		   			direction = 'down';
		   		}
  				
  				if (direction == 'up') {
  					$(this).parent('li').find('ul.sub').css({position:"absolute"}).css({top:"-235px"});
  				} else if (direction == 'down') {
  					$(this).parent('li').find('ul.sub').css({position:"absolute"}).css({top:"23px"});
  				}
				$(this).parent('li').find('ul.sub').fadeIn();
								
				//xajax_updateNav($(this).parent('li').find('ul.sub').attr('id'),'open');
				return false;
		   	}
		   });
		   
		   
		   //NORDIC LOGIN FUNCTIONS
		   

		      $('.nordic_login').keyup(function(e) {
		      //alert(e.keyCode);
		      if(e.keyCode == 13) {
				document.getElementById('fmLogin').submit();
				return false;
		      }
		      });
		      
		      
		      $('.nordic_submit').click(function() {
		      //alert(e.keyCode);
				document.getElementById('fmLogin').submit();
				return false;
		      });
		      
		      
		      $('#blogs_linker').change(function() {
				window.location.href=document.getElementById('blogs_linker').value;
				//return false;
		      });


		
		
//		   
//			$(".game-links .info a").hover(
//			      function () {
//					$(this).parent('div').parent('div').prev('div').css({visibility:"visible"}).show("");
//			      },
//			      function () {
//					$(this).parent('div').parent('div').prev('div').css({visibility:"visible"}).hide("");
//			      }
//			    );
		   
		   
		// Close Login Window
		$('a.login_close').click(function()
			{
				$(this).parent('div').parent('div').fadeOut('slow');
				$('.site_cover').slideUp();
				return false;
			}
		);
		
		   $("a.expand_contract").click(function()	{
		   	if($(this).parent('li').find('ul.sub').css('display') == 'block')
		   	{
				$(this).parent('li').find('ul.sub').slideUp("slow");
				$(this).parent('li').find('a.open').css({visibility:"hidden"});
				$(this).parent('li').find('a.closed').css({visibility:"visible"});
				xajax_updateNav($(this).parent('li').find('ul.sub').attr('id'),'close');
				return false;
		   	}
		   	else if($(this).parent('li').find('ul.sub').css('display') == 'none')
		   	{
				$(this).parent('li').find('ul.sub').slideDown("slow");
				$(this).parent('li').find('a.closed').css({visibility:"hidden"});
				$(this).parent('li').find('a.open').css({visibility:"visible"});
								
				xajax_updateNav($(this).parent('li').find('ul.sub').attr('id'),'open');
				return false;
		   	}
		   });
		   
		// Toggle Single Portlet
		$('a.expand').click(function()
			{
				$(this).parent('').next('div').slideDown("slow");
				$(this).css({visibility:"hidden"}).css({display:"none"});	
				$(this).next('a').css({visibility:"visible"}).css({display:"inline"});
			}
		);
		// Toggle Single Portlet
		$('a.contract').click(function()
			{
				$(this).parent('').next('div').slideUp("slow");
				$(this).css({visibility:"hidden"}).css({display:"none"});	
				$(this).prev('a').css({visibility:"visible"}).css({display:"inline"});
			}
		);
		/**
		$("#columns").sortable({ 	items: ".portlet",
									revert: true,
									containment: '#columns',
									opacity: 0.7});*/
		
		$("#scrollable").scrollable({items:'.items',horizontal:true});
		if (hu_offset) {
			$("#hu_scrollable").scrollable({size: 3,horizontal:true,speed: 1000,mousewheel:false,items: '.hu_items',prev:'.hu_prev',next:'.hu_next',offset:hu_offset});
			$(".current_hu").click();
		}
		
		
		$("#scrollable .items").css({height:"70px"});
		$("#scrollable .prev").css({display:"block"});
		$("#scrollable .next").css({display:"block"});
		
		
		// Toggle Image Popup
		$('#scrollable .items a').click(function()
			{
			   	if($('#image_popup').css('display') == 'none')
			   	{
					$('#image_popup').fadeIn("slow");
			   	}
					var title = $(this).attr("href");
					var desc = $(this).attr("title");
				    $("#image_popup .image_area").html("<img src=\""+title+"\"/><div class=\"image_desc\">"+desc+"</div>");
			   	
			 return false;  	
			}
		);
		
		
		// Close Image Popup
		$('#image_popup a').click(function()
			{
			   	if($('#image_popup').css('display') == 'block')
			   	{
					$('#image_popup').fadeOut("slow");
			   	}
			 return false;  	
			}
		);
		
	}
);
