$(document).ready(function(){

	var gallery = new Gallery();
	gallery.stopAnimation();
	gallery.resizeGalleryPhotos();
	gallery.setGalleryImagesPositions();
	gallery.bindPicNavigationEvents();
	gallery.bindHeaderNavigationEvents();	
	gallery.resizeNavigationArrows();
	gallery.resizeMenu();

	$('ul.gallery-subnavigation').jScrollPane({
		showArrows:true,
		arrowSize: 15,
		scrollbarWidth: 15,
		scrollbarHeight: 119
	});

	$('div.box-text-slider').jScrollPane({
		showArrows:true,
		arrowSize: 19,
		scrollbarWidth: 19,
		scrollbarHeight: 220
	});
});

$(window).resize(function(){
	var gallery = new Gallery();
	gallery.resizeGalleryPhotos();
	gallery.setGalleryImagesPositions();
	gallery.resizeNavigationArrows();	 
	gallery.resizeMenu();
});

var Gallery = function() {
	this.galleryWidth = "100%";
	this.curPic = 0;
	this.totalPics = $("ul.gallery-photos li").length;
	this.picMoving = false;
	this.picWidth = ($('body').height() > 768) ? $('#gallery').width() : $('body').width();
	this.bodyDefaultHeight = 768;
	_this = this;
}

