// Array.prototype.shuffle = function() {
// 	var s = [];
// 	while (this.length) s.push(this.splice(Math.random() * this.length, 1));
// 	while (s.length) this.push(s.pop());
// 	return this;
// }
var Website = {
	init: function() {
		Website.resize();
		$(window).bind('resize', function(){
			Website.resize();
		});
		Website.slideshow.init();
		Website.draggable.init();
	},
	draggable: {
		index: 103,
		init: function() {

			var windowWidth = $(window).width();
			var windowHeight = $(window).height();
			
			var horizontal = Math.round(((windowWidth * .9) - 520) / 3);
			var vertical = Math.round(((windowHeight * .9) - 620) / 3);
			horizontal = [0, horizontal * 2, horizontal];
			vertical = [0, vertical, vertical * 2];

			var draggableCount = 0;

			$(".draggable").draggable().bind("dragstart", function(e, ui){
				$(this).addClass("no-click");
				$(this).css("z-index", Website.draggable.index++);
			}).each(function(){
				
				var leftOffset = windowWidth * .05 - (Math.random() * (windowWidth * .1));
				var topOffset = windowHeight * .025 - (Math.random() * (windowHeight * .05));
				var left = (windowWidth * .1) + horizontal[draggableCount] + leftOffset;
				var top = 100 + vertical[draggableCount] + topOffset;
				$(this).css("left", left).css("top", top);
				
				draggableCount++;
				
			});
			$(".draggable > a").bind("click", function(e){
				if (!$(this).parent().hasClass("no-click")) {
					var items = $(this).find(".items");
					var currentItem = items.find(".current");
					var nextItem = currentItem.next();
					if (!nextItem.length) nextItem = items.find(":first-child");
					currentItem.removeClass("current");
					nextItem.addClass("current");
					$(this).parent().css("z-index", Website.draggable.index++);
				} else {
					$(this).parent().removeClass("no-click");
				}
			});
		}
	},
	resize: function() {
		var windowWidth = $(window).width();
		var left = 0;
		if (windowWidth < 1000) {
			left = -90;
		} else if (windowWidth < 1090) {
			left = windowWidth - 1090;
		}
		$("#container").css("left", left);
	},
	slideshow: {
		currentSlide: null,
		numberOfSlides: null,
		statusInfo: null,
		previousButton: null,
		nextButton: null,
		container: null,
		init: function() {
			var slides = $(".project-slideshow .slides li");
			this.numberOfSlides = slides.length;
			if (this.numberOfSlides > 1) {
				this.container = $(".project-slideshow .slides");
				this.currentSlide = 1;
				this.statusInfo = $(".interface .status");
				this.previousButton = $(".interface .previous");
				this.nextButton = $(".interface .next");
				$(".interface").fadeIn(350);
				this.updateInterface();
			}
		},
		previous: function() {
			this.currentSlide--;
			this.animate();
		},
		next: function() {
			this.currentSlide++;
			this.animate();
		},
		animate: function() {
			if (this.currentSlide < 1) this.currentSlide = 1;
			if (this.currentSlide > this.numberOfSlides) this.currentSlide = this.numberOfSlides;
			this.container.stop().animate({ left: -((this.currentSlide - 1) * 760) }, 350);
			this.updateInterface();
		},
		updateInterface: function() {
			this.statusInfo.text(this.currentSlide+" / "+this.numberOfSlides);
			this.previousButton.animate({ opacity: this.currentSlide > 1 ? 1 : .1 }, 350);
			this.nextButton.animate({ opacity: this.currentSlide < this.numberOfSlides ? 1 : .1 }, 350);
		}
	}
};

$(function(){ Website.init(); });
