var gblLevel2 = false //this is so pages within the section folders can see the clear x.gif
var gblIsArtistDetail = false //this is to identify when a artist detail loads so we can scroll the artist bio, gets used in turnPage()

//initialize
//this function takes care of calling other necessary functions and passing default values
function init(level){
	
	if (level == 'sub') {
		gblLevel2 = true;
		document.getElementById('scrollingcontentholder').style.width = ((countPages() * 290) + 10) + 'px';
		if (countPages() > 1)  {
			document.getElementById('next').style.visibility = "visible";
		}
	} 
	
	if (level == 'artistdetail') {
		gblIsArtistDetail = true;
		document.getElementById('scrollingartistbioholder').style.width = ((countPages() * 268) + 10) + 'px';
		if (countPages() > 1)  {
			document.getElementById('bionext').style.visibility = "visible";
		}
	}	
	
}
	
//--- CONTENT AREA SCROLLING FUNCTIONS

function countPages(){
	if (gblIsArtistDetail) {
		return getNumTagsWithClassName('biopage');
	} else {
		return getNumTagsWithClassName('page');
	}
	
}

//Content scrolling functions
//turnPage - content area - calls makePageTurn and passes the width of the content viewport (290)
function turnPage(direction) {
	if (gblIsArtistDetail) {
		makePageTurn(direction,268)
	} else {
		makePageTurn(direction,290)
	}
}

//makePageTurn - takes a direction and a pageWidth and move the scrallable area accordingly
var gblOnPage = 1;
function makePageTurn(direction,pageWidth) {

	var numPages = countPages();
	var coordToPass = 0;
	
	gblSlideImage = false; //set this to false in case someone clicked on an image

	if ((direction == "next") && (gblOnPage < numPages)) {
		scrollit((-1 * pageWidth),false);
		gblOnPage = gblOnPage + 1;
	}

	if ((direction == "previous") && (gblOnPage > 1)) {
		scrollit(pageWidth,false);
		gblOnPage = gblOnPage - 1;
	}	
	
	if (!isNaN(direction)) { //for alphabetical click through
		coordToPass = (((direction * pageWidth) * -1) + pageWidth);
		scrollit(coordToPass,true);
		gblOnPage = direction;
	}

	//show/hide previous button
	if (gblOnPage != 1) {
		if (gblIsArtistDetail) {
			document.getElementById('bioprevious').style.visibility = "visible"
		} else {
			document.getElementById('previous').style.visibility = "visible"
		}			
	} else {
		if (gblIsArtistDetail) {
			document.getElementById('bioprevious').style.visibility = "hidden"
		} else {
			document.getElementById('previous').style.visibility = "hidden"
		}	
	}
	
	//show/hide next button
	if (gblOnPage == numPages) {
		if (gblIsArtistDetail) {
			document.getElementById('bionext').style.visibility = "hidden"
		} else {
			document.getElementById('next').style.visibility = "hidden"
		}	
	} else {
		if (gblIsArtistDetail) {
			document.getElementById('bionext').style.visibility = "visible"
		} else {
			document.getElementById('next').style.visibility = "visible"
		}	
	}
		
}

//slideImage - for artist detail
var gblSlideImage = false;
function slideShow(imageNum) {
	coordToPass = (((imageNum * 268) * -1) + 268);
	scrollit(coordToPass,true);
	gblSlideImage = true
}

//scroll it function for scrollable content

var startPos = 0
var anitime = 1000
var aniTimer
var curPos = 0 //used for scrolling by width, not for scrolling by coordinate

function scrollit(x,blnCoord){

		startTime = (new Date()).getTime()
		if (blnCoord) {
			endPos = x /* move to a specific x coordinate */
		} else {
			endPos = x + curPos /* in order to move by whatever the pageWidth is */
		}
		dist = endPos - startPos
		accel = dist / anitime / anitime
		if (aniTimer) aniTimer = window.clearInterval(aniTimer)
		aniTimer = window.setInterval("scrollitme()", 10)
}

function scrollitme(){
	var now = (new Date()).getTime()
	var elapsed = now - startTime
	startPos = endPos
   	if (elapsed > anitime) endScrollit()
	else {
	  var t = anitime - elapsed
 		  var x = endPos - t * t * accel
		  jumpToit(x)
 	}
}

function endScrollit(){
	jumpToit(endPos)
//	var aniTimer = window.clearInterval(aniTimer)
	window.clearInterval(aniTimer)
}
   
function jumpToit(x){
	//window.frames["rightframe"].scrollTo(x,0) /* how you would move the iframe, if used */
	if (gblSlideImage) {
		document.getElementById('scrollingimagesholder').style.left = x + "px" /* need "px" in order to work in moz */
	} else {

		if (gblIsArtistDetail) {
			document.getElementById('scrollingartistbioholder').style.left = x + "px" /* need "px" in order to work in moz */
		} else {
			document.getElementById('scrollingcontentholder').style.left = x + "px" /* need "px" in order to work in moz */
		}		
		
	}
	
	if (!gblSlideImage) { //if we are sliding images, we are jumping to an actual coordinate, so we don't need to alter the curPos
		curPos = x
	}
}	
	
//--------- PNG FOR IE (also checks for pera which would appear in the userAgent string if the browser is opera---------

if (navigator.platform == "Win32" && navigator.appName == "Microsoft Internet Explorer" && window.attachEvent && (navigator.userAgent.indexOf("pera")==-1)) {
	document.writeln('<style type="text/css">img, input.image { visibility:hidden; } </style>');
	window.attachEvent("onload", fnLoadPngs);
}