Gallery.prototype = {
		resizeGalleryList: function(bodyHeight) {
			var list = $('.gallery-photos').find('li');

			$('.gallery-photos').css({"height": ((bodyHeight > _this.bodyDefaultHeight) ? bodyHeight : _this.bodyDefaultHeight), "width": _this.picWidth});

			list.each(function() {

				$(this).css({"height": ((bodyHeight > _this.bodyDefaultHeight) ? bodyHeight : _this.bodyDefaultHeight), "width": _this.picWidth});
				$(this).children(':first').attr("height", _this.getSize('h')).attr("width", _this.getSize('w'));				
			});
		},

		resizeGalleryBackground: function(bodyHeight) {
			var background = $('.gallery-archiv-background');

			$(background).css({"height": ((bodyHeight > _this.bodyDefaultHeight) ? bodyHeight : _this.bodyDefaultHeight), "width": _this.picWidth});			
			$(background).children(':first').attr("height", _this.getSize('h')).attr("width", _this.getSize('w'));
		},
		
		getSize: function(par) {
			var h = 150;
			var w = 112;
			var minwidth = w;
			var minheight = h;
			var ratio = w/h;

			var imagewidth = w;
			var imageheight = h;
			var browserwidth = $(window).width();
			var browserheight = $(window).height();

			if ((browserheight < minheight) && (browserwidth < minwidth)){
				imageheight = minheight;
				imagewidth=minwidth;
			}
			else{	
				if (browserheight > browserwidth){
					imageheight = browserheight;
					imagewidth = browserheight/ratio;

					if (browserwidth > imagewidth){
						imagewidth = browserwidth;
						imageheight = browserwidth * ratio;
					}

				}
				if (browserwidth >= browserheight){
					imagewidth = browserwidth;
					imageheight = browserwidth * ratio;

					if (browserheight > imageheight){
						imageheight = browserheight;
						imagewidth = browserheight/ratio;
					}
				}
			}

			if(par=='h') {
				return imageheight;
			} else {
				return imagewidth;	
			}	
		},

	resizeGalleryPhotos: function() {
		var bodyHeight = $('body').height();
		if (bodyHeight > _this.bodyDefaultHeight)
			$("#gallery").css({"width": $(window).width()});
		_this.picWidth = bodyHeight > _this.bodyDefaultHeight ? $('body').width() : $('body').width();

		if (bodyHeight > _this.bodyDefaultHeight)
			$("#gallery").css({"height": bodyHeight, "width": $(window).width()});
		else
			$("#gallery").css({"width": $(window).width()});

				_this.resizeGalleryList(bodyHeight);
				_this.resizeGalleryBackground(bodyHeight);


	},

	resizeNavigationArrows: function() {
		var bodyHeight = $(window).height();
		var bodyWidth = $(window).width();
			$(".gallery-arrow-navigation-left").css({"margin-top": ((bodyHeight/2)-80) + "px"});
			$(".gallery-arrow-navigation-right").css({"margin-top": ((bodyHeight/2)-80) + "px"});
			
			$(".gallery-body-navigation-left").css({"height": bodyHeight + "px"});
			$(".gallery-body-navigation-right").css({"height": bodyHeight + "px"});
			
			$(".gallery-header").css({"width": $(window).width() + "px"});			
	},	

	resizeMenu: function() {
		var bodyHeight = $('body').height();

		$(".gallery-footer").css({"top": (bodyHeight-230)});	
		$(".gallery-footer").css({"width": ($(window).width()-100)});
		
		$(".gallery-footer-background").css({"top": (bodyHeight-230)});	
		$(".gallery-footer-background").css({"width": $(window).width() + "px"});
	},

	setGalleryImagesPositions: function() {
		$("ul.gallery-photos li").each(function(i) {
			$(this).css({ 
				left: ($('body').width() * i) + "px",
				display: "block"
			});
		});
	},

	stopAnimation: function() {
		_this.picMoving = false;
	},

	nextPic: function() {
		var p = $('body').width();
		if (!_this.picMoving) {
			_this.picMoving = true;

			$("ul.gallery-photos li").each(function(i) {
				if (i == _this.totalPics - 1) {
					var callback = function() {
						_this.stopAnimation();

						var firstElement = $("ul.gallery-photos li:first");
						var lastElement = $("ul.gallery-photos li:last");

						lastElement.after(firstElement);
						firstElement.css("left", (_this.totalPics - 1) * p + "px");
					}
				} else {
					var callback = function() {};
				}

				$(this).animate({ 
					left: (parseInt($(this).css("left")) - p) + "px"
				}, 1000, callback);
			});
		}
	},

	previousPic: function() {
	var p = $('body').width();
		if (!_this.picMoving) {
			_this.picMoving = true;

			var firstElement = $("ul.gallery-photos li:first");
			var lastElement = $("ul.gallery-photos li:last");

			firstElement.before(lastElement);
			lastElement.css("left", p * -1 + "px");

			$("ul.gallery-photos li").each(function(i) {
				if (i == _this.totalPics - 1) {
					var callback = function() {
						_this.stopAnimation();
					}
				} else {
					var callback = function() {};
				}

				$(this).animate({ 
					left: (parseInt($(this).css("left")) + p) + "px"
				}, 1000, callback);
			});
		}
	},

		showArrow: function(ev) {
			$(ev.currentTarget).prev().animate({
				opacity: '0.7'
			},
			{queue: false});
		},

		hideArrow: function (ev) {
			$(ev.currentTarget).prev().animate({
				opacity: '0'
			},
			{queue: false});
		},

	showPicsNavigation: function(ev) {
		$("ul.gallery-footer-navigation").animate({
			top: '131px'
		}, {
			queue: false
		});
	},

	hidePicsNavigation: function(ev) {
		$("ul.gallery-footer-navigation").animate({
			top: '240px'
		}, {
			queue: false
		});
	},

	showSubnavigation: function(ev) {
		$(ev.currentTarget).addClass("active").find("div.slider").animate({
			width: '316px'
		}, {
			queue: false
		});
	},

	hideSubnavigation: function(ev) {
		$(ev.currentTarget).removeClass("active").find("div.slider").animate({
			width: '0px'
		}, {
			queue: false
		});
	},

		bindSubnavigation: function(ev) {
			$(ev.currentTarget).addClass("active");
		},

		unbindSubnavigation: function(ev) {
			$(ev.currentTarget).removeClass("active");
		},

	bindPicNavigationEvents: function() {
		$("div.gallery-footer, div.gallery-footer-background").bind("mouseenter", _this.showPicsNavigation);
		$("div.gallery-footer, div.gallery-footer-background").bind("mouseleave", _this.hidePicsNavigation);
		$("ul.gallery-footer-navigation li.big-folder").bind("mouseenter", _this.showSubnavigation);
		$("ul.gallery-footer-navigation li.big-folder").bind("mouseleave", _this.hideSubnavigation);
				$("div.slider ul.gallery-subnavigation li").bind("mouseenter", _this.bindSubnavigation);
				$("div.slider ul.gallery-subnavigation li").bind("mouseleave", _this.unbindSubnavigation);
		$('a.gallery-body-navigation-left').bind("click", _this.previousPic);
		$('a.gallery-body-navigation-right').bind("click", _this.nextPic);
				$('a.gallery-body-navigation-left, a.gallery-body-navigation-right').bind("mouseenter", _this.showArrow);
				$('a.gallery-body-navigation-left, a.gallery-body-navigation-right').bind("mouseleave", _this.hideArrow);
	},

	showLetsGoLabel: function() {
		$("#lets-go").show();
	},

	hideLetsGoLabel: function() {
		$("#lets-go").hide();
	},

	bindHeaderNavigationEvents: function() {
		$(".gallery-header").bind("mouseenter", _this.showLetsGoLabel);
		$(".gallery-header").bind("mouseleave", _this.hideLetsGoLabel);
	}
}

