$(function(){
	initNavigation();
	$('div.preview').galleryCircle({
		btPrev: false,
		btNext: false,
		scrollElParent: '> ul',
		scrollEl: '> li',
		step: true,
		numHolder: false,
		numCreate: false,
		easing: false,
		switchTime: false,
		duration : 1000
	});
	initAanmeldenNieuwsbrief();
	initVideoOverlay();
	
	$("#search").autofill({
		result_url: 'http://www.mijngemak.nl/ajax-zoekresultaten?key='
	});	
	initEasterEggs();
	trackPageViews();
	initNieuwsbriefAfmelden();
})

function initNavigation()
{
	$('.navigation li').hover(function()
	{
		$(this).addClass('hover');
	},
	function()
	{
		$(this).removeClass('hover');
		setTimeout(function(){Cufon.refresh('ul.navigation > li > a')}, 10);
	});
}

/*--- gallery function ---*/
jQuery.fn.galleryCircle = function(_options){
	// defaults options
	var _options = jQuery.extend({
		btPrev: 'a.prev',
		btNext: 'a.next',
		holderList: 'div',
		scrollElParent: 'ul',
		scrollEl: 'li',
		numHolder: false,
		numCreate: false,
		step: false,
		innerMargin: 0,
		curPage: false,
		onClick: null,
		easing: 'linear',
		switchTime: false,
		duration : 1500
	},_options);

	return this.each(function(){
		var _this = jQuery(this);
		var _next = jQuery(_options.btNext, _this).length ? jQuery(_options.btNext, _this) : false;
		var _prev = jQuery(_options.btPrev, _this).length ? jQuery(_options.btPrev, _this) : false;
		var _holderList = jQuery(this);
		var _scrollElParent = jQuery(_options.scrollElParent, _holderList);
		var _scrollEl = jQuery(_options.scrollEl, _scrollElParent);
		var _numHolder = false ;
		if (_options.numHolder) _numHolder = jQuery(_options.numHolder, _this).length ? jQuery(_options.numHolder, _this) : false;
		var _step, _t = null;
		var _widthSum = 0;
		_scrollEl.each(function(){_widthSum += jQuery(this).outerWidth(true);})
		var _startPosition = _scrollEl.index(_scrollEl.filter('.active'));
		if (_startPosition==-1) _startPosition=0;
		_scrollEl.removeClass('active');
		var _easing = _options.easing;

		if (!_options.step) _step = _holderList.innerWidth();
		var _margin = _widthSum;
		_scrollElParent.append(_scrollEl.clone(true));
		_scrollElParent.prepend(_scrollEl.clone(true));

		var _offsetStartPosition =0;
		_offsetStartPosition = culcOffset(_startPosition);

		_scrollElParent.css('marginLeft', (-_margin+_options.innerMargin-_offsetStartPosition));

		//auto rotation
		if (_options.scrollEl.length > 1)
		{
			if (_options.switchTime) {
				_t = setTimeout(function(){
					nextSlides();
				},_options.switchTime);
			}
		}
		
		//button next "click"
		if (_options.btNext) {
			_next.click(function(){
				if (!_scrollElParent.is(':animated')) {
					if (jQuery.isFunction(_options.onClick)) _options.onClick.apply(_this);
					nextSlides();
				}
				return false;
			});
		}

		//button prev "click"
		if (_options.btPrev) {
			_prev.click(function(){
				if (!_scrollElParent.is(':animated')) {
					if (jQuery.isFunction(_options.onClick)) _options.onClick.apply(_this);
					prevSlides();
				}
				return false;
			});
		}

		//curent position
		function getCurElIndex(){
			var _curMargin = parseInt(_scrollElParent.css('marginLeft')) + _widthSum - _options.innerMargin;
			for(i=0; i < _scrollEl.length; i++){
				if (_curMargin == 0) return i;
				if (_curMargin <= _options.innerMargin) _curMargin += _scrollEl.eq(i).innerWidth(true);
				else _curMargin -= _scrollEl.eq(i).innerWidth(true);
				if (_curMargin == _options.innerMargin) return i+1;
			}
		}

		// offset of gallery if when activ element not first at start 
		function culcOffset(_ind){
			var _tmpcounter=0;
			var _pos=0;
			while (_tmpcounter < _ind){
				_pos += _scrollEl.eq(_tmpcounter).outerWidth(true);
				_tmpcounter++;
			};
			return _pos;
		}

		//go next slide
		function nextSlides(){
			if (_t) clearTimeout(_t);
			if (_options.step) {
				_curElIndex = getCurElIndex();
				_step = _scrollEl.eq(_curElIndex).innerWidth(true);
			};
			_margin = -parseInt(_scrollElParent.css('marginLeft'));
			_margin += _step;
			
			_scrollElParent.animate({'marginLeft':(-_margin+_options.innerMargin)}, {duration:_options.duration, easing: _easing, complete:function(){
				if (_margin >= _widthSum*2) {
					_margin = _widthSum + (_margin - _widthSum*2);
				}
				_scrollElParent.css({'marginLeft':-_margin+_options.innerMargin});
				jQuery.fn.galleryCircle.numListActive(_numHolder, _scrollEl);

				//autoslide
				if (_options.switchTime) {
					_t = setTimeout(function(){
						nextSlides();
					},_options.switchTime)
				}
			}});
		}

		//go prev slide
		function prevSlides(){
			if (_t) clearTimeout(_t);
			if (_options.step) {
				_curElIndex = getCurElIndex();
				if (_curElIndex == 0) _curElIndex= _scrollEl.length;
				_step = _scrollEl.eq(_curElIndex-1).innerWidth(true);
			};
			_margin = -parseInt(_scrollElParent.css('marginLeft'));
			_margin -= _step;
			_scrollElParent.animate({'marginLeft':(-_margin+_options.innerMargin)}, {duration:_options.duration, easing: _easing, complete:function(){
				if (_margin < _widthSum) {
					_margin = _widthSum*2 - (_widthSum - _margin);
				}
				_scrollElParent.css({'marginLeft':-_margin+_options.innerMargin});
				jQuery.fn.galleryCircle.numListActive(_numHolder, _scrollEl);

				//autoslide
				if (_options.switchTime) {
					_t = setTimeout(function(){
						nextSlides();
					},_options.switchTime)
				}
			}});
		}

		// Number list Create
		jQuery.fn.galleryCircle.numListCreate = function(_numHolder, _scrollEl){
			var _numListElC = '';
			for(var i=0; i<_scrollEl.length; i++){
				_numListElC += '<li><a href="">'+(i+1)+'</a></li>';
			}
			jQuery(_numHolder).html('<ul>'+_numListElC+'</ul>');
		};

		// Number list Activate
		jQuery.fn.galleryCircle.numListActive = function(_numHolder, _scrollEl){
			_curElIndex = getCurElIndex();
			if (jQuery(_options.curPage, _this).length && _options.curPage) jQuery(_options.curPage, _this).text('Pagina '+(getCurElIndex()+1)+'/'+_scrollEl.length);
			if (_numHolder) {
				jQuery('a',_numHolder).removeClass('active');
				jQuery('a',_numHolder).eq(_curElIndex).addClass('active');
			}
		};

		//click on control elemens
		function numClick() {
			jQuery(_options.numHolder, _this).find('a').click(function(){
				if (_t) clearTimeout(_t);
				var _aList = jQuery(_options.numHolder, _this).find('a');
				var _index = _aList.index(jQuery(this));
				_margin = _widthSum + _index * _scrollEl.outerWidth(true);
				_scrollElParent.animate({'marginLeft':(-_margin+_options.innerMargin)}, {duration:_options.duration, easing: _easing, complete:function(){
					if (_margin >= _widthSum*2) {
						_margin = _widthSum + (_margin - _widthSum*2);
					}
					_scrollElParent.css({'marginLeft':-_margin+_options.innerMargin});
					_aList.removeClass('active').eq(_index).addClass('active');

					//autoslide
					if (_options.switchTime) {
						_t = setTimeout(function(){
							nextSlides();
						},_options.switchTime)
					}
				}});
				return false;
			});
		};

		// init creating num list
		if (_options.numCreate) jQuery.fn.galleryCircle.numListCreate(_numHolder, _scrollEl);

		// pagination first init (example Page 2/6)
		if (jQuery(_options.curPage, _this).length && _options.curPage) jQuery(_options.curPage, _this).text('Pagina '+(getCurElIndex()+1)+'/'+_scrollEl.length);

		// init activate num list item and init numClick()
		if (_options.numHolder) {
			jQuery.fn.galleryCircle.numListActive(_numHolder, _scrollEl);
			numClick();
		}
	});
}

