

var advertensiewissel={
	csszindex: 100,
	ajaxloadingmsg: '<b>Fetching Content. Please wait...</b>',
	glide:function(config, showpage, isprev){
		var begininhoud=parseInt(showpage)
		if (begininhoud>=config.$contentdivs.length){ 
			begininhoud=0
		}
		var $target=config.$contentdivs.eq(begininhoud)
		
		if (config.$wisselaardiv.attr('lastbegininhoud')==null || parseInt(config.$wisselaardiv.attr('lastbegininhoud'))!=begininhoud){
			var $begininhoudlink=config.$toc.eq(begininhoud)
			config.$next.attr('loadpage', (begininhoud<config.$contentdivs.length-1)? begininhoud+1+'pg' : 0+'pg')
			config.$prev.attr('loadpage', (begininhoud==0)? config.$contentdivs.length-1+'pg' : begininhoud-1+'pg')
			var startpoint=(isprev=="previous")? -config.startpoint : config.startpoint
			$target.css(config.leftortop, startpoint).css("zIndex", this.csszindex++) 
			var endpoint=(config.leftortop=="left")? {left:0} : {top:0} 
			$target.animate(endpoint, config.spoed)
			config.$toc.removeClass('begininhoud')
			$begininhoudlink.addClass('begininhoud')
			config.$wisselaardiv.attr('lastbegininhoud', begininhoud+'pg')
		}
	},

	getajaxinhoud:function(config){
		config.$glider.html(this.ajaxloadingmsg)
		$.ajax({
			url: config.ajaxinhoud,
			error:function(ajaxrequest){
				config.$glider.html('Error fetching content.<br />Server Response: '+ajaxrequest.responseText)
			},
			success:function(content){
				config.$glider.html(content)
				advertensiewissel.setupwisselaar(config)
			}
		})
	},

	aligncontents:function(config){
		config.$contentdivs=$("#"+config.advertensiereeksid+" ."+config.inhoudklas)
		config.$contentdivs.css(config.leftortop, config.startpoint).css({height: config.$glider.height(), visibility: 'visible'}) 
	},

	setupwisselaar:function(config){
		this.aligncontents(config)
		config.$wisselaardiv.hide()
		config.$toc.each(function(index){
				$(this).attr('pagenumber', index+'pg')
				if (index > (config.$contentdivs.length-1))
					$(this).css({display: 'none'}) 
		})
		var $nextandprev=$("#"+config.wisselid+" .next, #"+config.wisselid+" .prev")
		$nextandprev.click(function(event){ 
			advertensiewissel.glide(config, this.getAttribute('loadpage'), this.getAttribute('buttontype'))
			event.preventDefault() 
		})
		config.$toc.click(function(event){ 
			advertensiewissel.glide(config, this.getAttribute('pagenumber'))
			event.preventDefault()
		})
		config.$wisselaardiv.fadeIn(1000, function(){
			advertensiewissel.glide(config, config.begininhoud)
			if (config.outoroteer==true){ 
				config.stepcount=0 
				config.totalsteps=config.$contentdivs.length*config.outoroteerconfig[1] 
				advertensiewissel.outoroteer(config)
			}
		})
		config.$wisselaardiv.click(function(){
			advertensiewissel.canceloutoroteer(config.wisselid)
		})
	},

	outoroteer:function(config){
		var rotatespoed=config.spoed+config.outoroteerconfig[0]
		window[config.wisselid+"timer"]=setInterval(function(){
			if (config.totalsteps>0 && config.stepcount>=config.totalsteps){
				clearInterval(window[config.wisselid+"timer"])
			}
			else{
				config.$next.click()
				config.stepcount++
			}
		}, rotatespoed)
	},

	canceloutoroteer:function(wisselid){
		if (window[wisselid+"timer"])
			clearInterval(window[wisselid+"timer"])
	},

	getCookie:function(Name){ 
		var re=new RegExp(Name+"=[^;]+", "i") 
		if (document.cookie.match(re)) 
			return document.cookie.match(re)[0].split("=")[1] 
		return null
	},

	setCookie:function(name, value){
		document.cookie = name+"="+value
	},

	init:function(config){
		$(document).ready(function(){
			config.$glider=$("#"+config.advertensiereeksid)
			config.$wisselaardiv=$("#"+config.wisselid)
			config.$toc=config.$wisselaardiv.find('.toc')
			config.$next=config.$wisselaardiv.find('.next')
			config.$prev=config.$wisselaardiv.find('.prev')
			config.$prev.attr('buttontype', 'previous')
			var begininhoud=(config.onthou)? advertensiewissel.getCookie(config.advertensiereeksid) : config.begininhoud
			config.begininhoud=(isNaN(parseInt(begininhoud))) ? config.begininhoud : begininhoud 
			config.leftortop=(/up/i.test(config.rigting))? "top" : "left" 
			config.heightorwidth=(/up/i.test(config.rigting))? config.$glider.height() : config.$glider.width() 
			config.startpoint=(/^(left|up)/i.test(config.rigting))? -config.heightorwidth : config.heightorwidth 
			if (typeof config.ajaxinhoud!="undefined" && config.ajaxinhoud.length>0)
				advertensiewissel.getajaxinhoud(config)
			else
				advertensiewissel.setupwisselaar(config)
			$(window).bind('unload', function(){ 
				config.$wisselaardiv.unbind('click')
				config.$toc.unbind('click')
				config.$next.unbind('click')
				config.$prev.unbind('click')
				if (config.onthou)
					advertensiewissel.setCookie(config.advertensiereeksid, config.$wisselaardiv.attr('lastbegininhoud'))
				config=null
				
			})
		})
	}
}
