
var isAlice;
var lastUrl;
$(document).ready(
	function()
	{
	isAlice = true;
	var hatterPages = "";
		$('#dock').Fisheye(
			{
				maxWidth: 90,
				items: 'a',
				itemsText: 'span',
				container: '.dock-container',
				itemWidth: 55,
				proximity: 60,
				halign : 'center'
			}
		)
		loadImagebox();
		
		var url1 = location.href;
		var url = url1.substring(0,url1.lastIndexOf("/"))+"/";
		replaceLinks(url);
		$.ajaxHistory.initialize(loadFirstPage);

		//catchQueryStringsOnLoad();
		$('#contentBody').before('<div style="display:none" id="loader"><h1 style="color:#7777ff">Loading &nbsp;<span id="pageRef"></span>...</h1><br /><br /><br /><img src="images/ajax-loader.gif" alt="loading...."></img></div>');
		applyBehaviours();
		//hatter manipulation
		$('.dock-container a').each(function(i){
			if(i%2 == 1){
				hatterPages = hatterPages + $(this).attr("href");
			}
		});
		$('#contentBody').ajaxSend(function(a, b, c){
			if(c.type=="GET"){
				$('#pageRef').html(getPureHash());
				$('#loader').slideDown(500);
			} 
		});
		$('#contentBody').ajaxComplete(function(a, b, c){
			if(c.type=="GET"){
			//hatter manipulation
				toggleHatterAlice(hatterPages, isAlice);
				replaceLinks(url);
				applyBehaviours();
			} else {
				assignGuestbookResponse(b.responseText);
			}
		});
		cleanIE6();
	});
	
function applyBehaviours(){
	loadImagebox();
	initialiseGalleryAccordion();
	initialiseEventsAccordion();
	initialiseAccordion();
	initialiseThankYouPage();
	signGuestbook();
	
}
function initialiseThankYouPage(){
	var thankyoupage = $('p.thankyou');
	if(thankyoupage.length == 1) {
		$('#contentBody').css('background', 'white url(images/seasonGreetingsKryz.jpg) no-repeat scroll bottom center');
	}else{
		$('#contentBody').css('background', 'white none no-repeat scroll bottom center');
	}
	
}
function cleanIE6(){
	if( $.browser.msie && (jQuery.browser.version < 6.9) ) {
		$.ifixpng('images/pixel.gif');
		$('img[@src$=.png]').ifixpng();
	}
}
function readmethods(object){
	var k;
	var result = "";
	for(k in object){
		result = result +"\n"+ k;
	}
	return result;
}
function replaceLinks(url){
	$('a').not('.cached, .cachable, .dock-item, .not-cachable').attr('target', "_blank");
	$('a.dock-item, a.cachable').not('.cached').addClass('cached')
		.attr("title", function(){
		  var re = new RegExp("\\bpage=[^&]*[&]*\\b", "g");
			var re2 = new RegExp("\\bcontentOnly=[^&]*[&]*\\b", "g");
			var arguments = substringFromLast($(this).attr("href"), "/").replace(re, "").replace(re2, "").replace("?", "-");
			//alert($(this).attr("href") +" current title:"+ $(this).attr("title"));
			if(arguments.length >1){
			//alert(defaultOnNull($(this).attr("title"), getPureHash()) +"\narguments:"+ arguments.replace(/&/g,";").replace(/=/g,":"));
				return defaultOnNull($(this).attr("title"), getPureHash()) + arguments.replace(/&/g,";").replace(/=/g,":");
			}
			return $(this).attr("title");
		})
		.attr("href",function(){
			var re = new RegExp("\\bpage=[^&]*[&]*\\b", "g");
			var ref = $(this).attr("href").match(re)[0];
			return url+"?"+ ref + "&contentOnly=true";
			
		})
		.fadingAnimated('#contentBody', historyCallback);
}

function historyCallback(){
	$('#loader').hide(500, function(){
		$('#contentBody').fadeTo(700, 100, function(){$(this).loadFinish()});
		$('#footer').show();
		//alert(pageLoad?"true" : "false");
		//pageLoad=false;
		
	});
	
}

