

// JQUERY CUSTOM COMMANDS
$(document).ready( function() {  // start javascript when document is loaded
	
	footerCorrectie(true);
	contentCorrectie();
	tabCorrectie();
	syncColumnHeights();
	
	$(window).resize(function() {
	  footerCorrectie(true);
	});

	//top timeline opebklapper
	timeline_openklapper();
	
	//header animation - startup
	var headAnimation = new HeaderAnimation();
	
	$("#accordion").accordion({
		autoHeight: false,
		navigation: true,
		active: 0
	});

	$('#accordion').bind('accordionchange', function(event, ui) {
		var pageTracker = _gat._getTracker('UA-2590514-31');
		var trackUrl = '/miniSidebar/switchTo_' + ui.newHeader.attr('title') + '/';
		pageTracker._trackPageview(trackUrl);
	});
	
	$('#agreementsLink')
		.attr('href', UTIL.Url.create({format: 'html'}, $('#agreementsLink').attr('href')))
		.fancybox({
			width: 600,
			height: 400,
			autoDimensions: false
		});
		
	// fancy box
	fancyBox_images();
	
	attachFooterAnim();
	
	initScenarios();
	
}); // end ready function


function fancyBox_images()
{
	
	$("a.single_image").fancybox({
		'zoomSpeedIn': 300,
		'zoomSpeedOut': 300,
		'overlayShow': true
	});
	
	// Using custom settings 
	$(".extra_pictures a.inline").fancybox({
		'hideOnContentClick': true
	});
	
	$("a.group").fancybox({
		'zoomSpeedIn': 300,
		'zoomSpeedOut': 300,
		'overlayShow': true
	});
}


var Stellingen = {
		
	onStemClick: function(stelling)
	{
		var selectedInput = $('#stelling' + stelling + ' input:checked');
		if (selectedInput.length) {
			this.stem(stelling, parseInt(selectedInput.val()));
		} else {
			alert('');
		}
	},
	
	stem: function(stelling, eens)
	{
		var eens_static = eens;
		$.getJSON('/stellinggroep/index/stem/format/json/eens/' + (eens ? '1' : '0') + '/stelling/' + stelling, function(response) {
			if (!response.success) {
				alert(response.error);
			} else {
				
				var pageTracker = _gat._getTracker('UA-2590514-31');
				var trackUrl = '/stem/id_' + stelling + '/eens_' + eens_static + '/';
				pageTracker._trackPageview(trackUrl);

				var newContent = $(response.content);
				var stellingEl = $('#stelling' + stelling);
				
				stellingEl.find('.bg_holder').remove();				
				stellingEl.append(newContent);
				
				
				var eens = newContent.find('.eens .bar');
				var oneens = newContent.find('.oneens .bar');
				
				eens
					.css('right', -eens.width())
					.animate({
						right: 0
					}, 2000);
				
				oneens
					.css('left', -oneens.width())
					.animate({
						left: 0
					}, 2000);	
				
				//refresh
				Cufon.set('fontFamily', 'Apex New Bold').replace('.stelling_naming .title');					
				Cufon.set('fontFamily', 'Apex New Book').replace('.stelling_naming .title_stelling');
				
				Cufon.set('fontFamily', 'Apex New Book').replace('.user_input label');
			}
		});
	}
};

function timeline_openklapper(){
	
	timeline_klap_element = $("#timeline .timeline_holder");
	timeline_klap_button =  $("#timeline .timeline_controls .button");
	
	timeline_klap_button.click(function() {
		if ($(this).parent('.opener').hasClass('open')) {
			timeline_klap_element.slideUp(200);
			$(this).parent('.opener').removeClass('open');
			$(this).parent('.opener').addClass('closed');
			
			$.cookie('timeline_hatch', 'closed', { expires: 7, path: '/'});
		} else {
			timeline_klap_element.slideDown(400);
			$(this).parent('.opener').removeClass('closed');
			$(this).parent('.opener').addClass('open');
			
			$.cookie('timeline_hatch', 'open', { expires: 7, path: '/'});
		}
	});
}



