// Starting Cufon
Cufon.replace('h1', { fontFamily: 'Eau' });
Cufon.replace('h2', { fontFamily: 'Eau' });
Cufon.replace('h3', { fontFamily: 'Eau' });
Cufon.replace('#menu a.other', { fontFamily: 'Eau', hover: {color: '#9F0'} });
Cufon.replace('#menu a.current', { fontFamily: 'EauBold', hover: false });
Cufon.replace('div.home p', { fontFamily: 'Eau' });
Cufon.replace('h5', { fontFamily: 'Eau' });
Cufon.replace('#categories a', {
	fontFamily: 'Eau',
	hover: true,
});
Cufon.replace('#nrtext', {
	fontFamily: 'Eau',
	hover: true,
	hoverables: { a: true, div: true, span: true },
});


// Global Variables
var worksState = "#concepts1"; // Memorizes last viewed item on "All Works"-page (by anchor)
var slide = false; // True if the last effect was a sliding transition
var mainURL = "http://ndesign.nathan-design.com";
var catArray = new Array();
var historyHash;
if(window.location == "http://ndesign.nathan-design.com/404") { historyHash = false; } else { historyHash = true; }

// Initial code
jQuery(document).ready(function() {
	jQuery("h1 span").css("margin-top", "-4px");
	
	initWorks();
	initLinks();
	
	jQuery(".nav a").click(function(){ selector(this); return false; });
	jQuery("#nav a").click(function(){ selector(this); return false; });
	jQuery("#categories a").click(function(){ selector(this); return false; });
	selectWorks();
	
	
	jQuery.address.change(function(event) {
		// do something depending on the event.value property, e.g.
		// $('#content').load(event.value + '.xml');
		if(historyHash) {
			curURL = getAbsolutePath();
			jQuery(document).stop(true,true);
			if(curURL != mainURL + "/") {
				window.location = mainURL + "/#" + curURL.replace(mainURL, "");;
			}
			else if(event.value == "/") {
				activateLink(mainURL + event.value, true);
			}
			else {
				activateLink(mainURL + event.value);
			}
		}
	});

});


// Ajaxifys internal links that use .internal class
function initLinks(){
	jQuery('a[rel=internal]').click(function(){
		var thisURL = jQuery(this).attr('href');
		thisURL = thisURL.replace(mainURL, "");
		jQuery.address.value(thisURL);
		/*activateLink(jQuery(this).attr('href'));
		
		jQuery("#menu .current").removeClass("current").addClass("other");
		jQuery(this).removeClass("other").addClass("current");
		jQuery("#menu").html(jQuery("#menu").html());
		Cufon.refresh();
		initLinks();
		
		var _Href = this.href+"?ajax=1";
		
		if(slide) { 
			pageLoad(_Href, true, false); 
		}
		else {
			pageLoad(_Href);
		}*/
		return false;
	});
}

function activateLink(_Href, instant){
	//var thisURL = _Href.replace(mainURL, "");
	//jQuery.address.value(thisURL);
	jQuery("#menu .current").removeClass("current").addClass("other");
	jQuery("a[href="+_Href+"],a[href="+_Href+"home/]").removeClass("other").addClass("current");
	jQuery("#menu").html(jQuery("#menu").html());
	Cufon.refresh();
	initLinks();
	
	_Href = _Href+"?ajax=1";
	if(instant) { 
		pageLoad(_Href, true); 
	}
	else {
		pageLoad(_Href);
	}
}

// Returns current path of URL
function getAbsolutePath() {
	var loc = window.location;
	var pathName = loc.pathname.substring(0, loc.pathname.lastIndexOf('/') + 1);
	return loc.href.substring(0, loc.href.length - ((loc.pathname + loc.search + loc.hash).length - pathName.length));
}

// Loading and rendering page _Href.
function pageLoad(_Href, instant){
	if (instant == null){ instant = false; }
	
	jQuery('#loading').fadeIn(50, function(){
		jQuery.ajax({
			type:	'GET',
			url:	_Href,
			data:	"ajax=true",
			dataType:	'html',
			timeout:	5000,
			success: function(d,s){
				if(instant){
					jQuery('#box').html(d);
					Cufon.refresh();
					selectWorks();
					initWorks();
					slide = true;
					jQuery('#loading').fadeOut(50);
					jQuery(".back").click(function(){
						//pageLoad("http://ndesign.nathan-design.com/all-work/", true, false);
						return false;//history.back();
					});
					Shadowbox.clearCache();
					Shadowbox.setup();
				}
				else{
					jQuery('#box').fadeOut(120,function(){
						jQuery('#box').html(d);
							Cufon.refresh();
						jQuery('#box').fadeIn(200,function(){
							selectWorks();
							initWorks();
							slide = false;
							jQuery('#loading').fadeOut(50);
						});
						jQuery(".back").click(function(){
							/*var thisURL = jQuery(this).attr('href');
							thisURL = thisURL.replace(mainURL, "");
							jQuery.address.value(thisURL);*/
							history.back();
							//pageLoad("http://ndesign.nathan-design.com/all-work/", true, false);
							return false;
						});
						Shadowbox.clearCache();
						Shadowbox.setup();
					});
				}
			},
			error: function(o,s,e){
				window.location = _Href;
				return false;
			}
		});
	});
}

