/* ===========================================================================
 * Filename: javascript/functions.css
 * Author: Rebecca Skeers, rebecca@webmistress.com.au, www.webmistress.com.au except where noted
 * Date: 11 April 2007
 * Description: Contains JavaScript functions to enhance the Brand Tasmania website.
 * =========================================================================== 
 */





/* ===========================================================================
 * SCHEDULE THE BEHAVIOURS
 * =========================================================================== 
 */

attachEventListener(window, "resize", resizePage, false);
attachEventListener(window, "load", initPage, false);
attachEventListener(window, "unload", unloadPage, false);

function initPage() 
{	
	initLinks();
	tidyPage();
}

function resizePage()
{
	tidyPage();
}

function unloadPage() 
{
	
}




/* ===========================================================================
 * THE FUNCTIONS
 * =========================================================================== 
 */



function tidyPage()
{
	// Set the stylesheet for smalls screens if necessary.
	/*
	var theWidth = getBrowserWidth();
	if (theWidth <= 900)
		setActiveStyleSheet("Small screen");
	else
		unsetActiveStyleSheet("Small screen");
	*/

	// Make the page height at least as big as the browser to display the background image to the bottom of the browser.
	
	var b = document.getElementsByTagName("body");
 	var theBody = b[0];
	
	if(document.getElementById("column1"))
	{
		if(theBody.className.match("homepage"))
		{
			var col1 = document.getElementById("column1");
			var col2 = document.getElementById("column2");
			var col3 = document.getElementById("column3");
			
			var height1 = col1 ? col1.offsetHeight : 0;
			var height2 = col2 ? col2.offsetHeight : 0;
			var height3 = col3 ? col3.offsetHeight : 0;
		
			var divs1 = col1.getElementsByTagName("div");
			var divs2 = col2.getElementsByTagName("div");
			var divs3 = col3.getElementsByTagName("div");
			
			var inner1 = divs1[0];
			var inner2 = divs2[0];
			var inner3 = divs3[0];
			
			if(height1 > height2 && height1 > height3)
			{
				inner1.style.height=(height1-30)+'px';
				inner2.style.height=(height1-30)+'px';
				inner3.style.height=(height1-30)+'px';
			}
			else if(height2 > height1 && height2 > height3)
			{
				inner1.style.height=(height2-30)+'px';
				inner2.style.height=(height2-30)+'px';
				inner3.style.height=(height2-30)+'px';
			}
			else if(height3 > height1 && height3 > height2)
			{
				inner1.style.height=(height3-30)+'px';
				inner2.style.height=(height3-30)+'px';
				inner3.style.height=(height3-30)+'px';
			}
		}
		else if(theBody.className.match("widepage"))
		{
			
		}
		else
		{
			var col1 = document.getElementById("column1");
			var col2 = document.getElementById("column2");
			
			var height1 = col1 ? col1.offsetHeight : 0;
			var height2 = col2 ? col2.offsetHeight : 0;
		
			var divs1 = col1.getElementsByTagName("div");
			var divs2 = col2.getElementsByTagName("div");
			
			var inner1 = divs1[0];
			var inner2 = divs2[0];
			
			if(height1 > height2)
			{
				inner1.style.height=(height1-30)+'px';
				inner2.style.height=(height1-30)+'px';
			}
			else if(height2 > height1)
			{
				inner1.style.height=(height2-30)+'px';
				inner2.style.height=(height2-30)+'px';
			}
			
		}
		
	}
	
	
	
	
	
	
	var divs = document.getElementsByTagName("div");
	for (var i = 0; i < divs.length; i++)
	{
		
		// Fix up the image caption widths
		if(divs[i].className.match("image-"))
		{
			var imagelist = divs[i].getElementsByTagName("img");
			var captionlistp = divs[i].getElementsByTagName("p");
			var captionlistdiv = divs[i].getElementsByTagName("p");
			
			if(imagelist.length>0)
			{
				if(imagelist[0].className.match("border"))
					divs[i].style.width = (imagelist[0].width+2)+"px";
				else
					divs[i].style.width = (imagelist[0].width)+"px";
				/*	
				for (var j = 0; j < captionlistp.length; j++)
				{
					if(captionlistp[j].className.match("caption"))
					{
						captionlistp[j].style.width = (imagelist[0].width)+"px";
					}
				}
				
				for (var k = 0; k < captionlistdiv.length; k++)
				{
					if(captionlistdiv[k].className.match("caption"))
					{
						captionlistdiv[k].style.width = (imagelist[0].width)+"px";
					}
				}
				*/
			}
		}
	}
}



