// JavaScript Document
//Contains global UI functions




//Global Loader:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
//
//The Global loader currently provides 3 different sizes.  Small, Large, & Text (Text will only display text and no graphic)
//:::: binder = the div that will be matched in size and height by the white screen
//:::: text = text string to be displayed above the loader
//:::: size = takes a size parameter small OR large OR text (note: text removes the graphic and will only display the text past in the 'text' parameter)
//:::: newId = If declared, assigns an id to the loaderfor instances where multiple loading graphics may be present at once.  newId must be passed when clearLoad is called, if supplied to globalLoad
//
globalLoad = function (binder, text, size, newId, clone) {
		
		lHeight = $(binder).getHeight();
		lWidth = $(binder).getWidth();
		Element.insert($(binder), '<div id="contentLoader" class="contentLoader" style="height:' + lHeight + 'px;width:' + lWidth + 'px; display:none;" align="center"><div id="loaderImg' + size + '">' + text +'</div></div>');
		
		
		$('contentLoader').appear();
		if (typeof newId != "undefined") {
			$('contentLoader').id = "contentLoader" + newId;
		}		
}


//Clear loader
clearLoad = function (closeId) {
	if (typeof closeId != "undefined" || typeof closeId != "0" ) {
		$('contentLoader' + closeId).remove();
	} else {
		$('contentLoader').remove();
	}
}







//Global Lightwindow:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
//Note: globalWin utilizes the globalBase element that is created with the DE global header and footer
var IE = document.all?true:false;


globalWin = function (path,hideEmbed,bio) {
	
	
	var flashEls = $$("object");
	for (var i=0; i<flashEls.length; i++) {
		flashEls[i].style.visibility = "hidden";
		}
		

	
	
	//special exception for player to hide embedded media
	if (hideEmbed == 'true'){
		var vidHeight = $('vidWindow').getHeight();
		if (vidHeight > 300) {
			$('placeholder').style.height = vidHeight+"px";
		}
		embedHide = 1;
		$('vidWindow').style.position = "absolute";
		$('vidWindow').style.left = "-1200px";
	} else {
		embedHide = 0;
	}
	
	
	//create the screen
	var win = "<div id='globalWin' onclick='clearWin();' align='center'></div>";
	//insert into DOM
	Element.insert($('base'), win);
	//set it's width and height
	//$('globalWin').clonePosition($('globalBase'));
	var newDim = document.viewport.getHeight()
	var glblDim = $('base').getHeight()
	if (newDim < glblDim){
		newHt = glblDim;	
	}else{
		newHt = newDim;	
	}

	$('globalWin').style.height = newHt+"px";
	$('globalWin').style.width = "100%";
	
	
	
	
	//window.scrollTo(0,0);
	
	if	(IE) {
		removeEls('hide');
	}
	
	
	//create the content window
	var conWin = "<div id='conWin' align='center'><div id='conWinInner' style='top:40px;visibility:hidden;' align='center'></div></div>";
	//insert into DOM
	$('globalWin').insert({before: conWin});
	$('conWinInner').style.visibility = "hidden";
	
	
	Event.observe(window, 'scroll', function() {
		if ($('globalWin')){										 
			resizeWin();
		}
	});


	Event.observe(window, "resize", function() {
		if ($('globalWin')){										 
			resizeWin();
		}
	});
	
	//creat the content window
	new Ajax.Updater('conWinInner', path, {
  		parameters: {  },
		evalScripts: true,
		onComplete: function(){
			var windowContent = $('conWinInner').firstDescendant();
			var conWidth = $(windowContent).getWidth();
			$('conWinInner').style.width = conWidth+"px";

			//insert close window link
			var closeWin = "<a href='#' class='closeWin' id='closeWin' onclick='clearWin(); return false;' style='display:none;'></a><div id='win_top'></div><div id='win_bot'></div>";
			$('conWinInner').insert(closeWin);
			
			
			//get the viewport height and center the content if the viewport is tall enough
/*			var vpH = document.viewport.getHeight();
			var cwH = $('conWinInner').getHeight();
			var diff = (vpH - cwH) / 2;
			if (diff > 40){
				$('conWinInner').style.top = diff+"px";
			}*/
		//	var coordinates = $('conWinInner').viewportOffset();
  		 // alert("Distance from the Left : " +  coordinates[0] );
   			//alert("Distance from the Top  : " +  coordinates[1] );
			resizeWin(0);
			$('conWinInner').hide();
			$('conWinInner').style.visibility = "";
			$('conWinInner').appear();
						
			closemyWin();
			//initiate custom scrollbar
			if(bio == 1){
			
				givemeScroll();
				
			}
			
			
		}
	});
	
	
}

closemyWin = function(){
	setTimeout(faders,2000)	
}

givemeScroll = function(){
	setTimeout(initScroll,3000)	
}

faders = function() {
	//alert('Surprise!')
	$('closeWin').appear();	
}

removeEls = function(act) {
	if (act == 'hide'){
	
		$$('select').each(function(s,index) {
				s.hide();
		});
	
	} else {
	
		$$('select').each(function(s,index) {
			s.show();
		});
		
	}
	
}



