/********************************************************
Name: 			javascript stylesheet
Description: 	loads javascript specific stylesheet
********************************************************/

document.write('<link rel="StyleSheet" href="css/coutureJavascript.css" type="text/css" media="screen" />');


/* ID to show. Sets default shown item */	
var showShItem = "sh01";	

function fnConfig() {
	fnAnchorInit();
	fnHeroInit();
	fnRunHeroShowHide();
}


/* 	What is this? - Someone comment if they know what function it has

	this is a function to aid debugging. It creates a string which lists all the properties of an object and their values,
	(gP stands for getProperties). e.g. alert(gP(window));
	- KM
*/
function gP(o) {
	var s = "[";
	for (var p in o) {
		s += p + "->" + o[p] + ",<br/>\n";
	}
	s += "]";
	return s;
}

/* Set up user agent string for browser eval */
var ua = navigator.userAgent.toLowerCase();


/******************************************************************************************
set bolean to detect safari on mac - the flash text does not seem to work with 
the opacity background on any gecko browsers on mac
******************************************************************************************/
mac = (ua.indexOf("windows") == -1)? true:false; 
safari = (ua.indexOf("safari") != -1)? true:false; 

/******************************************************************************************
set bolean to detect IE7 - the text sizing sniffer has problems with IE7 zoom.
******************************************************************************************/
ie7 = (ua.indexOf("msie 7") != -1)? true:false;

/******************************************************************************************
set bolean to detect Operas 
******************************************************************************************/
op7 = (ua.indexOf("opera/7") != -1)? true:false;
op9 = (ua.indexOf("opera/9") != -1)? true:false;


/******************************************************************************************
set bolean to detect IE5 Mac
******************************************************************************************/
ieMac = ((ua.indexOf("msie") != -1)&&(ua.indexOf("windows") == -1))? true:false;

/******************************************************************************************
set bolean to detect various ie browsers for use with fnMoveFocus()
******************************************************************************************/
iepc = (((ua.indexOf("msie 7") != -1)||(ua.indexOf("msie 6") != -1)||(ua.indexOf("msie 5.5") != -1))&&(ua.indexOf("windows") != -1)&&(ua.indexOf("opera") == -1))? true:false;


/******************************************************************************************
set bolean to detect Netscape - small:large class resize 
******************************************************************************************/
ns6 = (ua.indexOf("netscape6") != -1) || (ua.indexOf("netscape5") != -1) || (ua.indexOf("netscape4") != -1)? true:false;
ns7 = (ua.indexOf("netscape/7") != -1)? true:false;
ns8 = (ua.indexOf("netscape/8") != -1)? true:false;


function pop(a, w, h) {
	if (a && a.href) {
		window.open(a.href, 'pop', 'width='+w+',height='+h+',toolbars=no');
		return false;
	}
}
function popScroll(a, w, h) {
	if (a && a.href) {
		window.open(a.href, 'pop', 'width='+w+',height='+h+',toolbars=no,scrollbars=yes');
		return false;
	}
}
function popNew(a) {
	if (a && a.href) {
		window.open(a.href, 'pop');
		return false;
	}
}


/**********************************************************************************

Force Focus to move past iFrame
onfocus() event has minor browser compatability issues. Not used for Mozilla Browser.
Selects the moveTo ID and jumps to the first link within 
@param: moveTo - must be an ID within the page

************************************************************************************/

function fnMoveFocus(moveToID) {
	if(iepc) {
		document.getElementById(moveToID).getElementsByTagName("A")[0].focus();
	}
}



/********************************************************************************
 
Name:			fnAnchorShowHide
Description:	Show / Hide the tabbed content.  Non-Js fall back to link to page anchors. 
				Anchor contains link to ID of element to show/hide.  JS takes the 
				url, splits string to get ID after the "#", and uses this ID to target
				element to show/hide.  

*********************************************************************************/	

/* Called when link is clicked.  Gets ID from selected link */	
function fnAnchorShowHide(targ) {
	showShItem = targ.href.split("#")[1];	
	fnRunShowHide();
	fnHideInfo('heroImgWrap');
}	

/* Runs on load.  If anchor link is in URL, it hides all other IDs.  If no ID in URL, it hides all items except the default */	
function fnAnchorInit() {
	if(document.getElementById("showHideWrapper")) {	
		var stUrl = document.location.toString();	

		if(stUrl.match("#")) {
			showShItem = stUrl.split("#")[1];
		}
		fnRunShowHide();
	}
}


