var currenthash = window.location.hash.split(',');
var pics = []; // used for back/fwd in pic
var allowkey = true;

$(function(){
	
	$('.page').each(function(i){
		$(this).css({'position': 'absolute'}).hide();
    });	
	
	$(window).hashchange(function() {
		currenthash = window.location.hash.split(',');
		if( currenthash[0] != '#fotos' || currenthash.length < 3 ) {
			$('#lightbox').fadeOut();
			$('#lightbox-bg').fadeOut();
		}
		navigate();
	});

	$(document).keydown(function(event) {
		if( allowkey && currenthash[0] == '#fotos' )
			if( currenthash.length > 3 ) {
				if( event.which == 37 )
					$('#pic-prev').click(); // prev photo
				else if( event.which == 39 )
					$('#pic-next').click(); // next photo
			} else if( event.which == 37 )
				$('#thumbpage-prev').click(); // prev photopage
			else if( event.which == 39 )
				$('#thumbpage-next').click(); // next photopage
	});

    $('a').click(function(){
    	var hash = this.href.substr(this.href.indexOf('#'));
    	if( hash == currenthash.join(',') ) {
    		//console.log('browser fail averted');
    		return false;
    	}
    });
 
    // picture pages
    
    $('#thumbpageholder').css('overflow', 'hidden');
    var i = 0;
    $('.thumbpage').each(function(j) {
	    $('.thumb a', this).each(function(){
	    	var url = this.href;
	    	if( url.indexOf('img/foto') > 0  ) {
	    		var pic = url.substr(url.indexOf('img/foto') + 8).split('/');
	    		pics[i] = pic;
	    		this.href = '#fotos,' + pic[0] + ',' + j + ',' + pic[1];
	    	}
	    	++i;
	    });    	
    });
    $('#thumbpage-prev').click(function(){
    	if( thumbpage > 0 ) document.location.hash = '#fotos,,' + (thumbpage - 1);
    	//	goThumbPage(thumbpage - 1);
    	return false;
    });
    $('#thumbpage-next').click(function(){
    	if( thumbpage < 18 ) document.location.hash = '#fotos,,' + (thumbpage + 1);
    	//goThumbPage(thumbpage + 1);
    	return false;
    });

    // picture popup
    
    $('form').submit(function(){
    	return false;
    });
    $('#pic-next').click(function(){
    	for( var i = 0; i < pics.length - 1; ++i ) {
    		if( pics[i][0] == currenthash[1] && pics[i][1] == currenthash[3] ) {
    			document.location.hash = '#fotos,' + pics[i+1][0] + ',' + currenthash[2] + ',' + pics[i+1][1];
    		}
    	}
    	return false;
    });
    $('#pic-prev').click(function(){
    	for( var i = 1; i < pics.length; ++i ) {
    		if( pics[i][0] == currenthash[1] && pics[i][1] == currenthash[3] ) {
    			document.location.hash = '#fotos,' + pics[i-1][0] + ',' + currenthash[2] + ',' + pics[i-1][1];
    		}
    	}
    	return false;
    });
    $('#lightbox').click(function(){
    	document.location.hash = '#fotos,' + currenthash[1] + ',' + currenthash[2];
    	return false;
    });
    $('#lightbox #box').click(function(){
    	return false;
    });
    $('#pic-send-form').hide();
    $('#pic-open-send').click(function(){
    	$('#pic-navigate').hide();
    	$('#pic-send-form').show();
    	allowkey = false;
    });
    $('#pic-send-cancel').click(function(){
    	$('#pic-navigate').show();
    	$('#pic-send-form').hide();   	
    	allowkey = true;
    });
    /*$('#pic-opt-box').click(function(){
    	$('#pic-opt-in').val(!$('#pic-opt-in').val());
    	return false;
    });*/
    

    $('#pic-send-button').click(function(){

    	var fail = false;
    	if( $('#pic-opt-in').val() && $('#pic-sender-email').val().length == 0 ) {
    		$('#pic-sender-email').addClass("error");
    		fail = true;
    	}
    	if( $('#pic-receiver-email').val().length == 0) {
    		$('#pic-receiver-email').addClass("error");
    		fail = true;
    	}
    	
    	if( !fail ) {    	
    		
    		var url = '/process/photo.aspx'
	    				+ '?newsletter=N' // + ($('#pic-opt-in').val() ? 'Y' : 'N')
	    				+ '&sender_email=' + $('#pic-sender-email').val()
	    				+ '&sender_name=' + $('#pic-sender-name').val()
	    				+ '&recipient_email=' + $('#pic-receiver-email').val()
	    				+ '&recipient_name=' + $('#pic-receiver-name').val()
	    				+ '&url=/' + currenthash;
    		url = url.replace('#', '%23');
    		
    		//console.log(url);
    		
    		//$(this).hide();
    		$('#pic-feedback').show().html("versturen ....");
	    	$.ajax({
	    		url:url,
	    		context:document.body,
	    		success: function(html) {
	            	$("#pic-feedback").html('verzonden');
	        	},
	    		error: function(html) {
	            	$("#pic-feedback").html('niet gelukt');
	        	}	        	
	    	});
    	}
    	
    });  
    
	
    
    // newsletter (home)
    
    $('.newsletter-submit').click(function(){
  
    	$(".newsletter-feedback").html("versturen ....");
    	$(".newsletter-feedback").show();
    	var email = $('.newsletter-email').val();
    	$.ajax({
    		url:'/process/newsletter.aspx?email=' + email,
    		context:document.body,
    		success: function(html) {
            	$('.newsletter-feedback').html('verzonden');
            	$('.newsletter-email').val();
        	},
			error: function(html) {
            	$('.newsletter-feedback').html('mislukt');
        	}    	
    	});
    	//return false;
    });
  
    // program
   
    $('.schedule').hide();
    //$('.schedule:first').show();    
    $('.tabs li').each(function(i){
    	$(this).click(function(){
    		$('.tabs li').removeClass('active');
    		$('.schedule').hide();
    		
    		$('.tabs li').eq(i % 4).addClass('active');
    		$('.tabs li').eq(i % 4 + 4).addClass('active');
    		$('.schedule').eq(i % 4).show();
    		
    		document.location.hash = '#programma';
    		return false;
    	});
    });
    
    // resizing and scrolling
    $(window).resize(onResize);
    onResize();
    navigate();
    
});


