/**
 * @author denodell
 */

 var Pigment = new function() {
 	this.Movie = function(input) {
		var element = input.element || "#movie";
		var movie = input.movie || "movie.flv"; 
		var width = input.width || "480";
		var height = input.height || "360";
		var autoplay = input.autoplay || "true";
		var controlbar = input.controlbar || "bottom";
		
		$(element).flash({
			swf: "/site/js/third-party/player.swf",
			width: width,
			height: height,
			hasVersion: 9,
			flashvars: {
				'file': '/site/files/' + movie,
				'stretching': 'fill',
				'autostart': autoplay,
				'controlbar': controlbar,
				'skin': '/site/js/third-party/simple.swf',
				'screencolor': '#CCCCCC'
			},
			params: {
				allowfullscreen: true,
				allowscriptacccess: 'always',
				menu: false
			}
		});
	};
	
	this.Home = function() {
		$("body").addClass("loading");
		var images = $('#home img');
		
		if (!$("#coverup")[0]) {
			$("#nav").append('<div id="coverup"></div>');
		}
		$("#coverup").css({
			display: "block",
			opacity: 1
		});
		
		/*
		$("#nav,#logo,#nav-furniture,#footer").animate({
			top: -1250
		});
		*/
		
/*
		var mySound;
		soundManager.waitForWindowLoad = true;
		soundManager.url = '/site/js/third-party/';
		soundManager.onload = function(){
			mySound = soundManager.createSound({
				id: 'aSound',
				url: '/site/audio/explosion.mp3'
			});
		}
		*/
		var showImages = function() {
			var images = $('#home img');
			images.hide();
			
			images.each(function(index, image) {
				window.setTimeout(function() {					
					$(image).css({
						width: 0,
						height: 0,
						top: "50%",
						left: "50%",
						opacity: 1
					})
					$(image).animate({
						width: "125px",
						height: "125px",
						top: -10,
						left: -13
					}, 350, "swing", function() {
						$(image).animate({
							width: "100px",
							height: "100px",
							top: 0,
							left: 0
						}, 150, "swing");
					});
					
					window.setTimeout(function() {
						$(image).animate({
							width: "200px",
							height: "200px",
							top: -37,
							left: -50,
							opacity: 0
						}, 500, "swing");
					}, 2550);
					
				}, Math.random() * 1000);
			});
			/*
			window.setTimeout(function() {
				if (mySound) {
					mySound.play();
				}
			}, 2150);
			*/
			
			
			window.setTimeout(function() {				
				$("#home-logo").removeClass("hide");
			}, 2000);
			
			window.setTimeout(function() {
				$("#replay-intro").removeClass("hide");
				
				$("#coverup").animate({
					opacity: 0
				}, 300, function() {
					this.style.display = "none";
				});
				
				/*
				$("#nav,#logo,#nav-furniture,#footer").animate({
					top: 0
				})*/
			}, 3250);
		}
		
		var onLoadCalled = false;
		
		var onLoad = function() {
			onLoadCalled = true;
			$("#home").removeClass("hide");
			$("body").removeClass("loading");
			showImages();
			$("#replay-intro").click(function(){
				$("#coverup").css({
					opacity: 1,
					display: "block"
				});
				/*
				$("#nav,#logo,#nav-furniture,#footer").animate({
					top: -1250
				});
				*/
				showImages();
				
				$("#home-logo").addClass("hide");
				$("#replay-intro").addClass("hide");
			});
		};
		
		var count = 0;
		images.load(function() {
			count++;
			if (count == images.length) {
				if (!onLoadCalled) {
					onLoad();
				}
			}
		});
		
		$(window).load = function() {
			if (!onLoadCalled) {
				onLoad();
			}
		}
	};
	
	this.Book = function(input) {
		this.pages = input.pages || [];
		this.currentPage = 1;
		
		this.showPage = function(num) {
			if (num >= 1 && num <= this.pages.length) {
				var pageURL = this.pages[num - 1];
				$("#book-img").html('<img src="' + pageURL + '" alt="" />');
			}
			
			if (num == 1) {
				$("#book-previous").addClass("hide");
			} else {
				$("#book-previous").removeClass("hide");
			}
			
			if (num == this.pages.length) {
				$("#book-next").addClass("hide");
			} else {
				$("#book-next").removeClass("hide");
			}
			
			$("#book-page").html("Page " + this.currentPage + " of " + this.pages.length);
		}
		
		var self = this;
		
		$("#book-previous").click(function(e) {
			if (self.currentPage > 1) {
				self.currentPage--;
			}
			self.showPage(self.currentPage);
		});
		
		$("#book-next").click(function(e) {
			if (self.currentPage >= 1 && self.currentPage < self.pages.length) {
				self.currentPage++;
			}
			self.showPage(self.currentPage);
		});
		
		this.showPage(1);
	};
	
 	this.Saver = function(elem) {
		var elem = $(elem);
		elem.attr('disabled', 'disabled');
		elem.addClass('disabled');
		elem.val("Sending...");
	};
	
	this.Galleria = function() {
		$('ul.gallery').galleria({
			history   : false,
			insert    : '#main_image',
			clickNext: false,
			onImage   : function(image,caption,thumb) {
				if(! ($.browser.mozilla && navigator.appVersion.indexOf("Win")!=-1) ) { // FF/Win fades large images terribly slow
					$(image.parent()).css('display','none').fadeIn(300);
				}
				
				var _li = thumb.parents('li');
				_li.siblings().children('img.selected').fadeTo(300,0.75); // fade out inactive siblings
				thumb.fadeTo('fast',1).addClass('selected'); // fade in active thumbnail
			},
			onThumb : function(thumb) { // thumbnail effects goes here
				var _li = thumb.parents('li'); // fetch the thumbnail container
				var _fadeTo = _li.is('.active') ? '1' : '0.75'; // if thumbnail is active, fade all the way.
				thumb.css({display:'none',opacity:_fadeTo}).fadeIn(150); // fade in the thumbnail when finished loading
				
				$(thumb.parent()).click(function() {
					$.galleria.activate(thumb.attr('src'));
				});
				thumb.click(function(e) {
					e.preventDefault();
					e.stopPropagation();
				})
				thumb.css("margin-left", 0);
				// hover effects
				$(thumb.parent()).hover(
					function() { thumb.fadeTo(150,1); },
					function() { _li.not('.active').children('img').fadeTo(150,0.75); } // don't fade out if the parent is active
				)
			}
		}); 
	}
	
	this.Menu = function() {
		this.open = function(url) {
			
		};
		
		this.close = function() {
			
		}
	}();
	
	function hideMenu() {
		$('#nav-main li .slide').slideUp(function() {
			$('#nav-main li .slide').removeClass("hide");
		});
		$("#nav-main li.selected").removeClass("selected");
	}
	
	function openMenu() {
		$("#nav-furniture a.selected").removeClass("selected");
		if ($(this).hasClass("artform")) {
			$(".artlink.selected").removeClass("selected");
			if (!$(this).parents("li").hasClass("selected")) {
				$('#nav-main li.selected .slide').slideUp(function(){
					$(this).parents("li").removeClass("selected");
				});
				$(this).parents("li").addClass("selected");
				$(this).parent().find('.slide').slideDown();
			}
		} else if ($(this).hasClass("artlink")) {
			var slide = $($(this).parents(".slide")[0]);
			var group = $($($(this).parents("div.artwork")[0]).parent());
			slide.slideDown();
			group.addClass("selected");
			$(".artlink.selected").removeClass("selected");
			$(this).addClass("selected");
		}			
	}
	
	(function setupMenu() {		
		$(document).ready(function() {
			hideMenu(); 
			
			$.each($("#nav-main div.artwork"), function(i, item) {
				var item = $(item);
				var ul = item.find('ul');
				var maxHeight = parseInt(item.css('max-height'));
				var ulHeight = ul.height();

				if (ulHeight > maxHeight) {
					item.addClass("scroll");
					
					var topMostPoint = 20;
					var bottomMostPoint = ulHeight;
					var pageSize = maxHeight *5 /8;
					var page = 1;
					var pageCount = Math.ceil(ulHeight / pageSize);
					
					item.find(".previous").addClass("hide")
					item.find(".previous").click(function() {
						page--;
						if (page >= 1) {
							var to = ((page) * pageSize <= topMostPoint) ? topMostPoint - pageSize : (page -1) * pageSize;
							ul.animate({
								"top": -to+ "px"
							}, 300);
						}
						
						if (page == 1) {
							$(this).addClass("hide");
						}
						item.find(".next").removeClass("hide");
					});
					
					item.find(".next").click(function() {
						page++;
						if (page <= pageCount) {
							var to = ((page ) * pageSize > ulHeight) ? ulHeight - pageSize - (topMostPoint*2) : (page - 1) * pageSize;
							ul.animate({
								"top": -to + "px"
							}, 300);
						}
						
						if (page == pageCount) {
							$(this).addClass("hide");
						}
						item.find(".previous").removeClass("hide");
					});
				}
			});			
			
			openMenu.call($('a[href=' + window.location.pathname + ']'));
			
			$('#nav-furniture a').click(function(e) {
				hideMenu();
				$('#nav-furniture a.selected').removeClass("selected");
				$(this).addClass("selected");
			});
			
			$('#nav-main li a').click(function(e) {
				openMenu.call(this);
			});
		});
	})();

	(function setupAjaxAndHistory() {
		function doAjax(url) {
			if (url.indexOf("/portfolio/") < 0) {
				hideMenu();
			} else {
				openMenu.call($('a[href=' + url + ']'));
			}
			$('a.selected').removeClass("selected");
			$('a[href=' + url +']').addClass("selected");

			$('#layout').fadeTo(100, 0, function() {
				$('body').addClass('loading');
			});
			
			$.ajax({
				type: "GET",
				url: url,
				success: function(result) {
					$('#layout').html(result);
					$('#layout').fadeTo(100, 1);
				},
				complete: function() {
					$('body').removeClass('loading');
				}
			});
		}
	
		$(document).ready(function(){
			$(document).click(function(e){
				var link = e.target.tagName.toLowerCase() == "a" ? e.target : ($(e.target).parents("a").length > 0 ? $(e.target).parents("a") : null);
				if (link) {
					if ($(link).attr('target') != "_blank") {
						e.preventDefault();
						var url = $(link).attr('href');
						$.history.add(url);
						doAjax(url);
					}
				}
			});
			
			if (window.location.hash) {
				doAjax(window.location.hash.replace("#", ""));
			} else {
				$.history.add(window.location.pathname);
			}
			
			$(window).history(function(e, hash){
				doAjax(hash);
			});
		});
	})();
 }();