/* Hides all items, then shows whichever ID the variable "showShItem" refers to */
function fnRunShowHide() {
	var shItems = document.getElementById("showHideWrapper").getElementsByTagName("DIV");
	for (i=0;i<shItems.length;i++) { /* Loop through to hide all items */
		if(shItems[i].className == "anc" || shItems[i].className == "anc selected") {
			fnHideItem(shItems[i].parentNode);
		}
		
	}	
	var e = document.getElementById(showShItem);
	if (e && e.parentNode) {
		fnShowItem(e.parentNode);
		e.className = "anc selected";
		fnSelectNav(showShItem);
	}
}


/* Sets navigation select state dependent on whichever ID the variable "showShItem" refers to */
function fnSelectNav(navToSelect) {
	var navLinks = document.getElementById("shNav").getElementsByTagName("A");	
	for (i=0;i<navLinks.length;i++) {
		navLinks[i].parentNode.className = "";		
		if(navLinks[i].href.split("#")[1] == showShItem) {
			navLinks[i].parentNode.className = "selected";
		}
	}	
}


/* Places item on screen */
function fnShowItem(targ) {
	if (typeof(targ) == "string") {
		targ = document.getElementById(targ);
	}
	targ.style.position = "static";	
	targ.style.top = "0";	
	targ.style.left = "0";	
	targ.style.display = "block";
}

/* Moves item off of the screen */	
function fnHideItem(targ) {
	targ.style.position = "absolute";	
	targ.style.top = "126px";
	targ.style.left = "-3000px";
	targ.style.display = "block";
	}
	
	
	
/********************************************************************************
 
Name:			fnHeroShowHide
Description:	Show / Hide the hero image and it's content.  Non-Js fall back to 
				link to page anchors. 
				Anchor contains link to ID of element to show/hide.  JS takes the 
				url, splits string to get ID after the "#", and uses this ID 
				to target Element to show/hide.  

*********************************************************************************/	

/* ID to show. Sets default shown item */	
var heroShItem = "infoWrap01";	


/* Runs on load.  If anchor link is in URL, it hides all other IDs.  If no ID in URL, it hides all items except the default */	
function fnHeroInit() {
	if(document.getElementById("showHideWrapper")) {	
		var heroStUrl = document.location.toString();	

		if(heroStUrl.match("#")) {
			heroShItem = "infoWrap"+heroStUrl.split("#sh")[1];
		}		
		fnRunShowHide();
	}

	// find all the hero images and add a onclick handler to them
	var e = document.getElementById("heroImgWrap");
	if (e) {
		var imgs = []
		imgs = e.getElementsByTagName("img");
		for (var i in imgs) {
			if (imgs[i] && imgs[i].className == "hero") {
				imgs[i].onclick = function() {
					fnHideInfo('heroImgWrap');
				}
			}
		}
	}
}


/* Called when link is clicked.  Gets ID from selected link */	
function fnHeroShowHide(targ) {
	heroShItem = "infoWrap"+targ.href.split("#sh")[1];	
	fnRunHeroShowHide();
}	

/* Hides all items, then shows whichever ID the variable "heroShItem" refers to */
function fnRunHeroShowHide() {
	var heroItems = [];
	var e = document.getElementById("heroImgWrap");
	if (e) {
		heroItems = e.getElementsByTagName("DIV");
		for (i=0;i<heroItems.length;i++) { /* Loop through to hide all items */
			if(heroItems[i].className == "heroSHItem") { /* only hide show hide items - class="heroSHItem" */
				fnHideHero(heroItems[i]);
			}
			
		}	
	}
	if (heroShItem && heroShItem != "") {
		e = document.getElementById(heroShItem);
		if (e && e.parentNode) {
			fnShowHero(e.parentNode);		
			fnSelectNav(heroShItem);
		}
	}
}


/* Places item on screen */
function fnShowHero(targ) {
	targ.style.position = "absolute";	
	targ.style.top = "0";	
	targ.style.left = "0";	
	targ.style.display = "block";
	}

/* Moves item off of the screen */	
function fnHideHero(targ) {
	targ.style.position = "absolute";	
	targ.style.top = "126px";
	targ.style.left = "-3000px";
	targ.style.display = "block";
	}
	
	
	
	
/***********************************************************************************

Name:			toggle show hide information box
Description:	onclick will toggle the classname of id from show/hide

@param targId:	must be an ID within the page

Notes:			Set up .show / .hide css styles in content and javascript stylesheets

************************************************************************************/

/* set up global variable */
var toggleSH = "";

