/// <reference path="jQuery.intellisense.js" />
var map=null;
var submenuTimerID=null;
var activeSubMenu=null;

function GetWindowWidth(){var width=0;if(typeof(window.innerWidth)=='number'){width=window.innerWidth;}else if(document.documentElement&&document.documentElement.clientWidth){width=document.documentElement.clientWidth;}else if(document.body&&document.body.clientWidth){width=document.body.clientWidth;}if(!width||width<100){width=100;}return width;}
function GetWindowHeight(){var height=0;if(typeof(window.innerHeight)=='number'){height=window.innerHeight;}else if(document.documentElement&&document.documentElement.clientHeight){height=document.documentElement.clientHeight;}else if(document.body&&document.body.clientHeight){height=document.body.clientHeight;}if(!height||height<100){height=100;}return height;}


function txtSearchKeyword_OnFocus(o)
{
	var s=$(o);
	if (s.val()==s.attr("WatermarkText"))
		s.val("");
}


function txtSearchKeyword_OnBlur(o)
{
	var s=$(o);
	var length =  s.val().length;
	//(($("#" + btnSearchClientID).attr("disabled",length==0 ? "disabled" : "");
	if (s.val()=="")
	{
		s.val(s.attr("WatermarkText"));
	}
}

function txtSearchKeyword_OnChange(o)
{
	alert($(o).val().length);
	if ($(o).val().length==0)
	{
		$("#" + btnSearchClientID).attr("disabled","disabled" );
	}
	else
		$("#" + btnSearchClientID).attr("disabled","" );
	
}




function Header_OnHover(o)
{
	for(var i=1;i<10;i++)
		SetVisibility("HeaderOverlay" + i, false);

	var l=GetPositionLeft(o);
	var t=45;
	
	switch(o.className)
	{
		case "TopNavigationBlock TopNavigationBlock1":
			SetVisibility("HeaderOverlay1",true);
			SetPosition("HeaderOverlay1",l,t);
			break;
			
			
		case "TopNavigationBlock TopNavigationBlock2":
			SetVisibility("HeaderOverlay2",true);
			SetPosition("HeaderOverlay2",l,t);
			break;
		case "TopNavigationBlock TopNavigationBlock3":
			SetVisibility("HeaderOverlay3",true);
			SetPosition("HeaderOverlay3",l,t);
			break;
			
		case "TopNavigationBlock TopNavigationBlock4":
			SetVisibility("HeaderOverlay4",true);
			SetPosition("HeaderOverlay4",l,t);
			break;
		case "TopNavigationBlock TopNavigationBlock5":
			SetVisibility("HeaderOverlay5",true);
			SetPosition("HeaderOverlay5",l-7,t);
			break;
		case "TopNavigationBlock TopNavigationBlock6":
			SetVisibility("HeaderOverlay6",true);
			SetPosition("HeaderOverlay6",l,t);
			break;
			
		case "TopNavigationBlock TopNavigationBlock7":
			SetVisibility("HeaderOverlay7",true);
			SetPosition("HeaderOverlay7",l,t);			
			break;
			
		case "TopNavigationBlock TopNavigationBlock8":
			SetVisibility("HeaderOverlay8",true);
			SetPosition("HeaderOverlay8",l,t);			
			break;
			
		case "TopNavigationBlock TopNavigationBlock9":
			SetVisibility("HeaderOverlay9",true);
			SetPosition("HeaderOverlay9",l,t);			
			break;
	}
}

function SetVisibility(name,visible)
{
	var o = document.getElementById(name);
	if (o!=null)
		o.style.display=(visible==true ? "block" : "none");
}

function SetPosition(id,l,t)
{
	var o=document.getElementById(id);
	o.style.left=l+"px";
	o.style.top=t+"px";
}

function GetPositionLeft(This)
{
	var el = This;
	var pL = 0;
	
	while(el)
	{
		pL+=el.offsetLeft;
		el=el.offsetParent;
	}
	return pL;
}

