var atag = false;

/////////////////////////////
//SWFADDRESS FUNC
/////////////////////////////

//init function
function initAddressManager()
{
	if(ajax_available)
	{
		addLinkClickListeners();
		SWFAddress.addEventListener(SWFAddressEvent.CHANGE, handleSWFAddressEvent);
	}
	else
	{
		page_url = segs.join('/');
		handleSwfAddressMenuUpdate(page_url);
	}
	if(page_url != site_url){}
}

//upates address
function updateAddress(href)
{
	//get uri
	var tmp_page_url = href.replace(site_url, '');
	
	//update address
	if(tmp_page_url != "" && tmp_page_url != page_url)
	{
		//if new address force players to stop
		stopAllPlayers();
		
    	//set the page url, if not null or not current url
    	page_url = tmp_page_url;
    	SWFAddress.setValue(page_url); 
	}
	return false;
}

//handles address change
function handleSWFAddressEvent(evt)
{
	gallery_url = '';
	debug(deeplinkDebug, "handleSWFAddressEvent:"+evt.path);
	if(evt.path != "/")
	{
		//set page url
		page_url = (evt.path).substr(1);
		
		//handle menu, and check if we need to update content
		var processLink = handleSwfAddressMenuUpdate(page_url);
		if(processLink)
		{
			//if its in the sitemap
			var href = processLink.attr('href');
			var temp_url 	= href.replace(site_url, '');

			var temp_segs 	= removeEmpty((page_url.replace(temp_url, '')).split('/'));
			if(temp_segs.length == 1)
			{
				gallery_url	= temp_segs[0];
				page_url	= temp_url;
				debug(deeplinkDebug, "its a gallery call:"+page_url);
			}
			
			debug(deeplinkDebug, "found menu item:"+temp_url+", page_url:"+page_url);
			if(temp_url == page_url)
			{
				//process call
				debug(deeplinkDebug, "process call");
				processPageCall();
			}
		}
		else if(!isIntialDeepLink())
		{
			debug(deeplinkDebug, "not intial deeplink, not a valid url");
			
			//else if not a valid uri, or if folder
			//and if page is not set
			if(!pageData && !is_mobile)
			{
				//if not mobile then show current page
				SWFAddress.setValue("/");
				renderFirstPage();
			}
		}
	}
	
	//intial ddeplink pasted
	intialDeepLink = false;
}

/////////////////////////////
//LISTENERS FUNC
/////////////////////////////

//add link listener
function addLinkClickListeners()
{
	if(!is_mobile)
	{
		///add drag, and audio only if not mobile
		$(".draggable").draggable({ containment: "parent", handle: '.handle'});
		$(".audioblock .audiolist a").click(function(){onAudioLinkClicked($(this)); return false;});
	}

	$("a[href*='"+site_url+"'][target!=_blank]").not(".no_msg_panel a, .switch a, .sitemap_arrow a").click(function(){onLinkClicked($(this)); return false;});
}

//content link listener function
function onLinkClicked(link) 
{    
	if(typeof(link.attr) == "function" && typeof(link.attr('href')) == "string")
	{
		atag = link;
		var href = atag.attr('href');
		
		//remove focus if there
		if(href.indexOf('#focus') >= 0)
		{
			href = popSegs(href, 1, '#');
		}
		
		
		//get id e.g. carousel_14335
		var galleryId = isGalleryClicked(atag);

		//is a multi gallery link clicked
		if(galleryId && isMulitGallery())
		{
			debug(deeplinkDebug, "multi gallery update:"+galleryId);
			//if multi gallery
			//intialise gallery
			initGallery(galleryId);

			//update gallery
			updateGallery();
		}
		else
		{
			//if the uri needs to be updated
			debug(deeplinkDebug, "update address");
			updateAddress(href);
		}
	}
	return false;
}

//on audio link clicked
function onAudioLinkClicked(link)
{
	if(typeof(link.attr) == "function" && typeof(link.attr('href')) == "string" && !is_mobile)
	{
		atag = link;
		var href 	= atag.attr('href');
		var abox 	= atag.closest('.audiobox');
		
		abox.find('.audiolist div.selected').toggleClass('selected', false);
		atag.parent().toggleClass('selected', true);

		//hide all info boxes
		abox.find('.audiolist_info, .audiolist_info .audioitem').hide();
		
		//find clicked item
		if(atag.parent().size() > 0)
		{
			//get id of item
			var au_id = atag.parent().attr('id');
			var aui_id = au_id.replace('au_', 'aui_');
			if(abox.find('.audiolist_info #'+aui_id).size())
			{
				abox.find('.audiolist_info').show();
				abox.find('.audiolist_info #'+aui_id).show();
			}
		}
		
		var iframe 	= abox.find('.audioplayer iframe');
		if(iframe.size() > 0)
		{
			//if iframe found then 
			//flash is not available
			iframe.attr('src', href);
		}
		else
		{
			//if flash available then update flash component
			updateSource(abox.find('object'), href);			
		}
	
	}
}










