// JavaScript Document

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;
	}
};

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: "http://www.miningweekly.com/images/mw/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 all DOM elements using the sprite image:
		$$('*').each(Images.fixSpriteImages);
	},
	
	fixSpriteImages: function()
	{
		
	},
	
	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});
			}
		}
	}
}

function popUpPrint(url) {
	var w = document.viewport.getWidth();
	width = (w/2)-200;
window.open(url, '_blank', 'Print Version', 'scrollbars=1,location=0,statusbar=1,width=700,height=700,left = '+ width + ',top = 34');
}

document.observe('dom:loaded', Images.fixAll);
