var _pageElem = window.location.hash;
_pageElem = _pageElem.replace('#', '');

//if(_pageElem!='')
//{
//	document.getElementById('NewsSelect').selectedIndex = _pageElem;
//}

//alert(_pageElem);

var _state = '0';

 $(document).ready(function(){
    
    if(_pageElem=='')
    {
    		$.ajax({
			type: "GET",
			url: "server_scripts/getnewsfunctions.php",
			data: 'fx=mr&id=&dir=',
			async: false,
			success: function(data){
				//alert(data);
				$('div#News').empty();
				$('div#News').append(data);
				
			}
		});
	
		curId = document.getElementById('hidden_NewsId').value;
		//alert(curId);
		if(IsNumeric(curId))
		{
			window.location.hash = curId;
		}
    }
  });

function ShowNewsSelector()
{
	if(_state=='0'){
		$("div#newsselector2").slideDown("slow");
		$("#newsArrow").attr("src", 'images/up_arrow.png');
		_state = '1';
	} else {
		$("div#newsselector2").slideUp("slow");
		$("#newsArrow").attr("src", 'images/down_arrow.png');
		_state = '0';
	}
}
function SwitchNews(direction)
{
	var curId = window.location.hash;
	curId = curId.replace("#","");
	

	var qs = "id=" + curId + "&dir=" + direction + "&fx=dir";
	//alert(qs);
//	alert(indexid);

	$.ajax({
			type: "GET",
			url: "server_scripts/getnewsfunctions.php",
			data: qs,
			async: false,
			success: function(data){
				//alert(data);
				$('div#News').empty();
				$('div#News').append(data);
				
			}
		});
	
	curId = document.getElementById('hidden_NewsId').value;
	//alert(curId);
	if(IsNumeric(curId))
	{
		window.location.hash = curId;
	}
}

function SelNewsArticle(indexid)
{
	$('div#News').empty();

//	alert(indexid);
	
	$.ajax({
			type: "GET",
			url: "server_scripts/getnewsarticle.php",
			data: "id=" + indexid,
			success: function(data){
				$('div#News').append(data);
			}
		});

	// close the window and set the status to 0	
	$("div#newsselector2").slideUp("slow");
	$("#newsArrow").attr("src", 'images/down_arrow.png');
	_state = '0';
	window.location.hash = indexid;
}

function SelectedNewsChanged(id)
{
	var indexid = ''
	if(id=='')
	{
		
	}
	else
	{
		indexid = id;
		$('div#News').empty();
		//	alert(indexid);
		$.ajax({
				type: "GET",
				url: "server_scripts/getnewsarticle.php",
				data: "id=" + indexid,
				success: function(data){
					$('div#News').append(data);
				}
			});
		
	window.location.hash = indexid;
	}
	
	
}


function LoadMenu()
{
	var hashText = '';
	if(window.location.hash.length>0)
	{ 
		hashText = window.location.hash; 
		hashText = hashText.replace('#', '');
	}
	$("div#newsselector2").empty();
	
	$.ajax({
		type: "GET",
		url: "server_scripts/newsfunctions.php",
		data: "fx=div&id=" + hashText,
		async: false,
		success: function(data){
			$("div#newsselector2").append(data);
		}
	});
	
	SelectedNewsChanged(hashText);
}

function IsNumeric(sText)
{
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;

 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
   
}

//function LoadMenu()
//{
//	var hashText = '';
//	if(window.location.hash.length>0)
//	{ 
//		hashText = window.location.hash; 
//		hashText = hashText.replace('#', '');
//	}
//	
//	$.ajax({
//		type: "GET",
//		url: "server_scripts/newsfunctions.php",
//		data: "fx=select&id=" + hashText,
//		async: false,
//		success: function(data){
//			var selectbox = document.getElementById('NewsSelect');
//			var chunks = data.split(';');
//			var i;
//			for(i=0;i<chunks.length;i++)
//			{
//				if(chunks[i].length>0)
//				{
//					var pieces = chunks[i].split(',',3);
//					if(hashText=='')
//					{ hashText = pieces[0]; }
//					
//					if(pieces[0]==hashText)
//					{
//						selectbox.options[selectbox.options.length] = new Option(pieces[1], pieces[0], true, true);
//					}
//					else
//					{ selectbox.options[selectbox.options.length] = new Option(pieces[1], pieces[0], false, false); }
//				}
//			}
//		}
//	});
//	
//	SelectedNewsChanged(hashText);
//}