
// enable IE6 PNG transparency
var enablePngTransparency = function(pictureID, url)	{
	var picture = $(pictureID);
	// set ie6 opacity
	var version = parseFloat(navigator.appVersion.split('MSIE')[1]);
	if ((version >= 5.5) && (version < 7) && (document.body.filters)) {
		picture.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + url + "')";
		picture.style.backgroundImage = "none";
	} else {
		picture.setStyle('background','url(' + url + ') no-repeat');
	}

}

function openPopup(url)	{
	  var newwindow = window.open(url,'popup','width=380,height=430,scrollbars=yes,top=40');
	  if (window.focus)	newwindow.focus();
	  return false;
}

function parseJson(request)	{
	// check if its bitrockers-json encoded
	if(request.substr(0,4) != "JSON")	{
		var response = {html: request, onlyHtml: true};
	} else {
		// parse json
		var parseCode = request.substr(4);
		var response =  eval('(' + parseCode + ')');
	};
	return response;
}

var hiddenIFrame = {

	iframe: null,

	init:		function(layerID)	{
		// build hidden iframe into dom
		if (!window.ie6)	return;
		var layer = $(layerID);
		var iframe = new Element('iframe');
		iframe.id = '_hiddenIFrame';
		var newLeft = layer.getLeft() - layer.getParent().getLeft();
		var newTop = layer.getTop();
		iframe.setStyles({ width: layer.offsetWidth + 'px', height: layer.offsetHeight + 'px', top: newTop + 'px', left: newLeft + 'px',
				'z-index': '29', position: 'absolute', display: 'block', 'border': '0px', background: '#fff' });
		iframe.injectBefore(layerID);
		this.iframe = iframe.id;
	},

	close:		function()	{
		if (this.iframe)	$('_hiddenIFrame').remove();
		this.iframe = null;
	}

};



// ==============================================

var navMenu = new Class({

	navElement: null,
	activated: false,


	initialize:			function(navElement)	{
		this.navElement = navElement;
		if (!$(this.navElement))	return;
		if ($(this.navElement).hasClass('active'))	return;
		$(this.navElement).addEvent('mouseover', this.activate.bind(this));
		$(this.navElement).addEvent('mouseout', this.deactivate.bind(this));
	},

	activate:			function()	{
		if (this.activated)	return;
		this.activated = true;
		$(this.navElement).addClass('active');
	},

	deactivate:			function()	{
		if (!this.activated) return;
		this.activated = false;
		$(this.navElement).removeClass('active');
	}

});

var formfieldHover = new Class({

	el:		false,
	focus:	false,
	mouseOver: false,

	initialize:	function(el)	{
		this.el = $(el);
		el.onmouseover = this.mouseOver.bind(this);
		el.onmouseout = this.mouseOut.bind(this);
		el.onfocus = this.onFocus.bind(this);
		el.onblur = this.onBlur.bind(this);
	},

	mouseOver:	function()	{
		this.mouseOver = true;
		if (this.focus)	return;
		this.el.addClass('formfieldHighlight');
	},

	mouseOut:	function()	{
		this.mouseOver = false;
		if (this.focus)	return;
		this.el.removeClass('formfieldHighlight');
	},

	onFocus:	function()	{
		this.el.addClass('formfieldHighlight');
		this.focus = true;
	},

	onBlur:	function()	{
		this.focus = false;
		if(!this.mouseOver)	this.el.removeClass('formfieldHighlight');
	}



});


var domIsReady = function()	{

	// prepare nav hover
	if ($('nav'))	{
		var navHome = new navMenu('navHome');
		var navVoting = new navMenu('navVoting');
		var navSearch = new navMenu('navSearch');
		var navMessageBox = new navMenu('navMessageBox');
		var navProfile = new navMenu('navProfile');
		var navSupport = new navMenu('navSupport');
		var navLogout = new navMenu('navLogout');
	} else if ($('navOuter'))	{
		var navHome = new navMenu('navHome');
		var navSearch = new navMenu('navSearch');
		var navSignUp = new navMenu('navSignUp');
		var navLogin = new navMenu('navLogin');
		var navSupport = new navMenu('navSupport');
	}



	// prepare input focus/hover
	var fields = $$('.formfield');
	$each(fields, function(el)	{
		new formfieldHover(el);
	});

	var version = parseFloat(navigator.appVersion.split('MSIE')[1]);
	if ((version >= 5.5) && (version < 7) && (document.body.filters)) {
			$$('.autoPNG').each(function(element){
			// url(...)
			var url = element.getStyle('background-image').toString();
			url = url.substring(4, url.length - 1);
			element.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src=" + url + ", xsizingMethod='scale')";
			element.style.backgroundImage = "none";
		});
	}

	// if (devel == 1) var Tooltips = new Tips($$('.Tooltip'));
	// var Tooltips = new Tips($$('.Tooltip'));



};
window.onDomReady(domIsReady);




