//----------------------------------------
//  zGallery
//----------------------------------------
//  Liniking to JQuery
$.prototype.zGallery = zGallery;

function zGallery(){
   zGallery.itemCount = $(this).length;
  for (i=0;i< zGallery.itemCount;i++){
    zGallery[i] = new $(this).eq(i);
    var id = zGallery[i][0].id;
    zGallery[i].ul = $("#"+ id +" ul:eq(0)");
    zGallery[i].li = zGallery[i].ul.children();
	zGallery[i].$next = $("#"+ id +" .arrow.right.active");
    zGallery[i].$back = $("#"+ id +" .arrow.left.active");
    zGallery[i].liWidth = $(zGallery[i].li[0] ).width();
    zGallery[i].animating = false;
    zGallery[i].speed = 500;
	
    with(zGallery[i]){
      zGallery.zRefresh(i);
      
	  
		if ($("span",li)){li.hover(
			function(){$("span",this).animate({opacity:"1"},250)},
			function(){$("span",this).animate({opacity:"0"},250)})
		};
	  $next.click(function() {
        if ( !animating&&!$next.hasClass("inactive") ) {
          animating = true;
          
          offsetLeft = Math.round((parseInt(ul.css("left")) - liWidth)/liWidth)*liWidth;
  
          if ( offsetLeft + ul.width() > 0 ) {
            ul.animate({
              left: offsetLeft
            }, speed, function() {
              if ( parseInt( ul.css( "left" ) ) + ul.width() <= (liWidth-liWidth) ) {
                ul.animate({
                  left: 0
                }, speed, function() {animating = false;});
              }
              animating = false;
            });
          } else {
            
            ul.animate({
              left: 0
            }, speed, function() {animating = false;});
          }
        };
        if(!$next.hasClass("inactive")){
          if($back.hasClass("inactive")){  
            $back.removeClass('inactive');
            $back.addClass('active');
          };
		  if (Math.round((ul[0].offsetWidth+ul[0].offsetLeft-liWidth)/liWidth)*liWidth==Math.round(ul[0].parentNode.offsetWidth/liWidth)*liWidth){
            $next.removeClass('active');
            $next.addClass('inactive');
          };
          return false;
        };
      });
      $back.click(function() {
        if ( !animating&&!$back.hasClass("inactive") ) {
          animating = true;
          offsetRight = Math.round((parseInt( ul.css( "left" ) ) + liWidth)/liWidth)*liWidth;

		  
          if ( offsetRight + ul.width() <= ul.width() ) {
            ul.animate({
              left: offsetRight
            }, speed, function() {
              if ( parseInt( ul.css( "left" ) ) >0 ) {
                 ul.animate({
                    left: -li.length * liWidth
                   }, speed, function() {animating = false;});
              }
              animating = false;
            });
          } else {
              ul.animate({
                    left: -(li.length * liWidth)
                   }, speed, function() {animating = false;});
          }
        }
        if (!$back.hasClass("inactive")){
          if($next.hasClass("inactive")){  
            $next.removeClass('inactive');
            $next.addClass('active');
          };
         if (Math.round((ul[0].offsetLeft+liWidth)/liWidth)==0){
            $back.removeClass('active');
            $back.addClass('inactive');
          };
          return false;
        };
      });
    };
  };
  
};

zGallery.zRefresh = function zRefresh(i){
  if (zRefresh.arguments) if (typeof zRefresh.arguments[0] == "number"){doRefresh(i)}
  else
  {for (i=0; i < (zGallery.itemCount); i++){doRefresh(i)};};
  function doRefresh(i){
    if (zGallery[i][0].parentNode.style.cssText !== "display: none; "){
      with(zGallery[i]){
		liWidth = li[0].clientWidth;
        ul.css( "width", ( li.length * liWidth ) );
		ul.css("width", li.length * liWidth+(li.length * liWidth - ul[0].scrollWidth)*li.length);
		liWidth = Math.round(ul[0].scrollWidth/li.length);
		ul.css("left",Math.round(parseInt(ul.css("left"))/liWidth)*liWidth);
		$("span",zGallery[i]).css("width",$("img",zGallery[i]).outerWidth()-$(this).outerWidth+$(this).innerWidth);
		$(".zbg",zGallery[i]).css("width",$("img",zGallery[i]).outerWidth());
        if (ul[0].offsetLeft==0){
          $back.removeClass('active');
          $back.addClass('inactive');
        }
        else
        if($back.hasClass("inactive")){  
          $back.removeClass('inactive');
          $back.addClass('active');
        };
        if (ul[0].offsetWidth==ul[0].parentNode.offsetWidth){
          $next.removeClass('active');
          $next.addClass('inactive');
        }
        else
        if($next.hasClass("inactive")){  
          $next.removeClass('inactive');
          $next.addClass('active');
        };
  
      }
    };
  }
  
};

$(window).resize(zGallery.zRefresh);

//------------------------------------------
//  End zGallery
//------------------------------------------
