/**
 * Browser Hacks
 *  v1.0
 *  
 *  fixes common problems with various browsers - mostly ie6
 * 
 * @author kchevalier@suntouchdesign.com
 * march - august, 2009
 */

var browserHacks = {
	
	browser: '',
	version: 0,
	pngList: '',
	fix: true,
	stylesheetIE6: '',
	
	init: function() {
		
		// giving errors on login/register page - disabled for now
		//browserHacks.detectBrowser();
		
		if ( browserHacks.fix ) {
			
			// fix IE6
			if ( document.all && !window.opera && !window.XMLHttpRequest ) {
				browserHacks.fixIE6( browserHacks.pngList );
				if ( browserHacks.stylesheetIE6 != '' ) {
					document.createStyleSheet( browserHacks.stylesheetIE6, 1 );
				}
			}
		}
		
	},
	
	// detect browser name and version
	detectBrowser: function() {

	    var BO = new Object(); 
	    BO["ie"]        = false /*@cc_on || true @*/; 
	    BO["ie4"]       = BO["ie"] && (document.getElementById == null); 
	    BO["ie5"]       = BO["ie"] && (document.namespaces == null) && (!BO["ie4"]); 
	    BO["ie6"]       = BO["ie"] && (document.implementation != null) && (document.implementation.hasFeature != null) && (window.XMLHttpRequest == null); 
	    BO["ie55"]      = BO["ie"] && (document.namespaces != null) && (!BO["ie6"]); 
	    BO["ie7"] 		= (BO["ie"] && document.implementation != null && document.compatMode != null && window.XMLHttpRequest != null);
	    BO["ns4"]       = !BO["ie"] &&  (document.layers != null) &&  (window.confirm != null) && (document.createElement == null); 
	    BO["opera"]     = (self.opera != null); 
	    BO["gecko"]     = (document.getBoxObjectFor != null); 
	    BO["khtml"]     = (navigator.vendor == "KDE"); 
	    BO["konq"]      = ((navigator.vendor == 'KDE') || (document.childNodes) && (!document.all) && (!navigator.taintEnabled)); 
	    BO["safari"]    = (document.childNodes) && (!document.all) && (!navigator.taintEnabled) && (!navigator.accentColorName); 
	    BO["safari1.2"] = (parseInt(0).toFixed == null) && (BO["safari"] && (window.XMLHttpRequest != null)); 
	    BO["safari2.0"] = (parseInt(0).toFixed != null) && BO["safari"] && !BO["safari1.2"]; 
	    BO["safari1.1"] = BO["safari"] && !BO["safari1.2"] && !BO["safari2.0"]; 
			
		if ( BO["ie"] ) { browserHacks.browser = 'internet explorer'; }
		if ( BO["ie4"] ) { browserHacks.version = 4; }
		if ( BO["ie5"] ) { browserHacks.version = 5; }
		if ( BO["ie6"] ) { browserHacks.version = 6; }
		if ( BO["ie55"] ) { browserHacks.version = 5.5; }
		if ( BO["ie7"] ) { browserHacks.version = 7; }
		if ( BO["ns4"] ) { browserHacks.browser = 'netscape'; browserHacks.version = 4; }
		if ( BO["opera"] ) { browserHacks.browser = 'opera'; }
		if ( BO["gecko"] ) { browserHacks.browser = 'gecko'; }
		if ( BO["khtml"] ) { browserHacks.browser = 'khtml'; }
		if ( BO["konq"] ) { browserHacks.browser = 'konq'; }
		if ( BO["safari"] ) { browserHacks.browser = 'safari'; }
		if ( BO["safari1.2"] ) { browserHacks.version = 1.2; }
		if ( BO["safari2.0"] ) { browserHacks.version = 2; }
		if ( BO["safari1.1"] ) { browserHacks.version = 1.1; }
		
	},
	
	// apply ie6 hacks
	fixIE6:  function( pngList ) {
		
		if (document.all && !window.opera && !window.XMLHttpRequest) {
			
			// define regexes
			var rePNG = /url\([\'\"](.+\.png)[\'\"]\)/;		// get URLs for PNGs
			var rePx = /(-?)(\d+)px/;						// get numeric values for CSS pixel values
			
			
			// handle elements with transparent PNG backgrounds
			jQuery.each( pngList, function(){
				
				if ( this.height() ) {
				
					var png = this;
					var backSrc = rePNG.exec( png.css( 'background-image' ) );			
					
					png.css('background-image', 'none')
					png.css('filter', 'progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true, sizingMethod=crop src="' + backSrc[1] + '")');
				}
	
			} );
				
			// handle img tags with transparent PNGs
			$("img[src$='png']").each(function(){
			
				// define some variables
				var imgWidth = $(this).width();
				var imgHeight = $(this).height();
				var imgSource = $(this).attr('src');
				
				// create wrapper div
				$(this).wrap("<div class='pngImg' style='height:" + imgHeight + "px; width:" + imgWidth + "px;'></div>");
				$(this).parent().css('filter', 'progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true, sizingMethod=scale src="' + imgSource + '")');
				$(this).parent().css({
					'margin-left': $(this).css('margin-left'),
					'margin-right': $(this).css('margin-right'),
					'margin-top': $(this).css('margin-top'),
					'margin-bottom': $(this).css('margin-bottom'),
					'padding-top': $(this).css('padding-top'),
					'padding-bottom': $(this).css('padding-bottom'),
					'padding-left': $(this).css('padding-left'),
					'padding-right': $(this).css('padding-right'),
					'float': $(this).css('float'),
					'clear': $(this).css('clear'),
					'position': $(this).css('position'),
					'top': $(this).css('top'),
					'right': $(this).css('right'),
					'left': $(this).css('left'),
					'bottom': $(this).css('bottom')
				});
				
				// remove original image
				$(this).css('display', 'none');
				
			});
			
			
			// loop through all body elements for general IE bugs
			$('body *').each(function(){
			
				// handle double-margin bug
				
				// check for floated elements
				if ($(this).css('float') == ('left' || 'right')) {
				
					// initialize internal variables
					var update = $(this);
					var floatStyle = update.css('float');
					
					// check for positive side margins assigned in pixels and retrieve numerical value
					if (sideMargin = rePx.exec(update.css('margin-' + floatStyle))) {
					
						// only alter positive margins
						if (sideMargin[1] != '-') {
						
							// reassign side margin with half its value
							var newSideMargin = Math.round(sideMargin[2] / 2);
							update.css('margin-' + floatStyle, newSideMargin + 'px');
						}
					}
				}
			});
			
			
			// handle min-height bug
			$('#wrapper *').each(function(){
			
				if (minHeight = rePx.exec($(this).css('min-height'))) {
				
					// initialize internal variables
					var update = $(this);
					minHeightValue = minHeight[2];
					
					// assign height hack to element if needed
					if (update.height() < minHeightValue) {
						update.css('height', minHeightValue + 'px');
					}
					
				}
				
			});
		}
	}
	
}
