String.prototype.escapeHTML = function(quotes) {
	if (quotes) this.replace(/'/g,'&#39;').replace(/"/g,'&38;');
	return this.replace(/&/g,'&amp;').replace(/</g,'&lt;').replace(/>/g,'&gt;');
};

String.prototype.unescapeHTML = function() {
	return this.replace(/&amp;/g,'&').replace(/&lt;/g,'<').replace(/&gt;/g,'>');
};

Array.prototype.deobfuscate = function() {
	var res = '';
	for(var i = 0; i < this.length; i++) res += String.fromCharCode(this[i] ^ (i*i % 13));
	return res;
};

function setCookie(name, value, expires, path, domain, secure) {
  var curCookie = name + "=" + escape(value) +
      ((expires) ? "; expires=" + expires.toGMTString() : "") +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      ((secure) ? "; secure" : "");
  document.cookie = curCookie;
}

function getCookie(name) {
  var dc = document.cookie;
  var prefix = name + "=";
  var begin = dc.indexOf("; " + prefix);
  if (begin == -1) {
    begin = dc.indexOf(prefix);
    if (begin != 0) return null;
  } else
    begin += 2;
  var end = document.cookie.indexOf(";", begin);
  if (end == -1)
    end = dc.length;
  return unescape(dc.substring(begin + prefix.length, end));
}

function deleteCookie(name, path, domain) {
  if (getCookie(name)) {
    document.cookie = name + "=" +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
}

function getWindowSize() {
	return {
		w: window.innerWidth != null? window.innerWidth: document.documentElement && document.documentElement.clientWidth ? document.documentElement.clientWidth:document.body != null ? document.body.clientWidth:null,
		h: window.innerHeight != null? window.innerHeight: document.documentElement && document.documentElement.clientHeight ? document.documentElement.clientHeight:document.body != null? document.body.clientHeight:null
	}
}

function getPageSize() {
    var D = document;
    return {
		w: Math.max(
	        Math.max(D.body.scrollWidth, D.documentElement.scrollWidth),
	        Math.max(D.body.offsetWidth, D.documentElement.offsetWidth),
	        Math.max(D.body.clientWidth, D.documentElement.clientWidth)
		),
		h: Math.max(
	        Math.max(D.body.scrollHeight, D.documentElement.scrollHeight),
	        Math.max(D.body.offsetHeight, D.documentElement.offsetHeight),
	        Math.max(D.body.clientHeight, D.documentElement.clientHeight)
		)
	};
}

function preloadImage(src) {
    setTimeout(function() {
        var i = new Image;
        i.src = src;
        preloadedImages[src] = i;
    }, 5);
}

function registerAfterPreloadHandler(fn, delay) {
	var tmId = setInterval(function() {
		var finished = true;
		for(var i in preloadedImages) 
			if (typeof(preloadedImages[i]) == 'object' && typeof(preloadedImages[i].complete) != 'undefined')
				if (!preloadedImages[i].complete) finished = false;
		if (finished) {
			clearInterval(tmId);
			if (typeof(fn) == 'function') {
				if (delay) 
					setTimeout(fn, delay);
				else
					fn();
			}
		}
	}, 100);
}

$.fn.getImageSrc = function() {
	if (this.attr('src')) {
		return this.attr('src');
	} else {
		return this.css('filter').match(/src='(.+?)'/)[1];
	}
};

$.fn.replaceImageSrc = function(fn) {
	if (this.attr('src')) {
		this.attr('src', fn(this.attr('src')));
	} else {
		this.css('filter', this.css('filter').replace(/src='.+?'/, 'src=\'' + fn(this.css('filter').match(/src='(.+?)'/)[1]) + '\''));
	}
	
	return this;
};

$.fn.highlightImage = function(size, speed, fn) {

	if (!this.length) return this;
	
	var i = this.get(0)._initial;
	
	if (i) {
		this.replaceImageSrc(function(src) {
			return src.replace(/-\d+\.png$/, '-' + size + '.png');
		});
		var css = {
			top: i.top - (size - i.height)/2,
			left: i.left - (size - i.width)/2,
			width: size, 
			height: size
		};
		if (speed) {
			this.animate(css, speed, fn);
		} else {
			this.css(css, fn);
		}
	}
	
	return this;
};

$.fn.dehighlightImage = function(speed, fn) {

	if (!this.length) return this;
	
	var i = this.get(0)._initial;
	
	if (i) {
		this.replaceImageSrc(function(src) {
			return src.replace(/-\d+\.png$/, '-' + i.width + '.png');
		});
		var css = {
			top: i.top,
			left: i.left,
			width: i.width, 
			height: i.height
		};
		if (speed) {
			this.animate(css, speed, fn);
		} else {
			this.css(css, fn);
		}
	}
	
	return this;
};

$.fn.colorize = function() {
	this.replaceImageSrc(function(src) {
		return src.replace(/-gs(\.png)$/, '$1');
	});
};

$.fn.uncolorize = function() {
	this.replaceImageSrc(function(src) {
		return src.match(/-gs\.png$/) ? src : src.replace(/(\.png)$/, '-gs$1');
	});
};

$.fn.transparencyFixStart = function() {
	if ($.browser.msie && parseInt($.browser.version) >= 7) this.addClass('transparency-fix')
		.css('backgroundPosition', (-110 - this.position().left - $('#content-left').position().left) + 'px ' + (- this.offset().top) + 'px');
	return this;
};

$.fn.transparencyFixEnd = function() {
	if ($.browser.msie && parseInt($.browser.version) >= 7) { 
		this.css({opacity: ''}).removeClass('transparency-fix');
	}
	return this;
};

var IMAGE_SIZE_SMALL = 60,
    IMAGE_SIZE_HL    = 70,
    IMAGE_SIZE_LARGE = 350;
	
	
function switchStyle(style) {
	if (style != currentStyle) {
		setCookie('style', style);
		$('body').removeClass().addClass('wr' + style);
		currentStyle = style;
	}
	return false;
}

function addToFavorites(el) {
	var rv = false;
	try {
		if(window.sidebar){
			window.sidebar.addPanel(document.title, 'http://'+location.host+location.pathname+location.hash, '');
		} else if(window.external){
			window.external.AddFavorite('http://'+location.host+location.pathname+location.hash, document.title);
		} else if(window.opera && window.print){
			el.setAttribute('href', 'http://'+location.host+location.pathname+location.hash);
			el.setAttribute('title', document.title);
			el.click();
			rv = true;
		}
		$.get('/favorite.html?page='+escape(location.hash));
	} catch(e) {}
	return rv;
}

function adjustVerticalPosition() {
	$('#content-left, #content-right').stop();
	$('#content').css('height', 'auto');
	var fh = $('#content-basement').outerHeight();
	var rh = getPageSize().h - $('#header').outerHeight() - parseInt($('#content').css('paddingTop')) - parseInt($('#content').css('paddingBottom')) - fh;
	if ($('#content-right').outerHeight() < rh) {
		//$('#content').css('height', rh + fh);
		$('#content-right').css({top: (rh - $('#content-right').outerHeight() ) / 3});
	} else {
		//$('#content').css('height', $('#content-right').outerHeight() + fh);
		$('#content-right').css('top', 0);
	}
	
	$('#content-basement').css('bottom', 0);
}
