/* Effect.js

This document sets the menu effect as well as applying IE6 PNG fix using mootools.

@author: Andrew Lay
@date: 15/07/2009

*/
var isIE6 = getInternetExplorerVersion() == 6;

function getInternetExplorerVersion() {
  var rv = -1; // Return value assumes failure.
  if (navigator.appName == 'Microsoft Internet Explorer')
  {
    var ua = navigator.userAgent;
    var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
    if (re.exec(ua) != null)
      rv = parseFloat( RegExp.$1 );
  }
  return rv;
}

function iePatch() {
	// IE6 PNG fix
	//isIE6 = false;
	if (isIE6) {
		 $$('div').each(function(el){
			// for some elements, this causes an error in IE so use try/catch
			try {
				// fix for background image
				var imgURL = el.getStyle('background-image');
				var imgURLLength = imgURL.length;
				if ( imgURL != 'none' && imgURL.indexOf(".png") > 0){
					el.setStyles({
						background: '',
						filter: "progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled='true', sizingMethod='scale', src='" + unescape(imgURL.substring(5,imgURLLength  - 2)) + "')"
					});
				};
			} catch (e) {}			
		})
		$$('img[src$=png]').each(function(el) {
			try {
				var newEl = new Element('input', {
					'styles': {
					'filter': "progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled='true', sizingMethod='crop', src='" + el.getProperty('src') + "')",
					'position': 'relative',
					'background': 'transparent'
					},
					'title': el.getProperty('alt')
				}).setStyles(el.getStyles('padding','margin','border','height','width','display')).setProperties(el.getProperties('id','class')).setProperty("disabled", "true");
				newEl.replaces(el);
			} catch (e) {}
		});
	}
}

/* change stylesheet on the fly */
function changeStyle(_from, _to) {
	if (!document.styleSheets) return;
	
	var header = document.getElementsByTagName('head');
	var styleElements = header[0].getElementsByTagName('link');
	
	for (var j = 0; j< styleElements.length; j++) {
		if (styleElements[j].href.indexOf(_from) > -1) {
			header[0].removeChild(styleElements[j]);
						
			var newElement = document.createElement('link');
			newElement.setAttribute('href', 'styles/'+_to);
			newElement.setAttribute('rel', 'stylesheet');
			newElement.setAttribute('type', 'text/css');

			header[0].appendChild(newElement);
			break;
		}
	}
}

function resizeDiv(_id, _swfID, _w, _h) {
	// line required for IE6
	document.getElementById(_swfID).height = _h;
	
	// all other browsers
	document.getElementById(_id).style.width = _w;
	document.getElementById(_id).style.height = _h;
}

function initBkg() {
	var bkg_img;
    if (document.getElementById('content_panel_mid')) {
    	bkg_img = document.getElementById('content_panel_mid');
        var contentPanel_div = document.getElementById('content_panel');
        bkg_img.width = "999";
        bkg_img.height = contentPanel_div.clientHeight;
		//document.getElementById('content_panel').style.height = bkg_img.height+"px";
    }
}

function changeTheme(_to) {
	window.location.href = 'php/theme_controller.php?url='+escape(window.location.href)+'&theme='+_to;
}

function init() {
	iePatch();
	initBkg();	
	try {
		initPage();
	} catch (e) {}
}

var originalFile;
function swapImage(_obj) {
	originalFile = _obj.src;
	_over = _obj.src;
	_over_array = _over.split(".jpg");
	_obj.src = _over_array[0]+"_over.jpg";
}

function restoreImage(_obj) {
	_obj.src = originalFile;
}

window.addEvent('domready', init);
window.onload = initBkg;