sotm = {

	mode : "std",

	/*--- PROJECT ---*/

	// current project
	currentProject : 1,

	// store the bg color value
	currentBackground : { type : "color", val : null },

	// distance to move project
	projectDistance : 340,

	urlPrefix : "/",

	init : function() {
		this.getProjectWrapper();
		if( this.mode === "std" ) {
			this.loadBackground(this.currentProject, "false");
		} else if( this.mode === "archive" ) {
			this.loadBackground(this.currentProject, "false", "archived");
		}
		this.setCurrentSlides();
		this.activateProjectControls();
		this.loadProjectDescription();
		this.addSlideControls();
		this.loadProjectCSS();
		if( this.mode === "backroom" ) {
			this.moveSlide(1);
		}
	},

	setMode : function(mode) {
		this.mode = mode;
	},

	loadProjectFromURL : function() {
		var urlparts = [],
		url = location.href;
		urlparts = url.split("#/project/");
		this.urlPrefix = urlparts[0];
		if( typeof(urlparts[1]) !== "undefined" ) {
			try {
				var ID = this.getProjectID(urlparts[1]);
				this.moveProject(ID);
			} catch(e) { }
		}
	},

	updateURL : function() {
		var pid = $(this.wrapper).siblings("span.id").attr("id") || "";
		location.href = this.urlPrefix + "#/project/" + pid;
	},

	getProjectID : function(pid) {
		var pos = {};
		if( this.mode === "std" ) {
			pos.top = 0;
			pos = $("#" + pid).parent().position() || 0;
			pos = (pos.top > 0) ? Math.floor((pos.top - 30) / this.projectDistance) + 1 : 1;
		} else {
			return fasle;
		}
		return pos;
	},

	getProjectWrapper : function() {
		if( this.mode === "about" ) {
			this.wrapper = $(".project:first .project-wrapper");
		} else if( this.mode === "backroom" ) {
			this.wrapper = $("#brvideo-wrapper");
		} else {
			this.wrapper = $("#project_" + this.currentProject + " .project-wrapper");
		}
		return this.wrapper;
	},

	getProjectCount : function() {
		this.projectCount = $("#projects .project").size() || 1;
		return this.projectCount;
	},

	getProjectHex : function() {
		var classes = $(this.wrapper).children("p").attr("class") || ":";
		c = classes.split(":");
		this.projectHex = c[1];
		return this.projectHex;
	},

	moveProject : function(num) {
		this.getProjectCount();
		this.stopAutoSlide();
		$(".project").removeClass("curr");

		switch(num) {
			case "down":
				if( this.currentProject < this.projectCount ) {
					this.currentProject++;
				}
			break;
			case "up":
				if( this.currentProject > 1 ) {
					this.currentProject--;
				}
			break;
			default:
				// hide the "view" tab when moving up
				if( num < this.currentProject ) {
					$("#project_" + num + " .project-shadow a").addClass("hide");
				}
				// this would only happen with key strokes
				if( num > this.projectCount ) {
					this.currentProject = this.projectCount;
				} else {
					this.currentProject = parseInt(num); // just incase we're grabbing this from the #ID
				}
			break;
		}

		// get project wrapper object
		this.getProjectWrapper();

		var newTop = -((this.currentProject - 1) * this.projectDistance),
		isGrid = this.wrapper.parent().hasClass("thumbs");
		// make sure we never go the wrong way
		newTop = (newTop > 0) ? 0 : newTop;

		// we fade out the info box while moving the project
		if( isGrid === true ) {
			$("#info").fadeOut("fast");
			this.clearSlideControls();
		}
		$("#projects").animate({ top : newTop }, 500, null, function() {
			// wait for it...
			if( isGrid === false ) {
				$("#info").fadeIn("fast");
				sotm.loadProjectDescription();
				sotm.addSlideControls();
			}
			sotm.activateProjectControls();
			sotm.loadProjectCSS();
			sotm.updateURL();
			// show the "view" tab again
			$("#project_" + num + " .project-shadow a").removeClass("hide");
		});
		$("#project_" + this.currentProject).addClass("curr");
		setTimeout(function() { sotm.loadBackground(sotm.currentProject, "true") }, 1500);

		return false;
	},

	activateProjectControls : function() {
		if( this.currentProject === 1 ) {
			$("#up").hide();
		} else {
			$("#up").show();
		}

		if( this.currentProject === this.projectCount ) {
			$("#down").hide();
		} else {
			$("#down").show();
		}
	},

	loadProjectDescription : function() {
		if( this.mode !== "about" ) {
			var txt = $(this.wrapper).children("p.desc").html() || "";
			$("#info p").html(txt);
		}
	},

	loadBackground : function(pid, fade, nofadediv) {
		nofadediv = nofadediv || "background-old";
		var bg = { type : "color", val : "#333333", cssval : "#333333" };

		// grab background color or image
		var bgc = $(this.wrapper).children("span.bg").css("background-color");
		if( bgc !== "transparent" && bgc !== "rgba(0, 0, 0, 0)" ) {
			bg.val = $(this.wrapper).children("span.bg").css("background-color");
			bg.type = "color";
		} else {
			bg.val = $(this.wrapper).children("span.bg").css("background-image");
			bg.val = bg.val.replace("url(", "");
			bg.val = bg.val.replace(")", "");
			bg.type = "image";
		}

		// only change the background for the current slide and if it's a different background image
		if( pid === this.currentProject && bg.val !== this.currentBackground.val ) {
			// fade between backgrounds
			if( fade === "true" ) {
				if( typeof(bg.val) === "string" ) {
					// insert new background
					if( bg.type === "image" ) {
						bg.cssval = "url(" + bg.val + ")";
					} else {
						bg.cssval = bg.val;
					}
					$("#background-new").css({ background : bg.cssval });

					// start fading out the old background
					// after that's done, swap the new background to the #background-old container for the next switch
					// now kick-punch-chop!
					$("#background-old").animate({ opacity : 0 }, 500, null, function() {
						$("#background-old").css({ background : bg.cssval, "opacity" : 1 });
						$("#background-new").css({ background : "none" });
					});
				// this is a fallback if we don't have an image or hex color defined we fade back to the default background set in the CSS
				} else {
					$("#background-new").css({ background : "" });
					$("#background-old").animate({ opacity : 0 }, 500, null, function() {
						$("#background-old").css({ background : "", "opacity" : 1 });
						$("#background-new").css({ background : "none" });
					});
				}
			// no fade, just replace the bg
			} else {
				if( bg.type === "image" ) {
					bg.cssval = "url(" + bg.val + ")";
				} else {
					bg.cssval = bg.val;
				}
				$("#" + nofadediv).css({ background : bg.cssval });
			}
			this.currentBackground.type = bg.type;
			this.currentBackground.val = bg.val;
		}
	},

	loadProjectCSS : function() {
		$("#project-css").remove();
		var hex = this.getProjectHex(),
		css = '<style type="text/css" id="project-css">\n<!--\n'+
			'  #info strong { color: #' + hex + '; }\n'+
			'  #info p a { text-decoration: none; color: #' + hex + '; }\n'+
			'  #info p span a { text-decoration: underline; }\n'+
			'  #info .scrollies a:hover { color: #' + hex + '; border: 1px solid #' + hex + '; }\n'+
			'  #info .scrollies a.here { background: #' + hex + '; border: 1px solid #' + hex + '; }\n'+
			'-->\n</style>\n';
		$("#css").after(css);
	},

	/*--- SLIDE ---*/

	// current slide of each project
	currentSlide : [],

	// distance to move slide
	slideDistance : 600,

	autoSlideTimeout : null,

	// sets defaults
	setCurrentSlides : function() {
		for(p=1; p <= this.getProjectCount(); p++) {
			if( typeof(this.currentSlide[p]) === "undefined" || this.currentSlide[p] < 0 ) {
				this.currentSlide[p] = 1;
			}
		}
	},

	getSlideCount : function() {
		var width = $(this.wrapper).width();
		this.slideCount = Math.floor(width / this.slideDistance);
		return this.slideCount;
	},

	autoSlide : function(pid) {
		this.getSlideCount();
		if( pid === this.currentProject && this.currentSlide[pid] < this.slideCount ) {
			this.moveSlide("right");
			this.autoSlideTimeout = setTimeout(function() { sotm.autoSlide(pid) }, 5000);
		} else {
			// we've reached the end, go home
			this.stopAutoSlide();
			this.moveSlide(1);
		}
	},

	stopAutoSlide : function() {
		if( this.autoSlideTimeout ) {
			clearTimeout(this.autoSlideTimeout);
		}
	},

	moveSlide : function(num) {
		this.stopAutoSlide();
		this.getSlideCount();

		// take them to the top
		if( this.mode === "backroom" ) location.href = "#top";

		switch(num) {
			case "right":
				if( this.currentSlide[this.currentProject] < this.slideCount ) {
					this.currentSlide[this.currentProject]++;
				}
			break;
			case "left":
				if( this.currentSlide[this.currentProject] > 1 ) {
					this.currentSlide[this.currentProject]--;
				}
			break;
			default:
				// this would only happen with key strokes
				if( num > this.slideCount ) {
					this.currentSlide[this.currentProject] = this.slideCount;
				} else {
					this.currentSlide[this.currentProject] = num;
				}
			break;
		}

		$(this.wrapper).animate({ left : -((this.currentSlide[this.currentProject] - 1) * this.slideDistance) }, 500, null, function() {
			sotm.activateSlideControls();
			sotm.slideScrollies();
		});
		if( this.mode === "backroom" ) {
			this.loadSlideDescription(this.currentSlide[this.currentProject]);
			$(".icon-row div").removeClass("here");
			$("#icon_" + this.currentSlide[this.currentProject]).addClass("here");
		}
		return false;
	},

	clearSlideControls : function() {
		// remove any previous controls
		$(".pcontrols").remove();
	},

	addSlideControls : function() {
		this.getProjectWrapper();
		this.getSlideCount();

		this.clearSlideControls();

		// add slide controls over images
		$(this.wrapper).before('<div class="pcontrols"><span id="back"><a href="#prev-slide" title="<< Previous slide"><img src="/images/ieisretarded.gif" alt="" /></a></span> <span id="next"><a href="#next-slide" title="Next slide >>"><img src="/images/ieisretarded.gif" alt="" /></a></span></div>');

		// add control handlers
		$("#back a").click(function(e) {
			e.preventDefault();
			sotm.moveSlide("left");
			$(this).removeClass("hover");
		});
		$("#next a").click(function(e) {
			e.preventDefault();
			sotm.moveSlide("right");
			$(this).removeClass("hover");
		});

		$("#back a, #next a").hover(function() {
			$(this).addClass("hover");
		},
		function() {
			$(this).removeClass("hover");
		});

		this.activateSlideControls();
		this.slideScrollies();
	},

	activateSlideControls : function() {
		if( this.currentSlide[this.currentProject] === 1 ) {
			$("#back a").hide();
		} else {
			$("#back a").show();
		}

		if( this.currentSlide[this.currentProject] === this.slideCount ) {
			$("#next a").hide();
		} else {
			$("#next a").show();
		}
	},

	slideScrollies : function() {
		var imgs = "";

		$("#info .scrollies").html("");
		if( this.slideCount > 1 ) {
			for(i=1; i <= this.slideCount; i++) {
				if( this.currentSlide[this.currentProject] === i ) {
					imgs += '<a class="here">' + i + '</a>';
				} else {
					imgs += '<a href="" onclick="return sotm.moveSlide(' + i + ');">' + i + '</a>';
				}
			}
			$("#info .scrollies").html(imgs);
		}
	},

	loadSlideDescription : function(sid) {
		var txt = $("#icon_" + sid + " p").html() || "";
		$("#info p").html(txt);
	}
}
