/*********************************************
	pngify 1.4 jQuery | IE6 PNGs with jQuery

	Author: Joel Nagy

	EXPLANATION:
		If you are using PNGs in <img>s or <input>s then do nothing special [slower than using only classes]

		If you are using PNGs with no extension then apply class "pngimg" to all <img>s or <input>s with a png src attribute
		If you are using elements with png backgrounds applied via CSS or using Table backgrounds use class "png"
		If there are some <img>s or <input>s that are PNGs but for some reason you do not want them to be pngified then use class "nopng"

		Special Cases:
		If you want to only use classes to identify what to pngify then set $ALLPNGS to false either in this file or in a <script> tag after loading this file
///		if you do not want to have any elements use a class then you can set $ALLOBJS to true and all objects will be checked for a PNG background-image
**********************************************/

var $ALLPNGS = $ALLPNGS == null? true: $ALLPNGS;
///var $ALLOBJS = $ALLOBJS == null? false: $ALLOBJS;
var EMPTYGIF = EMPTYGIF == null? '/fruit2day/images/empty.gif': EMPTYGIF; // transparent pixel gif

function _pngify(element, img, w, h, repeat) {
	try {
		element.css("filter", "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+ img +"', sizingMethod='"+ repeat +"')");
		if (w != null && w.toString() != 'NaN') {
			if (parseInt(w) == w) w += 'px';
			element.css("width", w);
		}
		if (h != null && h.toString() != 'NaN') {
			if (parseInt(h) == h) h += 'px';
			element.css("height", h);
		}
//element.css('background-color', 'red');
	} catch(e) { }
}

function _wrapperdiv(height, width) {
	height = height != null? height: 0;
	width = width != null? width: 0;
	if (height == parseInt(height)) height += 'px';
	if (width == parseInt(width)) width += 'px';

	$x = $('<div class="pngwrap" style="position: absolute; display: block; margin: 0; padding: 0; height: '+ height +'; width: '+ width +';" />');
	return $x;
}

function _pngBG(png, bg, repeat) {
	var top = left = 0;
	var $png = $(png);
	var posx = png.currentStyle['backgroundPositionX'];
	var posy = png.currentStyle['backgroundPositionY'];
	var repeatXY = png.currentStyle['backgroundRepeat'];
	var w = parseInt($png.css('width'));
	var h = parseInt($png.css('height'));
	var image = new Image();
	image.src = bg;

	// prep base image
	$png.css('background-image', 'url('+ EMPTYGIF +')'); // replace with empty image
	var $x = _wrapperdiv(repeatXY == 'repeat-y'? '100%': image.height, repeatXY == 'repeat-x'? '100%': image.width);
	if (repeat == 'scale')
		_pngify($x, bg, w, h, repeat); // allows for images that are set to repeat to make a nice background if done correctly
	else
		_pngify($x, bg, image.width, image.height, repeat); // all non-repeating images MUST have no-repeat set

	$png.css('position', 'relative');
	$x.css('position', 'absolute');

	// modify positioning
	if (posx == 'center' || posx == '50%') {
		left = parseInt((w - image.width) / 2);
		$x.css('left', left +'px');
	} else if (posx.indexOf('px') > 0) {
		$x.css('left', posx);
	} else if (posx == 'right') {
		right = (w - parseInt(image.width));
		$x.css('left', right +'px');
	} // else left

	if (posy == 'center' || posy == '50%') {
		top = parseInt((h - image.width) / 2);
		$x.css('top', top +'px');
	} else if (posy == 'bottom') {
		top = -1 * (parseInt(image.height) - h)
		$x.css('top', top +'px');
	} else if (posy.indexOf('px') > 0) {
		$x.css('top', posy);
	} // else top

	// IE6 doesn't clip properly like IE7
	if (h != null && w != null && w.toString() != 'NaN' && h.toString() != 'NaN' && (image.height > h || posy.indexOf('-') >= 0 || image.width > w || posx.indexOf('-') >= 0 || parseInt(posy) + image.height > h || parseInt(posx) + image.width > w)) {
		clip_left = posx == 'right' && image.width > w? image.width - w: posx.indexOf('-') >= 0? -1*parseInt(posx): 0;
		clip_top = posy == 'bottom' && image.height > h? image.height - h: posy.indexOf('-') >= 0? -1*parseInt(posy): 0;
		clip_bottom = parseInt(posy) + image.height > h? h - parseInt(posy): image.height;
		clip_right = parseInt(posx) + image.width > w? w - parseInt(posx): image.width;
		$x.css("clip", "rect("+ clip_top +" "+ clip_right +" "+ clip_bottom +" "+ clip_left +")");
	}

	// inject the elements wrapped nicely
	$a = $('<div style="overflow: hidden; position: relative" class="pngx">');
	$a.insertBefore($png);
	$a.append($x);
	$a.append($png);
}

function _pngTABLE(TABLE, bg, repeat) {
	// set TABLE background to empty
	TABLE.attr('background', EMPTYGIF); // replace with empty image
	TABLE.css('background-image', 'url('+ EMPTYGIF +')'); // replace with empty image
	_pngify(TABLE, bg, '100%', '100%', repeat);
}

function _prepIMG(PNG, specificClass) {
	try {
		specificClass = specificClass == null? '': specificClass;

		for (var i=0, l=PNG.length; i < l; i++) {
			var $png = $(PNG[i]);
			var tagName = $png[0].tagName.toLowerCase();
			if (tagName == 'img' || tagName == 'input') {
				var img = $png.attr('src');
				var className = $png.attr('class');
				if (img.lastIndexOf(EMPTYGIF) >= 0 || className.indexOf("nopng") >= 0 || (specificClass == '' && img.lastIndexOf('.png') < 0) || (specificClass != '' && className.indexOf(specificClass) < 0))
					$png = null;  // if the class to check does not exist or the img extension is not png then move on
					
				if ($png != null) {
					var w = parseInt($png.attr('width'));
					var h = parseInt($png.attr('height'));

					// handle the src png
					$png.attr('src', EMPTYGIF);
					_pngify($png, img, w, h, 'crop'); // filter set png background
				}
			}
		}
	} catch (e) { }
}

function _prepOBJ(PNG, specificClass) {
	try { 
		specificClass = specificClass == null? '': specificClass;

		for (var i=0, l=PNG.length; i < l; i++) {
			var repeat = 'crop';
			var png = PNG[i];
			var $png = $(png);
			var bg = ($png.css('background-image')).toString().replace(/^url\(["']*([^)'"]*)["']*\)$/, '$1');
			var className = $png.attr('class');
			var tagName = $png[0].tagName.toLowerCase();

			// if a table object check if bg is set as attribute of table
			if ("|table|thead|th|tbody|tr|td|tfoot|".indexOf('|'+tagName+'|') >= 0 && $png.attr('background') != null)
				bg = $png.attr('background');

			if (bg.lastIndexOf(EMPTYGIF) >= 0 || className.indexOf("nopng") >= 0 || (specificClass == '' && bg.lastIndexOf('.png') < 0) || (specificClass != '' && className.indexOf(specificClass) < 0))
				bg = null;  // if the class to check does not exist or the img extension is not png then move on

			if (bg != null && bg != '') {
				repeat = ($png.css('background-repeat')).indexOf('no-repeat') >= 0? 'crop': 'scale';

				if ("|table|thead|th|tbody|tr|td|tfoot|".indexOf('|'+tagName+'|') >= 0)
					_pngTABLE($png, bg, repeat)
				else
					_pngBG(png, bg, repeat);
			}
		}
	} catch (e) { }
}

jQuery(function() {
	if ($.browser.msie && $.browser.version < 7) { // only IE < 7
		try {
			_prepIMG(jQuery(".pngimg"), "pngimg"); // only cares about "pngimg" class
			if ($ALLPNGS) {
				_prepIMG(jQuery("img")); // <img>s with pngs and no class
				_prepIMG(jQuery("input")); // <input>s with pngs and no class
			}

			// all elements that contain class "png" and background style pngs
			_prepOBJ(jQuery(".png"), "png");
		} catch (e) {}
	} //: if(msie6-)
});