// ArtsAlive.ca Javascript functions

// January 2006



$(document).ready(function() {
						   
	// clears the earch field contents and appends a class on focus
	$("input.txtKeywords").focus(function() {
		$(this).addClass("fieldFocus");
		$(this).val("")
	});
	$("input.txtKeywords").blur(function() {
		$(this).removeClass("fieldFocus");
		$(this).val("Search")
	});
	
});






// Opens a link in a new window when class = linkExternal

function doPopups() {
  if (!document.getElementsByTagName) return false;
  var links = document.getElementsByTagName("a");
  for (var i=0; i < links.length; i++) {
    if (links[i].className.match("linkExternal")) {
      links[i].onclick = function() {
        window.open(this.href);
        return false;
      }
    }
  }
}



// CLEAR SEARCH FIELD
function clearFieldDefault(element) {
	if (element.defaultValue==element.value) element.value = ""
	element.className='clearForm'
}	


// General purpose filter menu, used in tour admin
function selectView()
{
	if (document.fmSelectView.mnuViews[document.fmSelectView.mnuViews.selectedIndex]) {
		top.location.href=document.fmSelectView.mnuViews[document.fmSelectView.mnuViews.selectedIndex].value;
	}
}



// General purpose Media Window popup script ***************************

function openMediaWindow(mediaURL,mediaWidth,mediaHeight,mediaCaption) {
// set the default sizes if none were provided, usually for audio
	if (mediaWidth == "") {
		mediaWidth = 300;} 
	if (mediaHeight == "") {
		mediaHeight = 0;}
	var popWidth = 350, popHeight = mediaHeight + 40; 
	// if media is wider than the default 300 then expand the window 
	if (mediaWidth > 300) {
		popWidth = mediaWidth + 50;} 
	// add more vertical space if a caption is provided
	if (mediaCaption != "") {
		popHeight = (popHeight + 50);}
	//setup the rest of the window varibles	
	var features = 'fullscreen=no,toolbar=no,location=no,status=no,menubar=no,scrollbars=no,resizable=no', winAlign = 'center', ignorelink = 'ignorelink' ;
	//setup the popup window centering 
  var leftPos=0,topPos=0, ontopIntervalHandle, w = 480, h = 340;  
  if (popWidth > 0) features += (features.length > 0 ? ',' : '') + 'width=' + popWidth;
  if (popHeight > 0) features += (features.length > 0 ? ',' : '') + 'height=' + (popHeight + 110);
  if (winAlign && winAlign != "" && popWidth > 0 && popHeight > 0) {
    if (document.all || document.layers || document.getElementById) {w = screen.availWidth; h = screen.availHeight;}
		if (winAlign.indexOf("center") != -1) {topPos = (h-popHeight)/2;leftPos = (w-popWidth)/2;}
		if (winAlign.indexOf("bottom") != -1) topPos = h-popHeight; if (winAlign.indexOf("right") != -1) leftPos = w-popWidth; 
		if (winAlign.indexOf("left") != -1) leftPos = 0; if (winAlign.indexOf("top") != -1) topPos = 0; 						
    features += (features.length > 0 ? ',' : '') + 'top=' + topPos+',left='+leftPos;}
  if (window["popupWindow"] == null) window["popupWindow"] = new Array();
  var wp = popupWindow.length;
	// detect whether the media windows was evoked form the en or fr site
	if (top.location.href.match("/fr/")) {
		popupWindow[wp] = window.open('/fr/mus/mediaWindow/mediaWindow.asp?media=' + mediaURL + '&mediaWidth=' + mediaWidth + '&mediaHeight=' + mediaHeight + '&mediaCaption=' + mediaCaption,'winMedia',features);
	}
	else {
	// assume this is an english page so show the english media window
		popupWindow[wp] = window.open('/en/mus/mediaWindow/mediaWindow.asp?media=' + mediaURL + '&mediaWidth=' + mediaWidth + '&mediaHeight=' + mediaHeight + '&mediaCaption=' + mediaCaption,'winMedia',features);
	}
  //popupWindow[wp] = window.open('/en/mediaWindow.asp?media=' + mediaURL + '&mediaWidth=' + mediaWidth + '&mediaHeight=' + mediaHeight + '&mediaCaption=' + mediaCaption,'winMedia',features);
  if (popupWindow[wp].opener == null) popupWindow[wp].opener = self;  
  document.MM_returnValue = (ignorelink && ignorelink != "") ? false : true;
}




