$(document).ready( function() {
	testCode('#test-code');
});

function numberFormat(a, b, c, d) {
 a = Math.round(a * Math.pow(10, b)) / Math.pow(10, b);
 e = a + '';
 f = e.split('.');
 if (!f[0]) {
  f[0] = '0';
 }
 if (!f[1]) {
  f[1] = '';
 }
 if (f[1].length < b) {
  g = f[1];
  for (i=f[1].length + 1; i <= b; i++) {
   g += '0';
  }
  f[1] = g;
 }
 if(d != '' && f[0].length > 3) {
  h = f[0];
  f[0] = '';
  for(j = 3; j < h.length; j+=3) {
   i = h.slice(h.length - j, h.length - j + 3);
   f[0] = d + i +  f[0] + '';
  }
  j = h.substr(0, (h.length % 3 == 0) ? 3 : (h.length % 3));
  f[0] = j + f[0];
 }
 c = (b <= 0) ? '' : c;
 return f[0] + c + f[1];
}

function testCode(s) {
	var $code = jQuery(s);
	
	if ( $code.length ) {
		var $val = $code.find('.value');
		var code = new Number( $val.text() );
		
		$val.text( numberFormat(code, 0, ',', ' ') );
	}
}

 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 - 747)/2;
//    d.getElementById('popup_viewver').style.top = (top_correct+getBodyScrollTop())+'px';
    
    d.getElementById('popup_viewver').style.top = getBodyScrollTop() + 50 + '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() + 50 + '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';
    
    $('.b-adsBanner').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');
	}
	
    $('.b-adsBanner').css( 'visibility', 'visible' );
}

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-300;
    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;

