﻿/// <reference path="C:\References\jquery-1.4.1-vsdoc.js" />

// ********************************************************************
// 
// Manheim Retail Marketing
// ********************************************************************
// Copyright © 2010 Manheim Retail Marketing.
//
//  Summary
// ******************************
// $Workfile: PngFix.js $
// Notes:
//
// File History Information
// ************************				
// Created on:					    
// Last Modified:					$Modtime: 22/03/10 16:58 $
// Original Author:					Liam Prescott
// Last Modified by:				$Author: Prescottl $
// Last JS Lint:					N/A
//
// Source Control Information
// **************************
// File Version:					$Revision: 1 $
// VSS Location:					$Archive: /Manheim.Portfolio/Manheim.Portfolio.Web.UI.Client.Assets/assets/js/mrm/lib/utilities/1.0/PngFix.js $
//
// ********************************************************************



// Check / create namespace
if (!mrm.global.isNamespaceDefined("mrm.lib.utilities"))
{
	mrm.global.createNamespace("mrm.lib.utilities", "1.0");
}


// Class definition
mrm.lib.utilities.PngFix = Object.subClass
(
	{
		
		/*
		=============================
		CONSTANTS
		=============================
		*/
			"DEFAULT_URL_BLANK_GIF" : "/common/assets/images/_blank.gif",
			
			"VALIDATE_PNG_IMAGES" : true,

			
		/*
		=============================
		CONSTRUCTOR
		=============================
		*/
			"init" : function (autoRun, urlBlankGif, validatePngImages)
			{
				this._urlBlankGif = (urlBlankGif) ? urlBlankGif : this.DEFAULT_URL_BLANK_GIF;
				if (validatePngImages != undefined) this.VALIDATE_PNG_IMAGES = validatePngImages;	
				if (autoRun) this._runFullDocFix();
			},
			
			
			
			"_urlBlankGif" : undefined,
				
				
			
		/*
		=============================
		PUBLIC MEMBERS
		=============================
		*/
			"fixPNGs" : function (jqContainer) 
			{
				if(this._isValidBrowser()) 
				{
					var pngs = jqContainer.find(".png");
					var len = pngs.length;
						
					for (var i = 0; i < len; i++) 
					//for (var i = len; i > 0; i--)
					{
						this.fixPNG(pngs[i]);
					}
				}
			},
				
				
			"fixPNG" : function (imageObj) 
			{
				//
				//'runtimeStyle' property is an IE ONLY property therefore return of !IE
				//
					
				var io = imageObj;
					
				var pngURL;
					
				if(!this._isValidBrowser())
				{
					return;
				}
				
				// Store node name
				var io_nn = io.nodeName;
				var nn_img = "IMG";
				var nn_input = "INPUT";

				if (io_nn == nn_img || io_nn == nn_input) 
				{
					pngURL = io.src;
					
					//Check our image is actually a png and bail if not.
					if (io_nn == nn_img)
					{
						if(this.VALIDATE_PNG_IMAGES && !this._isImagePng(pngURL)) return;
					}
					else { if(!this._isImagePng(pngURL)) return;}
						
					io.runtimeStyle.backgroundImage = "none";
					io.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + pngURL + "', sizingMethod='image')";
					io.src = this._urlBlankGif;
					io.style.visibility="visible";	
				} 
				else 
				{
					pngURL = (io.currentStyle) ? this._returnBgndImg(io.currentStyle.backgroundImage) : '';
						
					//Check our image is actually a png and bail if not.
					if(!this._isImagePng(pngURL)) return;
						
					if (pngURL != 'ne')
					{
						switch (io.currentStyle.backgroundRepeat)
						{
							case "no-repeat" :
									
								switch (io.currentStyle.overflow)
								{
									case "hidden" : //"visible"
										io.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + pngURL + "',sizingMethod='crop')";
										break;
										
									default :
										io.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + pngURL + "',sizingMethod='image')";
										break;
								}
								break;
								
							default :
								io.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + pngURL + "',sizingMethod='scale')";
								break;
						}
						io.style.backgroundImage="none";
						io.style.visibility="visible";
					}
				}
			},



		/*
		=============================
		PRIVATE MEMBERS
		=============================
		*/
			"_runFullDocFix" : function ()
			{
				if (this._isValidBrowser()) this.fixPNGs($(document).find("body"));
			},
				
				
			/*
			=============================
			UTILITY METHODS
			=============================
			*/
				"_validBrowser" : undefined,
					
				"_isValidBrowser" : function ()
				{
					var vb = this._validBrowser;
					if (!vb) vb = ($.browser.msie && $.browser.version < 7) ? true : false;
					return vb;
				},
				
					
				"_returnBgndImg" : function (obj)
				{
					return obj.substring(5, obj.length - 2); //[ "none" returned as "ne" ]
				},


				"_isImagePng" : function (pngURL)
				{
					return (pngURL.substring(pngURL.length - 3, pngURL.length) == "png");
				}
					
					
					
					
	}
);