function fnToggle(targId) {
	/* check id exists */
	if(targId){		
		/* check state of toggle - if not showing, show it */
		if (toggleSH != "show") {
			fnShowInfo(targId);
			toggleSH = "show"; //set toggle
		}		
		/* or if showing, hide it */
		else if (toggleSH == "show") {
			fnHideInfo(targId);
			toggleSH = "hidden"; //set toggle
		}
	}
}	


/***********************************************************************************

Name:			show hide information box
Description:	onfocus(), onMouseOver() event - fnShowItem()
				onBlur(), onMouseOut() event - fnHideItem()

@param targ:	must be an ID within the page

************************************************************************************/
/* Places item on screen */
function fnShowInfo(targ) {
	toggleSH = "show"; //set toggle
	var e = document.getElementById(targ);
	if (e) {
		e.className = "show";
	}
}

/* Moves item off of the screen */	
function fnHideInfo(targ) {
	toggleSH = "hidden"; //set toggle
	var e = document.getElementById(targ);
	if (e) {
		e.className = "hidden";
	}
}


/* competition functions */
function initCompetition() {
	var e = document.getElementById("terms");
	var l = document.getElementById("termsLink");
	if (e && l) {
		fnHideItem(e);
		l.onclick = function() { fnShowItem("terms"); }
	}
}


/* e-postcards functions */
var _selectedPcardLabel = null;

function initPostcards() {
	var s = document.getElementById("pcardSelector");
	if (s) {
		var pcards = s.getElementsByTagName("td");
		if (pcards && pcards.length > 0) {

			for (var p=0; p<pcards.length; p++) {
				var l = pcards[p].getElementsByTagName("label");
				if (l && l.length > 0) {
					l = l[0];
				}
				else {
					l = null;
				}
				var i = pcards[p].getElementsByTagName("input");
				if (i && i.length > 0) {
					i = i[0];
				}
				else {
					i = null;
				}

				if (l && i) {	
					if (p == 0) {
						_selectedPcardLabel = l;
					}
					l._input = i;
					l.onclick = function() {
						this._input.checked = true; 
						this.className = "selected";
						if (_selectedPcardLabel) {
							_selectedPcardLabel.className = "";
						}
						_selectedPcardLabel = this;
					}

					i._label = l;
					//fnHideItem(i);
				}
			}
		}
	}
}

function getElementsByTagAndClassName(e, tag, c) {
	var ret = new Array();
	if (e) {
		var _es = e.getElementsByTagName(tag);
		if (_es && _es.length > 0) {	
			for (var i=0; i<_es.length; i++) {
				if (_es[i].className == c || _es[i].className.indexOf(c + " ") != -1) {
					ret.push(_es[i]);
				}
			}
		}
	}
	return ret;
}

function getFirstElementByTagAndClassName(e, tag, c) {
	var es = getElementsByTagAndClassName(e, tag, c);
	if (es && es.length > 0) {
		return es[0];
	}
	return null;
}

/* exhibition highlights */
function ExhibHighManager() {
	this._highs = new Array();
	this._openedi = -1;

	this.add = function(inHigh) {
		if (inHigh) {
			this._highs.push(inHigh);
			if (inHigh.className.indexOf("opened") == -1) {
				fnHideItem(inHigh.subBody);
			}
			else {
				this._openedi = (this._highs.length - 1);
			}
		}
	}

	this.open = function(inHigh) {
		for (var h=0; h<this._highs.length; h++) {
			if (this._highs[h] == inHigh) {
				// close opened one if any
				if (this._openedi >= 0 && this._openedi < this._highs.length) {
					fnHideItem(this._highs[this._openedi].subBody);
				}

				// open this one
				fnShowItem(this._highs[h].subBody);
				this._openedi = h;
			}
		}
	}
}
var _exhibHighManager = new ExhibHighManager();

function initExhibHigh() {
	var e = document.getElementById("exhibHighWrapper");
	if (e) {
		var subs = getElementsByTagAndClassName(e, "div", "highlightSub");
		if (subs && subs.length > 0) {
			for (var i=0; i<subs.length; i++) {
				var subBody = getFirstElementByTagAndClassName(subs[i], "div", "highlightSubBody");
				var dl = getFirstElementByTagAndClassName(subs[i], "a", "detailLink");
				if (dl && subBody) {
					dl.parentHigh = subs[i];
					dl.subBody = subBody;
					dl.onclick = function() {
						_exhibHighManager.open(this.parentHigh);
					};
					subs[i].subBody = subBody;

					_exhibHighManager.add(subs[i]);
				}
			}
		}
	}
}

