jQuery(document).ready(function(){
  $('a#zoomer').click(function(){
    ShowImage($(this).attr('href'));
    return false;
  });
});

function ShowImage(src){
  if(src){
    Loading(src);
    t_image = new Image();
    t_image.src = src; 
    if(t_image.width>0 && t_image.height){
        var width  = t_image.width;
        var height = t_image.height;
        LoadImage(src,width,height);
    }
    t_image.onload = function mm(){
        var width  = t_image.width;
        var height = t_image.height;
        LoadImage(src,width,height);
    }
    return false;  
  }else{
    return true;
  }
  
}


function LoadImage(url,w,h){
  var page_size = getPageSize();
  var max_width  =  (page_size[2]*80)/100;
  var max_height =  (page_size[3]*80)/100;
	var nw = w;
	var nh = h; 

	if(max_width > 0) {
		 if (w > max_width) {
			 nw = max_width;
			 nh = (h / (w / max_width));
		 }
		 if (max_height > 0 && nh > max_height) {
			 nw = (nw / (nh / max_height));
			 nh = max_height;
		 }
	}else if (max_height > 0) {
	   if(h > max_height) {
			 nh = max_height;
			 nw = (w / (h / max_height));
		 }			
	}
	  
    var marginLeft = Math.max(40, parseInt($(window).width()/2 - nw/2));
    var marginTop  = Math.max(40, parseInt(($(window).height()/2 - nh/2)+$(document).scrollTop()));

    $('#ShowImage-Conteiner img#loader').remove();
    $('#ShowImage-Conteiner').animate({
          'width': nw+'px','height': nh+'px',
          'left': marginLeft+'px','top': marginTop+'px'
    },function(){
    
        $('#ShowImage-inner').html('<div class="ShowImage"><img src="'+url+'" width='+nw+' height="'+nh+'" border="0"></div>');
        $('#close').show();
        $('#full').show();
    });
  
}

function Loading(url){
    var page_size = getPageSize();
    var shadow = $('<div id="ShowImage-shadow"></div>');
    var full   = $('<a id="full" href="'+url+'" target="_blank"><img src="/skin/images/zoomer/sprite.png" border="0"></a>');
    var close  = $('<a id="close"><img src="/skin/images/zoomer/close.png" border="0"></a>');
    
    $(shadow).mousedown(function(){
        var marginLeft = Math.max(40, parseInt($(window).width()/2));
        var marginTop  = Math.max(40, parseInt(($(window).height()/2)+$(document).scrollTop()));
        $('#ShowImage-inner').html('');
        $('#ShowImage-Conteiner').animate({
            'width': '0px',
            'height': '0px',
            'left': marginLeft+'px','top': marginTop+'px'
        },function(){
          $('#ShowImage-Conteiner').remove();
          $('#ShowImage-shadow').remove();
        });
    }); 
                                 
    $(close).mousedown(function(){
        var marginLeft = Math.max(40, parseInt($(window).width()/2));
        var marginTop  = Math.max(40, parseInt(($(window).height()/2)+$(document).scrollTop()));
        $('#ShowImage-inner').html('');
        $('#ShowImage-Conteiner').animate({
            'width': '0px',
            'height': '0px',
            'left': marginLeft+'px','top': marginTop+'px'
        },function(){
          $('#ShowImage-Conteiner').remove();
          $('#ShowImage-shadow').remove();
        });
    });
    
    $('body').append(shadow);
    $('#ShowImage-shadow').css({'width': '100%','height': page_size[1]+'px','position': 'absolute','opacity':'0.5'});
    $('body').append('<div id="ShowImage-Conteiner"><div id="ShowImage-inner"><img id="loader" src="/skin/images/zoomer/loader.gif" border="0"></div></div>');
    $('#ShowImage-Conteiner').append(close);
    $('#ShowImage-Conteiner').append(full);
    var marginLeft = Math.max(40, parseInt($(window).width()/2 - $('#ShowImage-Conteiner').width()/2));
    var marginTop  = Math.max(40, parseInt(($(window).height()/2 - $('#ShowImage-Conteiner').height()/2)+$(document).scrollTop()));
    $('#ShowImage-Conteiner').css({'left': marginLeft+'px','top': marginTop+'px'});
}
function  getPageSize(){
       var xScroll, yScroll;

       if (window.innerHeight && window.scrollMaxY) {
                  xScroll = document.body.scrollWidth;
                  yScroll = window.innerHeight + window.scrollMaxY;
       } else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
                  xScroll = document.body.scrollWidth;
                  yScroll = document.body.scrollHeight;
       } else if (document.documentElement && document.documentElement.scrollHeight > document.documentElement.offsetHeight){ // Explorer 6 strict mode
                  xScroll = document.documentElement.scrollWidth;
                  yScroll = document.documentElement.scrollHeight;
       } else { // Explorer Mac...would also work in Mozilla and Safari
                  xScroll = document.body.offsetWidth;
                  yScroll = document.body.offsetHeight;
       }
 

       var windowWidth, windowHeight;
       if (self.innerHeight) { // all except Explorer
                 windowWidth = self.innerWidth;
                 windowHeight = self.innerHeight;
       } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
                 windowWidth = document.documentElement.clientWidth;
                 windowHeight = document.documentElement.clientHeight;
       } else if (document.body) { // other Explorers
                 windowWidth = document.body.clientWidth;
                 windowHeight = document.body.clientHeight;
       }
  
  
       // for small pages with total height less then height of the viewport
       if(yScroll < windowHeight){
          pageHeight = windowHeight;
       } else {
          pageHeight = yScroll;
       }

       // for small pages with total width less then width of the viewport
       if(xScroll < windowWidth){
                 pageWidth = windowWidth;
       } else {
                 pageWidth = xScroll;
       }

       return [pageWidth,pageHeight,windowWidth,windowHeight];
}
