//
// Global variables
var cId = "EXTERIOR",
	cWidth = 760,
	
	//expand-collapse content
	cListId = new Array,
    cValueId = new Array,
	cCnt = 0,
    chrefId = new Array,
    cAllId = "collapse_text", // default value
	
 	//preload images
	preImgList = new Array,

	//image slideshow with links
	sldImgPreload = new Array,
	sldImgList = new Array,
 	sldURIList = new Array,
 	sldStep = -1,								// start from latest added slide
	sldImgId,
	sldhrefId,
	
	//autoxpire
	axp_count = 0,
	axp_type,
	axp_default = 'no items';
	

// =================================================================================================  
// init display

//phased out!
function FLE_fixDisplay(init,containerId,containerWidth)
// fix container positions on resize of window
//
// init - (true/false) initialize?
// containerName - Id of container to reposition
// containerWidth - width of container to reposition
{
	if (init==true) { 
		document.body.onresize = FLE_fixDisplay;
		if ((containerId!=null)&&(containerId!='')) cId = containerId;
		if ((containerWidth!=null)&&(containerWidth!='')) cWidth = containerWidth;
	}

	// centers the container and adjust if negative
//	x = document.getElementById(cId);
//	x.style.left = Math.max(0,(document.body.offsetWidth-cWidth)/2);
}

// =================================================================================================  
// expand-collapse content

function FLE_expand(compactId,hrefId)
// expands to display the rest of lengthy text
// usage: <span class="hidden" id="expand_id">text to hide</span>
//        <a id="compact_id" class="compact" href="javascript:FLE_expand('expand_id','compact_id');">more...</a>
//
// id - span id of content to expand
// linkId - span id of link trigger
//
// notes: null errors - please check ids
//
{
	var x,y,k;
	
	k = null;
	for(i=0;i<cListId.length;i++) { if(cListId[i]==compactId) { k=i; break; } }

	if (k==null) {
		cListId.push(compactId);
		cValueId.push('compacted');
		chrefId.push(hrefId)
		k = cValueId.length-1;
	}
	
	x = document.getElementById(compactId);
	y = document.getElementById(hrefId);		

	mode = cValueId[k];

	switch(mode) {
		case 'expanded':	x.style.display = 'none';
							y.innerHTML = ' more... ';
							cValueId[k] = 'compacted';
							cCnt--;
							if (cCnt<1) { FLE_execCollapseAll(); cCnt=0 };
							break;
		// default state is 'compacted' to be expanded
		default: 			x.style.display = 'inline';
							y.innerHTML = ' &laquo; compact ';
							cValueId[k] = 'expanded';
							FLE_initCollapseAll();
							break;
	}		
}

function FLE_initCollapseAll()
{
	x = document.getElementById(cAllId);
	if (x) x.outerHTML = '<a id="collapse_text" href="javascript:FLE_execCollapseAll();"><img class="icon" src="/images/icons/collapse_all.gif" alt="Collapse all content" />Collapse</a>';
	cCnt++; // add counter to collapsible content
}

function FLE_execCollapseAll()
// collapses all expanded text
// collapse all ID must be 'collapse_all'
{
	for(i=0;i<cListId.length;i++) { 
		x = document.getElementById(cListId[i]);
		y = document.getElementById(chrefId[i]);
		
		x.style.display = 'none';
		y.innerHTML = ' more... ';
		cValueId[i] = 'compacted';
	}
	
	x = document.getElementById(cAllId);
	if (x) x.outerHTML = '<a id="collapse_text"><img class="icon" src="/images/icons/collapse_all.gif" alt="Collapse all content" />Collapse</a>';

}

// =================================================================================================  
// preload images

function FLE_preloadImages(Path)
// preload images
//
// Path - path to images
// Array of image filenames - no path
{
	a = FLE_preloadImages.arguments;

	if (a.length<2) return 1;

	p = Path;
	j = preImgList.length;
	
	// if last character is forward slash
	if (p[p.length-1]!='/') p[p.length]='/';

	for (i=0;i<a.length;i++) {
		
		if (a[i].indexOf("#")!=0) { 
			preImgList[j]=new Image; 
			preImgList[j++].src=Path+a[i];
		}		
	}
}

// =================================================================================================  
// image slideshow with links

function FLE_addSlides(Path)
// add images with links to slide show
//
// first parameter - path to images
// odd no. parameter (base 0) - image uri
// even no. parameter (base 0) - link uri
{
	a = FLE_addSlides.arguments;

	if (a.length<2) return 1;

	p = Path;
	j = sldImgPreload.length;
	
	// if last character is forward slash
	if (p[p.length-1]!='/') p[p.length]='/';

	for (i=0;i<(a.length/2)-1;i++) {
		d = (i*2)+1;
		if (a[d].indexOf("#")!=0) { 
//	  alert(a[d]+" : "+a[d+1]);
			sldImgPreload[j]=new Image; 
			sldImgPreload[j].src=Path+a[d];
			sldImgList[j] = Path+a[d];
			sldURIList[j++] = a[d+1];
		}		
	}
	
}

