var d2sLightGallery = {
	currentImage : 0,
	_gallery : {},
	_galleryLength : 0,
	_isVisible : false,
	_imageTag : null,
	_loadingImage : null,

	nextImage : function() {
		this.currentImage++;
		this._refresh();
	},
	previousImage : function() {
		this.currentImage--;
		this._refresh();
	},
	show : function() {
		this._isVisible = true;
		this._refresh();
	},
	hide : function() {
		this._isVisible = false;
		this._refresh();
	},
	flush : function() {
		this.hide();
		this.currentImage = 0;
		this._gallery = null;
	},
	_refresh : function() {
		if (this.currentImage >= this._galleryLength) {
			this.currentImage = 0;
		}
		if (this.currentImage < 0) {
			this.currentImage = this._galleryLength - 1;
		}
		
		if (this._isVisible == false || this._galleryLength == 0) {
			$("#d2s_light_gallery").css("display", "none");
			return false;
		} else {
			$("#d2s_light_gallery").css("display", "block");
			d2sLightGallery._fixScrollPosition();
			$(window).scroll(function() {
				d2sLightGallery._fixScrollPosition();
			});
		}

		var currentImagePath = this._gallery[this.currentImage].src;
		$("#d2s_light_gallery_image").attr("src", currentImagePath);

		this._loadingImage = new Image();
		this._loadingImage.src = currentImagePath;
		
		$("#d2s_light_gallery input[name='element']").val(this.currentImage + 1);
		$("#d2s_light_gallery input[name='elements']").val(this._galleryLength);

		$("#d2s_light_gallery_image").parent().css("display", "none");
		$("#d2s_light_gallery_loader").parent().css("display", "block");
		
		$("#d2s_gallery_description_text").html(this._gallery[this.currentImage].description);
		
		this._preload();
	},
	_preload : function() {
		if (this._loadingImage.complete == true) {
			$("#d2s_light_gallery_image").parent().css("display", "block");
			$("#d2s_light_gallery_loader").parent().css("display", "none");
			
			var imageWidth = $("#d2s_light_gallery_image").parent().width();
			$("#d2s_light_gallery_loader").parent().css("width", imageWidth + "px");

			var imageHeight = $("#d2s_light_gallery_image").parent().height();
			$("#d2s_light_gallery_loader").parent().css("height",imageHeight + "px");
			
			for(var property in this._gallery[this.currentImage]){
				if(property == "src"){
					continue;
				}
				$("#d2s_light_gallery_"+property).html(this._gallery[this.currentImage][property]);
			}
		} else {
			setTimeout(function() {
				d2sLightGallery._preload();
			}, 100);
		}
	},
	_fixScrollPosition : function() {
		var pageYOffset = Math.max($(window).scrollTop(), $(document.documentElement).scrollTop());
		var pageXOffset = Math.max($(window).scrollLeft(), $(document.documentElement).scrollLeft());

		$("#d2s_light_gallery").css("left", pageXOffset + "px");
		$("#d2s_light_gallery").css("top", pageYOffset + "px");
	},
	setGallery : function(gallery){
		this._gallery = $.parseJSON(gallery);
		
		this._galleryLength = 0;
		for (var image in this._gallery){
			this._galleryLength++;
		}
		
	}
};



var d2sLightGalleryVideo = {
	currentImage : 0,
	_videoPath : null,
	_gallery : {},
	_galleryLength : 0,
	_isVisible : false,
	_imageTag : null,
	_loadingImage : null,

	show : function($videoPath) {
		this._videoPath = $videoPath;
		this._isVisible = true;
		this._refresh();
	},
	hide : function() {
		this._isVisible = false;
		this._refresh();
	},
	flush : function() {
		this.hide();
		this.currentImage = 0;
		this._gallery = null;
	},
	_refresh : function() {
		if (this._isVisible == false) {
			$("#d2s_light_gallery_video").css("display", "none");
			$("#divSWFObject").html('<embed type="application/x-shockwave-flash" src="/Project/Frontend/Resources/d2s_tools/media_box/player.swf" style="" id="d2s.player" name="d2s.player" bgcolor="#FFFFFF" quality="high" allowfullscreen="true" allownetworking="all" allowscriptaccess="always" flashvars="" width="560" height="460"></embed>');
			return false;
		} else {
			$("#d2s_light_gallery_video").css("display", "block");
			d2sLightGalleryVideo._fixScrollPosition();
			$(window).scroll(function() {
				d2sLightGalleryVideo._fixScrollPosition();
			});
		}

		var video = this._videoPath;
		var currentFlashvars = "config=/Project/Frontend/Resources/d2s_tools/media_box/config.xml&amp;file=" + video;
		
		$("#d2s.player").attr("flashvars", currentFlashvars);
		
		var swfJWPlayer = new SWFObject("/Project/Frontend/Resources/d2s_tools/media_box/player.swf","d2s.player","560","460","9","#FFFFFF");
		swfJWPlayer.addParam("allowfullscreen","true");
		swfJWPlayer.addParam("allownetworking","all");
		swfJWPlayer.addParam("allowscriptaccess","always");
		swfJWPlayer.addParam("flashvars", currentFlashvars);
		swfJWPlayer.write("divSWFObject");
	},
	_fixScrollPosition : function() {
		var pageYOffset = Math.max($(window).scrollTop(), $(document.documentElement).scrollTop());
		var pageXOffset = Math.max($(window).scrollLeft(), $(document.documentElement).scrollLeft());

		$("#d2s_light_gallery_video").css("left", pageXOffset + "px");
		$("#d2s_light_gallery_video").css("top", pageYOffset + "px");
	},
	setGallery : function(gallery){
		this._gallery = $.parseJSON(gallery);
		
		this._galleryLength = 0;
		for (var image in this._gallery){
			this._galleryLength++;
		}
	}
};

