var wlp = window.location.pathname;
QueryLoader.selectorPreload = "#container";
QueryLoader.init();

$(document).ready(function() {
	if(wlp == "/") {
		// load preview image
		var src = $(".first img").attr("src").replace("small", "large");
		$(".first img").attr("src", src);
		$(".first").css({float : "none", width : "800px"});
		$("#start div:nth-child(2)").css({ paddingLeft : "0"});
		
		// insert twitterbox
		$("#start br.clear").remove("br");
		$("#start").append("<div id='twitterBox' class='post'><div id='tweets'></div><p class='title'>Twitter / News</p></div>");
		$("#start").append("<br class='clear' />");
		$("#container").prepend("<div class='twitterIcon'><img src='/wp-content/themes/default/img/twitter.png' alt='' /></div>");
		$(".twitterIcon").css({position: "absolute", top: "auto", bottom : "50px", right : "45px"});
		
		$(document).bind('loaded', function() { 
			$("#tweets").getTwitter({
				userName: "artischocki",
				numTweets: 2,
				loaderText: "Lade Twitter ...",
				slideIn: true,
				slideDuration: 750,
				showHeading: false,
				showProfileLink: false,
				showTimestamp: false
			});
		});
		
	} else if (wlp.indexOf('2008') != -1 || wlp.indexOf('2009') != -1 || wlp.indexOf('2010') != -1 || wlp.indexOf('2011') != -1 || wlp.indexOf('2012') != -1 || wlp.indexOf('2013') != -1 || wlp.indexOf('2014') != -1) {
		$('.images').cycle({ 
			fx:     'fade', 
			speed:  'fast', 
			timeout: 0, 
			next:   '.next', 
			prev:   '.prev', 
			before: onBefore,
			after: onBefore
		});
		
		$(document).bind('loaded', function() { 
			$(".secDesc").prepend("<li>" + $('ul.images').children().size() + " Fotos</li>");
		});
		
		$(document.documentElement).keyup(function(event) {
			var direction = null;
			if (event.keyCode == 37) {
				direction = 'prev';
			} else if (event.keyCode == 39) {
				direction = 'next';
			}
			if (direction != null) {
				$('#imgDesc').each(function(index) {
					$('.prev, .next', this)[direction]().click();
				});
			}
		});
	}

	function onBefore(curr,next,opts) {
		var $slide = $(next);
		var w = $slide.outerWidth();
		var h = $slide.outerHeight();
		$slide.css({ marginTop: (500 - h) / 2, marginLeft: (500 - w) / 2 });
		if(h == 0 && w == 0) {
			var imagePreloader = new Image();
			imagePreloader.src = $("ul.images:first-child img").attr("src");
			imagePreloader.onload = function() {
				w = imagePreloader.width;
				h = imagePreloader.height;
				$slide.css({ marginTop: (500 - h) / 2, marginLeft: (500 - w) / 2 });
			}; 						
		}
	};

});

