/** ********************************UTIL + HISTORY */
/**
 * Some useful String methods not included to this class
 */
String.prototype.startsWith = function(str){
	return (this.match("^" + str) == str);
};
String.prototype.endsWith = function(str){
	return (this.match(str + "$") == str);
};
String.prototype.replaceAll = function(toReplace, replacement){
	toReplace = new RegExp(toReplace, "gim");
	return this.replace(toReplace, replacement);
};
if (!Array.indexOf) {
	Array.prototype.indexOf = function (obj) {
		for (var i = 0; i < this.length; i += 1) {
			if (this[i] == obj) {
				return i;
			}
		}
		return -1;
	};
}
function Util(){
	this.ua = navigator.userAgent.toLowerCase();
	this.isOpera = this.ua.indexOf("opera") > -1;
	this.isSafari = (/webkit|khtml/).test(this.ua);
	this.isIE = !this.isOpera && this.ua.indexOf("msie") > -1;
	this.isIE7 = !this.isOpera && this.ua.indexOf("msie 7") > -1;
	this.isGecko = !this.isSafari && this.ua.indexOf("gecko") > -1;
	this.ieVersion = parseInt(this.ua.substring(this.ua.indexOf("msie") + 4));
	if (isNaN(this.ieVersion) || (this.ieVersion == 0)) {
		this.ieVersion = 100;
	}
	this.scrollGap = (this.isIE && (this.ieVersion > 6)) ? 1011 : 1007;
}
Util.prototype = {
	extend: function(o, c, defaults){
	    if(defaults){
	        util.extend(o, defaults);
	    }
	    if(o && c && typeof c == "object"){
	        for(var p in c){
	            o[p] = c[p];
	        }
	    }
	    return o;
	},
    callback : function(cb, scope, args, delay){
        if(typeof cb == "function"){
            if(delay){
                cb.defer(delay, scope, args || []);
            }else{
                cb.apply(scope, args || []);
            }
        }
    },
	gW : function() {
		if (typeof (window.innerWidth) == "number") {
			return window.innerWidth;
		} else {
			if (document.documentElement
					&& (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
				return document.documentElement.clientWidth;
			} else {
				if (document.body
						&& (document.body.clientWidth || document.body.clientHeight)) {
					return document.body.clientWidth;
				}
			}
		}
	},
	gH : function() {
		if (typeof (window.innerHeight) == "number") {
			return window.innerHeight;
		} else {
			if (document.documentElement
					&& (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
				return document.documentElement.clientHeight;
			} else {
				if (document.body
						&& (document.body.clientWidth || document.body.clientHeight)) {
					return document.body.clientHeight;
				}
			}
		}
	},
	crEl : function(tagName, className, id) {
		var temp = document.createElement(tagName);
		if (className)
			temp.className = className;
		if (id)
			temp.id = id;
		return temp;
	},
	getTop : function(elm) {
		var yPos = elm.offsetTop;
		tmp = elm.offsetParent;
		while (tmp != null) {
			yPos += tmp.offsetTop;
			tmp = tmp.offsetParent;
		}
		return yPos;

	},
	removeChildren : function(fromNode) {
		if (fromNode === undefined || fromNode == null) {
			return;
		}
		while (fromNode.hasChildNodes()) {
			fromNode.removeChild(fromNode.firstChild);
		}
	}, 
	parseNumber : function(value) {
		var r = parseInt(value);
		if (isNaN(r))
			return 0;
		return r;
	},
	importXML : function()
	{
		var xmlDoc;
		if (window.ActiveXObject)
			xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
		else if (document.implementation) 
			xmlDoc=document.implementation.createDocument("","",null);
		else
			return;
		xmlDoc.async="false";
		return xmlDoc;
	}, 
	getElById : function(inEl, id) {
		if (!inEl || !inEl.childNodes)
			return null;
		for (var i = 0; i < inEl.childNodes.length; i++) {
			if (inEl.childNodes[i].hasAttributes && !inEl.childNodes[i].hasAttributes())
				continue;
			//log(i + " " + inEl.childNodes[i] + " -- " + inEl.childNodes[i].getAttribute("id"));
			if (inEl.childNodes[i].getAttribute("id") == id)
				return inEl.childNodes[i];
		}
		return null;
	},
	getElementsByClassName : function(cn, parentToSearch) {
	    var result = new Array();
	    parentToSearch = parentToSearch ? document.getElementById(parentToSearch) : document;
	    var listToExamine = parentToSearch.getElementsByTagName("*");
	    for (var i = 0; i < listToExamine.length; i++) {
	        if (listToExamine[i].className === cn) {
	            result.push(listToExamine[i]);
	        }
	    }
	    return result;
	}
};
var util = new Util();

var WIDTH_STEP = 40;
var HEIGHT_STEP = 28;
var loadingImage;
var previewTimer;
var overlay = document.getElementById("overlay");
var preview = document.getElementById("preview");
var previewImage = document.getElementById("preview-image");

function showPreview(img) {
	if (!img.image) {
		img.image = new Image();
		img.image.src = img.src;
	}
	var image = img.image;
	if (previewTimer)
		return;
	util.removeChildren(previewImage);
	previewImage.appendChild(image);
	overlay.style.display = "block";
	preview.style.width = "0px";
	preview.style.height = "0px";
	preview.style.display = "block";
	preview.style.top = parseInt(util.gH() / 2) + "px";
	preview.style.left = parseInt(util.gW() / 2) + "px";
	preview.op = 1;
	if (util.isIE) 
		preview.style.filter = "alpha(opacity=100)";
	else 
		preview.style.opacity = preview.op;
	previewTimer = window.setInterval("displayPreview()", 15);
}
function displayPreview() {
	preview.style.width = (util.parseNumber(preview.style.width) + WIDTH_STEP) + "px";
	preview.style.height = (util.parseNumber(preview.style.height) + HEIGHT_STEP) + "px";
	preview.style.left = (util.parseNumber(preview.style.left) - 20) + "px";
	preview.style.top = (util.parseNumber(preview.style.top) - 15) + "px";
	if (util.parseNumber(preview.style.width) >= 660) {
		window.clearInterval(previewTimer);
		previewTimer = null;
		preview.style.width = "660px";
		preview.style.height = "460px";
	}
}
function fixImage() {
	previewImage.removeChild(loadingImage);
}
function hidePreview() {
	if ((overlay.style.display == "none") || (previewTimer))
		return;
	overlay.style.display = "none";
	preview.style.display = "none";
}
function init() {
	if (util.isIE) {
		overlay.style.filter = "alpha(opacity=55)";
	}
	else {
		overlay.style.opacity = 0.55;
	}
	if (document.getElementById("img1")) {
	  document.getElementById("img1").onclick = function(){showPreview(document.getElementById("img1"));};
	  document.getElementById("img2").onclick = function(){showPreview(document.getElementById("img2"));};
	  document.getElementById("img3").onclick = function(){showPreview(document.getElementById("img3"));};
	  document.getElementById("img4").onclick = function(){showPreview(document.getElementById("img4"));};
	}
}
document.onclick = hidePreview;
window.onload = init;