function GetPositionTop(This)
{
	var el = This;
	var pT = 0;
	while(el)
	{
		pT+=el.offsetTop;
		el=el.offsetParent;
	}
	return pT;
}


function OpenDropdown(labelID,listID)
{
	var label=$("#"+labelID);
	var offs=label.offset();
	$('#'+listID).css("left",(offs.left-4)+"px").css("top",offs.top+label.outerHeight()+2 + "px").slideToggle('slow');
}


function FillDropdown(realDropdownID,labelID,listID,dropdownID, cssClass)
{ 
	var s="<ul class='DropDownListUL'>";
	
	$("#" + realDropdownID + " > *").each(function()
	{	
		style="";
		if ($(this).attr("bold")=="true")
			style+="font-weight:bold;"
			
		s+="<li class='DropdownListItem " + cssClass + "' style='" + style + "' onclick=\"$('#" + labelID + "').html($(this).html());" +
			"$('#" + realDropdownID + "').val($(this).html());" +
			"$('#" + listID + "').hide();"+
			"$('#" + realDropdownID + "').trigger('change');" +
			"\">" + $(this).html() +"</li>";
	});
	s+="</ul>";
	
	//alert(s);
	$("#" + listID).html(s);
	
	$("#"+dropdownID).bind("click",function(){OpenDropdown(labelID,listID);});
	$("#"+labelID).bind("click",function(){OpenDropdown(labelID,listID);});
	
	var v=document.getElementById(realDropdownID);
	var text=v.options[v.selectedIndex].innerHTML;
	$("#" + labelID).html(text);

}


function ShowListDetailPanel(id,colorscheme)
{
	ShowModalOverlay();
	var o=$("#ListDetailPanel");
	o.css("left",(GetWindowWidth()-(o.width()+16))/2);
	o.css("top", 20);
	o.css("position", "fixed");
	o.fadeIn("normal");
	
	$.post("templates/ObjectDetails.ashx","ID="+id+"&ColorScheme="+ colorscheme,ReceiveDetails);
}

function ReceiveDetails(arg)
{
	$("#ListDetailPanelBody").html(arg);
}

function HideListDetailPanel()
{
	HideModalOverlay();
	$("#ListDetailPanel").fadeOut("normal");
}

function ShowModalOverlay()
{
	$("#divModalOverlay").css("width",GetWindowWidth()).css("height",document.body.clientHeight).css("display","block").css("opacity","0").fadeTo("normal",0.7);
}

function HideModalOverlay()
{
	$("#divModalOverlay").fadeOut("normal");
}



function ShowSubMenu(o,index)
{
	var parent=$(o.parentNode);
	var offs=$(parent).offset();
	
	if (activeSubMenu!=null)
	{
		if (activeSubMenu.attr("id")=="TopNavigationSubmenu" + index)
			return;

		if ($.browser.msie==true&&$.browser.version=="6.0")
			activeSubMenu.hide();
		else
			activeSubMenu.slideToggle("normal");
	}
	
	var left=0;
	
	if (index==9)
		left=offs.left- ($("#TopNavigationSubmenu" + index).width()-parent.width());
	else
		left=offs.left;
		
	activeSubMenu =	$("#TopNavigationSubmenu" + index).css("left",left).css("top",offs.top+parent.height());
	
	if ($.browser.msie==true&&$.browser.version=="6.0")
		activeSubMenu.css("display","block");
	else
		activeSubMenu.slideToggle("normal");
}




function StartSubmenuTimer()
{
	submenuTimerID=window.setTimeout("HideMenu()",500);
}

function StopSubmenuTimer()
{
	if (submenuTimerID!=null)
	{
		window.clearTimeout(submenuTimerID);
		submenuTimerID=null;
	}
}

