// Written by Jonathan Yarbor of Blazed Designs
// http://blazed-designs.com
// View FormClear Homepage: http://blog.blazed-designs.com/2009/06/12/jquery-plugin-clearform/
// Jquery plugin
// Follow me on twitter @blazedd

(function($){$.fn.formclear = function(opts){var defaults = {inactivecolor: '#586774',activecolor: '#586774',emptyval: ""  };var opts = $.extend({}, defaults, opts);return this.each(function() {var obj = $(this);  var normalval;var allowedtext = $(this).is(':text') || $(this).is(':password') || $(this).is('textarea');if(allowedtext){var normalval = obj.val();obj.css('color',opts.inactivecolor);}obj.bind('click',function(){if(allowedtext){if(obj.val()==normalval){obj.val(opts.emptyval);obj.css('color',opts.activecolor);}}});obj.bind('blur',function(){if(allowedtext){if(obj.val()==opts.emptyval || !$.trim(obj.val()).length){obj.val(normalval);obj.css('color',opts.inactivecolor);}}});});};})(jQuery);

//////////////////////////

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;
    
    d.getElementById('popup_viewver').style.top = getBodyScrollTop() + 30 + 'px';
}

function showImageViewer( iid )
{
     var d = document;
    //var top_correct = (d.getElementById('html').clientHeight - 747)/2;
    SetImg(iid, true);
   // d.getElementById('popup_viewver').style.top = (top_correct+getBodyScrollTop())+'px';
    d.getElementById('popup_viewver').style.top = getBodyScrollTop() + 30 + 'px';
    d.getElementById('popup_viewver').style.display = 'block';

	if ( $.browser.msie && 6 >= $.browser.version ) {
	   	$('form select').css('visibility', 'hidden');
	}
	
    d.getElementById('screen').style.height = getyScroll() + 'px'; 
    d.getElementById('screen').style.display = 'block';
    
    $('#sel_1, #sel_2, #sel_3').css( 'visibility', 'hidden' );
    $('.ads, .top-ads').css( 'visibility', 'hidden' );
}

function closeImageViewer()
{
   	var d = document;
    d.getElementById('popup_viewver').style.display = 'none';
    d.getElementById('screen').style.display = 'none';
    
    if ( $.browser.msie && 6 >= $.browser.version ) {
	   	$('form select').css('visibility', 'visible');
	}
    
    $('#sel_1, #sel_2, #sel_3').css( 'visibility', 'visible' );
    $('.ads, .top-ads').css( 'visibility', 'visible' );
}

function showCompareList()
{
    var d = document;
    d.getElementById('screen').style.height = getyScroll() + 'px'; 
    d.getElementById('screen').style.display = 'block';
    d.getElementById('popup_compare').style.display = 'block';
}

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}; 
}


function SetImg(iid,change)
{
    w=screenSize().w-300;
    h=screenSize().h-200;
    for (i=0;i<Imagelist.length;i++) {if (Imagelist[i]==iid) curImg=i;};
    document.getElementById('link_full').href=FullUrllist[curImg];
    document.getElementById('link_full_img').href=FullUrllist[curImg];
    document.getElementById('largeimg').src = "/images/preloader.gif";
	document.getElementById('largeimg').src=getImgURL(iid,w,h);
}
function SetImgOld(iid,change)
{
    w=screenSize().w-300;
    h=screenSize().h-200;
    for (i=0;i<Imagelist.length;i++) {if (Imagelist[i]==iid) curImg=i;};
    document.getElementById('link_full').href=FullUrllist[curImg];
    document.getElementById('link_full_img').href=FullUrllist[curImg];
    document.getElementById('largeimg').src = "/images/preloader.gif";
    new $.ajax({
    	url: '/ajax.php?mode=getimgurl&dw=' + w + '&dh=' + h + '&iid=' + iid + '&key=1',
        success: function(data)
        {
          document.getElementById('largeimg').src = data || "/images/nofoto.gif";
        },
        error: function()
        { document.getElementById('laregimg').src="/images/img_err.gif"; 
        }
    });     
}



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;


