/*!
 * Baldy Beanbag JavaScript Library v1.0
 * http://www.baldybeanbag.com/
 *
 * Copyright (c) 2009 Baldy Beanbag, Inc.
 * 
 */
 
 	$(document).ready(function()
	{ 
		loadSidebars();
		FB.Connect.get_status().waitUntilReady(function(status) {
		   switch(status) 
		   {
			   case FB.ConnectState.connected:
					$("#fb_connect_image").attr("src","/img/fb_logout.gif");
					$("#fb_connect_link").click(function() {
						FB.Connect.logout(loadHomePage);
						loadHomePage();
					});
					break;
				 
			   case FB.ConnectState.appNotAuthorized:
			   case FB.ConnectState.userNotLoggedIn:
					$("#fb_connect_image").attr("src","/img/fb_login.gif");
					$("#fb_connect_link").click(function() {
						FB.Connect.requireSession(function() { loadUserPage(); });
					});
					break;
			}
		});
	})
	
	function updateConnectStatus()
	{
		
	}
	
	function updateVideoSidebar(xml)
	{
		ajaxUpdate(xml,"#videoBox");
		LoadVideoBar();
	}
	
	function updateInfobox(xml)
	{
		ajaxUpdate(xml,"#infoBox");
		pathArray = window.location.pathname.split( '/' );
		if (pathArray[1] == "videos")
		{
			updateVideoInfoBox();
		}
	}
	
	function loadSidebars()
	{
		ajaxFetch("videos", "sidebar", function(xml) { updateVideoSidebar(xml); }); 
		pathArray = window.location.pathname.split( '/' );
		if (pathArray[1] != "mypage")
		{
			ajaxFetch("misc", "infobox", function(xml) { updateInfobox(xml); });
		}
	}
	
 	function ajaxUpdate(xml, blockname)
	{
		var htmlInfoData = $(xml).find("htmlInfo");
		var blockHtml = $("blockHtml", htmlInfoData).text();
		var rtnCode = $("rtnCode", htmlInfoData).text();
		if (rtnCode == "home")
		{
			window.location.href = '/';
		}
		else
		{
			$(blockname).html(blockHtml);
		}
	}
	
	function ajaxFetch(modname, opcode, data, callback) 
	{
		if ( jQuery.isFunction( data ) ) {
			callback = data;
			data = {};
		}
		
		var postUrl = '/ajax/'+modname;
		data['modname'] = modname;
		data['opcode'] = opcode;
		data['pageUrl'] = window.location.pathname;
		
		$.ajax({
			type: "POST",
			url: postUrl,
			data: data,
			success: callback,
			dataType: "xml",
			
			// Custom header to help prevent cross-site forgery requests
			// and to flag caching bootstrap that Ajax request is being made 
			beforeSend: function(xhr) {
			  xhr.setRequestHeader("BaldyBeanbag","YES");
			}
		});
	}
		
	function dummyReturnFunc(xml)
	{
		xml = xml;
	}
	
	function trim(str)
	{
		return str.replace(/^\s+|\s+$/g, '');
	}
	
	function validateEmpty(theField) {
			 
		if ( trim(theField.value) == '') {
		
			return true;
		} 
		return false;  
	}	
		
	function xtractPath(data)
	{
		var m = data.match(/(.*)[\/\\]([^\/\\]+\.\w+)$/);
		return m[1];
	}

	/* Get rid of dotted outline when pressing button */
	function buttonBlur( blnk ) 
	{
		try {
			blnk.blur();
		} catch(e) {};
	}
	
	function doCommonSearch()
	{
	
		var form = document.getElementById('menuSearchForm');
		var sel = document.getElementById('common-search-type');
		var searchType = sel.options[sel.selectedIndex].value;
		
		if (searchType == "resorts")
		{
			form.setAttribute("action", "/resorts/SR");
		}
		else if (searchType == "ads")
		{
			form.setAttribute("action", "/wantads/SR");
			document.getElementById('common-search-value').setAttribute("value", "99+0");
			document.getElementById('common-search-value').setAttribute("name", "category");
		}
		else if (searchType == "trips")
		{
			form.setAttribute("action", "/trips/SR");
			document.getElementById('common-search-value').setAttribute("value", "WW");
			document.getElementById('common-search-value').setAttribute("name", "location");
		}
		else if (searchType == "videos")
		{
			form.setAttribute("action", "/videos/SR");
			document.getElementById('common-search-value').setAttribute("value", "SS");
			document.getElementById('common-search-value').setAttribute("name", "location");
		}
		
		form.submit();
		return true;
	}
	
	function loadUserPage()
	{
		window.location.href = '/mypage'; 
	}
	
	function loadHomePage()
	{
		window.location.href = '/';
	}
	
	function loadNewPage(thePage)
	{
		window.location.href = thePage;
	}
	
	function goBackOnePage()
	{
		history.back();
	}
	