function FLE_initSlideShow(ImgId,hrefId){
	sldImgId = ImgId;
	sldhrefId = hrefId;

	setTimeout("FLE_slideShow()",12000);
}

function FLE_slideShow()
// execute slideshow with links
{ 
	x = document.images[sldImgId];
	y = document.getElementById(sldhrefId);
	l = sldImgList.length;
	
	if (!sldImgList||!x||!y) return 1;
	
	if (document.all) {
		if (sldStep<1) sldStep=l-1;
		x.filters.blendTrans.apply();
		x.src=sldImgList[sldStep];
		y.href=sldURIList[sldStep];
		x.filters.blendTrans.play();
		sldStep -= 1;
		setTimeout("FLE_slideShow()",12000);
	} 
	else setTimeout("FLE_slideShow()",1000);
}

/*=================================================================================================  
  AUTOEXPIRE ITEMS FLE v1.0 

	CODE example

	<ul>
		<script type="text/javascript">
			FLE_initAxp('mixed','no items');
			FLE_writeAxp('09/12/05','List #1','#1');
			FLE_writeAxp('09/14/05','List #2','#2');
			FLE_writeAxp('09/16/05','List #3','#3');
			FLE_concludeAxp();
		</script>
	</ul>	
*/

function FLE_initAxp(itemType,defaultText)
// init autoexpire
// itemType = normal,list,link,mixed
//   link/mixed forces a link with <a></a> no href!
{
	axp_count = 0;
	axp_default = defaultText;
	axp_type = itemType.toLowerCase();
}

function FLE_writeAxp(expDate,htmlCode,activeLink)
// display item if not yet expired
// usage FLE_writeAxp(<'mm/dd/yy'>, <html code>, [activeLink]);
{
	var s1,s2,t1,t2;
	s1=s2=t1=t2='';
	
	if (!expDate) return 1;

	// covert to javascript date format
	mm = Number(expDate.substr(0,2))-1;
	dd = Number(expDate.substr(3,2));	
	yy = Number(expDate.substr(6,2))+2000;
	
	// expire till midnight
	last_day = new Date(yy,mm,dd,23,59,00,00);
	this_day = new Date();
	
	EXT_fixDate(last_day);
	EXT_fixDate(this_day);
	
	if (this_day <= last_day) {
		
		switch (axp_type) {
			case 'list': s1 = '<li>'; s2 = '</li>'; break;
			case 'link': t1 = '<a>'; t2 = '</a>'; break;
			case 'mixed': s1 = '<li>'; s2 = '</li>'; t1 = '<a>'; t2 = '</a>'; break;
			// default = normal
		}
		
		if (activeLink) {
			t1 = '<a href="' + activeLink + '">';
			t2 = '</a>';
		}
		
		document.write(s1+t1);
		document.write(htmlCode);
		document.write(t2+s2);
		axp_count++;
	}
}

function FLE_concludeAxp()
// concludes an autoexpire block
{
	if (axp_count==0) FLE_writeAxp(11,31,99,axp_default);
	axp_count=0;
}

// =================================================================================================  
// autolist

var artist_data = new Array;
var catalogue_data = new Array;
var genre_data = new Array;

// list sort - used internally to compare list items
function ATL_sort_code(a,b) {	if (a.code < b.code) return -1; if (a.code > b.code) return 1;	return 0;	}
function ATL_sort_title(a,b) {	if (a.title < b.title) return -1; if (a.title > b.title) return 1;	return 0;	}
function ATL_sort_newest(a,b) {	if (a.datestamp > b.datestamp) return -1; if (a.datestamp < b.datestamp) return 1;	return 0;	}

// getIndex 
function getIndex(code,a) {
	for (ii=0;ii<a.length;ii++) {
		//alert('code='+code+', '+a[ii].code);
		if (a[ii].code==code) {return ii; break;}
	}
	return -1;
}

// public functions

function FLE_autolist_add(mode,code,title,subtitle,group,datestamp)
// images in /images/photos/CODE.jpg
// image alt = title
// mode - 'artist', 'catalogue', 'genre'
// code - url / EVSA007 = EVSA007.html; marciorangel = marciorangel.html; jazz = _jazz.html
// date format "mm/dd/yy"
// group - grouping (e.g. 'ABC', 'DEFG',)
{
	switch (mode) {
		case 'artist': ATL_data = artist_data; break;
		case 'catalogue': ATL_data = catalogue_data; break;
		case 'genre': ATL_data = genre_data; break;
		default : return -1;
	}

	tmp = new Object();
	tmp.code = (mode=='genre') ? ('_'+code) : code;		// adjust for underscore prefix for genre links
	tmp.title = title;
	tmp.subtitle = subtitle;
	tmp.group = group;	
	
	// covert to javascript date format
	mm = Number(datestamp.substr(0,2))-1;
	dd = Number(datestamp.substr(3,2));	
	yy = Number(datestamp.substr(6,2))+2000;
	
	tmp.datestamp = new Date(yy,mm,dd);	
	
	ATL_data.push(tmp);
}

