var animating = false;
var mouseover = false;

$(document).ready(function(){

	$('#header h1').replaceWith('&nbsp;');
	
	$('#poster').click(function() {
		$(this).find('img:visible').fadeOut();
		$(this).find('img:hidden').fadeIn();
	});
	
	$('a.fbox').fancybox({
		'titleShow' : false,
		'padding' : 0,
		'autoScale': true,
		'overlayShow': true,
		'cyclic': true,
		'overlayOpacity': 0.85,
		'overlayColor': '#222222',
		'transitionIn' :'elastic',
		'transitionOut' :'elastic'
	});
	
	$('a.fpbox').fancybox({
		'titleShow' : false,
		'padding' : 0,
		'autoScale': true,
		'overlayShow': true,
		'cyclic': true,
		'type' : 'image',
		'overlayOpacity': 0.85,
		'overlayColor': '#222222',
		'transitionIn' :'elastic',
		'transitionOut' :'elastic'
	});
	
	$(".vimeo").click(function() {
		$.fancybox({
			'padding'		: 0,
			'autoScale'		: true,
			'transitionIn'	: 'none',
			'overlayShow': true,
			'overlayOpacity': 0.85,
			'overlayColor': '#222222',
			'transitionOut'	: 'none',
			'title'			: this.title,
			'href'			: this.href.replace(new RegExp("([0-9])","i"),'moogaloop.swf?clip_id=$1'),
			'type'			: 'swf'
		});

		return false;
	});
	
	$('td.flyer:not([id="flyerSelect"]) > div.flyer').click(function () {
		$.fancybox({
			'titleShow' : false,
			'padding' : 0,
			'autoScale': true,
			'overlayShow': true,
			'cyclic': true,
			'type' : 'image',
			'href' : $(this).find('a').attr('href'),
			'overlayOpacity': 0.85,
			'overlayColor': '#222222',
			'transitionIn' :'elastic',
			'transitionOut' :'elastic'
		});
	});
	
	function formatTitle(title, currentArray, currentIndex, currentOpts) {
    	return '<div id="tip7-title">' + (title && title.length ? '<b>' + title + '</b> - ' : '' ) + 'Foto ' + (currentIndex + 1) + ' van ' + currentArray.length + '</div>';
	}
	
	$('a.fancypics').fancybox({
		'overlayShow'	: true,
		'titleShow'		: true,
		'overlayOpacity': 0.8,
		'padding'		: 10,
		'transitionIn'	: 'elastic',
		'overlayColor'	: '#000',
		'titlePosition' : 'inside',
		'titleFormat'	: formatTitle,
		'onComplete'    : function() {
			var picture = $(this).attr('href').split('nl/');
			var picture_base = picture[1];
			if (pageTracker !== false) {
				pageTracker._trackPageview('/' + picture_base);
			}
		}

	});

	
	// Photo pages
	$.fn.image = function(src, f){ 
		return this.each(function(){ 
			var i = new Image(); 
			i.src = src; 
			i.onload = f; 
			this.appendChild(i);
		}); 
	}
	
	$('.control').fadeTo('fast', 0.5).hover(
		function () {
			$(this).stop().fadeTo(100, 1);
		},
		function () {
			$(this).stop().fadeTo(100, 0.5);
		}
	);
	
	$('td.photo img').fadeTo('fast', 0.7);
	
	$('#seperator').fadeTo('fast', 0.3);
	
	$('#fotocanvas').one('mouseover', function(){
		mouseover = true;
	});
	
	if (!mouseover) $('.fotocontrols').slideUp('fast');
	
	function checkMouse() {
		if (mouseover && $('.fotocontrols').is(':hidden')) {
			$('.fotocontrols').slideDown('fast', function () { checkMouse(); animating = false; });
		}
		if (!mouseover && $('.fotocontrols').is(':visible')) {
			$('.fotocontrols').slideUp('fast', function () { checkMouse(); animating = false; });
		}
	}
	
	$('#fotocanvas').hover(
		function () {
			mouseover = true;
			if ($('.fotocontrols').is(":hidden") && animating == false) {
				animating = true;
				$('.fotocontrols').slideDown('fast', function () { checkMouse(); animating = false; });
			}
		}, 
		function () {
			mouseover = false;
			if ($('.fotocontrols').is(":visible") && animating == false) {
				animating = true;
				$('.fotocontrols').slideUp('fast', function () { checkMouse(); animating = false; });
			}
		}
	);
	
	$('td.photo img').hover(
		function () {
			$(this).stop().animate({opacity: 1.0}, 100);
		},
		function () {
			$(this).stop().animate({opacity: 0.7}, 100);
		}
	);
		
	$('td.photo img').click(function() {
		var request = $(this).parent().attr('href').substring(1).split('/');
		var setid = $('#setID').val();
		if (request[1] != undefined) {
			var imgSrc = '/p/' + setid + '/' + request[1];
			var imagePreloader = new Image();
			imagePreloader.src = imgSrc;
			imagePreloader.onload = function() {
				$('#canvas').css('height', imagePreloader.height + 'px');
			}; 
			$('#canvas').css('background', 'url(/p/' + setid + '/' + request[1] + ') no-repeat');
		}
	});
	
	if (window.location.hash.match(/\/*\.jpg/)) {
		var request = window.location.hash.substring(1).split('/');
		var setid = $('#setID').val();
		if (request[1] != undefined && setid != undefined) {
			var imgSrc = '/p/' + setid + '/' + request[1];
			var imagePreloader = new Image();
			imagePreloader.src = imgSrc;
			imagePreloader.onload = function() {
				$('#canvas').css('height', imagePreloader.height + 'px');
			};
			$('#canvas').css('background', 'url(/p/' + setid + '/' + request[1] + ') no-repeat');
		}
	}
	
	$('.agendaItem h2').click(function(){
		var container = $(this).parent().parent().parent();
		var contentcheck = container.find('.longdesc').text();
		if (contentcheck.length < 1) {
			return false;
		}
		var check = container.find('.desc').css('marginBottom');
		if (check == '0px' && animating == false) {
			animating = true;
			try {
				pageTracker._trackEvent('Agenda', 'View', container.find('h2').text() + ', ' + container.find('h3').text());
			} catch(err) {}
			container.find('.desc').css('marginBottom', '20px');
			container.find('.longdesc').slideDown('fast', function() {
				container.find('div.flyer').animate( { height:'193px' }, { queue:true, duration:300 }).find('img').fadeIn('fast', function () { animating = false; });
				//container.find('img').fadeIn('fast', function () {
				//	animating = false;
				//	container.find('div.flyer').animate( { height:'193px' }, { queue:true, duration:300 });
				//});
			});
		} else {
			if (animating == false) {
				animating = true;
				container.find('div.flyer').animate( { height:'95px' }, { queue:true, duration:300 });
				container.find('img').fadeOut('fast', function () {
					container.find('.longdesc').slideUp('fast', function () {
						container.find('.desc').css('marginBottom', '0px');	
						animating = false;
						//container.find('div.flyer').animate( { height:'95px' }, { queue:true, duration:300 });
					});
				});
			}
		}
	});
	
	$('.photoItem h2').click(function(){
		var container = $(this).parent().parent().parent().parent().parent().parent().parent();
		var check = container.find('.desc').css('marginBottom');
		if (check == '0px' && animating == false) {
			animating = true;
			container.find('.desc').css('marginBottom', '20px');
			container.find('.photosheet').slideDown('slow');
		} else {
			if (animating == false) {
				animating = true;
				container.find('.photosheet').slideUp('fast', function () {
					container.find('.desc').css('marginBottom', '0px');	
					animating = false;
				});
			}
		}
	});
	
	// Menu page
	$('.idletab').slideUp('fast');
	$('#menu a').click(function() {
		var target = $(this).attr('href');
		$('#menu a').removeClass('active');
		$(this).addClass('active');
		$('.activetab').removeClass('activetab').fadeOut('fast', function () {
			$(target).addClass('activetab').removeClass('idletab').fadeIn('fast');
		});
		return false;
	});
	// Disabled for being annoying
	//if (window.location.hash.match(/\#/)) {
	//	$('a[href="' + window.location.hash + '"]').trigger('click');
	//}
	
});