/**
 * jQuery custom for "felixstreuli.ch"
 *
 */
 
// Debug vars
debugArr = [];
debugObj = {};

// Global vars
base_url    = 'http://www.felixstreuli.ch/';

$(function() {
	
	// Detect IEs
	var isIE6 = ($.browser.msie && parseInt(jQuery.browser.version) == 6) ? true : false;
	var isIE7 = ($.browser.msie && parseInt(jQuery.browser.version) == 7) ? true : false;
	var isIE8 = ($.browser.msie && parseInt(jQuery.browser.version) == 8) ? true : false;
	
	$("a").focus(function(){ 
		$(this).blur();
	});
	
	$(".thumbnails-wrapper").focus(function(){ 
		$(this).blur();
	});
		
	$(".nav-item-1").click(function() {
		if ($(this).next("ul").is(":hidden")) {
			$(".nav-level-2").slideUp("fast");
			$(this).next("ul").slideDown("fast");
			$(".nav-item-1").removeClass("nav-item-active");
			$(this).addClass("nav-item-active");
		}
	});
	
	// Images
	$("#thumbnails .inactive img").css("opacity", 0.5);
	$("#thumbnails .current img").css("opacity", 1);
	
	$("#thumbnails .inactive img").hover(function(){
		$(this).css("opacity", 1);
	}, function(){
		if (!$(this).parents("a").hasClass("current")) {
			$(this).css("opacity", 0.5);
		}
	});
	
	$('.thumbnails-wrapper').jScrollPane();
	
	$(".detail").click(function() {
		$(this).doSlide(this);
		return false;
	});
	
	$(".forward").click(function() {
		$(this).forward();
		return false;
	});
	
	$(".backward").click(function() {
		$(this).backward();
		return false;
	});
	
	// Enable Keyboard navigation
	$(document.documentElement).keyup(function(event) {
		if (event.keyCode == 37) {
			$(this).backward();
		}
		 
		if (event.keyCode == 39) {
			$(this).forward();
		}
	});
	
	$.fn.forward = function() {
		var next = $(".current").next("a");
		var max  = $("#image-count").html();
		if ($(next).attr("index") <= parseInt(max)) {
			$(this).doSlide(next);
		}
		var step = $(".current").children("img").width() + 6;
		var api  = $('.thumbnails-wrapper').data('jsp');
		api.scrollBy(parseInt(step),0);	
	}
	
	$.fn.backward = function() {
		var previous = $(".current").prev("a");
		if ($(previous).attr("index") >= 1) {
			$(this).doSlide(previous);
		}
		var step = $(".current").children("img").width() + 6;
		var api  = $('.thumbnails-wrapper').data('jsp');
		api.scrollBy(-parseInt(step),0);	
	}
	
	$.fn.doSlide = function(img) {

		// switch classes for all imgs
		$(".detail").removeClass("current");
		$(".detail").addClass("inactive");
		
		// add classes to current img
		$(img).removeClass("inactive");
		$(img).addClass("current");
		
		// change opacity for both classes
		$("#thumbnails .inactive img").css("opacity", 0.5);
		$("#thumbnails .current img").css("opacity", 1);				
		
		// footer
		var caption = $(img).attr("title");
		var index   = $(img).attr("index");
		
		// image		
		var image = new Image();
  		image.src = $(img).attr("href");
			
		$("#detail-image-loader").show();	
		$(image).load(function() {
			// hide loader and add image, caption & index
			$("#detail-image-loader").hide();
			$("#detail-image").children("img").remove();
			$("#detail-image").append(image);
			$("#image-caption").html(caption);
			$("#image-index").html(index);
			// update wrapper width
			var width = $("#detail-image").children("img").width();
			$(".detail-image-wrapper").css({width: width + "px"});	
		});	
		
	}
	
	
	
});