// Ajaxifys "All Works"-page when clicking on a link to single-item view
function initWorks(){
	changeNav();
	loadCatArray();
	jQuery(".nav a").click(function(){ selector(this); return false; });
	jQuery("#nav a").click(function(){ selector(this); return false; });
	jQuery("#categories a").click(function(){ selector(this); return false; });
	jQuery("#nav").show();
	jQuery(".nav").hide();
	changeNav();
	jQuery('.fullpost').click(function(){
		var _Href = jQuery(this).attr('href');
		var thisURL = jQuery(this).attr('href');
		thisURL = thisURL.replace(mainURL, "");
		jQuery.address.value(thisURL);
		//pageLoad(_Href, false, true);
		return false;
	});
}

// Scrolls to last element
function selectWorks(tar){
	if (tar == null){ 
		if (worksState == null){ 
			tar = "#art1";
		}
		else{
			tar = worksState;
		}
	}
	selector(tar, 0);
}

// Scroll to element (tar)
function selector(tar, speed){
	if (speed == null || speed >= 2000){ speed = 650; }
	
	var str = new String(tar);
	if(str.match("http://")) {
		tar = jQuery(tar).attr("href");
	}
	jQuery('#content').stop(true).scrollTo(tar, speed,{queue:true});
	selectCat(tar);
	worksState = tar;
	changeNav();
}

// selects the correct category in the category-menu and gives it ".selected"-class
function selectCat(tar){
	var str = new String(tar);
	if(!str.match("http://")) {
		tar = tar.substring(0,tar.length-1);
		cat = tar;
		tar = "#categories a[href="+tar+"1]";
	}
	jQuery(".selectedCat").removeClass("selectedCat")
	jQuery("span").remove("#selectarrows"); 
	jQuery(tar).addClass("selectedCat").prepend("<span id=\"selectarrows\">&gt;&gt; </span>");
	Cufon.refresh();
}

function loadCatArray(){
	jQuery("#categories li").each(function(i){
		cathash = jQuery(this).children("a").attr("href");
		catArray[i] = new Array();
		catArray[i][0] = cathash.substring(0,cathash.length-1); // hashname, like #art
		catArray[i][1] = jQuery(this).children(".workcount").html(); // count of items in this cat
	});
	changeNav();
}

function changeNav() {
	if(undefined===window.catArray[0]) {
		return false;
	}
	else
	{
		var catName = worksState.substring(0,worksState.length-1);
		var curnr = parseInt(worksState.charAt(worksState.length-1));
		for(i in catArray) {
			if(catArray[i][0] == catName) {
				catMax = catArray[i][1];
			}
		}
		jQuery("#totalnr").html(catMax);
		jQuery("#itemnr").html(curnr);
		if(curnr > 1) {
			prev = String(catName + (curnr - 1));
			jQuery("#prevworks a").attr({href: prev}).fadeIn(150);
		}
		else {
			jQuery("#prevworks a").fadeOut(150);
		}
		if(curnr < catMax) {
			next = String(catName + (curnr + 1));
			jQuery("#nextworks a").attr({href: next}).parent().fadeIn(150);
		}
		else {
			jQuery("#nextworks a").parent().fadeOut(150);
		}
		Cufon.refresh()
	}
}

// Changes the background
// b = source, t = title used in html-tag, a = alt-text used in html-tag
function initPage(title, b,t,a){
	docTitle = document.title;
	if(docTitle.indexOf(" - ") != -1) {
		document.title = title + " - " + docTitle.split(" - ")[1];
	}
	else {
		document.title = title + " - " + docTitle;
	}
	t = typeof(t) != 'undefined' ? t : "Background Image";
	a = typeof(a) != 'undefined' ? a : "Background Image";
	if(jQuery("#bgcontainer img").attr({ 
		src: b,
		title: t,
		alt: a
		})){
		//jQuery(".bgfront").stop(true, true).fadeOut(50), function(){ jQuery(".bgback").removeClass("bgback").addClass("bgfront"); jQuery(this).removeClass("bgfront").addClass("bgback").fadeIn(0);});
		return true
	}
}
