// JavaScript Document
			
//load html content
function lhc(sUrl)
{	
	lastSecureLink = lastglobalMsg = "";
	
	if(sUrl.indexOf(".html") >=0 || sUrl.indexOf(".aspx")) //sUrl.search(/.aspx|.htm/i)>=0
	{		
		ShowUpdatingProgress(true);
		$(contentAreaID).hide();	
		$.cookie(lastPageCookieKey,sUrl);		
		
		$.get(sUrl,function(data){
			$(contentAreaID).html(data);
			$(contentAreaID).fadeIn('slow');
			ShowUpdatingProgress(false);
		});
		
		$("#statusBar").text(sUrl);
	}
	else
	{
		$(contentAreaID).html(formatError("<b>URL Not supported:</b> \"" + sUrl + "\""));
	}
}

function formatError(msg)
{
	return "<div class='ContentPaddingStyle'><h1 style='color:red'>ERROR:</h1>" + msg + "</div>";
}

function ShowUpdatingProgress(show)
{
	if(show)
		SetAjaxWaitingStatus("Updating content. Please wait...","#UpdateProgress");
	else
		$("#UpdateProgress").text("");
}

function ShowContent(sTarget,show)
{
	var target = document.getElementById(sTarget);
	if(target)
	{
		if(show == true)
			show = "";
		else
			show = "none";
			
		target.style.display = show;		
	}
}

//load homepage content
function lhp()
{
	ShowUpdatingProgress(true);
	lhc('home.html');
	
	var ct = document.getElementById('content_area');
	if(ct)
	{
		ct.style.display = "block";
	}
	
	var cc = document.getElementById('Catalog');
	if(cc)
	{
		cc.style.display = "none";
	}		
	ShowUpdatingProgress(false);
	
	//Reset accordion to none selected
	selectedAccordion = -1;
	$.cookie(selectedAccordionCookieKey,selectedAccordion);
	selectAccordion();
}			

var sCatFilter = "";
function loadCategory(catName)
{
	lhc('category.html');
	//var rgn = Spry.Data.getRegion('CatalogCategory');
    //if (rgn)
	{	
		//SetText('content_area', "DataRegion found: CatalogCategory");
		try
		{
			ShowUpdatingProgress(true);
			
			SetText('SelectedCategory',catName);
			
			sCatFilter = catName;
			dsCategory.setURL("xml/catalog.xml");
			dsCategory.setXPath("Catalog/Categories");
			//,{filterFunc:filterCategory, useCache:true});
			dsCategory.loadData();
			
			//dsCategory.filterData(filterCategory); // Filter the rows in the			
			//Spry.Data.updateRegion('CatalogCategory');
			//rgn.updateRegion();
			
			Spry.Data.initRegions();
			
			//ShowCatalog(true);
			ShowUpdatingProgress(false);
		}
		catch(e)
		{
			alert(e);
		}
	}
	/*else
	{
		SetText('content_area', "DataRegion not found: CatalogCategory");
	}*/
}


var filterCategory = function(dataSet, row, rowNumber)
{
	if (row["ParentName"]!= null && row["ParentName"] != "" && row["ParentName"].search(sCatFilter) >= 0)
		return row; // Return the row to keep it in the data set.
	
	return null; // Return null to remove the row from the data set.
	
}

function SetText(sTarget, text)
{
	var target = document.getElementById(sTarget);
	if(target)
	{
		target.innerHTML = text;
	}
}

function ShowCatalog(show)
{
	ShowContent('Catalog',show);
	ShowContent('content_area',!show);
}


var req;
function retrieveURL(url)
{   
	if(window.XMLHttpRequest)
	{
		req = new XMLHttpRequest();
		req.onreadystatechange =  processStateChange;
		try{
		req.open("GET",url,true);
		}catch(e){ alert(e);}
		req.send(null);
	}else if(window.ActiveXObject)
	{
		req = new ActiveXObject("Microsoft.XMLHTTP");
		if(req)
		{
		   req.onreadystatechange = processStateChange;
           req.open("GET", url, true);
           req.send();
		}
	}
}

