var sDirection = '';
var sCurrentId = '';
var sNextId = '';
var iCurrentPos = -1;
var iPrevPos = -1;
var iNextPos = -1;
var bGettingPage = false;
var iImagesToLoad = 0;
var igalleryCount = 0;
var oFailSafe = 0;

$(document).ready(function() {
	$('#sitemap').append('<img id="toggle-button" src="/assets/images/show_sitemap.png" width="9" height="34" alt="Sitemap zeigen" title="Sitemap zeigen" onclick="show_sitemap()" />');
	$('#sitemap').css({left: 644});
	$('#sitemap').css({cursor: 'pointer'});
	
	$('#loader').fadeOut(0);
	$('#navi-main .container').hide();
	
	if ($('#navi-main li.active').index() == 0) {
		iCurrentPos = iPrevPos = 0;
		iNextPos = 1;
	} else if ($('#navi-main li.active').index() == $('#navi-main li:last').index()) {
		iCurrentPos = iNextPos = $('#navi-main a').length - 1;
		iPrevPos = $('#navi-main a').length - 2;
	} else {
		if ($('#navi-main li.active').length == 0) {
			iPrevPos = $('#navi-main li').length - 1;
		} else {
			iCurrentPos = $('#navi-main li.active').index();
			iPrevPos = iCurrentPos - 1;
			iNextPos = iCurrentPos + 1;
		}
	}
	$('#navi-main').append('<a id="show-prev"><img width="15" height="24" /></a>');
	$('#navi-main').append('<a id="show-next"><img width="15" height="24" /></a>');
	if (iCurrentPos == iPrevPos) {
		$('#show-prev img').attr('src', '/assets/images/showPrev_dis.png');
	} else {
		var sPrevUrl = $('#navi-main a:eq(' + iPrevPos + ')').attr('href');
		$('#show-prev img').attr('src', '/assets/images/showPrev.png');
		$('#show-prev').attr('href', sPrevUrl);
	}
	if (iCurrentPos == iNextPos) {
		$('#show-next img').attr('src', '/assets/images/showNext_dis.png');
	} else {
		var sNextUrl = $('#navi-main a:eq(' + iNextPos + ')').attr('href');
		$('#show-next img').attr('src', '/assets/images/showNext.png');
		$('#show-next').attr('href', sNextUrl);
	}
	
	$('#navi-main img').removeAttr('title');
	$('#navi-main img').removeAttr('width');
	$('#navi-main img').removeAttr('height');
	$('#navi-main img').attr('alt', function(index, attr){return $(this).attr('src');});
	
	$('#navi-main .container').removeAttr('style');
	$('#navi-main .container').css('height', 'auto');
	$('#navi-main .container').css('width', '100%');
	
	$('#navi-main ul a').appendTo('#navi-main .container');
	$('#navi-main .container ul').detach();
	
	var dockOptions = {
		align: 'bottom',
    	labels: 'tc',
    	size: 55,
    	duration: 200
    };
    $('#navi-main .container').jqDock(dockOptions);
    
    $('#navi-main .container a').append('<div class="highlight">&nbsp;</a>');
    $('#navi-main .container a:eq(' + iCurrentPos + ')').addClass('active');
	
	
	if ($('.navi-sub').length > 0) {
		sAboveUrl = '';
		sBelowUrl = '';
		
		if ($('.navi-sub li.active').length > 0) {
			iSubPos = $('.navi-sub li.active').index();
			
			if (iSubPos < $('.navi-sub li:last').index()) {
				if (iSubPos == 0) {
					sAboveUrl =  $('#navi-main li.active a').attr('href');
				} else {
					sAboveUrl =  $('.navi-sub li:eq(' + (iSubPos - 1) + ') a').attr('href');
				}
				
				sBelowUrl =  $('.navi-sub li:eq(' + (iSubPos + 1) + ') a').attr('href');
			} else {
				sAboveUrl =  $('.navi-sub li:eq(' + (iSubPos - 1) + ') a').attr('href');
			}
			
			$('#temp1 .navi-sub li.active').removeClass('active');
		} else {
			sBelowUrl = $('.navi-sub li:first a').attr('href');
		}
		
		if (sAboveUrl != '') {
			$('.navi-sub').append('<a class="show-above" href="' + sAboveUrl + '"><img src="/assets/images/showAbove.png" width="24" height="15" /></a>');
		} else {
			$('.navi-sub').append('<a class="show-above"><img src="/assets/images/showAbove_dis.png" width="24" height="15" /></a>');
		}
		if (sBelowUrl != '') {
			$('.navi-sub').append('<a class="show-below" href="' + sBelowUrl + '"><img src="/assets/images/showBelow.png" width="24" height="15" /></a>');
		} else {
			$('.navi-sub').append('<a class="show-below"><img src="/assets/images/showBelow_dis.png" width="24" height="15" /></a>');
		}
	}
	
	for (i = 0; i < $('a').length; i++) {
		if ($('a:eq(' + i + ')').attr('href')) {
			var sUrl = $('a:eq(' + i + ')').attr('href');
			if (sUrl.search(/:/) != -1) continue;
			if (sUrl.search(/\.(jpg|gif|png|pdf|mp4|swf|flv)$/i) != -1) continue;
			
			$('a:eq(' + i + ')').attr('href', "javascript:get_page('" + sUrl + "')");
		}
	}
	
	$("a[rel^='prettyPhoto']").attr('rel', 'prettyPhoto[' + (++igalleryCount) + ']');
	
	$("a[rel^='prettyPhoto']").prettyPhoto({
		theme: 'light_rounded',
		overlay_gallery: false
	});
	
	if (location.hash != '') {
		sName = location.hash.substring(1);
		if (sName == 'Impressum') {
			sHref = $('#footer a').attr('href');
		} else {
			sHref = $('#sitemap a:contains("' + sName + '")').attr('href');
		}
		
		sUrl = sHref.match(/'[^']*/) + '';
		get_page(sUrl.substring(1));
	}
});