function initAanmeldenNieuwsbrief(){
	$(".btn-nieuwsbrief").click(function(e){
		e.preventDefault();
		$("body").overlay({
			opacity: 90,
			window_width: 700,
			window_height: 650,
			close_img: '/images/overlay-close.gif',
			src: '<iframe src="/aanmelding-nieuwsbrief" width="650" height="600" frameborder="0" name="nieuwsbriefframe"></iframe>'
		});
	});
}

//======================
	function initNieuwsbriefAfmelden() {
		var url = ""+window.location;
		var nieuwsbrief = url.split("#")[1];
		
		if (nieuwsbrief == "afmelden-nieuwsbrief")
		{
			$("body").overlay({
				window_width: 725,
				window_height: 600,
				window_background_color: 'transparent',
				opacity: '80',
				src: '<div class="nieuwsbrief-afmelden-overlay-btm"><iframe width="600" height="580" src="/afmelding-nieuwsbrief" frameborder="0" scrolling="no"></iframe></div>',
				close_img: '/images/overlay-close.gif'
			});
		}
	}
	
	function initNieuwsbriefAanmelden() {
		
		var url = ""+window.location;
		var nieuwsbrief = url.split("#")[1];
		
		if (nieuwsbrief == "aanmelden-nieuwsbrief")
		{
			$("body").overlay({
				window_width: 725,
				window_height: 775,
				window_background_color: 'transparent',
				opacity: '80',
				src: '<div class="nieuwsbrief-afmelden-overlay-btm"><iframe name="nieuwsbriefframe" width="600" height="775" src="/aanmelding-nieuwsbrief" frameborder="0" scrolling="no"></iframe></div>',
				close_img: '/images/overlay-close.gif'
			});
		}
		
	}


