
	var IsCreative = false;
	var IsFirstSearch = true;
	
	/// << ****************** SESSION FUNCTIONS
	var m_timer = null;
	
	function SessionTimer()
	{
		//m_timer = setTimeout("SendHeartbeat()", (1000 * 60 * 5))
	}
	
	function VoidSession()
	{
		Cookie.remove("FWSession");	
		alert('Session Expired');
	}
	
	function ResetSessionTimer()
	{
		if(m_timer != null)
			clearTimeout(m_timer);
			
		SessionTimer();
	}	
	
	/// >> ****************** SESSION FUNCTIONS
	
	/// << ****************** HEARTBEAT FUNCTIONS
	
	function SendHeartbeat()
	{
		var url = '/fotoweb/heartbeat.fwx' + NoCache('?');		
		load_url(url, null, null);
	}
	
	function Complete_SendHeartbeat()
	{
		ResetSessionTimer();
	}
	
	/// >> ****************** HEARTBEAT FUNCTIONS
	
	/// ****************** AJAX FUNCTIONS
	function load_url(url, dest, oncomplete)
	{
		var myAjax = new Ajax(url, {method: 'get', update: dest, onComplete: oncomplete });
		myAjax.request();
	}
	/// ****************** AJAX FUNCTIONS	
	function SetDiv(sourcediv, targetdiv)
	{
		if( sourcediv != false )
		{
			targetdiv.setHTML( sourcediv.getValue() );		
		}
	}
	
	function Ajax_Complete_Lightbox()
	{
		SetDiv($('lb_albumname'), $('lb_name'));
		SetDiv($('lb_albumsize'), $('lb_amount_display'));
		SetDiv($('lb_albumdesc'), $('lb_desc'));
		
		var select = $('lightbox_select');
		var text = $('lb_albumname').getValue() + " (" + $('lb_albumsize').getValue() + ")";
		
		select.options[ select.selectedIndex ].text = text;
		
		ResetSessionTimer();
	}
	function RenderLightbox()
	{
		var url = "__lightboxes.fwx";
		load_url(url, $('lb_list'), Complete_RenderLightbox);
	}
	function Complete_RenderLightbox()
	{		
		//alert( $('LightBoxList').innerHTML );
		$('lb_list').innerHTML = $('LightBoxList').innerHTML;		
		load_lightbox();
	}
	
	function Ajax_Begin()
	{		
		$('search_result_grid').setHTML('<img src="graphics/LoadingCircle.gif" />');
	}
	function RemoveFromLightbox(albumid, foxid)
	{		
		var url = '/fotoweb/cmdrequest/RemoveFromAlbum.fwx?';
		url += 'fileId=' + foxid;
		url += '&AlbumId=' + albumid;
		
		load_url(url, null, Ajax_Complete_RemoveFromLightbox);
	}
	function Ajax_Complete_RemoveFromLightbox()
	{
		load_lightbox();
	}
	function objectname_search(objectname)
	{
		var url = 'cpimages_searchresults.fwx?archiveId=';
		url += $('archive_cb2').getValue();
		url += '&search=(IPTC005%20contains%20' + objectname + ')';
		$('LastSearchValue').setProperty('value', '');		
		Ajax_Begin();
		load_url(url, $('search_result_grid'), Ajax_Complete_Search);
	}
	function load_lightbox()
	{		
		if( $('lightbox_select').length > 0)
		{
			var lightboxid = $('lightbox_select').getValue();
			var url = 'cpimages_lightbox.fwx?AlbumId=' + lightboxid + NoCache('&');
			load_url(url, $('LightBoxRendering'), Ajax_Complete_Lightbox);
			$('searchform_position').setProperty('value', '0');
		}
		else
		{
			$('LightBoxRendering').innerHTML = '';
		}		
	}

	function load_search()
	{
		var archiveid = $('archive_cb2').getValue();
		var url = 'cpimages_searchresults.fwx?ArchiveId=' + archiveid;
		Ajax_Begin();
		load_url(url, $('search_result_grid'), Ajax_Complete_Search);
		
		$('LastSearchValue').setProperty('value', '');
		Show_SearchInResults_Checkbox(false);		
	}
	
	function execute_search(form)
	{
	    ///get the checkbox that indicates if we are to search witin the results 
		var obj = $('CheckBox_SearchInResults');
			
		///if the search in results box is not checked
		///we empty out the last search property so that 
		///the new search criteria will not append the value 
		///to subsequent searches.
		if(!obj.checked)
		    $('LastSearchValue').setProperty('value', '');
			    
		$('searchform_searchfield').value = $('searchform_searchfield').value.replace(/'/g, "\\'");
		var str = BuildGet(form);
		$('searchform_searchfield').value = $('searchform_searchfield').value.replace(/\\'/g, "'");
		var url = 'cpimages_searchresults.fwx?' + str + NoCache('&');
		load_url(url, $('search_result_grid'), Ajax_Complete_Search);
		Ajax_Begin();
				
		Show_SearchInResults_Checkbox(true);
		
		return false;
	}
	function Ajax_Complete_Search()
	{		
		$('LastSearchValue').setProperty('value', $('hiddenQueryValue').getValue() );

		$('searchform_searchfield').select();// = '';			
		$('CurrentNav').setHTML($('hiddenNavigation').innerHTML );
		
    
		$('CheckBox_SearchInResults').checked = false;	
	
		Set_SearchInResults_Value( IsCreative );	
		
		if(IsCreative)
		{
			if(IsFirstSearch)
			{
				showLB();
				IsFirstSearch = false;
			}
		}
		
		/// reset the div scroll position
		$('grid').scrollTop = 0;
		ResetSessionTimer();		
		
		/// reset the sorting checkbox
		$('CheckBox_Sorting').checked = false;
		Set_Sorting_Value();
	}
	function Ajax_Complete_AddLightbox()
	{
		if( $('lightbox_select').length == 0)
			RenderLightbox();
		else
			load_lightbox();
	}
	function EmailLightbox()
	{
		var url = 'cpimages_lighboxemail.pop.fwx?AlbumId=' + $('lightbox_select').getValue();;
		WindowPopup(url, 'EmailLightbox', '600', '500');
	}
	function Complete_EmailLightbox()
	{
	}
	
	function AddAlert(theform)
	{
		var url = '/fotoweb/cmdrequest/Alerts.fwx';
	    new Ajax(url, {postBody: theform, update: null, onComplete: Complete_AddAlert}).request();
	    return false;
	}
	function Complete_AddAlert()
	{
		alert('Alert successfully added!');
		ResetSessionTimer();
	}
	function RefreshSearch()
	{
		var url = "cpimages_searchresults.fwx?";
		
		url += "position=" + $('search_currpage').getValue();
		url += "&archiveId=" + $('archive_cb2').getValue();
		url += "&sorting="  + $('CheckBox_Sorting').getValue();
		url += "&search="  + $('LastSearchValue').getValue();
		url += NoCache('&');
		
		Ajax_Begin();
		load_url(url, $('search_result_grid'), Ajax_Complete_Search);
	}
	function GoToPage(archiveid, position)
	{
		var url = "cpimages_searchresults.fwx?";
		
		url += "position=" + position;
		url += "&archiveId=" + archiveid;		
		url += "&sorting="  + $('CheckBox_Sorting').getValue();
		url += "&search="  + $('LastSearchValue').getValue();
		url += NoCache('&');
		
		Ajax_Begin();
		load_url(url, $('search_result_grid'), Ajax_Complete_Search);
	}	
	
	/// <summary>
	/// This method will build a string based on form of current active editor.
	/// </summary>
	function BuildGet(theform)
	{
	   //return element from form into array
	   var FormElements = theform.elements;
	   //form string
	   var strForm = "";
	   //loop through FormElements array and build append to stringbased on form.
	   for (i=0; i < FormElements.length; i++)
	   {
		   
			if(i != 0)
			{
				if(strForm.charAt(strForm.length - 1) != "&")	
				 	strForm += "&";
			}
			
			if(FormElements[i].value != null)
			 	strForm += FormElements[i].name + "=" + FormElements[i].value.replace(/&/g, "%26");
	   }

	   return strForm;
	}
	
	function Show_SearchInResults_Checkbox(visible)
	{		
		var obj = $('Hidable_SearchInResults');
		if(visible)
		{
			obj.style.display = "";
			obj.checked = false;
		} 
		else 
		{
			obj.style.display = "none";
		}		 
	}
	
	function Set_SearchInResults_Value()
	{
		var obj = $('CheckBox_SearchInResults');
				
		if(obj.checked)	
			obj.value = '1';
		else
			obj.value = '0';				
	}
	
	function Set_Sorting_Value()
	{
		var obj = $('CheckBox_Sorting');
		var value = 'ModifiedTimeDsc';
		
		if(!obj.checked)		
		{
			value = 'ModifiedTimeAsc';
		}
		
		obj.setProperty('value', value);		
	}

	function AddToCart(id, divid)
	{
		/// TODO: 
		
		var url = '/fotoweb/cmdrequest/AddToShoppingCart.fwx?f=' + id + '&SuccessURL=/fotoweb/cpimages_cartsize.fwx' + NoCache('?') + '&ErrorURL=/fotoweb/cpimages_cartsize.fwx';

		load_url( url, divid, Ajax_Complete_AddCart);		
	}		
	
	function Ajax_Complete_AddCart()
	{
		ResetSessionTimer();
	}
	
	function AddToLightbox(id)
	{		
		/// TODO: 
		var url = '/fotoweb/cmdrequest/AddToAlbum.fwx?f=' + id + NoCache('&');
		load_url( url, null, Ajax_Complete_AddLightbox);		
	}

	function ToggleItem(divid, foxid)
	{
		if( $(divid).hasClass('item') )
		{
			$(divid).toggleClass('item_selected');
		}
		else
		{
			$(divid).toggleClass('item');
		}		
	}	
	
	function SelectAll()
	{	
		var inputcollection = $ES("input", "LightBoxRendering");
		
		for(i=0; i< inputcollection.length; i++)
		{					
			if(inputcollection[i].type == "checkbox")
			{
				var chk = $(inputcollection[i].id);
				if(!chk.checked)
				{	
					var parts = inputcollection[i].id.split("_");				
							
					ToggleItem("item_" + parts[1], parts[0]);
					chk.checked = true;
				}
			}
		}
		
	  
	}
	function SelectNone()
	{
		var inputcollection = $ES("input", "LightBoxRendering");
		
		for(i=0; i< inputcollection.length; i++)
		{					
			if(inputcollection[i].type == "checkbox")
			{
				var chk = $(inputcollection[i].id);
				if(chk.checked)
				{	
					var parts = inputcollection[i].id.split("_");				
							
					ToggleItem("item_" + parts[1], parts[0]);
					chk.checked = false;
				}
			}
		}
	}
	function RemoveSelected()
	{
		var inputcollection = $ES("input", "LightBoxRendering");
		
		for(i=0; i< inputcollection.length; i++)
		{					
			if(inputcollection[i].type == "checkbox")
			{
				var chk = $(inputcollection[i].id);
				if(chk.checked)
				{	
					var parts = inputcollection[i].id.split("_");				
							
					RemoveFromLightbox($('archive_cb2').getValue(), parts[0]);					
				}
			}
		}
	}
	function AddSelectedToCart()
	{
		var inputcollection = $ES("input", "LightBoxRendering");
		
		for(i=0; i< inputcollection.length; i++)
		{					
			if(inputcollection[i].type == "checkbox")
			{
				var chk = $(inputcollection[i].id);
				if(chk.checked)
				{
					var parts = inputcollection[i].id.split("_");
					AddToCart(parts[0], $('cart_amount'));					
				}
			}
		}
	}
	
	function AddLightboxToCart()
	{
		var inputcollection = $ES("input", "LightBoxRendering");
		
		for(i=0; i< inputcollection.length; i++)
		{					
			if(inputcollection[i].type == "checkbox")
			{
				var parts = inputcollection[i].id.split("_");
				AddToCart(parts[0], $('cart_amount'));				
			}
		}
	}
	
	function DeleteLightbox()
	{
		var response = confirm('Are you sure you wish to delete this lightbox?');

		/// only remove is response is true (Yes)
		if(response)
		{			
			/// TODO: 
			var url = '/fotoweb/cmdrequest/DeleteAlbum.fwx?AlbumId=' + $('lightbox_select').getValue();	
			load_url(url, null, Ajax_Complete_DeleteLightbox);
		}
		
	}
	function Ajax_Complete_DeleteLightbox()
	{	
		var select = $('lightbox_select');
		select.options[ select.selectedIndex ] = null;				
		load_lightbox();
	}
	 
	 function ShowFeaturedCollection()
	 {		
		 var url = '/fotoweb/galleries/featured_collection.html' + NoCache('?');
		load_url(url, $('left_column'), Complete_ShowFeaturedCollection);
	 }
	 
	 function ShowCurrentEvents()
	 {			
		var url = '/fotoweb/galleries/event_gallery.html' + NoCache('?');
		load_url(url, $('left_column'), Complete_ShowCurrentEvents);
	 }
	  function ShowCategories()
	 {			
		var url = '/fotoweb/galleries/collections.html' + NoCache('?');
		load_url(url, $('left_column'), Complete_ShowCategories);
	 }
	 
	  function ShowCanadianCollection()
	 {			
		var url = '/fotoweb/galleries/canadian_collection.html' + NoCache('?');
		load_url(url, $('left_column'), Complete_ShowCanadianCollection);
	 }
	 
	 function Complete_ShowFeaturedCollection()
	 {
		$('featuredimage').src = 'graphics/featured_active.gif';
		
		if(IsCreative)
			$('eventsimage').src = 'graphics/categories.gif';
		else
			$('eventsimage').src = 'graphics/events.gif';
	 }
	 
	 function Complete_ShowCurrentEvents()
	 {
		$('eventsimage').src = 'graphics/events_active.gif';
		$('featuredimage').src = 'graphics/featured.gif';
	 }
	 
	 function Complete_ShowCategories()
	 {		 
		$('eventsimage').src = 'graphics/categories_active.gif';
		$('featuredimage').src = 'graphics/can_collection.gif';
		init_collections();
	 }
	 
	 function Complete_ShowCanadianCollection()
	 {
		$('eventsimage').src = 'graphics/categories.gif';
		$('featuredimage').src = 'graphics/can_collections_active.gif';
		init_collections();
	 }
	 
	 
	 function WindowPopup(url, windowName, width, height )
	 {		
		var winl = (screen.width - width) / 2;
	    var wint = (screen.height - height) / 2;
	  	
	  	var windowFeatures = 'dependent=yes,height=' + height + ',width='+ width +',top=' + wint +',left=' + winl + ',menubar=0,scrollbars=1,status=0,titlebar=0,toolbar=0,resizable=1';
		var wnd;

		wnd = window.open(url, windowName, windowFeatures);
		wnd.focus();
	 }
	 
	 function LoadSearch(url)
	 {
		 url = "cpimages_searchresults.fwx?" + url.replace('&amp;', '&');
		 //alert(url);
		 Ajax_Begin();		 
		 load_url(url, $('search_result_grid'), Ajax_Complete_Search);
		 Show_SearchInResults_Checkbox(true);
	 }
	 
	 function ShowProgress(div)
	 {
		 div.setHTML('<img src="graphics/LoadingCircle.gif" />');
	 }
	 
	 function ShowAdvancedSearch()
	 {
		 url = "cpimages_advancedsearch.pop.fwx";
		 load_url(url, $('search_result_grid'), null);
	 }
	 
	function NoCache(start)
	{
		var string = start + Math.random() + '=' + Math.random();
		return string;
	}
	
	function EditLightbox()
	{
		var url = "cpimages_editalbum.pop.fwx?AlbumId=" + $('lightbox_select').getValue() + NoCache("&");
		WindowPopup(url, 'CreateLightBox', '300', '170');
	}
	
	function OnToolsChange()
	{
		var selected = $('ToolsSelect').getValue();
		
		if(selected == "0")
			return;
		else if(selected == "BarView.fwx")
		{
			var url = selected + '?' + $('hiddenBarView').getValue();
			WindowPopup(url, 'BarView', 160, 500);
		}
		else
		{			
			WindowPopup(selected, 'Tools', 730, 300);
		}
		
		$('ToolsSelect').selectedIndex = 0;
	}
	
	function SetSearchArchive(sender)
	{
	     /*
	     Fix for IE. IE does not like getValue when called on an INPUT of TYPE=radio
	     Legacy : $('archive_cb2').value = sender.getValue();
	     */
		 $('archive_cb2').value = sender.value;		         
	}
	
	function init_collections()
	{
		var item = $E('li', 'collections');
		var i=0;
		while(item.getNext()!=item.getLast())
		{
			i++;
			item = item.getNext();
			setItem(item,i);
		}
	}
	
	function setItem(item,i)
	{
		var isEven = !((i + 1)%2);
		
		if(isEven)
		{
			item.setStyle('background-color','#E9EDED');
		}
	}