// Gloassary window popup

function flvFPW1(){
	var v1=arguments,v2=v1[2].split(","),v3=(v1.length>3)?v1[3]:false,v4=(v1.length>4)?parseInt(v1[4]):0,v5=(v1.length>5)?parseInt(v1[5]):0,v6,v7=0,v8,v9,v10,v11,v12,v13,v14,v15,v16,v17,v18;if (v4>1||v1[2].indexOf("%")>-1){v10=screen.width;for (v6=0;v6<v2.length;v6++){v18=v2[v6].split("=");if (v18[0]=="width"){v8=parseInt(v18[1]);if (v18[1].indexOf("%")>-1){v8=(v8/100)*v10;v2[v6]="width="+v8;}}if (v18[0]=="left"){v9=parseInt(v18[1]);v11=v6;}}if (v4==2){v7=(v10-v8)/2;v11=v2.length;}else if (v4==3){v7=v10-v8-v9;}v2[v11]="left="+v7;}if (v5>1||v1[2].indexOf("%")>-1){v14=screen.height;for (v6=0;v6<v2.length;v6++){v18=v2[v6].split("=");if (v18[0]=="height"){v12=parseInt(v18[1]);if (v18[1].indexOf("%")>-1){v12=(v12/100)*v14;v2[v6]="height="+v12;}}if (v18[0]=="top"){v13=parseInt(v18[1]);v15=v6;}}if (v5==2){v7=(v14-v12)/2;v15=v2.length;}else if (v5==3){v7=v14-v12-v13;}v2[v15]="top="+v7;}v16=v2.join(",");v17=window.open(v1[0],v1[1],v16);if (v3){v17.focus();}document.MM_returnValue=false;}


// LANGUAGE TOGGLE

function toggleLanguage() {
	var strOrigPath = top.location.href
	//document.write(strOrigPath)
	// this is an english page so toggle to french
	if (strOrigPath.match("/en/")) {
		strNewPath = strOrigPath.replace("/en/","/fr/")
		//top.location.href = strNewPath
	}
	else {
	// assume this is a french page so toggle to english
	strNewPath = strOrigPath.replace("/fr/","/en/")
	//top.location.href = strNewPath
	}
	//document.write(strNewPath)
	top.location.href = strNewPath
}



// show/hide page objects

function hideObj(_objId,_stateId) {
	var thisObj = document.getElementById(_objId );
	var thisState = document.getElementById(_stateId );
	thisObj.className = "hide";
	thisState.className = "none";
	}
function showObj(_objId,_stateId){
	var thisObj = document.getElementById(_objId);
	var thisState = document.getElementById(_stateId);
	if(thisObj.className != "show"){
		thisObj.className = "show";
		thisState.className = "selected";
		}
	else {
		hideObj(_objId, _stateId);
		}
	}
	
	

// JANUARY 2006

// automatically create photo captions and credits from img TITLE and CAPTION attributes
// works in Windows IE5.01, IE5.5 IE6, NN6, NN7, Firefox
// works in Mac IE 5, Safari
// also requires CSS to style captions

// will only be applied to images in the ID=contentColumn
//<div class="photoRight"><img src="../scripts/photo.jpg" width="160" height="160" alt="caption [credit]" /></div>


// Create valid photo captions and credits from img attributes


