// http://www.thefutureoftheweb.com/blog/adddomloadevent
addDOMLoadEvent=(function(){var e=[],t,s,n,i,o,d=document,w=window,r='readyState',c='onreadystatechange',x=function(){n=1;clearInterval(t);while(i=e.shift())i();if(s)s[c]=''};return function(f){if(n)return f();if(!e[0]){d.addEventListener&&d.addEventListener("DOMContentLoaded",x,false);/*@cc_on@*//*@if(@_win32)d.write("<script id=__ie_onload defer src=//0><\/scr"+"ipt>");s=d.getElementById("__ie_onload");s[c]=function(){s[r]=="complete"&&x()};/*@end@*/if(/WebKit/i.test(navigator.userAgent))t=setInterval(function(){/loaded|complete/.test(d[r])&&x()},10);o=w.onload;w.onload=function(){x();o&&o()}}e.push(f)}})();


addDOMLoadEvent(init);

function init()
{
	autoShadow( Array('w174','w245','w200','w130','w460'), Array('Photoholder','Thumbs')  );
	autoRoundCorners('SidebarBox');
	prepareLinks();
	externalLinks();
}





	// Open external links in new window
	// http://cssglobe.com/post/1281/open-external-links-in-new-window-automatically
	// http://www.sitepoint.com/print/standards-compliant-world/
	// http://www.w3.org/TR/WCAG20-TECHS/SCR24.html
	this.externalLinks = function(){
		var hostname = window.location.hostname;
		hostname = hostname.replace("www.","").toLowerCase();
		//hostname = "skibb-konk.dev";
		//hostname = "skibbild.dk";
		var a = document.getElementsByTagName("a");	
		this.check = function(obj){
			var href = obj.href.toLowerCase();
			// SPECIAL SAUCE STARTS
			if (href.indexOf("/konkurrence/")!=-1) { return true; }
			if (document.getElementsByTagName('html')[0].id == 'konkurrence') {
				if (obj.innerHTML=="www.skibbild.dk") { return true; }
				if (obj.innerHTML=="i et andet vindue") { return true; }
				if (obj.innerHTML=="klikke her") { return true; }
			}
			// SPECIAL SAUCE ENDS
			//return (href.indexOf("http://")!=-1 && href.indexOf(hostname)==-1) ? true : false;
			return false;
		};
		this.set = function(obj){
			//obj.target = "_blank";
			obj.rel = "external";
			obj.className = "ExternalLink";
			
			obj.onclick = function(event){return launchWindow(this, event);}
			// UAAG requires that user agents handle events in a device-independent manner
			// but only some browsers do this, so add keyboard event to be sure
			obj.onkeypress = function(event){return launchWindow(this, event);}			
		};	
		for (var i=0;i<a.length;i++){
			if(check(a[i])) set(a[i]);
		};		
	}

	function launchWindow(objAnchor, objEvent)
	{
	  var iKeyCode, bSuccess=false;
	
	  // If the event is from a keyboard, we only want to open the
	  // new window if the user requested the link (return or space)
	  if (objEvent && objEvent.type == 'keypress')
	  {
		if (objEvent.keyCode)
		  iKeyCode = objEvent.keyCode;
		else if (objEvent.which)
		  iKeyCode = objEvent.which;
	
		// If not carriage return or space, return true so that the user agent
		// continues to process the action
		if (iKeyCode != 13 && iKeyCode != 32)
		  return true;
	  }
	
	  bSuccess = window.open(objAnchor.href);
	
	  // If the window did not open, allow the browser to continue the default
	  // action of opening in the same window
	  if (!bSuccess)
		return true;
	
	  // The window was opened, so stop the browser processing further
	  return false;
	}












// ----------------------------------------------------------------------
// All thumbnail links should be placed in a div with the id "Thumbs"
// The targetphoto should be enclosed in a div with the id "Photoholder"
// ----------------------------------------------------------------------

function prepareLinks()
{
	if (!document.getElementById) return false;
	
	if (document.getElementById('Thumbs')) {
		thumbLinks = document.getElementById('Thumbs').getElementsByTagName('a');
		for (i=0; i<thumbLinks.length; i++) {
			thumbLinks[i].onclick = function() { changeImage(this); return false; }
			if (i<1) classTool('add',thumbLinks[i],'On');
	  	}
  	}
	if (document.getElementById('Photoholder')) {
  		document.getElementById('Photoholder').getElementsByTagName('img')[0].setAttribute('id','BigPhoto');
  		document.getElementById('Photoholder').getElementsByTagName('p')[0].setAttribute('id','PhotoText');
  		// alert(document.getElementById('PhotoText').innerHTML);
  		if (!document.getElementById('PhotoText').innerHTML) {
  			setInnerHTML(document.getElementById('PhotoText'), '<span style="visibility: hidden;">-</span>');
  		}
  	}
}

function changeImage(l)
{
	url = l.href;
	//alert("* " + url + " *");
	document.getElementById('BigPhoto').src = url;
	
	if (l.getElementsByTagName('span')[0]) {
		setInnerHTML(document.getElementById('PhotoText'), l.getElementsByTagName('span')[0].innerHTML);
	} else {
		setInnerHTML(document.getElementById('PhotoText'), '<span style="visibility: hidden;">-</span>');
	}
	clearClasses();
	classTool('add',l,'On');
}

