/*
 * Cookie plugin
 *
 * Copyright (c) 2006 Klaus Hartl (stilbuero.de)
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 *
 */
jQuery.cookie=function(name,value,options){if(typeof value!="undefined"){options=options||{};if(value===null){value="";options=$.extend({},options);options.expires=-1}var expires="";if(options.expires&&(typeof options.expires=="number"||options.expires.toUTCString)){var date;if(typeof options.expires=="number"){date=new Date;date.setTime(date.getTime()+options.expires*24*60*60*1E3)}else date=options.expires;expires="; expires="+date.toUTCString()}var path=options.path?"; path="+options.path:"";var domain=
options.domain?"; domain="+options.domain:"";var secure=options.secure?"; secure":"";document.cookie=[name,"=",encodeURIComponent(value),expires,path,domain,secure].join("")}else{var cookieValue=null;if(document.cookie&&document.cookie!=""){var cookies=document.cookie.split(";");for(var i=0;i<cookies.length;i++){var cookie=jQuery.trim(cookies[i]);if(cookie.substring(0,name.length+1)==name+"="){cookieValue=decodeURIComponent(cookie.substring(name.length+1));break}}}return cookieValue}};

/*
 * Wheener Application
 *
 * Copyright (c) 2011 Michael Diolosa <MichaelDiolosa@me.com>
 * Licensed under the GPL v3 license.
 *
 */
var Wheener = function() {
	self.version = '1.0.1'
}

Wheener.prototype.init = function() {
	var obj = this;
	var first = null;
	
	$('<div id="Loading">Loading...</div>').appendTo("body");
	$('<div id="Error">Error...</div>').appendTo("body");

	$("#MainNavigation dd a").each(function (i) {
		$(this).addClass("selected");
		
		var href = $(this).attr("href");
		var folder = obj._info(href, /projects\/([^\/]+)\//);
		var images = obj._info(href, /images=([0-9]+)/);
		var color = obj._info(href, /color=([a-zA-Z0-9]+)/);
		if (images != null) images = parseInt(images);

		var ele = $(this);
		ele.attr("href", "#" + folder).data("folder", folder).data("images", images).data("color", color).click(function() {
			obj.loadProject(ele);
			$.cookie("navigation", ele.data("folder"), { path: "/" });
			
			return false;
		});
		
		if (i == 0) first = $(this);
	});
	
	var navCookie = $.cookie("navigation");
	
	if (navCookie != null && navCookie.length > 0) {
		$("#MainNavigation dd a").each(function (i) {
			if ($(this).data("folder") == navCookie) first = $(this)
		});
	}
	
	if (first != null) obj.loadProject($(first))
}

Wheener.prototype.showError=function(){$("#Loading").css("display", "none");$("#Error").css("display", "block");}
Wheener.prototype.showLoader=function(){$("#Loading").css("display", "block");$("#Error").css("display", "none");}
Wheener.prototype.hideLoader=function() {$("#Loading").css("display", "none");}

Wheener.prototype.loadProject = function(project) {
	var obj = this;
	
	this.showLoader();
	$("#Navigation dd a").removeClass("selected").addClass("unselected");
	$("a[href='#" + project.data("folder") + "']").removeClass("unselected").addClass("selected");
	
	var color = project.data("color");
	if (color == undefined) $("body").attr("class", "");
	else $("body").attr("class", color);
	
	var imageCount = project.data("images");
	if (imageCount == undefined) {
		$("#ImageLayout").empty();
	} else {
		imageCount = parseInt(imageCount);
		if (isNaN(imageCount)) {
			$("#ImageLayout").empty();
		} else {
			var html = "";
			for(var i = 1; i <= imageCount; i++) {
				html += "<tr><td>";
				html += "<img src='projects/" + project.data("folder") + "/image" + i + ".jpg' alt='' />";
				html += "</td></tr>";
			}
			$("#ImageLayout").html(html);
		}
	}

	$.ajax({
		type: "GET",
		url: "projects/" + project.data("folder") + "/index.html",
		dataType: "html",
		error: function(XMLHttpRequest, textStatus, errorThrown) {obj._descriptionOnError(XMLHttpRequest, textStatus, errorThrown)},
		complete: function(XMLHttpRequest, textStatus) {obj._descriptionOnLoad(XMLHttpRequest, textStatus)}
	});
}

Wheener.prototype._descriptionOnError=function(XMLHttpRequest, textStatus, errorThrown){this.showError();}
Wheener.prototype._descriptionOnLoad = function(XMLHttpRequest, textStatus) {
	this.hideLoader();
	
	data = $(XMLHttpRequest.responseText);
	
	$("#Blurb").css("display", "none");
	$("#Blurb").html(data.find("#Blurb").html());
	$("#Blurb").css("display", "block");
}

Wheener.prototype._info = function(str, reg) {
	var results = reg.exec(str);
	if (results != null && results.length > 1) return results[1];
	
	return null;
}

Wheener.navigateHome = function (url) {
	var reg = /projects\/([^\/]+)\//
	var results = reg.exec(document.location.href);
	if (results != null && results.length > 1) $.cookie("navigation", results[1], { path: "/" } );

	document.location.href = url;
}
