Prototype.Browser.IE6 = ( parseFloat(navigator.userAgent.split('MSIE')[1]) == 6 ) ? true : false;
Prototype.Browser.IE5_5 = ( parseFloat(navigator.userAgent.split('MSIE')[1]) == 5.5 ) ? true : false;

var Login = {
	show: function(url, type)
	{
		preBox.showMessage(document.body, Login._loginHTML());
		
		if ( Prototype.Browser.IE )
		{
			$('message-content').setStyle({background: '#2A2A2A'});
			if ( $('remember') )
			{
				$('remember').setStyle({marginTop: '-3px', marginBottom: '-3px'});
			}
		}
		
		var action = "/login.php?type=quick&";
		
		if ( url != undefined )
		{
			action += "url=" + encodeURIComponent(url) + "&";
		}
		
		$('login-form').action = action;
		
		$('message-container').getElementsBySelector('img').each( function(image) {
			Images.fix(image);
		});
		
		$('su_username').focus();
		
		document.fire('preBox:show');
	},
	
	show_mw: function(url, type)
	{
		preBox.showMessage(document.body, Login._loginHTML_mw());
		
		if ( Prototype.Browser.IE )
		{
			$('message-content').setStyle({background: '#2A2A2A'});
			if ( $('remember') )
			{
				$('remember').setStyle({marginTop: '-3px', marginBottom: '-3px'});
			}
		}
		
		var action = "/login.php?type=quick&";
		
		if ( url != undefined )
		{
			action += "url=" + encodeURIComponent(url) + "&";
		}
		
		$('login-form').action = action;
		
		$('message-container').getElementsBySelector('img').each( function(image) {
			Images.fix(image);
		});
		
		$('su_username').focus();
		
		document.fire('preBox:show');
	},
	
	hide: function()
	{
		preBox.hideMessage(document.body);
		
		document.fire('preBox:hide');
	},
	
	submit: function()
	{
		if ( !Login.check() )
		{
			return;
		}
		
		$('login-form').submit();
	},
	
	forgottenPassword: function()
	{
		window.location = "/login.php?op=forgot";
	},
	
	check: function()
	{
		if ( $F('su_username') == "" )
		{
			alert('Please enter your username.');
			$('su_username').focus();
			return false;
		}
		else if ( $F('su_password') == "" )
		{
			alert('Please enter your password.');
			$('su_password').focus();
			return false;
		}
		
		return true;
	},
	
	_loginHTML: function()
	{
		var html = '' + 
			'<div id="login-message-container">' +
			'<div id="login-message-container-inner"><a href="javascript: Login.hide();" id="message-close"><img src="http://us-cdn.creamermedia.co.za/template/mw/close.png" border="0" style="padding-top:5px" /></a></div>' +
			'<form action="/login.php" method="post" name="login-form" id="login-form" onsubmit="return Login.check();">' +
				'<div class="login_input_fields">' +
				'Your Username' +
					'<input name="su_username" type="text" id="su_username" value="" tabindex="1" class="login_su_input" />' +
				'</div>' +
				'<div class="login_input_fields">' +
				'Your Password' +
					'<input name="su_password" type="password" id="su_password" value="" tabindex="2" class="login_su_input" />' +
				'</div>' +
				'<div  class="login_submit_button">' +
					'<input name="Submit" type="submit" value="Submit" style="display: none;" tabindex="3" /><a href="javascript: Login.submit();" tabindex="4"><img src="http://us-cdn.creamermedia.co.za/template/mw/login.png" border="0" style="padding-top:9px" /></a>' +
				'</div>' +
				'<div class="login_forgot_pw">' +
					'Forgotten password? <a href="javascript: Login.forgottenPassword();" tabindex="4" style="font-family:arial;color:#9d9898;font-size:12px">Click here</a>' +
				'</div>' +
				'<div class="login_gain_access">' +
					'<img src="http://us-cdn.creamermedia.co.za/template/mw/gain_access_now.png" border="0" style="padding-top:9px" />' +
				'</div>' +
				'<div class="login_bottom_button_as login_bottom_buttons">' +
					'<a href="http://www.creamermedia.co.za/article/engineering-news-2011-03-24" tabindex="6">&nbsp;</a>' +
				'</div>' +
				'<div class="login_bottom_button_pb login_bottom_buttons">' +
					'<a href="http://www.creamermedia.co.za/article/project-browser-magazine-website-access-projects-2011-04-11" tabindex="7">&nbsp;</a>' +
				'</div>' +
			'</form>' +
			'</div>';
			
			
		return html;
	},
	
	_loginHTML_mw: function()
	{
		var html = '' + 
			'<div id="login-message-container">' +
			'<div id="login-message-container-inner"><a href="javascript: Login.hide();" id="message-close"><img src="http://us-cdn.creamermedia.co.za/template/mw/close.png" border="0" style="padding-top:5px" /></a></div>' +
			'<form action="/login.php" method="post" name="login-form" id="login-form" onsubmit="return Login.check();">' +
				'<div class="login_input_fields">' +
				'Your Username' +
					'<input name="su_username" type="text" id="su_username" value="" tabindex="1" class="login_su_input" />' +
				'</div>' +
				'<div class="login_input_fields">' +
				'Your Password' +
					'<input name="su_password" type="password" id="su_password" value="" tabindex="2" class="login_su_input" />' +
				'</div>' +
				'<div  class="login_submit_button">' +
					'<input name="Submit" type="submit" value="Submit" style="display: none;" tabindex="3" /><a href="javascript: Login.submit();" tabindex="4"><img src="http://us-cdn.creamermedia.co.za/template/mw/login.png" border="0" style="padding-top:9px" /></a>' +
				'</div>' +
				'<div class="login_forgot_pw">' +
					'Forgotten password? <a href="javascript: Login.forgottenPassword();" tabindex="4" style="font-family:arial;color:#9d9898;font-size:12px">Click here</a>' +
				'</div>' +
				'<div class="login_gain_access">' +
					'<img src="http://us-cdn.creamermedia.co.za/template/mw/gain_access_now.png" border="0" style="padding-top:9px" />' +
				'</div>' +
				'<div class="login_bottom_button_as login_bottom_buttons">' +
					'<a href="http://www.creamermedia.co.za/article/mining-weekly-2011-03-24" tabindex="6">&nbsp;</a>' +
				'</div>' +
				'<div class="login_bottom_button_pb login_bottom_buttons">' +
					'<a href="http://www.creamermedia.co.za/article/project-browser-magazine-website-access-projects-2011-04-11" tabindex="7">&nbsp;</a>' +
				'</div>' +
				'<div class="login_bottom_button_ra login_bottom_buttons">' +
					'<a href="http://www.creamermedia.co.za/article/miningweeklycom-research---1-user-2011-03-24" tabindex="8">&nbsp;</a>' +
				'</div>' +
			'</form>' +
			'</div>';
			
			
		return html;
	}
};