function clearClasses()
{
	thumbLinks = document.getElementById('Thumbs').getElementsByTagName('a');
	for (i=0; i<thumbLinks.length; i++) {
		classTool('remove',thumbLinks[i],'On');
	}	
}

function setInnerHTML(target, content) {
	if (!SI) { var SI = new Object(); };
	if (SI.IE && (target.nodeName == 'TBODY' || target.nodeName == 'TABLE'))
	{
		SI.IE.fixInnerHTML(target, content);
	} else {
		target.innerHTML = content;
	}
};


function autoShadow(shadowClasses, shadowIds)
{
	if (!document.getElementById) return false;

	LTEIE5_5 = false;
	if (document.getElementById('IE') != null && classTool('check',document.getElementById('IE'),'lte5_5')) { LTEIE5_5 = true; }	
	if (LTEIE5_5 == true) return false;

	for (j=0; j<shadowIds.length; j++) {
		//alert(shadowIds[j]);
		if (document.getElementById(shadowIds[j])) {
			imagesInside = document.getElementById(shadowIds[j]).getElementsByTagName('img');
			for (i=0; i<imagesInside.length; i++) {
				addShadow( imagesInside[i] );
			}
		}
	}

	divList = document.getElementById('Wrapper').getElementsByTagName('div');

	var imageDivList = new Array();
	for (j=0; j<divList.length; j++) {
		for (i=0; i<shadowClasses.length; i++) {
			if ( classTool('check',divList[j],shadowClasses[i]) ) { imageDivList.push(divList[j]); }
		}
	}
		
	for (j=0; j<imageDivList.length; j++) {
		classTool('add',imageDivList[j],'NoBorder');
		addShadow( imageDivList[j].getElementsByTagName('img')[0] );
	}
}



function autoRoundCorners(tagClass, container)
{
	if (container == undefined) container = document.body;
	elements = container.getElementsByTagName('div');
	 for (j=0; j<elements.length; j++) {
        if (classTool('check',elements[j],tagClass)) {
        			newDiv = document.createElement('div');
        			newDiv.className = "CornerTL";
        			elements[j].appendChild(newDiv);
        			newDiv = document.createElement('div');
        			newDiv.className = "CornerTR";
        			elements[j].appendChild(newDiv);        			
        			newDiv = document.createElement('div');
        			newDiv.className = "CornerBL";
        			elements[j].appendChild(newDiv);
        			newDiv = document.createElement('div');
        			newDiv.className = "CornerBR";
        			elements[j].appendChild(newDiv);
        			}
      }
}




function addShadow(element)
{
	// if the image is a link, the parent a-tag should be used instead
	elementParent = element.parentNode;
	if (elementParent.nodeName.toLowerCase() == 'a') element = elementParent;

	// create the shadow div-wrapper and divs for all corners
	containerDiv = document.createElement('div');
	containerDiv.className = 'shadow';
	element.parentNode.insertBefore(containerDiv,element);
	
	newDiv = document.createElement('div');
	newDiv.className = 'topleft';
	containerDiv.appendChild(newDiv);
	
	newDiv = document.createElement('div');
	newDiv.className = 'topright';
	containerDiv.appendChild(newDiv);	
	
	newDiv = document.createElement('div');
	newDiv.className = 'bottomleft';
	containerDiv.appendChild(newDiv);	
	
	newDiv = document.createElement('div');
	newDiv.className = 'bottomright';
	containerDiv.appendChild(newDiv);
	
	// move the image inside the shadow div-wrapper
	containerDiv.appendChild(element);
		
}

/*
<div class="shadow">
  <img src="image.jpg">
  <div class="topleft"></div>
  <div class="topright"></div>
  <div class="bottomleft"></div>
  <div class="bottomright"></div>
</div>
*/


function classTool(a,o,c1,c2)
// This function takes four parameters:
// a = defines the action you want to perform
// o = the object in question
// c1 = the name of the first class
// c2 = the name of the second class
//
// Possible actions are:
// swap		= replaces class c1 with class c2 in object o
// add		= adds class c1 to the object o
// remove	= removes class c1 from the object o
// check	= tests if class c1 is already applied to object o and return true or false
{
	switch (a)
	{
		case 'swap':
			o.className=!classTool('check',o,c1)?o.className.replace(c2,c1):
			o.className.replace(c1,c2);
		break;
		case 'add':
			if(!classTool('check',o,c1)) {o.className+=o.className?' '+c1:c1;}
		break;
		case 'remove':
			var rep=o.className.match(' '+c1)?' '+c1:c1;
			o.className=o.className.replace(rep,'');
		break;
		case 'check':
			return new RegExp('\\b'+c1+'\\b').test(o.className);
		break;
	}
}


function isArray(obj) {
	return !(
		!obj || 
		(!obj.length || obj.length == 0) || 
		typeof obj !== 'object' || 
		!obj.constructor || 
		obj.nodeType || 
		obj.item 
	);
}