$(document).ready(function(){
	// $(document).pngFix();

	$('body').addClass('js');

	// REFERENZEN //////////////////////////////////////////////////////////////

	if($('ul.refs').length>0) {
		$('ul.refs').load('/referenzen/ajax/1/', function(){
			positionOverlays(false);

			$(window).resize(function(){
				positionOverlays(false);
			})

			$('ul.referenceNav a').click(function(){
				$('ul.referenceNav li').removeClass('current');
				$(this).parent().addClass('current');

				var catId = getAnchorVal($(this).attr('href'), 'cat');

				if(catId>0) {
					$('.refs li.ref, #refSelector ul li').each(function(){
						if($(this).hasClass('cat'+catId)) {
							$(this).slideDown();
						} else {
							$(this).slideUp();
						}
					});
				} else {
					$('.refs li.ref').slideDown();
				}

				$.scrollTo(0);
				$('.refs ul.pager li').removeClass('current');
				$('.refs ul.pager li[rel=r1]').addClass('current');
				$('.refs ul.images').animate({'left': '0px'});

				window.setTimeout(function(){ $(window).resize() }, 500);
			});

			$('.refs .pager a').click(function(){
				var pager = $(this).parent().parent();

				var ref = getAnchorVal($(this).attr('href'), 'ref');
				var go = getAnchorVal($(this).attr('href'), 'go');

				var currentPosition = parseInt($('li.current a', pager).text());
				var totalPositions = $('li', pager).length - 2;

				if(go<1 || go>totalPositions) {
					go = 1;
				} else {
					go = parseInt(go);
				}

				$('li.current', pager).removeClass('current');
				$('li[rel=r'+go+']', pager).addClass('current');

				if(go==1) {
					var prevGo = 1;
					var nextGo = 2;
				} else if(go==totalPositions) {
					var prevGo = (totalPositions-1);
					var nextGo = totalPositions;
				} else {
					var prevGo = (go-1);
					var nextGo = (go+1);
				}

				$('li.next a', pager).attr('href', $('li[rel=r'+nextGo+'] a', pager).attr('href'));
				$('li.prev a', pager).attr('href', $('li[rel=r'+prevGo+'] a', pager).attr('href'));

				var images = $('.refs li.ref[rel=r'+ref+'] ul.images');

				var img = $('.refs li.ref[rel=r'+ref+'] ul.images li[rel=r'+go+']');
				var pos = img.position();
				$(images).animate({'left': '-'+pos.left+'px'});
			});

			// $('#refSelectorOpen').click(function(){
			// 	$('#refSelector ul').toggle();
			// 	return false;
			// });
			// 
			// $('#refSelector ul a').click(function(){
			// 	var ref = getAnchorVal($(this).attr('href'), 'ref');
			// 	$.scrollTo(
			// 		$('#ref li.ref[rel=r'+ref+']').offset().top - $('#fwHead').height(),
			// 		250
			// 	);
			// 
			// });

			window.setTimeout(function(){
				var cat = 0;
				if(cat = getAnchorVal(window.location.href, 'cat')) {
					$('ul.referenceNav a').each(function(){
						var aCat = getAnchorVal($(this).attr('href'), 'cat');
						if(cat==aCat) {
							$(this).click();
						}
					});
				}

				var ref = getAnchorVal(window.location.href, 'ref');
				if(ref && $('.refs li.ref[rel=r'+ref+']').length>0) {
					$.scrollTo(
						$('.refs li.ref[rel=r'+ref+']').offset().top - $('#fwHead').height(),
						250
					);

					window.setTimeout(function(){
						var go = getAnchorVal(window.location.href, 'go');
						if(go && $('.refs li.ref[rel=r'+ref+'] ul.pager li[rel=r'+go+'] a').length>0) {
							$('.refs li.ref[rel=r'+ref+'] ul.pager li[rel=r'+go+'] a').click();
						}
					}, 250);
				}
			}, 500);
		});
	}

	// CREDO ///////////////////////////////////////////////////////////////////

	if($('#fwContent.uid23').length>0) {
		window.setTimeout(function(){
			$('#fwContent.uid23 h2 img').fadeIn();
		}, 2000);
		window.setTimeout(function(){
			$('#fwContent.uid23 .indented').fadeIn();
		}, 4000);
	}

});

function getAnchorVal(href, key) {
	var anchor = href.substr(href.indexOf('#')+1);
	var bits = anchor.split(/,/);
	for(x=0; x<bits.length; x++) {
		bitBits = bits[x].split(/:/);
		if(bitBits[0]==key) {
			return bitBits[1];
		}
	}
	return false;
}

function positionOverlays(animate) {
	if($(window).width() < $('#fwContentInner').width()) {
		$('body').addClass('notWideEnough');
	} else {
		$('body').removeClass('notWideEnough');

		var leftWidth = parseInt($('#fwContentInner').offset().left + 15);
		var rightWidth = parseInt($('#fwContent').width() - $('#fwContentInner').offset().left - $('#fwContentInner').width() - 15);

		if(animate) {
			$('#left').animate({width: leftWidth});
			$('#right').animate({width: rightWidth});
		} else {
			$('#left').css({width: leftWidth});
			$('#right').css({width: rightWidth});
		}
	}
}
