var sLastSection = '';
var sLastSubSection = '';
var sSection = '';
var sSubSection = '';
var nCounter = 0;
var bFlashLoaded = false;
var bFirstSectionLoaded = false;

function setFlashLoaded()
{
	bFlashLoaded = true;	
}

function setSectionNested(section, subSection)
{
	sSection = section;
	sSubSection = subSection;
	window.location = '/beta/#'+section+"_"+subSection;	
}


function flashMovie(movieName)
{
    if (navigator.appName.indexOf("Microsoft") != -1) {
        return window[movieName];
    }
    else {
        return document[movieName];
    }
}

function getSection()
{
	var currentLocation = window.location.href;
	var section 	= currentLocation.substring(currentLocation.indexOf('#')+1,currentLocation.length);	
	var del = section.indexOf('_');
	if (del > 0)
		section = section.substring(0,del);
	return section;
}

function getSubSection()
{
	var currentLocation = window.location.href;
	var section 	= currentLocation.substring(currentLocation.indexOf('#')+1,currentLocation.length);	
	var subSection = '';
	var del = section.indexOf('_');
	if (del > 0)
		subSection 	= section.substring(del+1,section.length);
	return subSection;
}


function preloadLayoutImages()
{
	var img1 = Image(100,100);
	img1.src = "http://www.theanthologyproject.com/beta/images/site_bg.gif";
	var img2 = Image(100,100);
	img2.src = "http://www.theanthologyproject.com/beta/images/site_footer.gif";
}


function preloadSection()
{	
	var oFlash 			= flashMovie('theantho');
	var oHeader 		= document.getElementById('header');
	
	var section = getSection();
	
	if (section == 'shop')
	{
		oFlash.style.height = 204 +"px";
		oHeader.style.height = 204 +"px";
	}
	
	
	
}

function reloadSection()
{
	if (bFlashLoaded == true)
	{		
		var section = window.location.href;
		section = section.substring(section.indexOf('#')+1,section.length);
		if (section=='')
			loadSection('home');	
		else
			loadSection(section);	
	}
		
	window.setTimeout('reloadSection()',1000);
}





function pageInit()
{
	// init the heights for the dynamic sections
	for (i=0;i<s.length;i++) 
	{
		if (s[i] != "")
			sh[s[i]] = document.getElementById('sectionid_'+s[i]).offsetHeight;
	}
	reloadSection();
	window.setTimeout(reloadSection,1000);	
}





function loadSection(section) 
{
	var subSectionDel = section.indexOf('_');
	var subSection = '';
	var bStoreLoaded = false;

	if (subSectionDel > 0)
	{
		subSection = section.substring(subSectionDel+1,section.length);
		section = section.substring(0,subSectionDel);	
	}
	
	if (sSubSection!=subSection)
	{
		sSubSection = subSection;		
		if(section == 'shop')
		{
			if (subSection.length > 0)
			{
				document.getElementById("storeframe").src = '../store/'+sSubSection ;
			}
			else
			{
				document.getElementById("storeframe").src = '../store/index.php?main_page=product_info&cPath=1&products_id=5';	
			}
			bStoreLoaded = true;
		}
		
		if (sLastSubSection=='')
			sLastSubSection = sSubSection;
	
	}

    if (sSection!=section)
    {
		
		nCounter++;
		if (document.getElementById('sectionid_'+section)==null)
		{
			section = 'home';
			subSection = null;
		}

		if (section == 'shop')
		{
			/*
			if (bStoreLoaded == false)
			{
				sSubSection = "index.php?main_page=product_info&cPath=1&products_id=5";
			}*/
			setHeaderPreviewsVisible(false);
		}
		else
		{
			setHeaderPreviewsVisible(true);		
		}
		
		self.location.href = "#"+section+(sSubSection!=''?"_"+sSubSection:"");

		sLastSection = sSection;		
		sSection = section;
		if (sLastSection=='')
			sLastSection = sSection;

		flashMovie('theantho').setCursorLocation(sSection);
		handleResize(section,'section_main',0, nCounter)
	}

	document.title = "The Anthology Project - " + sSection.charAt(0).toUpperCase() + sSection.slice(1)			
	bFirstSectionLoaded = true;

}


function handleResize(section,target,height,counter)
{
	
	if (counter == nCounter)
	{
		oTarget = document.getElementById(target); 	
		nHeight = oTarget.offsetHeight;

		oSection  		= document.getElementById('sectionid_'+section);
		nSectionHeight 	= sh[section];

		if (height > 0)
		{
			if (Math.abs(nSectionHeight-height)<=1)
			{ }
			else
			{
				nNewHeight = height-(height-nSectionHeight)/2;
				oTarget.style.height = nNewHeight + "px";
				oSection.style.height = nNewHeight + "px";
				setTimeout ( 'handleResize("'+section+'","'+target+'",'+nNewHeight+','+counter+')' , 50 );    
			}
		}
		else
		{
			
			oSection.style.position = 'relative';
			oSection.style.top = "0px";
			oSection.style.left = "0px";			
				
			if (nHeight == 0 || bFirstSectionLoaded==false)
			{
				oTarget.style.height = sh[section] + "px";
				oSection.style.height = sh[section] + "px";
				return;
			}
			
			oLastSection = document.getElementById('sectionid_'+sLastSection);
			if (oLastSection)
			{
				oLastSection.style.position = 'absolute';
				oLastSection.style.top = "-9999px";
				oLastSection.style.left = "-9999px";
			}
			
			oTarget.style.height = nHeight + "px";
			
			handleResize(section,target,nHeight,counter); 
		}

	}
}




var nPreviewVisibleResizeCounter = 0;
function setHeaderPreviewsVisible(bVisible)
{
	if (bVisible == true)
	{
		nTargetSize = 371;
	}
	else
	{
		nTargetSize = 204;	
	}
	_previewResize(++nPreviewVisibleResizeCounter,nTargetSize)
}
function _previewResize(nCounter,nTargetSize)
{
	if (nCounter == nPreviewVisibleResizeCounter)
	{
		var oFlash 			= flashMovie('theantho');
		var oHeader 		= document.getElementById('header');
		var nHeaderHeight 	= oHeader.offsetHeight;
		
		if (Math.abs(nHeaderHeight-nTargetSize) <= 1)
		{
			oFlash.style.height = nTargetSize + "px";		
			oHeader.style.height = nTargetSize + "px";	
		}
		else
		{
			var nNewSize = nHeaderHeight-(nHeaderHeight-nTargetSize)/2;	
			oFlash.style.height = nNewSize + "px";	
			oHeader.style.height = nNewSize-1 + "px";
			setTimeout ( '_previewResize("'+nCounter+'","'+nTargetSize+'")' , 50 ); 
		}	
	}
}

//--------------------------------------------------------------------------
// SHOPPING CART FUNCTIONS
//--------------------------------------------------------------------------
function updateFlashCart(nSize)
{
	if (bFlashLoaded == true )
	{
		var fm = flashMovie('theantho');
		if (fm)
			fm.setCartSize(nSize);
	}
}



preloadLayoutImages();