// Event listeners

function addLoadListener(fn)
{
	if (typeof window.addEventListener != 'undefined')
	{
		window.addEventListener('load', fn, false);
	}
	else if (typeof document.addEventListener != 'undefined')
	{
		document.addEventListener('load', fn, false);
	}
	else if (typeof window.attachEvent != 'undefined')
	{
		window.attachEvent('onload', fn);
	}
	else
	{
		return false;
	}
	
	return true;
};


function toggleLayer( whichLayer )
{
  var elem, vis;
  if( document.getElementById ) // this is the way the standards work
    elem = document.getElementById( whichLayer );
  else if( document.all ) // this is the way old msie versions work
      elem = document.all[whichLayer];
  else if( document.layers ) // this is the way nn4 works
    elem = document.layers[whichLayer];
  vis = elem.style;
  // if the style.display value is blank we try to figure it out here
  if(vis.display==''&&elem.offsetWidth!=undefined&&elem.offsetHeight!=undefined)
    vis.display = (elem.offsetWidth!=0&&elem.offsetHeight!=0)?'block':'none';
  vis.display = (vis.display==''||vis.display=='block')?'none':'block';
}

function attachEventListener(target, eventType, functionRef, capture)
{
    if (typeof target.addEventListener != "undefined")
    {
        target.addEventListener(eventType, functionRef, capture);
    }
    else if (typeof target.attachEvent != "undefined")
    {
        target.attachEvent("on" + eventType, functionRef);
    }
    else
    {
        return false;
    }

    return true;
};


// ===


// Resolution
function checkBrowserWidth()
{
	var theWidth = getBrowserWidth();
	
	if (theWidth == 0)
	{
		var resolutionCookie = document.cookie.match(/(^|;)tmib_res_layout[^;]*(;|$)/);

		if (resolutionCookie != null)
		{
			setStylesheet(unescape(resolutionCookie[0].split("=")[1]));
		}
		
		addLoadListener(checkBrowserWidth);
		
		return false;
	}

	 if (theWidth > 1000)
	{
		setStylesheet("1024 x 768");
		document.cookie = "tmib_res_layout=" + escape("1024 x 768");
	}
	else
	{
		setStylesheet("");
		document.cookie = "tmib_res_layout=";
	}
	
	return true;
};




function getBrowserWidth()
{
	if (window.innerWidth)
	{
		return window.innerWidth;
	}
	else if (document.documentElement && document.documentElement.clientWidth != 0)
	{
		return document.documentElement.clientWidth;
	}
	else if (document.body)
	{
		return document.body.clientWidth;
	}
	
	return 0;
};




function setStylesheet(styleTitle)
{
	var currTag;

	if (document.getElementsByTagName)
	{
		for (var i = 0; (currTag = document.getElementsByTagName("link")[i]); i++)
		{
			if (currTag.getAttribute("rel").indexOf("style") != -1 && currTag.getAttribute("title"))
			{
				currTag.disabled = true;

				if(currTag.getAttribute("title") == styleTitle)
				{
					currTag.disabled = false;
				}
			}
		}
	}
	
	return true;
};

// ====


// Extras



function clearField(){
	if (typeof this.touched == 'undefined'){
		this.value = '';
		this.touched = true;
	}
}



// Insert rules into stylesheet - supply either a single rule as a string or an array of strings
function insertStyles(styles){
	// If array supplied, convert to string
	if (typeof styles == 'object')
		{ styles = styles.join(' '); }
		
	var head = document.getElementsByTagName('head')[0];
	var style = document.createElement('style');
	style.type = 'text/css';
	head.appendChild(style);
	
	if (style.styleSheet){ // IE
		style.styleSheet.cssText = styles;
	}
	
	else {// w3c
		var cssText = document.createTextNode(styles);
		style.appendChild(cssText);
	}
}



function dependentImageLoad(dependent, subject){
	var dependent = $(dependent);
	var subject = $(subject);
	if (!dependent || !subject){
		return false;
	}
	var fromBackground = (arguments.length > 2) ? arguments[2] : false;
	
	var subjectSrc = (fromBackground) ? Element.getStyle(subject, 'background-image').replace(/^url\(['"]?([^'"]*)['"]?\)$/, '$1') : subject.src;
	
	if (!subjectSrc){
		return false;
	}
	
	dependent.style.visibility = 'hidden';
	
	var loaderImg = new Image();
	loaderImg.style.display = 'none';
	loaderImg.onload = function(){
		dependent.style.visibility = 'visible';
		cleanUp(this);
	};
	
	document.body.appendChild(loaderImg);
	loaderImg.src = subjectSrc;
	
	function cleanUp(img){
		img.parentNode.removeChild(img);
		img.src = null;
		img.onload = null;
	}	
}


//

checkBrowserWidth();
attachEventListener(window, "resize", checkBrowserWidth, false);