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

$(window).load(function() {
	if(ww<1024){
		BoxHeight=Math.round(ww/ImageRatio,0);
		$('#GalleryContainer').css('height',BoxHeight+'px');
	}else{
	   $('#GalleryContainer').css('height','579px');
	}
	
	
	LoadHPGallery();
	$('div.ProjectNavLeft').live('click', function(event){
		gallery.prev();
		gallery.pause();
	});
	$('div.ProjectNavRight').live('click', function(event){
		gallery.next();
		gallery.pause();
	});
	
	$('div.galleria-image-nav').live('click', function(event){
		gallery.pause();
	});
	if(MobileState==true){
		$('.ProjectGalleryContainer').live('click', function(event){
			gallery.pause();
			$('#Gallery'+GalleryCount).trigger('click')
		});
	}else{
		$('#GalleryBackground img').live('click', function(event){
			gallery.pause();
			$('#Gallery'+GalleryCount).trigger('click')
		});
	}
	
	$("#ProjectInfoContent span").live("click", function() {
		var SpanId=$(this).attr('id');
		$('#DocLink').attr('href','project-information.php?id='+SpanId);
		$('#DocLaunch').trigger('click');
    });

	$("#ProjectInfoResponseContent span").live("click", function() {
		var SpanId=$(this).attr('id');
		$('#DocLink').attr('href','project-information.php?id='+SpanId);
		$('#DocLaunch').trigger('click');
    });
	
	$(".ProjectDescriptionContainer a").each(function() {
		var HtmlLink=$(this).text()
		$(this).html(HtmlLink+'<span class="Arrows">&#9658;</span>')
	});
	
	
	
	$(".ApplicationLink").live("click", function() {
		event.preventDefault();
		var Applink=$(this).attr('href');
		var AppTarget=$(this).attr('data-rel');
		Data={'AppTarget':AppTarget}
		$.ajax({
			type: 'POST',
			url: 'includes/StoreApplication.php',
			async: true,	
			data: Data,
			success: function(results) {
				DestTimer=setTimeout( function() {LaunchWebPage(Applink);}, 250 );
			}
		});

	});
	
	$('#ProjectReturn').bind('click', function(event){
		if(MobileState==false){
			event.preventDefault();
			RL=$(this).attr('href');
			$('.ProjectTitleContainer h1').animate({'padding-left': '0%'}, 1000, function () {
			})
			  DestTimer=setTimeout( function() {LaunchWebPage(RL);}, 1250 );
		}
	});
	
	//PrepareLinks=setTimeout( function() {StyleLinks();}, 2500 );
	
});

$(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() {
	ProjectType=$('#projecttype').val();
	SEO=$('#seo').val();
	if(ProjectType){
		Data={'type':ProjectType}
	}
	else if(SEO){
		Data={'SEO':SEO}
	}else{
		Data={}
	}
	$.ajax({
		type: 'POST',
		url: 'includes/RetreiveProjectData.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;
					$.each( data, function(i, l){
						NavNum=eval(i+1);
						DisplayNum=eval(GalleryCount-i);
						ImgNum=DisplayNum-1;
					});
				}
			});
			this.bind('loadfinish', function(e) {
				current = gallery.getData(gallery.getIndex());
				ImgId=current.id;
				GalleryCount=current.GalleryCount;
			
				ProjectName=current.name;
				
				SlideDescription=current.description;
				if(SlideDescription){
					ReadmoreLink='<span id='+ImgId+' title="Click for more info" class="ReadmoreProject"><strong>Read more &gt;</strong></span>';
				}else{
					ReadmoreLink='&nbsp;';
				}
				Colours=current.colours;
				Applications=current.applications;
				Designer=current.designer;
				Photographer=current.photographer;
				NewPhotographer=current.newphotographer;
				Fabricator=current.fabricator;
				if((GalleryCount>1)&&(NewPhotographer)){
					InformationCompile='<p>&nbsp;</p><p><strong>Corian&reg; Colour(s):</strong><br>'+Colours+'</p><p><strong>Applications:</strong><br>'+Applications+'</p><p class="PossibleLink"><strong>Design By:</strong><br>'+Designer+'</p><p class="PossibleLink"><strong>Fabricator:</strong><br>'+Fabricator+'</p><p id="PhotoMan" class="PossibleLink"><strong>Photographer:</strong><br>'+NewPhotographer+'</p>';
			        $('#PhotoMan').animate({'opacity': 0}, 1000, function () {
						$('#PhotoMan').html('<strong>Photographer:</strong><br>'+NewPhotographer);
					}).animate({'opacity': 1}, 1000);
				}else{
					InformationCompile='<p>&nbsp;</p><p><strong>Corian&reg; Colour(s):</strong><br>'+Colours+'</p><p><strong>Applications:</strong><br>'+Applications+'</p><p class="PossibleLink"><strong>Design By:</strong><br>'+Designer+'</p><p class="PossibleLink"><strong>Fabricator:</strong><br>'+Fabricator+'</p><p id="PhotoMan" class="PossibleLink"><strong>Photographer:</strong><br>'+Photographer+'</p>';
				}
				
				if(GalleryCount==1){
					$('#ProjectTitle,.ProjectInfoContent').animate({'opacity': 0}, 1000, function () {
						$('#ProjectInfoContent').html(InformationCompile);
						$('#ProjectInfoResponseContent').html(InformationCompile);
						$('#ProjectTitle').html(ProjectName);
					}).animate({'opacity': 1}, 1000);
					if(MobileState==false){
						$('.ProjectTitleContainer h1').animate({'padding-left': '15%'}, 1000, function () {
						});
					}
				}
				
				
			});
			
            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

function StyleLinks(){
	clearTimeout(PrepareLinks);
	if(MobileState==false){
		$('.ProjectTitleContainer h1').animate({'padding-left': '15%'}, 1000, function () {
		});
	}
}