var Alert = {
	show: function(msg)
	{
		preBox.showMessage(document.body, Alert._alertHTML(msg));
		
		if ( Prototype.Browser.IE )
		{
			$('message-content').setStyle({background: '#2A2A2A'});
		}
		
		$('message-container').getElementsBySelector('img').each( function(image) {
			Images.fix(image);
		});
		
		setTimeout(Alert.timeout, 15 * 1000);
		
		document.fire('preBox:show');
	},
	
	hide: function()
	{
		preBox.hideMessage(document.body);
		
		document.fire('preBox:hideMessage');
	},
	
	timeout: function()
	{
		if ( $('preBox-box') )
		{
			Alert.hide();
		} 
	},
	
	_alertHTML: function(msg)
	{
		var html = '' + 
			'<div id="message-container">' +
				'<div id="message-title">' +
					'<div id="message-title-text">Alert</div>' +
					'<a href="javascript: Alert.hide();" id="message-close"><img src="/images/mw/black_x.png" alt="Close" width="16" height="16" class="' + (( Prototype.Browser.IE ) ? 'float-right' : 'float-left') +'" /></a>' +
					'<div class="clear">&nbsp;</div>' +
				'</div>' +
				'<div id="message-content" style="width: 349px;">' +
					'<div id="alert-text">' +
						msg +
					'</div>' +
				'</div>' +
			'</div>';
			
		return html;
	}
}