function defaultOnNull(value, defaultValue){
	if(value == undefined || value == null){
		return defaultValue;
	}
	return value;
}

function getPureHash(){
	return substringTo(location.hash,"-").replace(/#/,"");
}

function substringTo(string, lastCharacter){
	var lastOccurrence = string.indexOf(lastCharacter);
	lastOccurrence = lastOccurrence == -1 ? string.length : lastOccurrence;
	return string.substring(0, lastOccurrence);
}
function substringFromLast(string, lastCharacter){
	var lastIndex = string.lastIndexOf(lastCharacter == undefined ? "/" : lastCharacter);
	if(lastIndex != -1)
		return string.substring(lastIndex+1);
	return string;
}
function loadFirstPage(){
	var phpArguments = substringFromLast(location.href, "/");
	//alert(phpArguments + "size" + phpArguments.length);
	if(phpArguments == ""){
		phpArguments = "?page=home&contentOnly=true";
	}else{
		phpArguments = phpArguments + "&contentOnly=true";
	}
	var target = $('#contentBody');
	$('#footer').hide();
	target.fadeTo(700, 0 ,function(){ 
		target.hide();
		target.load(phpArguments, function() {
			historyCallback();
		});
	});
}

function initialiseAccordion(){
	if($('.accordion').length > 0){
		$('.accordion .accordionText').hide(); 
		$('.accordion .accordionText').eq(0).show();
		$('.accordion .accordionTitle').click(function() {
			var $nextDiv = $(this).nextAll(".accordionText").eq(0);
			var $visibleSiblings = $(this).parent().parent().find('.accordionText:visible');
			if ($visibleSiblings.length ) {
				$visibleSiblings.slideUp('fast', function() {
					$nextDiv.slideToggle('fast');
				});
			} else {
				 $nextDiv.slideToggle('fast');
			}
		});
	}
}
var currentGallery;
function initialiseGalleryAccordion(){
	if($('.accordionFloating').length >0){
		$('.floating-' + currentGallery).nextAll(".accordionFloatingBlock").eq(0).addClass('showOnLoad');
		$('.accordionFloatingBlock').not('.showOnLoad').hide(); 
		$('.accordionFloatingTitle').click(function() {
			var gallerySection = $(this).find('img').attr('alt');
			var shownBlock = $(this).nextAll(".accordionFloatingBlock").eq(0);
			shownBlock.find('img').addClass(gallerySection).click(function(){
				currentGallery = $(this).attr('class');
			});
			$(this).nextAll(".accordionFloatingBlock").eq(0).slideToggle('fast');
		});
	}
}
function isEventRoot(){
	return location.hash.replace("#", "") == getPureHash() ;
}
function initialiseEventsAccordion(){
	
	if($('.eventMenu').length >0){
		$('.eventContent').not('.active').hide();
		$('.eventMenuTitle').not('.active').children("img.arrow").attr("src", "images/arrowUp.gif");
		$('.rightMenuWrapper').prepend('<img class="toggleMenuOpen" style="cursor:pointer;" alt="menu" src="images/evenMenuToggler.jpg"></img>');
		$('.eventMenuContentWrapper .borderTL').after('<img class="borderTR toggleMenuClose" style="cursor:pointer;margin: 0px;" alt="Close Menu" src="images/eventMenuClose.gif"/></img>');
		if(isEventRoot()){
			$('.toggleMenuOpen').hide();
		} else{
			$('.toggleMenuClose').hide();
			$('.eventMenuContentWrapper').hide();
			$('.eventMenuContentWrapper').width(40);
		}
		
		$('.toggleMenuOpen').click(function(){
			$('.toggleMenuOpen').hide();
			$('.eventMenuContentWrapper').show();
			$('.eventMenuContentWrapper').animate({ width:"220px"}, 600, function(){
				$('.toggleMenuClose').show();
			});
			
		});
		$('.toggleMenuClose').click(function(){
			$('.toggleMenuClose').hide();
			$('.eventMenuContentWrapper').animate({ width:"40px"}, 600, function(){
				$(this).hide();
				$('.toggleMenuOpen').show();
			});
			
		});
		$('.eventMenuTitle').click(function() {
			$(this).nextAll(".eventContent").eq(0).slideToggle('fast');
			var arrowImage = $(this).children("img.arrow");
			var arrowLoc = arrowImage.attr("src");
			if(arrowLoc.lastIndexOf( "arrowUp.gif") != -1){
				$(this).children("img.arrow").attr("src", "images/arrowDown.gif");
			} else {
				$(this).children("img.arrow").attr("src", "images/arrowUp.gif");
			}
			
		});
	}
}
function loadImagebox(){
	if($('#ImageBoxOverlay').length == 0 &&  ($('#contentBody a[@rel^="imagebox"]').length > 0 || $.browser.msie)){
		$.ImageBox.init(
			{
				loaderSRC: 'images/imagebox/loading.gif',
				closeHTML: '<img src="images/imagebox/close.jpg" style="margin-top: 5px" />',
				textImage: '<em class="keyboardShortcuts">N</em>: Next image, <em class="keyboardShortcuts">P</em>: Previous image, <em class="keyboardShortcuts">Esc</em>: exit</em><br/>Image '
			}
		);
	} else if($('#contentBody a[@rel^="imagebox"]').length > 0) {
		$.ImageBox.restart(
			{
				loaderSRC: 'images/imagebox/loading.gif',
				closeHTML: '<img src="images/imagebox/close.jpg" style="margin-top: 5px" />',
				textImage: 'Press N to see the next P for the previous and esc to exit<br/>Image '
			}
		);
	}
	
}


function toggleHatterAlice(hatterPages){
	var hash = location.hash;
	if(hatterPages.indexOf(hash)==-1 && !isAlice){
		isAlice = true;
		$("#hatter").animate({ width:"0px" }, 500,function(){
			$("#aliceimage").html("<img id=\"alice\" src=\"images/Alice_by_kryzad_terrodash_small.png\" width=\"0px\" height=\"450\" alt=\"Image created by Lurker, you can find other artworks on http://kryzad-terrodash.deviantart.com/\" />");
			$("#alice").animate({width:"300px"}, 500);
			cleanIE6();
		});
	}else if(isAlice && hatterPages.indexOf(hash)!=-1){
		isAlice = false;
		$("#alice").animate({ width:"0px" }, 500,function(){
		$("#aliceimage").html("<img id=\"hatter\" src=\"images/Hatter_by_kryzad_terrodash copy.png\" width=\"0px\" height=\"450\" alt=\"Image created by Lurker, you can find other artworks on http://kryzad-terrodash.deviantart.com/\" />");
			$("#hatter").animate({width:"300px"}, 500);
			cleanIE6();
			
		});
	}
}

function signGuestbook(){
	//$('#guestbookForm').removeAttr('action');
	$('#submitNoscript').hide();
	$('#submit').show().attr('onsubmit', 'javascript: return false;').click(function(){
		var sendValues = {action:"ak_attach", 'cookie': encodeURIComponent(document.cookie)};
		$('#guestbookForm input, #guestbookForm textarea').not('#human').each(function(){
			if($(this).attr('value') != undefined){
				sendValues[$(this).attr('name')] = $(this).attr('value');
			} else if ($(this).html() != undefined ){
				sendValues[$(this).attr('id')] = $(this).html();
			}
		});	
		jQuery.post('?page=sign&contentOnly=true', $.param(sendValues));
	});
}
function assignGuestbookResponse(data){
			if(data.search(/id='signAccepted'/) != -1){
				$('#guestbookErrorContainer').html("");
				$('#guestbookContainer').html(data);
			} else {
				$('#guestbookErrorContainer').html(data);
				$('.captchaImage').attr('src', '#').attr('src', 'php5/captcha.php?'+new Date().getTime());
			}
}