function initVideoOverlay(){
	$(".video-overlay").click(function(e){
		e.preventDefault();
		var videoUrl = $(this).attr("rel").split("#|#")[0];
		var titel = $(this).attr("rel").split("#|#")[1];
		if (videoUrl == "/swf/vidiecomp.flv")
			var videoImage = "/images/video_comp.jpg";
		else
			var videoImage = "";
		var input = getVideoPlayer(titel);
		if ($("#overlay").length == 0)
		{
			$("body").overlay({
				opacity: 85,
				window_width: 650,
				window_height: 450,
				close_img: '/images/overlay-close.gif',
				src: input,
				callback_ajax: 'getVideoScript("flashcontent", "'+videoUrl+'", "573", "347", "false","'+videoImage+'");'
			});
		}
	});
}

if (window.stepcarousel)
{
	
	stepcarousel.setup({
			galleryid: 'carrousel-box', //id of carousel DIV
			beltclass: 'carrousel', //class of inner "belt" DIV containing all the panel DIVs
			panelclass: 'panel', //class of panel DIVs each holding content
			autostep: {enable:true, moveby:1, pause:5000},
			panelbehavior: {speed:500, wraparound:true, persist:true},
			defaultbuttons: {enable: false, moveby: 1, leftnav: ['http://i34.tinypic.com/317e0s5.gif', -5, 80], rightnav: ['http://i38.tinypic.com/33o7di8.gif', -20, 80]},
			statusvars: ['statusA', 'statusB', 'statusC'], //register 3 variables that contain current panel (start), current panel (last), and total panels
			contenttype: ['inline'] //content setting ['inline'] or ['external', 'path_to_external_file']
		})
}

function getVideoPlayer(title){
	
	var output = '<div class="video-overlay-box">'+
		'<div class="video-container">'+
			'<h3>'+title+'</h3>'+
			'<div id="flashcontent">'+
				'<strong>U heeft geen flash player geinstalleerd. Om deze video te bekijken heeft u deze nodig.</strong>'+
			'</div>'+
		'</div>';
	output +='</div>';
	return output;
}

function getVideoScript(id, video, width, height, autoplay, image){	
	var flashvars = {
		videoPath: video,
		Width: width,
		Height: height,
		vColor: '0xc43930',
		rollColor: '0xFFFFFF',
		AutoPlay: autoplay,
		vidLoop: 'false',
		hideTime: '6000',
		screenImage: image
	};
	var videoparams = {
		allowFullScreen: 'true'
	};
	var videoattributes = {};
	swfobject.embedSWF("/swf/Advanced_video_player.swf", id, width, height, "9.0.0","expressInstall.swf", flashvars, videoparams, videoattributes);

}

function initEasterEggs(){
	$("#search").keyup(function(){
		var input = $(this).val();
		if (input == "control alt delete")
		{
			$("#wrapper").fadeOut("fast", function(){
				$("#wrapper").fadeIn("fast", function(){
					$("#wrapper").fadeOut("fast", function(){
						$("#wrapper").fadeIn("fast", function(){
							$("#wrapper").fadeOut("slow");
						});
					});
				});
			});
		}
		if (input == "ik wil pino")
		{
			$("img").each(function(){
				$(this).attr("replaced", $(this).attr("src"));
				$(this).attr("src", "/images/pino.jpg");
			});
			window.setTimeout('removePino()', 2000);
		}
		if (input == "kutlul")
		{
			$("body").overlay({
				window_width: 500,
				window_height: 200,
				close_img: '/images/overlay-close.gif',
				src: '<h1 style="margin-left: 25px;">Schaam je</h1><p style="margin-left: 25px;">Je mag natuurlijk geen scheldwoorden invoeren.</p><p style="margin-left: 25px;">Ga nu maar naar de website van de bond tegen het vloeken om het af te leren.</p>',
				callback_closing: window.setTimeout("window.location=\"http://www.bondtegenvloeken.nl/\"", 3000)
			});
		}
	});
}

function removePino(){
	$("img").each(function(){
		$(this).hide();
		$(this).attr("src", $(this).attr("replaced"));
		$(this).fadeIn();
	});
}

function trackPageViews(){
	$("a[rel|=outlink]").each(function(){
		var tracker = "javascript: pageTracker._trackPageview('"+$(this).attr("rel")+"')";
		$(this).attr("onclick", tracker);
	});
}
