jQuery(function($) { 
	if($('#stage').length) mss();
	$("#bg").mousedown(function(event){$("#container").fadeOut();});
	$("#bg").mouseup(function(event){$("#container").fadeIn();});
	if($('#welkom').length) $('#container').hide().fadeIn("slow");
	
	if($('a.tt').length) $('a.tt').tooltip({ track: true, delay: 0, showURL: false,  showBody: " - ",  fade: 250 });
	
	if($('.yyr').length) sy();
	if($('.rel').length) ini_rel();
	if($('.historie').length) ini_hst();
	if($('.ytb').length) ytb();
});

function ytb(){
	$('.ytb').each(function(index) {
    	vid = $(this).attr("id");
		
		$(this).after("<div id=v_"+index+"></div>");
		$(this).remove();

 		$("#v_"+index).flash({
			src: 'http://www.youtube.com/e/'+vid+'?enablejsapi=1&version=3',
			width: 460,
			height: 320
		});
	});
}

function ini_hst(){
	ini = false;
	$.history.init(function(hash){
        if(hash == "") {
			if (ini){
				$('#main').slideDown();
				$('#catch').slideUp();
			}
			ini = true;
        } else {
			$('#main').slideUp();
			$('#catch').slideDown();
			
			$.get('/site/data.php', {'id':hash}, function(data) {
				$('#catch').html(data);
				//$('#catch .zoom').find('img').jqueryzoom({});
				$("#catch .zoom").click(function() { pop_img($(this).find('img').attr("src"));});
				$("#catch .zoom").prepend("<div class='zoom_ico'></div>");
			});
        }
    },
    { unescape: ",/" });
	//$("#content .zoom").find('img').jqueryzoom({});
	$("#main .img.zoom").click(function() { pop_img($(this).find('img').attr("src"));});
	$("#main .img.zoom").prepend("<div class='zoom_ico'></div>");
}

function ini_rel(){
	$('.rel .thu').bind({
		click: function() {
			$('html, body').animate({scrollTop:0}, 'slow');
			var ic = $(this).parent().find('.tt');
			id = jQuery.url.setUrl(ic.find('a').attr('href')).attr("anchor");
			document.location = "#"+id;
			
		},
		mouseenter: function() {
			var ic = $(this).parent().find('.tt');
			$(this).fadeTo('fast', 1);
			$(ic).slideDown(100);
		},
		mouseleave: function() {
			var ic = $(this).parent().find('.tt');
			$(ic).slideUp(100);
			// do something on mouseenter
			$(this).fadeTo('fast', 0.8);
		}
	}).fadeTo('fast', 0.8);
	
	$('.rel .ic').addClass('tt').removeClass('ic');
}

var obj = null;
function checkHover() {
	if (obj) {
		obj.find('ul').fadeOut('fast');
		$('#yrs').css('height', '26px');
		a=false;
	} 
}
function sy() {
	$('.yyr > li').hover(function() {
		if (obj) {
			obj.find('ul').fadeOut('fast');
			obj = null;
		}
		$('#yrs').css('height', '400px');
		t = $(this).find('ul');
		t.fadeIn('fast');
		yy = $(this).find('a').attr('title');
		$.get('/site/data.php', {'yy':yy, 'pt':jQuery.url.segment(0)}, function(data) {
			t.html(data);
		});
	}, function() {
		obj = $(this);
		setTimeout("checkHover()",400);
	});
	
	yw = $('#yrs').width();
	bw = $('#bar').width();
	d=0;
	if(bw>yw){
		d=40;
		$('.yyr .act').each(function(i) {if($(this).position().left>(yw/1.3)){d=yw-bw;}});
		$('#nxt').bind({ mouseenter: function() {sb(-1);},mouseleave: function() {a=false}}).css('display', 'inline');
		$('#prv').bind({ mouseenter: function() {sb(1);},mouseleave: function() {a=false;}}).css('display', 'inline');
		$('#bar').animate({left:'+='+(d)});
	}
}
function sb(d){
	a=true;
	cl = parseInt($('#bar').css('left'));
	if (cl>34&&d>0){a=false;return;}
	if (cl<-(bw-yw)&&d<0){a=false;return;}
	
	$('#bar').animate({
		left: '+='+(d*34)
	}, 100, function() {
		if(a)sb(d);
	});
}

function onBefore() { 
    $('#caption').hide(); 
} 
function setCaption() { 
	$('.ic').hide();
	tid = '#ic_'+this.alt;
	$(tid).fadeIn();
	current_source = this.src;
}
function mss(){
	var slideContainer = $("#stage");					
	$(slideContainer).cycle({after:setCaption});
}

function pop_img(imgHref) {
	imgHref = imgHref.replace('_th', '');
	
	var imgOver = $("#imgOver"),
		imgOverImg = $(imgOver).find("img");
			$(imgOverImg).attr("src", imgHref);
			$(imgOver).fadeIn();
			centerTheImage();
			$("body").addClass("noScroll");
			
		$(imgOver).mousemove(function(e) { 
			var winH = $(window).height(),
				winW = $(window).width(),
				imgW = $(imgOverImg).width(),
				imgH = $(imgOverImg).height();
				
			if(imgW>winW) {
				var NWM = (e.clientX / winW) * (winW-imgW);
				$(imgOverImg).css({left: NWM});
			} else {
				var nLM = (winW - imgW) / 2;
				$(imgOverImg).css({left: nLM});
			}
			if(imgH>winH) {
				var NHM = (e.clientY/winH) * (winH-imgH);
				$(imgOverImg).css({top: NHM});
			} else {
				var nTM = (winH - imgH) / 2;
				$(imgOverImg).css({top: nTM});
			}						
		});

		function centerTheImage() {
			var winH = $(window).height(),
				winW = $(window).width(),
				imgW = $(imgOverImg).width(),
				imgH = $(imgOverImg).height();

				var nLM = (winW - imgW) / 2;
				$(imgOverImg).css({left: nLM});
				
				var nTM = (winH - imgH) / 2;
				$(imgOverImg).css({top: nTM});
		}		

		$(imgOver).click(function() {
			$(imgOver).fadeOut();
			$(imgOverImg).attr("src", "").css({top: 0, left: 0});
			$("body").removeClass("noScroll");
		});
		
}
