//var $J = jQuery.noConflict();
jQuery(document).ready(function(){
  $('a#showimage').click(function(){
    ShowImage($(this).attr('href'));
    return false;
  });
  $('img#ShowImage').click(function(){
    ShowImage($(this).attr('href'));
    return false;
  });
  
/* === */
// временные скрипты для наглядности верстки
	$('#menu a').live('click',function(){
		$(this).next('ul').show();
	});
	$('#menu a.active').next('ul').show();
	$('ul#menu ul li a.active').parent().parent().css('display','block');
	$('ul#menu ul li a.active').parent().parent().parent().find('#void').addClass('active')
/* === */

// управление презентацией
	i = 1; n = $('#slides_view img').length;
	var intervalID;
	intervalID = setInterval('slideshow()', 3000);
	$('#pause').toggle(function(){
		clearInterval(intervalID);
		$('img', this).attr({src: '/data/img/play.gif', alt: 'Play'});
	},function(){
		intervalID = setInterval('slideshow()', 3000);
		$('img', this).attr({src: '/data/img/pause.gif', alt: 'Pause'});
	});
	$('#prev').click(function(){
		clearInterval(intervalID);
		$('#pause img').attr({src: '/data/img/play.gif', alt: 'Play'});
		if (i > 1){
			$('#slides_view img.active').fadeOut().removeClass('active').prev('img').addClass('active').fadeIn();
			i--;
		} else {
			$('#slides_view img.active').fadeOut().removeClass('active');
			$('#slides_view img:last').addClass('active').fadeIn();
			i = n;
		}
	});
	$('#next').click(function(){
		clearInterval(intervalID);
		$('#pause img').attr({src: '/data/img/play.gif', alt: 'Play'});
		if (i < n){
			$('#slides_view img.active').fadeOut().removeClass('active').next('img').addClass('active').fadeIn();
			i++;
		} else {
			$('#slides_view img.active').fadeOut().removeClass('active');
			$('#slides_view img:first').addClass('active').fadeIn();
			i = 1;
		}
	});
	
	
});

$(window).resize(function(){
	
});

slideshow = function(){
	$('#slides_view img.active').each(function(){
		if (i < n){
			$(this).fadeOut().removeClass('active').next('img').addClass('active').fadeIn();
			i++;
		} else {
			$(this).fadeOut().removeClass('active');
			$('#slides_view img:first').addClass('active').fadeIn();
			i = 1;
		}
	});
}

BigImage = function(opt){
  alert(opt.max_height);
  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 max_width  = ($('body').width()-100);
  var max_height = ($('body').height()-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="/data/img/showimage/sprite.png" border="0"></a>');
    var close  = $('<a id="close"><img src="/data/img/showimage/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-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-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="/data/img/showimage/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];
}