function FLE_autolist_display(mode,sortType,filter,style,urlprefix)
// mode - 'artist', 'catalogue'
// sortType - (default) 'date', 'code', 'title'
// filter - selective group / or only those listed
// style - (default) 'menu', 'sidebar', list'
// urlprefix - absolute url prefix, e.g. '/artists/', '/zhs/artists/'
{
	var ATL_data = new Array;

// 0 - by group filtering, 1 - entry filtering
	var selective =(typeof(filter)=='object') ? true : false; 
	
	urlprefix = (urlprefix==null) ? '' : urlprefix;	
	
	switch (mode) {
		case 'artist': ATL_data = artist_data; break;
		case 'genre': ATL_data = genre_data; break; 
		default : ATL_data = catalogue_data; break;
	}

	switch(sortType) {	
		case 'code': ATL_data.sort(ATL_sort_code); break;
		case 'title': ATL_data.sort(ATL_sort_title); break;
		default: ATL_data.sort(ATL_sort_newest);
	}

	if (selective) {
		n = filter.length;
	} else {
		n = ATL_data.length;
	}
	//alert('n='+n);

	// style = menu | side menu | catalogue list
	if (style=='menu') {
		// display dropdown menu
		for(i=0;i<n;i++) {
			if (selective) {
				j = getIndex(filter[i],ATL_data);
				if (j>=0) {
					a = ATL_data[j].code;
					b = ATL_data[j].title;
				} else {
					continue;
				}
			} else {
				if ( ((filter!=null) && (filter!='')) && (filter!=ATL_data[i].group) ) continue;
				a = ATL_data[i].code;
				b = ATL_data[i].title;
			}
			document.write('<a href="' + urlprefix + a + '.html"');
			document.write('onmouseover="FLE_menuOver();" title="' + b + '">' + b + '</a>');
		}
	} else if (style=='sidebar') {
		// display sidebar list
		for(i=0;i<n;i++) {
			if (selective) {
				j = getIndex(filter[i],ATL_data);
				if (j>=0) {
					a = ATL_data[j].code;
					b = ATL_data[j].title;
				} else {
					continue;
				}
			} else {
				if ( ((filter!=null) && (filter!='')) && (filter!=ATL_data[i].group) ) continue;
				a = ATL_data[i].code;
				b = ATL_data[i].title;
			}
            document.write('<a href="' + urlprefix + a + '.html">' + b + '</a>');
            document.write('<div class="rule_3"></div>');
		}			
	} else {
		// display default list
		for(i=0;i<n;i++) {
			if (selective) {
				//alert('i='+i+':'+filter[i]);
				j = getIndex(filter[i],ATL_data);
				//alert(j)
				if (j>=0) {		
					a = ATL_data[j].code;
					b = ATL_data[j].title;
					c = ATL_data[j].subtitle;
				} else {
					continue;
				}
				//alert('loopback='+i);
			} else {
				if ( ((filter!=null) && (filter!='')) && (filter!=ATL_data[i].group) ) continue;
				a = ATL_data[i].code;
				b = ATL_data[i].title;
				c = ATL_data[i].subtitle;
			}
			document.write('<div class="ATL_entry" id="ATL' + i + '">');
			document.write('  <a href="' + urlprefix + a + '.html"><h3>');
			document.write('  <img src="/images/covers/' + a + '.jpg" alt="' + a + '" class="small" />');
			document.write('  ' + b + '</h3>');
			document.write('  <h3 class="subtitle">' + c + '</h3></a>');
			document.write('</div>');
			document.write('<div class="rule_2"></div>');		
		}
	}
}

function FLE_autolist_filter(filterType,filterStr)
// filter using substring....
// filterType - (default) 'showall', 'title', 'code', 'group'
// ('title','A') - all starting in 'A'
// ('title','A','Cr') - all starting in 'A' or 'Cr'
// ('code','EVS') - all evosound (EVS) titles...
// ('group','pop-rock') - all pop-rock entries
{
	
	////////////////
	//////////////// please repair!
	
	/*
	var t_data = new Array;
	
	for(i=0;i<ATL_data.length;i++) {
		switch(filterType){
			case 'title' : tmp = ATL_data[i].title.substr(0,filterStr.length);
			case 'code' : tmp = ATL_data[i].code.substr(0,filterStr.length);
			case 'group' : tmp = ATL_data[i].group.substr(0,filterStr.length);			
			default : tmp = null;
		}
		
		if ( (tmp=='filterStr') || (tmp==null) ) 
			t_data[i] = 'block';
		else
			t_data[i] = 'none';
	}
	
	// ....
	*/
}


// =================================================================================================  
// misc

// "repair" dates! from javascript.com?
function EXT_fixDate(date)
{
  var base = new Date(0);
  var skew = base.getTime();
  if (skew > 0)
    date.setTime(date.getTime() - skew);
}