function HeaderAnimation() {
	this.init();
}

HeaderAnimation.prototype = {
		
	current: 0,
	timerInterval: 10000,
	
	init: function() {

		var self = this;
		
		// defineer
		
		this.photoWrapper 		= $('.modules_main .item_selection_content li');
		this.linkWrappers 		= $('.modules_main .item_selection_button li');		
		//this.textWrapper 	= $('#main_banner li');
		//this.textHolderWrapper	= $('.top_text_layer');	
		
		// pause mode
		this.photoWrapper.hover(
			function() { self.pauseAutoTimer(); },
			function() { self.resetAutoTimer(); });
		this.hideAll();
		/*// pause mode		
		this.mainBannerWrapper.hover(
			function() { self.pauseAutoTimer(); },
			function() { self.resetAutoTimer(); });		*/
		
		
		//var self = this;
		// linkWrappers click actie
		this.linkWrappers.css('cursor', 'pointer').hover(
			function() {		
				self.setCurrent(
					self.linkWrappers.index($(this))
				);
			}			
		);
		
		this.resetAutoTimer();
	},
	
	pauseAutoTimer: function() {

		clearInterval(this.autoTimer);
	},
	
	resetAutoTimer: function() {

		var self = this;

		clearInterval(this.autoTimer);
		this.autoTimer = setInterval(function() {
			self.showNext();
		}, this.timerInterval);
	},
	
	hideAll: function() {		

		this.photoWrapper.css({
			opacity: 0,
			position: 'absolute',
			top: 0
		})
		.find('.buttons a').css('display', 'block').hide();	
		/*
		this.textWrapper.css({
			opacity: 0,
			position: 'absolute',
			top: 0
		});	*/
		 
		this.showCurrent();
	},
	
	showCurrent: function() {
		this.linkWrappers.eq(this.current).addClass('active');

		var buttons = this.photoWrapper.eq(this.current).find('.buttons a');
		
		this.photoWrapper.eq(this.current)
			.stop()
			.css({'zIndex':'10'}).animate({opacity: 1}, 1000, function() {
				$(this).css('filter', '');
				buttons.each(function(i) {
					setTimeout(function() {
						buttons.eq(i).show();
					}, i * 100);
				});
			});
	},
	
	hideCurrent: function() {
		this.linkWrappers.eq(this.current).removeClass('active');
		
		var buttons = this.photoWrapper.eq(this.current).find('.buttons a');
		
		var delay = 0;
		
		buttons.each(function(i) {
			delay = i * 100;
			setTimeout(function() {
				buttons.eq(i).hide();
			}, delay);
		});
		
		this.photoWrapper.eq(this.current)
			.stop()
			.css({'zIndex':'1'}).animate({opacity: 0, delay: delay}, 1000);
	},
	
	setCurrent: function(index) {
		
		if (this.current == index) {
			return;
		}
		
		//prev state
		this.hideCurrent();
		//set new state
		this.current = index;
		//new state
		this.showCurrent();
		
		//reset timer
		this.resetAutoTimer();
	},
	
	showNext: function() {
		//can only be a number as large as the collection!, otherwise it wil reset itself
		this.setCurrent((this.current + 1) % this.linkWrappers.length);
	}
};

function footerCorrectie(runAgain) {
  
  rockbottom_position = $('#rockbottom').position();
  rockbottom_top = rockbottom_position.top;
  rockbottom_height = $('#rockbottom').height();
  
  footer_position = $('#footer').position();
  footer_top = footer_position.top; 
  footer_height = $('#footer').height();

  document_height = $(document).height();
  content_height = $('#wrapper').height() + $('#timeline').height() + footer_height;

  // Verbind de footer aan rockbottom
  footer_top_new = rockbottom_top + rockbottom_height - footer_height;

  if (content_height > document_height) {
    $('#footer').css({
      'top': '0px',
      'position':'relative'
    });
  } else {
    $('#footer').css({
      'top': footer_top_new + 'px',
      'position':'absolute'
    });
  }
  
  if (runAgain) {
    footerCorrectie(false);
  }
}