function processStateChange()
{
	if(req.readyState == 4)
	{
		if(req.status == 200)
		{			    
			document.getElementById("contentInfo").innerHTML =  req.responseText;
			scroll(0,0);
		}else
		{
			alert("Errors: "+ req.statusText)
		}
	}
}

function ShowInfo(param) 
{
    param = param+"";
    var Html = retrieveURL(param);
}


function MM_openBrWindow(theURL,winName,features) 
{ 
	 window.open(theURL,winName,features);
}


function createVideoPlayer(href, container, width, height, modal)
{
	if(!container)
		container = "#VideoContainer";
	width = width == null ? 371 : width;
	height = height == null ? 350 : height;
	//$("#VideoContainer").html("Loading please wait...");
	//$("#VideoContainer").html(href);

	$.get("videoplayer.html",function(data){
		data = data.replace(/\$HREF/gi,href);
		data = data.replace(/\$WIDTH/gi,width);
		data = data.replace(/\$HEIGHT/gi,height);
		$(container).html(data);

		if(modal)
		{
			$(container + " #videoPlayer").dialog({width:width + 60,height:height + 120,
									buttons: {
						"Close": function() { 
							$(this).dialog("close"); 
							$(this).remove();
							//loadWelcomePage();
							//$("#cmsweb #loginWindow").dialog("open");
						}
					}
			});
		}
	});
}

function GetCacheBlock(usequest)
{
	usequest = usequest ? "?cb=" : "&cb=";
	
	return usequest + new Date().getTime();
}
	
function SetAjaxWaitingStatus(msg,targetid)
{
	msg = "<img src='images/ajax/loading.gif' align='middle'/> " + msg;
	$(targetid).show();
	$(targetid).html(msg);
}

function checkLogin()
{	
	if($DEBUG)
	{
		userLoggedInData  = "1;mykone;Mykone Saunders;";
		userLoggedIn = true;
	}
	else
	{
		userLoggedInData = $.cookie(userLoggedInCookieKey);
		userLoggedIn = userLoggedInData != "" && userLoggedInData != null ? true:false;
	}
	
	if(!userLoggedIn)
	{
		$("#topPanel #topMenu a#loginLogout").text("Please sign in");
		$("#topPanel #topMenu #welcome").text("Welcome. ");
		$("#topPanel #topMenu #adminLinks").remove();
	}
	else{		
		var data = userLoggedInData.split(";");
		var name = data[2];
		$("#topPanel #topMenu a[id='loginLogout']").text("Sign out");	
		$("#topPanel #topMenu #welcome").html("<b>Welcome back:</b> " + name + ",&nbsp;&nbsp;");
		$("a[class=secure]").removeAttr("display");
		
		if(GetUserLogin() == "admin")
		{
			$("#topPanelRightSideProfileBar").append("<span id='adminLinks'>| <a href='admin.html'>Admin Tools</a></span>");
		}
	}	
	
}

function GetUserID(){
	var data = userLoggedInData.split(";");
	return data[0];
}

function GetUserLogin(){
	if(userLoggedInData)
	{
		var data = userLoggedInData.split(";");
		return data[1];
	}
	else
	{
		return null;
	}
}


function ajaxFileUpload(callback, filename, dimensions, selector, elementID)
{
	$("#loading").ajaxStart(function(){
		$(this).show();
	}).ajaxComplete(function(){
		$(this).hide();
	});

	elementID = elementID != "" ? elementID : "fileToUpload";
	$.ajaxFileUpload
	(
		{
			url:'ajaxfileupload.aspx' + GetCacheBlock(true) + uidQS + '&fn=' + filename + '&dim=' + dimensions,
			secureuri:false,
			fileElementId: elementID,
			selector: selector,
			dataType: 'json',
			success: function (data, status)
			{
				if(typeof(data.error) != 'undefined')
				{
					if(data.error != '')
					{
						alert(data.error);
					}else
					{
						if(callback!=null)
						{
							callback.invoke();
						}
						
						if(data.msg!='')
						{
							alert(data.msg);
						}
					}
				}
			},
			error: function (data, status, e)
			{
				alert("Here in return:" + e);
			}
		}
	)	
	return false;
}