function HideMenu()
{
	if (activeSubMenu!=null)
	{
		activeSubMenu.slideUp("normal");
		activeSubMenu=null;
	}
}
// -------------------------------
// Planer functions
function AddPlanerItem(id,name, htmlelement,itemType)
{
	var flyout=$("#" + flyoutClientID);
	var offset=$(htmlelement).offset();
	var target=$("#PlanerPanel_PlanerPanelBody");
	var offsetTarget=target.offset();
	flyout.css("display","block").css("opacity","0.4").css("left",offset.left).css("top",offset.top).css("width","16").css("height","16").animate({"left":offsetTarget.left,"top":offsetTarget.top,"width":target.width()+8,"height":target.height()+8},500,"linear",function(){$("#"+ flyoutClientID).fadeOut("fast");$.post("templates/Planer.ashx", "Action=ADD&ID=" + id + "&Name=" + name + "&Type=" + itemType, ReceivePlaner);});
}

function DeletePlanerItem(id,htmlelement)
{
	$(htmlelement.parentNode.parentNode.parentNode.parentNode).fadeOut("slow",function(){$.post("templates/Planer.ashx", "Action=DELETE&ID=" + id, ReceivePlaner);});
}

function ReceivePlaner(arg)
{
	var rows = arg.split('^');
	
	$("#PlanerPanel_PlanerPanelBody").html(rows[1]);
}

function ShowMap(lat,lon)
{
	if (map!=null)
		map.DeleteAllShapes();
	var mapContainer = $("#MapContainer");
	var width = GetWindowWidth();
	var height = GetWindowHeight();
	mapContainer.css("left",32).css("left",(width/2)-32).css("top",(height/2)-32).css("width","64").css("height",64).css("display","block").css("position", "fixed");
	mapContainer.animate({"left":32,"top":32,"width":width-64,"height":height-64},500,function(){$("#divMap").show();GetMap();map.Resize(width-64,height-92);AddPin(lat,lon);});
}

function AddPin(lat,lon)
{
	var latlon= new VELatLong(lat,lon);
	var shape=new VEShape(VEShapeType.Pushpin,latlon);
	map.AddShape(shape);
	map.SetCenterAndZoom(latlon,14);
}

function HideMap()
{

	var mapContainer = $("#MapContainer");
	var width = GetWindowWidth();
	var height = GetWindowHeight();
	$("#divMap").hide();
	//$("#divModalOverlay").fadeOut("fast",function(){
	mapContainer.animate({"left":(width/2)-32,"top":(height/2)-32,"width":64,"height":64},500,function(){$("#MapContainer").css("display","none");});
	//});
}

function HideMap2()
{
alert('bla');
}

function GetMap(lat,lon)
{ 
	if (map==null)
	{
		map = new VEMap('divMap');
		map.LoadMap(new VELatLong(52.4,9.7), 9 ,'r' ,false);
		map.HideDashboard();
	}
}


function ToggleCategoryPanel(o)
{
	var $source=$(o);
	var offset=$source.offset();
	$("#EventCategoryPanel").css("left",offset.left).css("top",offset.top+$source.height()).fadeIn("slow");
}

function HideCategoryPanel()
{
	$("#EventCategoryPanel").fadeOut("slow");
}


function StartDateSelected()
{
	__doPostBack(txtStartDateClientID, 0);
}

function EndDateSelected()
{
	__doPostBack(txtStartDateClientID, 0);
}


function PngFix()
{
	if ($.browser.msie==true)
	{
		if ($.browser.version=="5.5"||$.browser.version=="6.0")
		{
			var divs=$("div,table");
	
			divs.each(function()
			{
				var d=$(this);

				if (d.css("background-image")!="none")
				{	
					var url=d.css("background-image");
					url=url.substring(5,url.length-2);
					d.css("background-image","none");
					d.css("filter","progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + url + "', sizingMethod='scale')");
				}
			});
		}
	}
}


// Workaround search block enter key
function forwardBtnClick(control)
{
	var e = window.event;
	var keyunicode = e.charCode || e.keyCode;
	if (keyunicode == 13)
	{
		$("#"+control).focus();
		$("#"+control).click();
	}
}

function SwitchLinkText(o)
{
	var obj = $(o);
	if (obj.html()==obj.attr("MoreText"))
		obj.html(obj.attr("LessText"))
	else
		obj.html(obj.attr("MoreText"))
}