function onResize() {
    var h = $(window).height()-7;
    if( h < 730 ) h = 730;
    $('#content').height(h);
    $('#pageholder').height(h - 80);    
	$('#programma .schedule').height(h - 250);
	$('#home .wrapper').height(h - 150);
	
	$('.page').height(h - 80);
}

function navigate() {

	allowkey = true;
	
	switch(currenthash[0]) {
	case '#home':
		goPage(0);
		break;
	case '#programma':
		goPage(1);
		break;
	case '#kaart':
		goPage(2);
		onShowMap();
		//filterMap(currenthash.length > 1 ? currenthash[1] : '25 april 2011');
		break;
	case '#fotos':
		goPage(2);
		if( currenthash.length > 3 ) showPic(currenthash[1], currenthash[3]);
		else hidePic();
		if( currenthash.length > 2 ) goThumbPage(currenthash[2]);
		else if( currenthash.length > 1 ) goYear(currenthash[1]);
		else goYear('13jun');
		break;
	case '#nieuws':
		goPage(3);
		break;	
	case '#voorwaarden':
		goPage(4);
		break;	
	case '#twitter':
		goPage(5);
		break;			
	default:
		if( page == -1 ) goPage(0);
		else return true; // allow navigation
	}
	return false;
}

var page = -1;
var iebgpos = 0;
var ietargetpos = 0;
function goPage(p) {

	$('#nav li').each(function(i) {
		if( i - 1 == p ) $(this).addClass("active");
		else if( i > 0 ) $(this).removeClass("active");
	});
	
	if( p != page ) {
		var prev;
		var next;
		$('.page').each(function(i){
			if(i==page) prev = this;
			else if(i==p) next = this;
			else $(this).hide();
		});
		if( p > page ) {// scroll left
			$(prev).animate({left:'-990px'}, 500).hide(0);
			$(next).css({left:'990px'}).show(0).animate({left:'0px'}, 500);
		} else { // scroll right
			$(prev).animate({left:'990px'}, 500).hide(0);
			$(next).css({left:'-990px'}).show(0).animate({left:'0px'}, 500);
		}
		if( $.browser.msie && parseInt($.browser.version) < 9 ) {
		
			ietargetpos = p * -700;
			setTimeout(updateBgPos, 50);
		
		} else {
			$('body').animate({'background-position-x': (p * -700) + 'px'}, 500);
		}
		//setTimeout(function(){ window.location.hash = currenthash; }, 500);
		page = p;
		
		$(".newsletter-feedback").hide();
	}
}

function updateBgPos() {
	iebgpos = .95 * iebgpos + .05 * ietargetpos;
	if( Math.abs(ietargetpos - iebgpos) < 2 )
	{
		iebgpos = ietargetpos;
	}
	else
	{
		setTimeout(updateBgPos, 30);
	}
	$('body').css({'background-position-x': iebgpos + 'px'});
}

var thumbpage = -1;
function goThumbPage(p) {
	//console.log('goThumbPage ' + p);
	if( thumbpage == -1 ) {
		$('.thumbpage').each(function(i){
			$(this).css({'position': 'absolute'}).hide();
	    });		
		$('.thumbpage:first').show();
		thumbpage = 0;
	}

	if( p != thumbpage ) {
		var prev;
		var next;
		$('.thumbpage').each(function(i){
			if(i==thumbpage) prev = this;
			else if(i==p) next = this;
			else $(this).hide();
		});
		if( p > thumbpage ) {// scroll left
			$(prev).animate({left:'-950px'}, 500).hide(0);
			$(next).css({left:'950px'}).show(0).animate({left:'0px'}, 500);
		} else { // scroll right
			$(prev).animate({left:'950px'}, 500).hide(0);
			$(next).css({left:'-950px'}).show(0).animate({left:'0px'}, 500);
		}
		thumbpage = parseInt(p);
	}
}

function goYear(year) {
	var cont = true;
	$('.thumbpage').each(function(i){
		if( cont && $(this).hasClass('year' + year) ) {
			goThumbPage(i);
			cont = false;
		}
	});
	setTimeout(function(){ window.location.hash = currenthash; }, 500);
}

function showPic(year, url) {
	//console.log("showpic", url);
	$('#lightbox img').attr('src', 'img/foto/' + url);
	$('#lightbox').fadeIn();
	$('#lightbox-bg').fadeIn();
}

function hidePic() {
	$('#lightbox').fadeOut();
	$('#lightbox-bg').fadeOut();
	$('#pic-navigate').show();
	$('#pic-send-form').hide();  	
}