var Cookie = {
	set: function(name, value, days_to_expire)
	{
		var expire = '';
		if ( days_to_expire != undefined )
		{
			var d = new Date();
			d.setTime(d.getTime() + (86400000 * parseFloat(days_to_expire)));
			expire = '; expires=' + d.toGMTString();
		}
		return (document.cookie = escape(name) + '=' + escape(value || '') + expire);
	},
	
	get: function(name)
	{
		var cookie = document.cookie.match(new RegExp('(^|;)\\s*' + escape(name) + '=([^;\\s]*)'));
		return (cookie ? unescape(cookie[2]) : null);
	},
	
	erase: function(name)
	{
		var cookie = Cookie.get(name) || true;
		Cookie.set(name, '', -1);
		return cookie;
	},
	
	accept: function()
	{
		if (typeof navigator.cookieEnabled == 'boolean')
		{
			return navigator.cookieEnabled;
		}
		
		Cookie.set('_test', '1');
		return (Cookie.erase('_test') === '1');
	}
};

var Images = {
	transparent_image: "/images/spacer.gif",
	
	fixAll: function()
	{
		// Check if the browser is IE 5.5 or IE 6
		if ( !Prototype.Browser.IE && !Prototype.Browser.IE5_5 && !Prototype.Browser.IE6 )
		{
			return;
		}
		
		// If there is a player on the page run again on load to fix all images after the player
		if ( $$('object').length > 0 )
		{
			Event.observe(window, 'load', Images.fixAll);
		}
		
		// Get all images
		$$('img').each(Images.fix);
	},
	
	fix: function(image)
	{
		if ( !Prototype.Browser.IE5_5 && !Prototype.Browser.IE6 )
		{
			return;
		}
		
		image = $(image);
	
		var filename = image.readAttribute('src');
		if ( filename.toLowerCase().endsWith('png') )
		{
			var opacity = image.getStyle('opacity');
			
			image.setStyle({
				filter: 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src="' + filename + '", sizingMethod="none")'
			});
			image.writeAttribute({
				src: Images.transparent_image
			});
			
			if ( opacity != 1 )
			{
				image.setStyle({opacity: opacity});
			}
		}
	}
}

var Util = {
	resize: function()
	{
		var min_width = Element.getStyle(document.body, 'min-width');
		var set_width = ( min_width ) ? false : true;
	
		min_width = min_width || '1045';
		min_width = min_width.gsub('px', '');
		
		var width = document.viewport.getWidth();
		
		var right_pos = (( width < min_width ) ? (width - min_width) : 0) + 'px';
		//$('indices-container').setStyle({right: right_pos});
		
		if ( set_width )
		{
			var body_width = ( width < min_width ) ? '1045px' : '';
			Element.setStyle(document.body, {width: body_width});
		}
	},
	
	getScrollOffset: function()
	{
		var x = 0, y = 0;
		if ( typeof( window.pageYOffset ) == 'number' )
		{
			//Netscape
			y = window.pageYOffset;
			x = window.pageXOffset;
		}
		else if ( document.body && ( document.body.scrollLeft || document.body.scrollTop ) )
		{
			//DOM
			y = document.body.scrollTop;
			x = document.body.scrollLeft;
		}
		else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) )
		{
			//IE6 standards compliant mode
			y = document.documentElement.scrollTop;
			x = document.documentElement.scrollLeft;
		}
		return {left: x, top: y};
	},
	
	getMaxHeight: function(elements, inclusive)
	{
		// Check if height must include padding
		if ( inclusive == undefined )
		{
			inclusive = false;
		} 
		
		var max = 0
		var height;
		
		elements.each( function(element) {
			
			if ( !inclusive )
			{
				var padding = parseInt(element.getStyle('paddingTop').gsub('px', '')) + parseInt(element.getStyle('paddingBottom').gsub('px', ''));
			}
			else
			{
				var padding = 0;
			}
			
			var height = element.getHeight() - padding;
			if ( height > max )
			{
				max = height;
			}
		});
		
		return max;
	}
};

Effect.Transitions.slowstop = function(pos)
{
	return 1 - Math.pow(0.60, 20 * pos);
}

// Attach events
document.observe('dom:loaded', Images.fixAll);
document.observe('dom:loaded', Util.resize);

Event.observe(window, 'resize', Util.resize);