function initLinks()
{
	if (!document.getElementsByTagName) 
 		return;
 		
 	var b = document.getElementsByTagName("body");
 	theBody = b[0];
 	if(theBody.className.match("popup"))
 	{
	 	is_popup=true;
 		 window.focus();
	}
 
 	var anchors = document.getElementsByTagName("a");
 	for (var i=0; i<anchors.length; i++) 
 	{
   		var anchor = anchors[i];
   		if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external")
   		{
     		anchor.target = "_blank";
 		}	
     	else if (anchor.className.match("print")) 
     	{
        	anchor.onclick = function() 
        	{
          		printPage();
          		return false;
        	};
    	}
    	else if (anchor.className.match("window")) 
     	{
        	anchor.onclick = function() 
        	{
          		popUp(this.getAttribute("href"));
          		return false;
        	};
    	}
    	else if (anchor.className.match("file")) 
     	{
        	anchor.onclick = function() 
        	{
          		window.open(this.getAttribute("href"));
          		return false;
        	};
    	}
    	else if (anchor.className.match("close")) 
     	{
        	anchor.onclick = function() 
        	{
          		window.close();
          		return false;
        	};
    	}
 	}
}





function popUp(URL)
{
	eval("window.open('" + URL + "','windowName', 'toolbar=0,scrollbars=1,location=0,statusbar=1,menubar=0,resizable=1,width=972,height=768');");
}


function printPage() 
{
  	if (window.print)
		window.print()
	else
		alert("Sorry, your browser doesn't support the print feature. Use the File menu on your browser to select Print.");
	return false;
}


function setClass(el, str)
{
	if(el)
	{
		if (el.className == "")
			el.className = str;
		else
			el.className += " "+str;	
	}	
	return true;
}


function removeClass(el, str)
{
	if(el)
	{
		el.className=el.className.replace(new RegExp(" "+str+"\\b"), "");
		el.className=el.className.replace(new RegExp(str+"\\b"), "");	
	}
}


function getChildrenByTagName(target, tagName)
{
	var children = target.childNodes;
	var matching = new Array();
	
	if (children != null)
	{
		for (var i = 0; i < children.length; i++)
		{
			if (children[i].nodeName.toLowerCase() == tagName)
			{
				matching[matching.length] = children[i];
			}
		}
	}
	
	return matching;
};






/* The JavaScript Anthology - James Edwards & Cameron Adams */
function attachEventListener(target, eventType, functionRef, capture)
{
	if (typeof target.addEventListener != "undefined")
	{ 
		target.addEventListener(eventType, functionRef, capture);
	}
	else if (typeof target.attachEvent != "undefined")
	{
		var functionString = eventType + functionRef;
		target["e" + functionString] = functionRef;
		target[functionString] = function(event)
		{
			if(typeof event == "undefined")
			{
				event = window.event
			};

			target["e" + functionString](event);
        };
		target.attachEvent("on" + eventType, target[functionString]);
	}
	else
	{
		eventType = "on" + eventType;

		if (typeof target[eventType] == "function")
		{
			var oldListener = target[eventType];
			target[eventType] = function()
			{
				oldListener();
				return functionRef();
			}
		}
		else
		{
			target[eventType] = functionRef;
		}
	}

	return true;
};



