/****************************************************/
/*	Luke Cuthbertson                                 */
/*	Haksi.co.uk                                      */
/*	21/04/08                                         */
/*                                                  */
/*	This code remains the property of Haksi          */
/*	This file and any code contained within it,      */
/*	may NOT be reused, redistributed, resold         */
/*	or copied for any other puropse than backup      */
/*	by the client. A backup copy must not be         */
/* accessible from the Internet/WAN. You may        */
/* alter the contents of this file at your own      */
/* risk. This notice must not be removed.           */
/*                                                  */
/****************************************************/

var isShift = false;

window.onload = function(){

   eolas();
	
   if(document.getElementById('gallery') != null){//if this is gallery page		
      applyNewWindowLinks();
      document.onkeydown = keyPress;
   }
}

function openInNewWindow() { 
   if(!isShift){
      var newWindow = window.open(this.getAttribute('href'), 'flickrWindow', "status=1, tootbar=1, location=1, menubar=1, directories=1, resizable=1, scrollbars=1, height=625, width=825"); 
      newWindow.focus();
   }
   else
      window.location = this.getAttribute('href');
   
   return false; 
} 

function applyNewWindowLinks(){
   var links = getElementsByClassName(document.getElementById('gallery'), 'a', 'new_window');
   for(var i = 0; i<links.length; i++){
      links[i].onclick = openInNewWindow;
   }
}

document.onkeyup=function(e){
	if(window.event && event.keyCode == 65)
      isShift=false;
   if(e && e.which == 65)
      isShift=false;
}
function keyPress(e){
	if(window.event && event.keyCode == 65)
      isShift=true;
   if(e && e.which == 65)
      isShift=true;
	return false;
}

function eolas(){
	/*eolas workaround*/
	var objects = document.getElementsByTagName("object");
	for (var i=0; i<objects.length; i++)
		objects[i].outerHTML = objects[i].outerHTML;
}			

/*************************helpers***********************/

//returns the target element of the event e	
function getTarget(e){
	if (e.target) targ = e.target;
	else if (e.srcElement) targ = e.srcElement;
	if (targ.nodeType == 3) // defeat Safari bug
		targ = targ.parentNode;
	return targ;		
}

//fetch the elements first 'true' sibling. 
function getNextSibling(elem){
	var sibling = elem.nextSibling;
	//strip out white space text nodes from Mozilla DOM
	while(sibling && sibling.nodeType != 1){
		sibling = sibling.nextSibling;
	};
	return sibling;
}

//fetch the elements first 'true' sibling. 
function getPreviousSibling(elem){
	var sibling = elem.previousSibling;
	//strip out white space text nodes from Mozilla DOM
	while(sibling && sibling.nodeType != 1){
		sibling = sibling.previousSibling;
	};
	return sibling;
}

function getElementsByClassName(oElm, strTagName, strClassName){

	/*Function Written by Jonathan Snook, http://www.snook.ca/jonathan
	Add-ons by Robert Nyman, http://www.robertnyman.com*/
	
	var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);
	var arrReturnElements = new Array();
	strClassName = strClassName.replace(/\-/g, "\\-");
	var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
	var oElement;
	for(var i=0; i<arrElements.length; i++){
		oElement = arrElements[i];
		if(oRegExp.test(oElement.className)){
			arrReturnElements.push(oElement);
		};
	};
	return (arrReturnElements);
}