function hide_sitemap() {
	$('#sitemap #toggle-button').remove();
	$('#sitemap').append('<img id="toggle-button" src="/assets/images/show_sitemap.png" width="9" height="34" alt="Sitemap zeigen" title="Sitemap zeigen" onclick="show_sitemap()" />');
	$('#sitemap').css({cursor: 'pointer'});
	
	$('#sitemap').animate({left: 644});
}

function show_sitemap() {
	$('#sitemap #toggle-button').remove();
	$('#sitemap').append('<img id="toggle-button" src="/assets/images/hide_sitemap.png" width="9" height="34" alt="Sitemap verstecken" title="Sitemap verstecken" onclick="hide_sitemap()" />');
	$('#sitemap').css({cursor: 'auto'});
	
	$('#sitemap').animate({left: 784});
}

function get_page(sUrl) {
	window.clearTimeout(oFailSafe);
	
	var iSelfFather = -1;
	var iSelfPos = -1;
	var iPageFather = -1;
	var iPagePos = -1;
	
	if (bGettingPage || sUrl == sCurrentPage) return;
	bGettingPage = true;
	
	//Check navi-indexes of old and new page
	for (i = 0; i < aPageTree.length; i++) {
		if (aPageTree[i]['url'] == sCurrentPage) iSelfPos = i;
		else if (aPageTree[i]['url'] == sUrl) iPagePos = i;
		
		for (j = 0; j < aPageTree[i]['children'].length; j++) {
			if (aPageTree[i]['children'][j]['url'] == sCurrentPage) {
				iSelfFather = i;
				iSelfPos = j;
			} else if (aPageTree[i]['children'][j]['url'] == sUrl) {
				iPageFather = i;
				iPagePos = j;
			}
			
			if (iSelfPos != -1 && iPagePos != -1) break;
		}
		
		if (iSelfPos != -1 && iPagePos != -1) break;
	}
	if (iSelfPos == -1) iSelfPos = 999;
	if (iPagePos == -1) iPagePos = 999;
	
	//Compute wipe direction
	sDirection = 'right';
	if (
		(iSelfFather == -1 && iSelfPos == iPageFather) ||
		(iPageFather == -1 && iPagePos == iSelfFather) ||
		(iSelfFather != -1 && iSelfFather == iPageFather)
	) {
		if (iSelfFather == -1) sDirection = 'up';
		else if (iPageFather == -1) sDirection = 'down';
		else {
			sDirection = (iSelfPos > iPagePos)? 'down': 'up';
		}
	} else {
		if (iSelfFather == iPageFather) {
			sDirection = (iSelfPos > iPagePos)? 'right': 'left';
		} else {
			if (iSelfFather == -1) {
				sDirection = (iSelfPos > iPageFather)? 'right': 'left';
			} else if (iPageFather == -1) {
				sDirection = (iSelfFather > iPagePos)? 'right': 'left';
			} else {
				sDirection = (iSelfFather > iPageFather)? 'right': 'left';
			}
		}
	}
	
	//Initialize main and sub-navi
	sNextId = 'main_' + (iPageFather + 1) + '_' + (iPagePos + 1);
	if (sCurrentId == '') {
		sCurrentId = 'main_' + (iSelfFather + 1) + '_' + (iSelfPos + 1);
		$('.main').attr('id', sCurrentId);
		
		if ($('.navi-sub').length > 0) {
			sSubId = (iSelfFather == -1)? 'navi-sub_' + (iSelfPos + 1): 'navi-sub_' + (iSelfFather + 1);
			$('.navi-sub').attr('id', sSubId);
			$('.navi-sub').append('<div class="sub-highlight">&nbsp;</div>');
			if ($('.navi-sub li.active').length > 0) {
				var oNaviOffset = $('.navi-sub').offset();
				var oActiveOffset = $('.navi-sub li.active').offset();
				var sTop = (oActiveOffset.top - oNaviOffset.top) + 'px';
				$('.sub-highlight').css({left: 10, top: sTop});
				$('.navi-sub li.active').removeClass('active');
			}
		}
	}
	
	//get new page via ajax
	if ($('#' + sNextId).length > 0) wipe_pages();
	else {
		$('#loader').css('display', 'block');
		$('#loader').fadeTo(200, .8);
		sUrl += (sUrl.search(/\?/) == -1)? '?ajax=1': '&ajax=1';
		
		$.get(
			sUrl,
			function(data) {
				$('body').append('<div id="temp1" style="display:none"></div>');
				$('body').append('<div id="temp2" style="display:none"></div>');
				$('#temp1').append(data);
				
				for (i = 0; i < $('#temp1 a').length; i++) {
					if ($('#temp1 a:eq(' + i + ')').attr('href')) {
						var sUrl = $('#temp1 a:eq(' + i + ')').attr('href');
						if (sUrl.search(/:/) != -1) continue;
						if (sUrl.search(/\.(jpg|gif|png|pdf|mp4|swf|flv)$/i) != -1) continue;
						
						$('#temp1 a:eq(' + i + ')').attr('href', 'javascript:get_page("' + sUrl + '")');
					}
				}
				
				$('#temp1 .main').attr('id', sNextId);
				$('#temp1 .main').clone().appendTo('#temp2');
				
				sSubId = (iPageFather == -1)? 'navi-sub_' + (iPagePos + 1): 'navi-sub_' + (iPageFather + 1);
				if ($('#temp1 .navi-sub').length > 0) {
					if ($('#' + sSubId).length < 1) {
						$('#temp1 .navi-sub').attr('id', sSubId);
						$('#temp1 .navi-sub li.active').removeClass('active');
						$('#temp1 .navi-sub').clone().appendTo('#temp2');
					}
				}
				
				$('#temp1').remove();
				$('#temp2>*').clone().prependTo('#mask');
				$('#temp2').remove();
				
				$('#' + sNextId + " a[rel^='prettyPhoto']").attr('rel', 'prettyPhoto[' + (++igalleryCount) + ']');
				
				$('#' + sNextId + " a[rel^='prettyPhoto']").prettyPhoto({
					theme: 'light_rounded',
					overlay_gallery: false
				});
				
				// waiting for alll images to be loaded
				if ($('#' + sNextId + ' img').length > 0) {
					igalleryCount = $('#' + sNextId + ' img').length;
					$('#' + sNextId + ' img').bind('load', function() {
						if (--igalleryCount < 1) wipe_pages();
					});
				} else {
					wipe_pages();
				}
				oFailSafe = window.setTimeout('fail_safe()', 5000);
			}
		);
	}
	
	//create main-navi arrows
	var iNextPos = (iPageFather == -1) ? iPagePos: iPageFather;
	if (iNextPos == 999) iNextPos = $('#navi-main .container a').length;
	if (iNextPos == 0) {
		$('#show-prev img').attr('src', '/assets/images/showPrev_dis.png');
		$('#show-prev').removeAttr('href');
	} else {
		var sPrevUrl = $('#navi-main .container a:eq(' + (iNextPos - 1) + ')').attr('href');
		$('#show-prev img').attr('src', '/assets/images/showPrev.png');
		$('#show-prev').attr('href', sPrevUrl);
	}
	if (iNextPos < $('#navi-main .container a').length - 1) {
		var sNextUrl = $('#navi-main .container a:eq(' + (iNextPos + 1) + ')').attr('href');
		$('#show-next img').attr('src', '/assets/images/showNext.png');
		$('#show-next').attr('href', sNextUrl);
	} else {
		var sPrevUrl = $('#navi-main .container a:eq(' + (iNextPos - 1) + ')').attr('href');
		$('#show-next img').attr('src', '/assets/images/showNext_dis.png');
		$('#show-next').removeAttr('href');
	}
	
	//Setting page title and history hash
	sTitle = 'SICHTBAHR - bewegte Kommunikation | ';
	if (iPagePos == 999) {
		sTitle += 'Impressum';
		location.hash = 'Impressum';
	} else {
		if (iPageFather != -1) {
			sTitle += aPageTree[iPageFather]['title'] + ' | ';
			sTitle += aPageTree[iPageFather]['children'][iPagePos]['title'];
			location.hash = aPageTree[iPageFather]['children'][iPagePos]['title'];
		} else {
			sTitle += aPageTree[iPagePos]['title'];
			location.hash = aPageTree[iPagePos]['title'];
		}
	}
	document.title = sTitle;
	
	sCurrentPage = sUrl;
}