function extractImageTitles() {
	
	images = document.getElementById("contentColumn").getElementsByTagName('img');
	for (var i = 0; i < images.length; i++) {
	
	// check to see if this image has a parent with the Lightbox attribute
	var strParent = images[i].parentNode.getAttribute('rel');
	//var strParent = images[i].parentNode.getAttribute('class');
	// only act on lightbox images  
	//if (strParent !='') {
	//if (strParent == 'lightbox') {
	if (strParent == 'thickbox') {
	
		// get the photo width to apply it to the caption/credits
		var strWidth = images[i].getAttribute('width');
		
		// separate the caption from the credit within the images ALT tag
		// assumes the caption comes first followed by the credit in square brackets
		var strAltTag = images[i].getAttribute('alt'); 
		var intCreditStart = (strAltTag.indexOf("["));
			if (intCreditStart < 0) {    //if there is no credit start
				var intCreditStart = 9999;  //extend the string selection to include nothing
			}
		var intCreditEnd = (strAltTag.lastIndexOf("]"));
			if (intCreditEnd < 0) {       //if there is no caption end
				var intCreditEnd = 9999;  //extend the string selection to include everything
			}
		var strCredit = strAltTag.substring(intCreditStart+1, intCreditEnd);
		var strCaption = strAltTag.substring(0,intCreditStart);
				

		// create photo credit div
		//var strCredit = images[i].getAttribute('title');
		if ((strCredit) && (strCredit != '')) {
			var newdiv = document.createElement('div');
			newdiv.className = 'credit';
			newdiv.style.width = strWidth+"px";
 			newdiv.appendChild(document.createTextNode(strCredit));
			images[i].parentNode.appendChild(newdiv);
			//images[i].removeAttribute('credit');
			}		

		
		// create photo caption div
		//var strCaption = images[i].getAttribute('alt');
		if ((strCaption) && (strCaption != '')) {
			var newdiv = document.createElement('div');
			newdiv.className = 'caption';
			newdiv.style.width = strWidth+"px";
			newdiv.appendChild(document.createTextNode(strCaption));
			images[i].parentNode.appendChild(newdiv);
			//images[i].removeAttribute('caption');
			}
		}
	}
}
//window.onload = extractImageTitles;



function doPopups() {
  if (!document.getElementsByTagName) return false;
  var links = document.getElementsByTagName("a");
  for (var i=0; i < links.length; i++) {
    if (links[i].className.match("linkExternal")) {
      links[i].onclick = function() {
        window.open(this.href);
        return false;
      }
    }
  }
}




function extractImageTitles2() {
	
	images = document.getElementById("contentColumn").getElementsByTagName('img');
	for (var i = 0; i < images.length; i++) {
	
	// check to see if this image has a parent with the Lightbox attribute
	//var strParent = images[i].parentNode.getAttribute('rel');
	//var strParent = images[i].parentNode.getAttribute('class');
	// only act on lightbox images  
	//if (strParent !='') {
	//if (strParent == 'lightbox') {
	//if (strParent == 'caption_img') {
	//if (strParent == 'caption_img' || strParent == 'caption_img left') {
	//if (strParent == 'caption_img' || strParent == 'caption_img center') {
	//if (strParent == 'caption_img') {
	if (images[i].parentNode.className.match("caption_img")) {	

		// get the photo width to apply it to the caption/credits
		var strWidth = images[i].getAttribute('width');
		
		// separate the caption from the credit within the images ALT tag
		// assumes the caption comes first followed by the credit in square brackets
		var strAltTag = images[i].getAttribute('alt'); 
		var intCreditStart = (strAltTag.indexOf("["));
			if (intCreditStart < 0) {    //if there is no credit start
				var intCreditStart = 9999;  //extend the string selection to include nothing
			}
		var intCreditEnd = (strAltTag.lastIndexOf("]"));
			if (intCreditEnd < 0) {       //if there is no caption end
				var intCreditEnd = 9999;  //extend the string selection to include everything
			}
		var strCredit = strAltTag.substring(intCreditStart+1, intCreditEnd);
		var strCaption = strAltTag.substring(0,intCreditStart);
				

		// create photo credit div
		//var strCredit = images[i].getAttribute('title');
		if ((strCredit) && (strCredit != '')) {
			var newdiv = document.createElement('div');
			newdiv.className = 'credit';
			newdiv.style.width = strWidth+"px";
 			newdiv.appendChild(document.createTextNode(strCredit));
			images[i].parentNode.appendChild(newdiv);
			//images[i].removeAttribute('credit');
			}		

		
		// create photo caption div
		//var strCaption = images[i].getAttribute('alt');
		if ((strCaption) && (strCaption != '')) {
			var newdiv = document.createElement('div');
			newdiv.className = 'caption';
			newdiv.style.width = strWidth+"px";
			newdiv.appendChild(document.createTextNode(strCaption));
			images[i].parentNode.appendChild(newdiv);
			//images[i].removeAttribute('caption');
			}
		}
	}
}


window.onload=function(){
	extractImageTitles();
	extractImageTitles2();
	doPopups();
}
