/* 
-----------------------------------------------------------------
	HRL Morrison & Co.
	css_helpers.js
	
	Description: 
 	COntains various Chrometoaster JavaScript/CSS helper functions
	
	Created 06/04/2009 by AK
	Last Updated: See SVN	
-----------------------------------------------------------------
*/


$(document).ready(function() {
    
	/* CSS HACKS
	   use only for non-critical fixes for non IE browsers
	------------------------------------------------------------- */
	
	if($.browser.safari) {	
		$('<link rel="stylesheet" type="text/css" href="/resources/styles/browser/safari-all.css" media="screen" />').appendTo("head");
		if(jQuery.browser.version.substr(0,3)<"500") {
			$('<link rel="stylesheet" type="text/css" href="/resources/styles/browser/safari-2down.css" media="screen" />').appendTo("head");
		}
	} else	if($.browser.opera) {
		//$('<link rel="stylesheet" type="text/css" href="/resources/styles/browser/opera-all.css" media="screen" />').appendTo("head");			
	} 
	
	
	/* IE-PNG fix
	------------------------------------------------------------- */	
	$('#flash-feature').supersleight();
	$('#zone-maincontent').supersleight();
	$('#zone-footer').supersleight();
	$('#zone-header').supersleight();
	
	
	/* CSS HELPERS
	------------------------------------------------------------- */
	/* Tables - adding first and last classes */
	$('tr th:first-child').addClass('first');
	$('tr td:first-child').addClass('first');
	$('tbody tr:first-child').addClass('first');
	$('tr th:last-child').addClass('last');
	$('tr td:last-child').addClass('last');
	$('tbody tr:last-child').addClass('last');
	
	/* Zebra */
	$('table tr:even, .contacts .contact:even').addClass('alt').supersleight();
	
	/* Print button */
	$('#footer-tools').append('<li><a href="#" id="printpage">Print page</a></li>');
	$('#footer-tools #printpage').click(function() {
		window.print();
		return false;
	});
	
	/* First & last CSS classes in nav lists */
	$('.anchor-nav li:first, #primary-nav li:first, #primary-nav-supplimentary li:first, .footer-links li:first').addClass('first');
	$('.anchor-nav li:last, #primary-nav li:last, #primary-nav-supplimentary li:last').addClass('last');
	$('.footer-links li:last').prev().addClass('last');
	
	/* Address in footer */
	$('#footer-addresses .address:first').addClass('first');
	$('#footer-addresses .address:eq(1)').removeClass('first').addClass('second');
	
	/* Mac Flash z-index bug fix */
	if(($.browser.mozilla) && (jQuery.platform['mac']==true)) {	
	//	if(jQuery.browser.version.substr(0,3)>="1.9") {
			$('#primary-nav li a').hover(
				function() {
					$('#primary-nav, #zone-header .logo').css('z-index','5');
					setTimeout("$('#primary-nav, #zone-header .logo').css('z-index','4')",1);
				},
				function() {
					$('#primary-nav, #zone-header .logo').css('z-index','5');
					setTimeout("$('#primary-nav, #zone-header .logo').css('z-index','4')",1);
				}
			);
	//	}
	}
	
});

/* Platform detection */
var userOSAgent = navigator.userAgent.toLowerCase();
// Figure out what browser is being used
jQuery.platform = {
        mac: /mac/.test(userOSAgent),
        osx: /mac os x/.test(userOSAgent),
        win: /win/.test(userOSAgent),
        linux: /linux/.test(userOSAgent)
};



/* IEpng fix
	 http://allinthehead.com/retro/338/supersleight-jquery-plugin
	-------------------------------------------------------------*/

jQuery.fn.supersleight = function(settings) {
	settings = jQuery.extend({
		imgs: true,
		backgrounds: true,
		shim: '/resources/images/ui/pixel.gif',
		apply_positioning: true
	}, settings);
	
	return this.each(function(){
		if (jQuery.browser.msie && parseInt(jQuery.browser.version) < 7 && parseInt(jQuery.browser.version) > 4) {
			jQuery(this).find('*').each(function(i,obj) {
				var self = jQuery(obj);
				// background pngs
				if (settings.backgrounds && self.css('background-image').match(/\.png/i) !== null) {
					var bg = self.css('background-image');
					var src = bg.substring(5,bg.length-2);
					var mode = (self.css('background-repeat') == 'no-repeat' ? 'crop' : 'scale');
					var styles = {
						'filter': "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "', sizingMethod='" + mode + "')",
						'background-image': 'url('+settings.shim+')'
					};
					self.css(styles);
				};
				// image elements
				if (settings.imgs && self.is('img[src$=png]')){
					var styles = {
						'width': self.width() + 'px',
						'height': self.height() + 'px',
						'filter': "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + self.attr('src') + "', sizingMethod='scale')"
					};
					self.css(styles).attr('src', settings.shim);
				};
				// apply position to 'active' elements
				if (settings.applyPositioning && self.is('a, input') && self.css('position') === ''){
					self.css('position', 'relative');
				};
			});
		};
	});
};
