(function($){
  $.fn.shuffle = function() {
    return this.each(function(){
      var items = $(this).children();
      return (items.length) ? $(this).html($.shuffle(items)) : this;
    });
  }
 
  $.shuffle = function(arr) {
    for(
      var j, x, i = arr.length; i;
      j = parseInt(Math.random() * i), x = arr[--i], arr[i] = arr[j], arr[j] = x
    );
    return arr;
  }
})(jQuery);

$(document).ready(function() {
	
	$('a[href^="http://"]:not(a[href^="http://player.vimeo.com"]),a[href^="https://"],#main ul.press li a, a[rel="external"]').click( function() {
        window.open( $(this).attr('href') );
        return false;
    });

	
	Shadowbox.init();
	
	/* START Top Navi */
	

	if(!$.browser.msie) {
		
		HidePanel();

		$("div.panel").mouseover(function() {
			$(this).show();
	 		$(this).prev().addClass("over");
	 	});
	 	$("div.panel").mouseout(function() {
	 		$(this).prev().removeClass("over");
	 	});

		$("li.top").mouseenter(function() {
			HidePanel()
			$(this).find("div.panel").fadeIn("fast");
			$("div.navrow").show();
		});
		$("li.top").mouseleave(function() {
			HidePanel()
		});	
		$("#header, #intro").mouseenter(function() {
			HidePanel()
		});
		
	}
	
	
	
	$("#nav li.active").parents("li.top").addClass("active")
	
	/* END Top Navi */
	
	if(!$.browser.msie) {
	
		/* Fade effect on News & events */
		$("#aktuell li a").mouseenter(function() {
			$(this).animate({"backgroundColor": "rgba(229,226,217,0.95)"}, "fast").animate({"backgroundColor": "rgba(229,226,217,0.05)"}, "slow");
		});
		
	} 

	$('.jcarousel-skin-tango').shuffle();
	
	$('.jcarousel-skin-tango').jcarousel({
	    start: 		1,
		scroll:		1,
		auto: 		10,
		easing: 	"swing",
		wrap:		"both",
		itemFirstInCallback: {
		  onBeforeAnimation: cb_Before,
		  onAfterAnimation: cb_After
		},
		initCallback: InitCarousel
	});
	
	// Newsletter
	
	$("#newsletter form #buttons input").click(function() {
		var p = "/assets/data/index.php";
		var e = $("#tf_email").val();
		var s = ($(this).attr("id") == "btn_subscribe") ? 1 : 0;
		var url = p + "?tf_email=" + e + "&tf_subscribe=" + s;
		$("#frm_msg").load(url, function() {});
		return false;
	});
	
	// Mitarbeiter
	
	$('div.bio-item blockquote p').addClass("hidden");
	
	$('div.bio-item blockquote h5').click(function() {
		$(this).parent().find("p").toggleClass("hidden");
	});
	
	// Ende
	
	$('#content #right #sidebar #sb_literatur li span.info').addClass("hidden");
	
	$('#content #right #sidebar #sb_literatur li span.title').click(function() {
		$(this).next().toggleClass("hidden");
	});
	
	$('.sb_box:eq(0) .plus').hide();
	
	$('#fs1 input').customInput();
	
	$('#register select').change(function() {
		$('#query_options').submit();
	})
	
	
	$('#fs1 h5').data("state", "off");
	
	$('#fs1 h5').click(function() {
		toggleCB($(this));
	});
	
	$('#t_results tr').click(function() {
		var link = $(this).find('a').attr('href');
		location.href = link;
	});
	
	$('#t_results tr:last td').css("border", "none");
	

	$("#t_results").tablesorter({
		sortList: [[0,0]] 
	});
	
	$('#de .startnewsitem').click(function() {
		var link = $(this).find('a').attr('href');
		location.href = link;
	});
	
	$('#de #startnewslist img[alt="IntroPic"]').attr("src", "http://kreativgesellschaft.org/assets/images/news/wip_start.png");
	
	$('#work-in-progress-16 #intro img, #work-in-progress-17 #intro img, #work-in-progress-18 #intro img').click(function() {
		var link = "http://www.work-in-progress-hamburg.de/anmeldung";
		window.open( link );
	}).css("cursor","pointer");
	
	$('#intro .jcarousel-prev, #intro .jcarousel-next').css("display","none");
	
});

function cb_Before(carousel, li, index, state) {	
	//alert("cb_Before");
}

function cb_After(carousel, li, index, state) {	
	//alert("cb_After");
}

function InitCarousel (carousel, state) {
	//alert("InitCarousel");
}

function toggleCB(e) {
	if($(e).data("state") == "off") {
		$(e).text("Alle aus");
		$(e).data("state","on");
		$('#fs1 input').attr("checked", true);
		$('#fs1 input').trigger('updateState');
		//alert("after off");
	} else {
		$(e).text("Alle an");
		$(e).data("state","off");
		$('#fs1 input').attr("checked", false);
		$('#fs1 input').trigger('updateState');
		//alert("after on");
	}	
}

function HidePanel() {
	$("div.panel").hide();
	$("div.navrow").hide();
}

function gohash(a) {
	if ( $.browser.msie ) {
		var id = $(a).attr("href").split("#");
		id = "#" + id[1];
		//alert("hello:" + id[1]);
		$('html,body').animate({scrollTop: $(id).offset().top},'fast');
		return false;
	}	
}

// TABS STARTPAGE
$(function () {
	var tabContainers = $('div.tabs > ul.pyear');
	tabContainers.hide().filter(':first').show();
	
	$('div.tabs ul.tabNavigation a').click(function () {
		tabContainers.hide();
		tabContainers.filter(this.hash).show();
		$('div.tabs ul.tabNavigation a').removeClass('selected');
		$(this).addClass('selected');
		return false;
	}).filter(':first').click();
});
