/* 
-----------------------------------------------------------------
	CRESA Website
	CRE.2735
	css_helpers.js
	
	Description: 
 	Contains various CSS helper functions
	
	Created 23.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
	------------------------------------------------------------- */	
	$('body').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 
	$('#tools_footer').append('<li class="tool_print"><a href="#" id="printpage">Print page</a></li>');
	$('#tools_footer #printpage').click(function() {
		window.print();
		return false;
	});
	*/
	
	/* remove Wordpress inline widths on image attachments */
	$('.wp-caption').each(function() {
		var $imageattachmentwidth = $(this).children().children('img').width();
		$(this).css('width',$imageattachmentwidth);
	});
	
	/* CSS Hover helper */
	$('.widget.publications .downloads li.pdf, #projects .project').hover(
		function() {
			$(this).addClass('hover');
		},
		function() {
			$(this).removeClass('hover');
		}
	);
	$('.widget.publications .downloads li').each(function() {
		if ($(this).find('a.download').length>0) {
			$(this).click(function() {
				var url = $(this).find('a.download').attr('href');
				window.location.href = url;
			});
		}
	});
	$('#projects .project').click(function() {
		if ($(this).children('h2').length>0) {
			var url = $(this).children('h2').children('a').attr('href');
		} else {
			var url = $(this).children('h3').children('a').attr('href');
		}
		window.location.href = url;
	});
	
	// Last row css helper
	$('.widget.publications .downloads li').addClass('last');
	
	/* Publications table */
	$('#publications table tr').each(function() {
		$(this).children('td:even').addClass('even');
	});
	$('#publications table th').each(function() {
		if ($(this).children('a').length>0) {
			$(this).hover(
				function() {
					$(this).addClass('hover');
				},
				function() {
					$(this).removeClass('hover');
				}
			);
		}
	});
	
});

/* 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');
				};
			});
		};
	});
};