function fail_safe() {
	if ($('#loader').css('display') != 'none') wipe_pages();
}

function wipe_pages() {
	$('#navi-main .container').show();
	if (sCurrentId == sNextId) return true;
	
	var sStartTop = 0;
	var sStartLeft = 0;
	var sEndTop = 0;
	var sEndLeft = 0;
	var sOldSubId = '';
	var sNewSubId = '';
	
	// Compute position values
	switch(sDirection) {
		case 'left':
			sStartLeft = $('#' + sCurrentId).width();
			sEndLeft = -1 * sStartLeft;
		break;
		case 'right':
			sEndLeft = $('#' + sCurrentId).width();
			sStartLeft = -1 * sEndLeft;
		break;
		case 'up':
			sStartTop = $('#' + sCurrentId).height();
			sEndTop = -1 * sStartTop
		break;
		case 'down':
			sEndTop = $('#' + sCurrentId).height();
			sStartTop = -1 * sEndTop;
		break;
	}
	
	//place new page at start position
	$('#' + sNextId).css('left', sStartLeft);
	$('#' + sNextId).css('top', sStartTop);
	
	//wipe pages
	sEndLeft += 'px';
	sEndTop += 'px';
	$('#loader').fadeTo(200, 0, function() {$('#loader').css('display', 'none')});
	$('#' + sCurrentId).css({left: 0, top: 0});
	$('#' + sCurrentId).animate({left: sEndLeft, top: sEndTop}, 500, function() {bGettingPage = false});
	$('#' + sNextId).animate({left: 0, top: 0}, 500);
	
	//wipe fake hotspot for navi-main
	var iCurrentFatherId = $('#navi-main li.active').index();
	var iFatherId = sNextId.match(/_\d+_/) + '';
	iFatherId = iFatherId.substring(1, iFatherId.length - 1) - 1;
	var iChildId = sNextId.match(/_\d+$/) + '';
	iChildId = iChildId.substring(1) - 1;
	var iNewId = (iFatherId == -1)? iChildId: iFatherId;
	if (iNewId != 999) {
		if ($('#navi-main a.active').length > 0) {
			var iOldPos = $('#navi-main .container a.active').offset();
			var iOldWidth = $('#navi-main .container a.active').outerWidth();
			var iNewPos = $('#navi-main .container a:eq(' + iNewId + ')').offset();
			var iNewWidth = $('#navi-main .container a:eq(' + iNewId + ')').outerWidth();
			var sNewLeft = iNewPos.left + 'px';
			var sNewWidth = iNewWidth + 'px';
			$('body').append('<div id="highlight">&nbsp;</div>');
			$('#highlight').css('left', iOldPos.left);
			$('#highlight').css('width', iOldWidth);
			$('#navi-main a.active').removeClass('active');
			$('#highlight').animate({left: sNewLeft, width: sNewWidth}, 500, function() {
 				$('#navi-main .container a:eq(' + iNewId + ')').addClass('active');
				$('#highlight').remove();
			});
		} else {
			$('#navi-main .container a:eq(' + iNewId + ')').addClass('active');
		}
	} else {
		$('#navi-main a.active').removeClass('active');
	}
	
	//wipe hotspot for navi-sub
	if (iFatherId != -1) {
		var oNewNaviOffset = $('#navi-sub_' + (iFatherId + 1)).offset();
		var oNewActiveOffset = $('#navi-sub_' + (iFatherId + 1) + ' li:eq(' + iChildId + ')').offset();
		var sNewTop = (oNewActiveOffset.top - oNewNaviOffset.top) + 'px'
		
		if (iFatherId == iCurrentFatherId && sCurrentId != 'main_0_' + (iCurrentFatherId + 1)) {
			$('#navi-sub_' + (iFatherId + 1) + ' .sub-highlight').animate({left: 10, top: sNewTop}, 500);
		} else {
			if ($('#navi-sub_' + (iFatherId + 1) + ' .sub-highlight').length < 1) {
				$('#navi-sub_' + (iFatherId + 1)).append('<div class="sub-highlight">&nbsp;</div>');
			}
			$('#navi-sub_' + (iFatherId + 1) + ' .sub-highlight').css({left: 10, top: sNewTop});
		}
	} else if ($('#navi-sub_' + (iChildId + 1)).length > 0) {
		$('#navi-sub_' + (iChildId + 1) + ' .sub-highlight').remove();
	}
	
	//wipe navi-sub
	sOldSubId = 'navi-sub_' + (iCurrentFatherId + 1);
	sNewSubId = 'navi-sub_' + (iNewId + 1);
	if (sDirection == 'right' || sDirection == 'left') {
		$('#' + sOldSubId).css('left', 0);
		$('#' + sNewSubId).css('left', sStartLeft);
		$('#' + sOldSubId).animate({left: sEndLeft}, 500);
		$('#' + sNewSubId).animate({left: 0}, 500);
	}
	
	//create sub-navi arrows
	if ($('#' + sNewSubId).length > 0) {
		if ($('#' + sNewSubId + ' .show-above').length < 1) $('#' + sNewSubId).append('<a class="show-above"><img width="24" height="15" /></a>');
		if ($('#' + sNewSubId + ' .show-below').length < 1) $('#' + sNewSubId).append('<a class="show-below"><img width="24" height="15" /></a>');
		
		if (iFatherId != -1) {
			if (iChildId < $('#' + sNewSubId + ' li').length - 1) {
				if (iChildId == 0) {
					$('#' + sNewSubId + ' .show-above').removeAttr('href');
					$('#' + sNewSubId + ' .show-above img').attr('src', '/assets/images/showAbove_dis.png');
				} else {
					sAboveUrl = $('#' + sNewSubId + ' li:eq(' + (iChildId - 1) + ') a').attr('href');
					$('#' + sNewSubId + ' .show-above').attr('href', sAboveUrl);
					$('#' + sNewSubId + ' .show-above img').attr('src', '/assets/images/showAbove.png');
				}
				
				sBelowUrl = $('#' + sNewSubId + ' li:eq(' + (iChildId + 1) + ') a').attr('href');
				$('#' + sNewSubId + ' .show-below').attr('href', sBelowUrl);
				$('#' + sNewSubId + ' .show-below img').attr('src', '/assets/images/showBelow.png');
			} else {
				sAboveUrl = $('#' + sNewSubId + ' li:eq(' + (iChildId - 1) + ') a').attr('href');
				$('#' + sNewSubId + ' .show-above').attr('href', sAboveUrl);
				$('#' + sNewSubId + ' .show-above img').attr('src', '/assets/images/showAbove.png');
				
				$('#' + sNewSubId + ' .show-below').removeAttr('href');
				$('#' + sNewSubId + ' .show-below img').attr('src', '/assets/images/showBelow_dis.png');
			}
		} else {
			$('#' + sNewSubId + ' .show-above').removeAttr('href');
			$('#' + sNewSubId + ' .show-above img').attr('src', '/assets/images/showAbove_dis.png');
			
			sBelowUrl = $('#' + sNewSubId + ' li:first a').attr('href');
			$('#' + sNewSubId + ' .show-below').attr('href', sBelowUrl);
			$('#' + sNewSubId + ' .show-below img').attr('src', '/assets/images/showBelow.png');
		}
	}
	
	sCurrentId = sNextId;
}
