var Base64 = {
	// private property
	_keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",

	// public method for encoding
	encode : function (input) {
		var output = "";
		var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
		var i = 0;

//		input = Base64._utf8_encode(input);

		while (i < input.length) {

			chr1 = input.charCodeAt(i++);
			chr2 = input.charCodeAt(i++);
			chr3 = input.charCodeAt(i++);

			enc1 = chr1 >> 2;
			enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
			enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
			enc4 = chr3 & 63;

			if (isNaN(chr2)) {
				enc3 = enc4 = 64;
			} else if (isNaN(chr3)) {
				enc4 = 64;
			}

			output = output +
			this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) +
			this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);

		}

		return output;
	},

	// public method for decoding
	decode : function (input) {
		var output = "";
		var chr1, chr2, chr3;
		var enc1, enc2, enc3, enc4;
		var i = 0;

		input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");

		while (i < input.length) {

			enc1 = this._keyStr.indexOf(input.charAt(i++));
			enc2 = this._keyStr.indexOf(input.charAt(i++));
			enc3 = this._keyStr.indexOf(input.charAt(i++));
			enc4 = this._keyStr.indexOf(input.charAt(i++));

			chr1 = (enc1 << 2) | (enc2 >> 4);
			chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
			chr3 = ((enc3 & 3) << 6) | enc4;

			output = output + String.fromCharCode(chr1);

			if (enc3 != 64) {
				output = output + String.fromCharCode(chr2);
			}
			if (enc4 != 64) {
				output = output + String.fromCharCode(chr3);
			}

		}

		output = Base64._utf8_decode(output);

		return output;

	}

}
function trimb64 (str) {
	str = str.replace(/=+/, '');
	for (var i = str.length - 1; i >= 0; i--) {
		if (/^=/.test(str.charAt(i))) {
			str = str.substring(0, i + 1);
			break;
		}
	}
	return str;
}
function getImgURL(iid,w,h)
{
	var paramstr='I'+iid+'TiW'+(Math.max(Math.round(Math.min(w,800)/40)*39,100))+'H'+(Math.max(Math.round(h/40)*39,100))+'Cy';
	paramstr=trimb64(Base64.encode(paramstr));
	return('/gallery/img_'+paramstr+'.jpg');
}


 function getBodyScrollTop()
 {
    return self.pageYOffset || (document.documentElement && document.documentElement.scrollTop) || (document.body && document.body.scrollTop);
 }

 function getyScroll()
 {
  yScroll = 0;

  if (window.innerHeight && window.scrollMaxY || window.innerWidth && window.scrollMaxX)
   {
    yScroll = window.innerHeight + window.scrollMaxY;
    xScroll = window.innerWidth + window.scrollMaxX;

    var deff = document.documentElement;
    var wff = (deff&&deff.clientWidth) || document.body.clientWidth || window.innerWidth || self.innerWidth;
    var hff = (deff&&deff.clientHeight) || document.body.clientHeight || window.innerHeight || self.innerHeight;

    xScroll -= (window.innerWidth - wff);
    yScroll -= (window.innerHeight - hff);
   } 
  else if (document.body.scrollHeight > document.body.offsetHeight || document.body.scrollWidth > document.body.offsetWidth)
   { // all but Explorer Mac
    yScroll = document.body.scrollHeight;
    xScroll = document.body.scrollWidth;
   } 
  else 
   { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
    yScroll = document.body.offsetHeight;
    xScroll = document.body.offsetWidth;
   }

  return yScroll;
 }


function pageScroll()
{
    var d = document;
    var top_correct = (d.getElementById('html').clientHeight - 640)/2;
    d.getElementById('popup_viewver').style.top = (top_correct+getBodyScrollTop())+'px';
}



function SetImg(iid,change)
{
    for (i=0;i<Imagelist.length;i++)
    {
        if (Imagelist[i]==iid)
        {
            document.getElementById('link_full').href=FullUrllist[i];
            break;
        };
    }
    w=screenSize().w-300;
    h=screenSize().h-300;
    if (!change) document.getElementById('largeimg').src = "/images/preloader.gif";
	var url=getImgURL(iid,w,h);
	document.getElementById('largeimg').src=url;
}

function SetImgOld(iid,change)
{
    for (i=0;i<Imagelist.length;i++)
    {
        if (Imagelist[i]==iid)
        {
            document.getElementById('link_full').href=FullUrllist[i];
            break;
        };
    }
    w=screenSize().w-300;
    h=screenSize().h-300;
    if (!change) document.getElementById('largeimg').src = "/images/preloader.gif";
    new Ajax.Request('/ajax.php?mode=getimgurl&dw='+w+'&dh='+h+'&iid='+iid+'&key=1',
    {     
        method:'get',
        onSuccess: function(transport)
        {       
          document.getElementById('largeimg').src = transport.responseText || "/images/nofoto.gif";
        },
        onFailure: function()
        { document.getElementById('laregimg').src="/images/img_err.gif"; 
        }
    });     
}


function showImageViewer(iid)
{
    var top_correct = (document.getElementById('html').clientHeight - 640)/2;
    SetImg(iid);
    document.getElementById('popup_viewver').style.top = (top_correct+getBodyScrollTop())+'px';
    document.getElementById('popup_viewver').style.display = 'block';
    document.getElementById('screen').style.height = getyScroll() + 'px'; 
    document.getElementById('screen').style.display = 'block';
}

function closeImageViewer()
{
    var d = document;
    d.getElementById('popup_viewver').style.display = 'none';
    d.getElementById('screen').style.display = 'none';  
}


function hideCompareList()
{
    var d = document;
    d.getElementById('popup_compare').style.display = 'none';
    d.getElementById('screen').style.display = 'none';  
}


function screenSize() {
    var w, h;
    w = (window.innerWidth ? window.innerWidth : (document.documentElement.clientWidth ? document.documentElement.clientWidth : document.body.offsetWidth));
    h = (window.innerHeight ? window.innerHeight : (document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body.offsetHeight));
    return {w:w, h:h}; 
}




if (window.addEventListener) //DOM method for binding an event
    window.addEventListener("scroll", pageScroll, false)
else if (window.attachEvent) //IE exclusive method for binding an event
    window.attachEvent("onscroll", pageScroll)
else if (document.getElementById) //support older modern browsers
    window.onscroll=pageScroll;