resizeWin = function(observeCall) {
	var element = 'conWinInner';
	if($(element) != null) {
		if(typeof window.innerHeight != 'undefined') {
			
			yWithScroll = document.body.scrollHeight;
			var maxTop = Math.round(yWithScroll - $(element).getHeight());
			
			var newHeight = Math.round(document.viewport.getScrollOffsets().top + 
			((window.innerHeight - $(element).getHeight()))/2);

			if (newHeight < 0) {
				var newHeight = 1;
			} /*else if (newHeight > maxTop){//if the new position is great than the height of the lightwindow - body height
				var newHeight = maxTop - 1;
			}*/
			
			if (window.innerHeight < $(element).getHeight() && observeCall != 1){
				$(element).style.top = newHeight+'px';
			} else if (window.innerHeight > $(element).getHeight()){
				$(element).style.top = newHeight+'px';
			}
				
		} else {//this is IE primarily 
			
			yWithScroll = document.body.scrollHeight;
			var maxTop = Math.round(yWithScroll - $(element).getHeight());
			
			var newHeight = Math.round(document.viewport.getScrollOffsets().top + 
			((document.viewport.getHeight() - $(element).getHeight()))/2)
			
			if (newHeight < 0) {
				var newHeight = 1;
			} /*else if (newHeight > maxTop){
				var newHeight = maxTop - 1;
			}*/
		
			
			if (document.viewport.getHeight() < $(element).getHeight() && observeCall != 1){
				$(element).style.top = newHeight+'px';
			} else if (document.viewport.getHeight() > $(element).getHeight()){
				$(element).style.top = newHeight+'px';
			}
			
		}
	}
}
	
	
/*	var newDim = document.viewport.getHeight()
	var glblDim = $('base').getHeight()
	if (newDim < glblDim){
		newHt = glblDim;	
	}else{
		newHt = newDim;	
	}

	$('globalWin').style.height = newHt+"px";
	
	//get the viewport height and center the content if the viewport is tall enough
	var vpH = document.viewport.getHeight();
	var cwH = $('conWinInner').getHeight();
	var diff = (vpH - cwH) / 2;
	if (diff > 40){
		$('conWinInner').style.top = diff+"px";
	}
	
}*/

clearWin = function () {
	$('conWinInner').fade();	
	setTimeout(removeWin,1000)
//	Element.remove($('globalWin'));
//	Element.remove($('conWin'));
	
	var flashEls = $$("object");
	for (var i=0; i<flashEls.length; i++) {
		flashEls[i].style.visibility = "visible";
	}
	
	
	
	if	(IE) {
		removeEls('show');
	}
	
	

	
	
	//special exception for player to hide embedded media
	if (embedHide == 1){
		embedHide = 1;
		$('vidWindow').style.position = "relative";
		$('vidWindow').style.left = "0px";
	}
	
	
}
removeWin = function(){
	Element.remove($('globalWin'));
	Element.remove($('conWin'));
	
}

initScroll = function(){
	var scrollbar = new Control.ScrollBar('scrollbar_content','scrollbar_track');	

	
}
loadProg = 0;
loadPage = function(page){
		$(page+'LoaderSmall').appear();
		if(loadProg == 1){return;}
		loadProg = 1;
		//$('masterLoader').style.visibility = "hidden";
		$('masterLoader').setOpacity(0.0);
		setNav(page);	
		new Ajax.Updater('masterLoader', '/v1/'+page+'.cfm', {
			parameters: {  },
			evalScripts: true,
			onComplete: function(){
				new Effect.Opacity('masterLoader', { from: 0.0, to: 1.0, duration: 0.5 });
				$(page+'LoaderSmall').fade();
				if(page == 'photos'){
					photosFunc();
				}else if(page == 'music'){
					albumAction();
				}
			},
			onSuccess: function(){
				loadProg = 0;	
			}
		});
}

setNav = function(page){
	$('content_wrap').className = page;
}

photosFunc = function(){
	/*thumbAction();
	albumBtnAction();*/	
	initMenu();	
}

/*Audio Section functions*/
albumAction = function(){
	$$('area.mapper').each(function(item) {
		item.observe('mouseover', albumHover);//using mouseenter/leave only currently available in prototype 1.6.1 RC2
		item.observe('mouseout', albumOff);
		item.observe('click', albumSel);
		
	});
}

albumHover = function(ev){
	var idCheck =  this.id;
	$('A'+idCheck).className = ($('A'+idCheck).className!='discSel' ? 'discHover' : 'discSel');
	
}

albumOff = function(ev){
	var idCheck =  this.id;
	$('A'+idCheck).className = ($('A'+idCheck).className!='discSel' ? '' : 'discSel');
	
}

currentAlbumSel = 'Atww1';

albumSel = function(ev){
	$('trackShell').hide();
	var idCheck =  this.id;
	$(currentAlbumSel).className='';
	$('A'+idCheck).className = 'discSel';	
	new Ajax.Updater('trackShell', '/v1/include_albums.cfm?albumName='+idCheck, {
		parameters: {  },
		onComplete: function(){
			switch (idCheck)
				{
				case 'rtc':
				  albumTitle = "Red in Tooth and Claw";
				  break;
				case 'tww1':
				  albumTitle = "The Way the World Works Volume 1";
				  break;
				case 'tww2':
				  albumTitle = "The Way the World Works Volume 2";
				  break;
				case 'tww3':
				  albumTitle = "The Way the World Works Volume 3";
				  break; 
				case 'tww4':
				  albumTitle = "The Way the World Works Volume 4";
				  break; 
				case 'tww5':
				  albumTitle = "The Way the World Works Volume 5";
				  break;   
				default:
				  albumTitle = "The Way the World Works Volume 1";
				}
			setAlbumTitle(albumTitle);	
			Effect.BlindDown('trackShell');$('trackShell').appear();


			//$('trackShell').appear();
		}
	});
	currentAlbumSel = 'A'+idCheck;
}

setAlbumTitle = function(tt){
	$('albumTitle').update(tt);
}

launchLyrics = function(aid){
	globalWin('/v1/includes/albumLyrics.cfm?albumName='+aid,0,1);
}