
//Title: 	Global Javascript Functions for QuebecScene.ca 
//Author:	jay.west
//Contact:	creative@jaywest.com
//Updated:	February 15, 2007 


// 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("external")) {
      links[i].onclick = function() {
        window.open(this.href);
        return false;
      }
    }
  }
}


// Sets the class of an input field to "fieldFocus" on focus or removes the className on blur.
// Will also clear the default field contents

function findField() {
  if (!document.getElementsByTagName) return false;
  var fieldList = document.getElementsByName("txtKeywords");
  for ( var i=0; i < fieldList.length; i++) {
    fieldList[i].onfocus = function() {
      this.className ="txtKeywords fieldFocus";
	  this.value = "";
    }
	fieldList[i].onblur = function() {
      this.className ="txtKeywords";
    }
  }
}


// adds history.go function to cancel buttons

function cancelButton() {
  if (!document.getElementsByName) return false;
  var cancel = document.getElementsByName("cancel");
  for (var i=0; i < cancel.length; i++) {
    if (cancel[i].name.match("cancel")) {
      cancel[i].onclick = function() {
		window.history.go(-1)
        //window.open(this.href);
        return false;
      }
    }
  }
}

// Add the OpenWYSIWYG editor to all textarea fields

function addWYSIWYGtools() {
	if (!document.getElementsByTagName) return false;
	var textareaList = document.getElementsByTagName("textarea");
	for (var i=0; i < textareaList.length; i++) {
		var strFieldName = textareaList[i].getAttribute('id');
		if (strFieldName != "comments") {  // don't add the tools to the comment form
			generate_wysiwyg(strFieldName);
		}
	}
}







// 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;
	}
}



// JUNE 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="photo.jpg" width="160" height="160" alt="caption [credit]" /></div>


// Create valid photo captions and credits from img attributes


function extractImageTitles() {
	
	images = document.getElementById("content_main").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');
	// only act on lightbox images  
	//if (strParent !='') {
	if (strParent == 'lightbox') {
	
		// 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;




// Run the functions once the page has loaded: 

window.onload=function(){
	extractImageTitles();
	doPopups();
	findField();
	cancelButton();
	addWYSIWYGtools();
}