function attachFooterAnim() {
  var footer_opacity = 0.2;

  //$('.footer_holder').fadeTo(1,footer_opacity);

    $('#footer').animate({
        backgroundColor:'#E6E6E6',
        'opacity' : footer_opacity
      },1);
  $('#footer').hover(function() {
    $('#footer .footer_holder .inner .title').css({color:'#00B2DB'});
    $('#footer .footer_holder .inner .abs_logos li.vanille a').css({backgroundPosition:'left bottom'});
    $('#footer .footer_holder .inner .abs_logos li.espire a').css({backgroundPosition:'left bottom'});
    $('#footer .footer_holder .inner .abs_logos li.overijssel a').css({backgroundPosition:'left bottom'});
    Cufon.refresh('#footer .footer_holder .inner .title');
    $('#footer').animate({
        backgroundColor:'#c2e6ef',
        'opacity' : 1
      },300);
      $('#footer .footer_holder .inner').animate({
         borderLeftColor: '#00B2DB',
         borderRightColor: '#00B2DB'
      },300);
    $('#footer .footer_holder .inner a').animate({
       color: '#00B2DB'
    },300);
  }, function() {
      $('#footer')
      	.stop().animate({
  			backgroundColor:'#E6E6E6',
			opacity: footer_opacity
      	},300)
	    .find('a')
	    	.stop().animate({
	    		color: '#666666'
	    	}, 300);
      $('#footer .footer_holder .inner').animate({
         borderLeftColor: '#AFAFAF',
         borderRightColor: '#AFAFAF'
      },300);
      $('#footer .footer_holder .inner a').animate({
         color: '#666666'
      },300, function() {
        $('#footer .footer_holder .inner .title').css({color:'#555555'});
        $('#footer .footer_holder .inner .abs_logos li.vanille a').css({backgroundPosition:'left top'});
        $('#footer .footer_holder .inner .abs_logos li.espire a').css({backgroundPosition:'left top'});
        $('#footer .footer_holder .inner .abs_logos li.overijssel a').css({backgroundPosition:'left top'});
        Cufon.refresh('#footer .footer_holder .inner .title');
      });
  });
}

function contentCorrectie() {
//  var min_height = 448;
//  if ($('.content_text').height() < min_height) {
//    $('.content_text').css({'height':min_height+'px'});
//  }
}

function tabCorrectie() {
  var min_width = 150;
  $('#wrapper .container .modules_main_tabs li').each(function() {
    if ($(this).width() < min_width) {
      //$(this).css({'width':min_width+'px'});
    } 
  });
}

function syncColumnHeights() {
  
  if ($('body.home').length!=0 || /^\/onderwerpArtikel/.test(window.location.pathname)) {
    // Startpagina
    var min_height = 470;
    if ($('.nieuws_blok').height() < min_height) {
      $('.nieuws_blok').css({'height':min_height+'px'});
    }
  } else {
    // !Startpagina

    var content_height = $('.modules_main').height();
    var sidebar_height = $('.nieuws_blok').height();
    
    if (content_height > sidebar_height) {
      $('.nieuws_blok').css({'height':content_height+'px'});
    } else {
      $('.modules_main').css({'height':sidebar_height+'px'});
    }
  }  
}

function initScenarios() {
	var fotodelay = 0;
	$('#toekomstvisie-beelden').cycle({
		timeout: 4000,
		speed: 800
	});
	/*
	$('.startpagina_scenario_images .img').each(function() {
		$(this).cycle({
			delay: fotodelay,
			timeout: 4000,
			speed: 800
		});
		fotodelay += 700;
	});*/
}
