var smoothScale = function () {
	var smoothScalePub = new Object();
	
	smoothScalePub.swfUrl = null;
	smoothScalePub.resizeDetect = null;
	smoothScalePub.installNoteLocation = null;
	
	var imageDirectory = new Array();	
	var flashArray = new Array();
	
	function isSmoothBrowser() {
	// Returns true for all those browsers that already smooth scaled images (See http://www.quirksmode.org/js/detect.html)
		if (navigator.platform.indexOf("Mac") === -1) {	// Not a mac
			if (navigator.userAgent.indexOf("Firefox") !== -1) { // Is Firefox
				if (parseFloat(navigator.userAgent.substring(navigator.userAgent.indexOf("Firefox") + 8)) < 3) {
					return false; // Not Firefox 3+
				} else {
					return true; // Is Firefox 3+
				}		
			} else {
				return false; // Not Firefox
			}
		} else {
			return true; // Is a Mac
		}
	}

	function repositionImage(targetImage) {
		for (var i = 0; i < imageDirectory.length; i++) {
			if (imageDirectory[i].imageElement === targetImage) {
				imageDirectory[i].FlashObject.position();
			}			
		}
	}
	
	smoothScalePub.getSmoothObject = function(targetImage) {
		for (var i = 0; i < imageDirectory.length; i++) {
			if (imageDirectory[i].imageElement === targetImage) {
				return imageDirectory[i].FlashObject;
			}			
		}
	}

	smoothScalePub.smoothImages = function(target) {
		if (!isSmoothBrowser()) {
			
			if (smoothScalePub.resizeDetect !== false && !resizeElement) {
				var resizeElement = document.createElement("DIV");
				resizeElement.style.display = "block";
				resizeElement.style.position = "absolute";
				resizeElement.style.width = "auto";
				resizeElement.style.height = "10em";
				resizeElement.style.overflow = "hidden";
				resizeElement.style.left = "100px";
				resizeElement.style.right = "100px";		
				resizeElement.style.top = "-100px";

				document.body.appendChild(resizeElement);

				var resizeWidth = resizeElement.clientWidth;
				var resizeHeight = resizeElement.clientHeight;	
			}			
			
			flashArray.push(new FlashReplace(smoothScalePub.swfUrl, target, "1", "1", "9.0.0"));
			var flash = flashArray[flashArray.length - 1];
			flash.version = 9;
			flash.insertionPoint = {"position": "before"};
			flash.params = {"wmode": "transparent"};
			flash.installNoteLocation = smoothScalePub.installNoteLocation;

			flash.ready = function () {
				// Setup each Flash object
				this.flashObjects(function (FlashObject) {

					FlashObject.addFlashVars("imgsrc", FlashObject.targetNode.getAttribute("src"));

					FlashObject.embedded = function () {
						swfobject.createCSS("#" + FlashObject.id, "position: absolute; z-index: 1;");
					};

					FlashObject.ready = function () {		
						imageDirectory.push({"imageElement":FlashObject.targetNode, "FlashObject":FlashObject});

						FlashObject.targetNode.style.opacity = 0;
						FlashObject.targetNode.style.filter = "alpha(opacity=0)";
						FlashObject.targetNode.style.position = "relative";
						FlashObject.targetNode.style.zIndex = "2";

						FlashObject.position();
					};

					FlashObject.position = function () {
						//console.log("position: "+FlashObject.element);
						if (FlashObject.element) {
							var zoomLevel = 1;
							if (resizeElement.style.pixelLeft) {
								// Detect IE7 zoom level, modified from http://blog.hedgerwow.com/2007/12/05/detect-page-zoom-for-ie7/
								var n = Math.ceil(resizeElement.offsetLeft / resizeElement.style.pixelLeft * 100);
								zoomLevel = n / 100;
							}

							FlashObject.element.setAttribute("width", (FlashObject.targetNode.clientWidth * zoomLevel) + "px");
							FlashObject.element.setAttribute("height", (FlashObject.targetNode.clientHeight * zoomLevel) + "px");
							FlashObject.element.style.left = (FlashObject.targetNode.offsetLeft / zoomLevel) + "px";
							FlashObject.element.style.top = (FlashObject.targetNode.offsetTop / zoomLevel) + "px";
						}
					};

				});

				function repositionFlashObjects() {
					flash.flashObjects(function (FlashObject) {
						FlashObject.position();
					});
				}

				if (smoothScalePub.resizeDetect !== false) {
					function checkResize() {		
						var newResizeWidth = resizeElement.clientWidth;
						var newResizeHeight = resizeElement.clientHeight;

						if (newResizeWidth !== resizeWidth || newResizeHeight !== resizeHeight) {
							repositionFlashObjects();
						}

						resizeWidth = newResizeWidth;
						resizeHeight = newResizeHeight;
					}

					// Timer to check resize status
					var resizeTimer = setInterval(checkResize, 50);
				}


				// Embed and initialise each Flash object
				flash.embed();
			};

			swfobject.addDomLoadEvent(flash.setup);		
		}
		
	}	
	
	return smoothScalePub;
}();


swfobject.addDomLoadEvent(function () {
	
	smoothScale.swfUrl = "http://www.studio323.co.uk/wp-content/themes/studio323/style/flash/SmoothScale.swf"; // Location of the smoothscale.swf file
	smoothScale.resizeDetect = true; // Wheather to detect window & font resizing (Boolean)
	smoothScale.installNoteLocation = document.getElementById('footer');		
	
	var imageArray = []; // Array of image elements
	var imageElements = document.getElementsByTagName("img");
	for (var i = 0; i < imageElements.length; i++) {
		if (typeof(imageElements[i].getAttribute("class")) == "string") {
			if (imageElements[i].getAttribute("class").indexOf("smooth-scale") != -1) {
				imageArray.push(imageElements[i])
			};	
		} else if (typeof(imageElements[i].getAttribute("className")) == "string") {
			if (imageElements[i].getAttribute("className").indexOf("smooth-scale") != -1) {
				imageArray.push(imageElements[i])
			};	
		}
	}
	
	smoothScale.smoothImages(imageArray);

});
