var VinnetteLoaded;
var gallery;
var SlimScrollInitiated;
var rtime;
var timeout = false;
var delta = 1000;
ImageRatio=0.75

$(window).load(function() {
	if(ww<768){
		BoxHeight=Math.round(ww/ImageRatio,0);
		$('#GalleryContainer').css('height',BoxHeight+'px');
	}
	
	LoadHPGallery();
	
	if(MobileState==true){
		$('.galleria-stage').live('click', function(event){
			gallery.pause();
			$('#Gallery'+GalleryCount).trigger('click')
		});
	}else{
		$('#GalleryBackground img').live('mouseup', function(event){
			gallery.pause();
			$('#Gallery'+GalleryCount).trigger('click')
		});
	}
	
	$('div.galleria-image-nav').live('click', function(event){
		gallery.pause();
	});
	
	
	
	
	
});

$(window).resize(function(){
	ww=$(window).width();
		if(ww<1024){
			BoxHeight=Math.round(ww/ImageRatio,0);
			$('#GalleryContainer').css('height',BoxHeight+'px');
	    }else{
		   $('#GalleryContainer').css('height','579px');
	    }
	
});
function LoadHPGallery() {
	FinishesSEO=jQuery('#seo').val();
	Data={'seo':FinishesSEO};
	$.ajax({
		type: 'POST',
		url: 'includes/RetreiveProductMedia.php',
		async:true,
		data: Data,
		success: function(results) {
			data = $.parseJSON(results);
			var DataLen=eval(data.length-1);
			PCount=data[DataLen]['GalleryCount']
			InitiliaseHPGallery(data,PCount);
		}
	});
}

function InitiliaseHPGallery(data,PCount){
    Galleria.loadTheme('js/galleria.classic.min.js');
    Galleria.run('#GalleryBackground', {
		dataSource: data,
		extend: function(options) {

			
			gallery = this;
			this.bind('loadstart', function(e) {
				var current = gallery.getData(gallery.getIndex());
				if(PCount==1){
					$('.ProjectNav').css('display','none');
				}
				if(!VinnetteLoaded){
					var GalleryCount=data.length;
					VinnetteLoaded='Loaded';
				}
				
				
			});
			this.bind('loadfinish', function(e) {
				current = gallery.getData(gallery.getIndex());
				var Caption=current.caption;
				ImgId=current.id;
				GalleryCount=current.GalleryCount;
				$('.MagPanel').css({'display': 'block'});
				$('#GalleryBackground').css('cursor','pointer');
				var GalleryHeight=$('#GalleryContainer').height();
				$('.stack-top.left').css('visibility','visible');
				
			});
			
            this.$('stage').click(function() {
            });
		}
    });
	if(MobileState==false){
		Autoplay=true;
	}else{
		Autoplay=false;
	}

	if(PCount==1){
		GalleryNav=false;
	}else{
		GalleryNav=true;
	}
    SpeedofTransition=3000;
	PositionOfImage='center';
	Galleria.configure({
		thumbnails: "empty",
		showCounter: false,
		showInfo: false,
		imageCrop: true,
		responsive: true,
		//swipe: 'disabled',
		autoplay: Autoplay,
		preload: 2,
		imageMargin: 0,
		transitionSpeed: 3000,
		transition: 'fade',
		touchTransition: 'fade',
		idleMode: false,
		imageTimeout: 5000,
		showImagenav: GalleryNav,
		imagePosition: 'center',
		pauseOnInteraction: false,
		modestbranding: 1
	});    
	
}//Endof Homepage Gallery