function fnLoadPngs() {
	var rslt = navigator.appVersion.match(/MSIE (\d+\.\d+)/, '');
	var itsAllGood = (rslt != null && Number(rslt[1]) >= 5.5);

	for (var i = document.images.length - 1, img = null; (img = document.images[i]); i--) {
		if (itsAllGood && img.src.match(/\.png$/i) != null) {
			fnFixPng(img);
			img.attachEvent("onpropertychange", fnPropertyChanged);
		}
		img.style.visibility = "visible";
	}

	var nl = document.getElementsByTagName("INPUT");
	for (var i = nl.length - 1, e = null; (e = nl[i]); i--) {
		if (e.className && e.className.match(/\bimage\b/i) != null) {
			if (e.src.match(/\.png$/i) != null) {
				fnFixPng(e);
				e.attachEvent("onpropertychange", fnPropertyChanged);
			}
			e.style.visibility = "visible";
		}
	}
}

function fnPropertyChanged() {
	if (window.event.propertyName == "src") {
		var el = window.event.srcElement;
		if (!el.src.match(/x\.gif$/i)) {
			el.filters.item(0).src = el.src;
			el.src = "x.gif";
		}
	}
}

function dbg(o) {
	var s = "";
	var i = 0;
	for (var p in o) {
		s += p + ": " + o[p] + "\n";
		if (++i % 10 == 0) {
			alert(s);
			s = "";
		}
	}
	alert(s);
}


function fnFixPng(img) {
	var src = img.src;
	img.style.width = img.width + "px";
	img.style.height = img.height + "px";
	img.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "', sizingMethod='scale')"
	img.src = "images/x.gif";
	if (gblLevel2) {
		img.src = "../images/x.gif";
	}		
}
 

//close iframe window function
	
	function closeArtistDetail() {
		// parent.document.getElementById('artistdetail').style.display = 'none';
		if (parent.document.getElementById('artistdetail')) {
			parentHide('artistdetail');	
		}
	}	
 	function closeLoginPanel() {
		// parent.document.getElementById('login').style.display = 'none';
		if (parent.document.getElementById('login')) {
				parentHide('login');
		}
	}	
 	function closeProfilePanel() {
		// parent.document.getElementById('formarea').style.display = 'none';	
		if (parent.document.getElementById('formarea')) {
			parentHide('formarea');
		}
	}	
 	function closeRightPanels() {
		closeArtistDetail();
		closeLoginPanel();
		closeProfilePanel();	
	}	
	function closePhotoGallery() {
		if (parent.document.getElementById('photogallery')) {
			parentHide('photogallery');
		}
	}
	function openPhotoGallery() {
		if (parent.document.getElementById('photogallery')) {
			parentShow('photogallery');
		}
	}

	
//-------LIB FUNCTIONS-----------

//getNumTagsWithClassName - param = string, returns the number of classes which match the name passed in
//Create an array of all the tags on the page
//var allPageTags = new Array(); 

function getNumTagsWithClassName(className) {  
	var numTagsOfClassName = 0;		
	var allPageTags = new Array(); 
	//Populate the array with all the page tags  
	var allPageTags = document.getElementsByTagName("div");  	//NOTE: I specified div here for versions 5.01 and 5.5 of IE, it didn't work with * (meaning all tags)
	//Cycle through the tags using a for loop  	
	for (i=0; i<allPageTags.length; i++) {  
	//Pick out the tags with our class name 	
		if (allPageTags[i].className == className) {  
		//Manipulate this in whatever way you want  
			numTagsOfClassName = numTagsOfClassName + 1;
		}  
	} 
	return numTagsOfClassName;
}	

DOM = (document.getElementById) ? true : false;
NS4 = (document.layers) ? true : false;
IE 	= (document.all) ? true : false;

//popWin
function popWin(urlString,wparam,hparam,scrollparam) {

	parentLeft = 20;
	parentTop = 20;
	
	offsetLeft = 20;
	offsetTop = 20;
	
	w = wparam + 20;
	h = hparam + 20; 
	
	if (DOM || IE) {
	
		parentLeft  	= (window.screenLeft + offsetLeft);
		parentTop   	= (window.screenTop + offsetTop);							
	
	} else if (NS4) {
	
		parentLeft      = (window.screenX + offsetLeft);
		parentTop       = (window.screenY + offsetTop);	
	
	} 	
	
	if (scrollparam == "scroll") { 
		window.open(urlString,"_blank","height=" + h + ",width=" + w + ",scrollbars=yes"); 
	} else {
		//window.open(urlString,"new","height=" + h + ",width=" + w + ",top=" + parentTop + ",left=" + parentLeft + ""); 
		window.open(urlString,"_blank","height=" + h + ",width=" + w + ""); 
	}
} 
	
//show and hide layers
function show(id) {	  
	if (document.getElementById) {		  
		document.getElementById(id).style.display="block";
	} else if (document.all) {		  
		document.all[id].style.display="block";		
	} else if (document.layers) {		  
		document.layers[id].display="block";		
	} 
} 

function hide(id) {
  	if (document.getElementById) {    
		document.getElementById(id).style.display="none";    
	} else if (document.all) {
		document.all[id].style.display="none";    } 
	else if (document.layers) {    
		document.layers[id].display="none";    
	} 
} 	
function parentShow(id) {	  
	if (document.getElementById) {		  
		parent.document.getElementById(id).style.display="block";
	} else if (document.all) {		  
		parent.document.all[id].style.display="block";		
	} else if (document.layers) {		  
		parent.document.layers[id].display="block";		
	} 
} 

function parentHide(id) {
  	if (document.getElementById) {    
		parent.document.getElementById(id).style.display="none";    
	} else if (document.all) {
		parent.document.all[id].style.display="none";    } 
	else if (document.layers) {    
		parent.document.layers[id].display="none";    
	} 
} 	
	
//stay does nothing, used in the href attribute so the page doesn't go anywhere
function stay() {
}	
