(function($){

/********************************************* GALLERY SETTINGS ***********/
//TIME
var slideChangeTime = 4000;
var clickDelay = 2000;
var slideFadeTime = 300;
var descrFadeTime = 500;
var descrChangeTime = slideChangeTime-2*descrFadeTime-slideFadeTime;
//Default ID
var boxID = '#hGallery';
/********************************************* GALLERY SETTINGS ***********/



var current = 0;
var imagesQ = 0;
var loadedQ  = 0;
var tmout = 0;
var tmout2 = 0;
var tmout3 = 0;
var height = 0;
var width = 0;
var dHeight = 0;
var dWidth = 0;

   qtGallery_galleryPreload =  function (){
        imagesQ = $(boxID+' img').length;
        $(boxID).append('<div id="preload">'+loadedQ+' images loaded from '+imagesQ+'</div>');
        $(boxID+' img').each(function(el){
          var oldImg = $(this);
          var newImg = new Image();
          var imgSrc = oldImg.attr('src');
           if(oldImg.get(0).complete){
              loadedQ++;
            } else {
               newImg.onload = qtGallery_checkLoaded;
               newImg.src = imgSrc;
            }
        });
        if(imagesQ==loadedQ) qtGallery_checkLoaded();
    }

    qtGallery_checkLoaded = function (){
        if(imagesQ!=loadedQ) loadedQ++;
        $('#preload').html(loadedQ+' images loaded from '+imagesQ);
        if(imagesQ==loadedQ){
            $('#preload').fadeOut(function(){
                $(this).remove();
            });
            qtGallery_firstStart();
        }
    }

    qtGallery_firstStart = function (){
    height = $(boxID).height();
    width = $(boxID).width();

        $('#hBoxes').append('<div id="triangle"></div>');
        $(boxID+' div.gItem').fadeOut(0);
        $(boxID+' div.gItem:first').fadeTo(slideFadeTime,1,function(){
/*                    sn = 1;
                //    qtGallery_animateTriangle(0);
				//	$('#triangle').show();
                    $(boxID+' div.gItem').each(function(el){
                    sn++;
                });
*/
/*                    $('#hBoxes .hBox:first').addClass('active');
                    $('.hBox').click(function(el){
                    n = $(this).index();
                    qtGallery_changer(n,1);
                    });
*/					
            invl  = setInterval( "qtGallery_clicker()", slideChangeTime );
        });
        
    }


    qtGallery_changer = function (n,p){
        current = n;
	if(p){
            clearInterval(invl);
            if(tmout){
              clearTimeout(tmout);
            }
            tmout = setTimeout(function(){

              invl  = setInterval( "qtGallery_clicker()", slideChangeTime );

            }, clickDelay);
	}

/* FADE ALL VARIANT
	if(!$('#hBoxes .hBox:eq('+current+')').hasClass('active')){
         $(boxID+' .gItem').stop().css('display','none');
         $(boxID+' div.active:not(.hBox)').fadeTo(slideFadeTime,0);
		 $(boxID+' div.active').removeClass('active');
		 $(boxID+' div.gItem:eq('+current+')').fadeTo(slideFadeTime,1).addClass('active');
		 $(boxID+' .hBox:eq('+current+')').addClass('active');
         qtGallery_animateTriangle(current,1);
	  }  
	}
*/

if(!$('#hBoxes .hBox:eq('+current+')').hasClass('active')){
		$(boxID+' .gItem').stop();
		$(boxID+' div.active:not(.hBox)').addClass('last-active');
		$(boxID+' div.gItem:eq('+current+')').fadeTo(0,0).addClass('active').fadeTo(slideFadeTime,1,function(){
			$('.last-active').removeClass('active last-active');
		});
	/*	$(boxID+' .hBox.active').removeClass('active');
		$(boxID+' .hBox:eq('+current+')').addClass('active');
		qtGallery_animateTriangle(current,1);
	*/	
	}  
}



	qtGallery_clicker = function () {
	  if(current==$('#hBoxes .hBox:last').index()){
	  	  current = 0;
	  } else {
		 current++;
	  }
	  qtGallery_changer(current,0);
	}

        qtGallery_animateTriangle = function (number,p){
        if(p){
          descTime = descrChangeTime + clickDelay;
        } else {
          descTime = descrChangeTime;
        }
            elm = $(boxID+' .hBox:eq('+number+')');
            var offset = elm.position();
			var offsetTop = offset.top + elm.height()/2 - 4;
			$('#triangle').stop().animate({'top': offsetTop},slideFadeTime);             
   }

    qtGallery_init = function(boxID){
        qtGallery_galleryPreload();
    }


  $(document).ready(function() {
	qtGallery_init();
  });

})( jQuery );

 

    
