(function($) {
	var defaults = {
		play: true,
		list: [],
		state: 'default',
		path: '',
		index: 0,
		draggable: false,
		dragOptions: {},
		interval: 6,
		button: false,
		container: false
	};
	var options = {};
	var selector;
	var timer;
	
	$.fn.backgroundmanager = function(action, new_options) {
		if(typeof(action)=='object'){
			new_options = action;
			action='init'
		}else if(typeof(action)=='undefined'){
			new_options = {};
			action ='init';
		}
		//Set Options
		options = $.extend({}, defaults, options, new_options);
		
		//Loop elements
		selector = this;
		this.each(function(){
			if(action=='init'){
				//init
				markup(this);
				//Events
				$(".ui-backgroundmanager-prev, .ui-backgroundmanager-next, .ui-backgroundmanager-play", this).bind("mouseover", function(){
					$(this).addClass("ui-state-hover");
				}).bind("mouseout", function(){
					$(this).removeClass("ui-state-hover");
				}).bind("click", function(){
					if($(this).hasClass("ui-backgroundmanager-prev")){
						if(--options.index < 0) options.index=options.list.length-1;
					}
					if($(this).hasClass("ui-backgroundmanager-play")){
						$(".ui-icon", this).toggleClass("ui-icon-play");
						$(".ui-icon", this).toggleClass("ui-icon-pause");
						options.play = options.play ? false : true;
						if(!options.play) clearTimeout(timer);
					}
					if($(this).hasClass("ui-backgroundmanager-next")){
						if(++options.index >= options.list.length) options.index=0;
					}
					loadBackground();
				});
			}
		});
		loadBackground();
		return this;
	};
	
	function markup(object){
//		if(options.state=='default'){
//			$(object).addClass("ui-backgroundmanager");
//		}else if(options.state=='compact'){
//			$(object).addClass("ui-backgroundmanager-small");
//		}
		//$(object).addClass("ui-widget ui-widget-content ui-corner-all ui-helper-clearfix");
		//var html = "<div class='ui-widget-header ui-helper-clearfix ui-corner-all'><a class='ui-corner-all ui-backgroundmanager-prev' title='Prev'><span class='ui-icon ui-icon-seek-prev'>Prev</span></a><a class='ui-corner-all ui-backgroundmanager-play' title='Play'>";
		//if(!options.play) html += "<span class='ui-icon ui-icon-play'>Play</span></a>";
		//else html+="<span class='ui-icon ui-icon-pause'>Play</span></a>";
		//html+="<a class='ui-corner-all ui-backgroundmanager-next' title='Next'><span class='ui-icon ui-icon-seek-next'>Next</span></a><span class='ui-backgroundmanager-title'>&nbsp;</span></div>";		
		//$(object).html(html);
		if(options.container){
			$("<div class='ui-background-content'>"+options.container+"</div>").appendTo(object);
		}
		if(options.button){
			if($.fn.button){
				$(object).append("<div id='buttonPane'></div>");
				$("<button>"+options.button.text+"</button>").appendTo("#buttonPane").css('width', '17em').button().click(options.button.onClick).parent().buttonset();
			}
		}
		
		if(options.draggable) $(object).draggable(options.dragOptions);
	}
	function loadBackground(){
		clearTimeout(timer);
		if(typeof(options.list[options.index])=='string'){
			var bg = {img:options.list[options.index],repeat:'no-repeat',color:'#fff'};
		}else if(typeof(options.list[options.index])=='object'){
			var bg = $.extend({repeat:'no-repeat',color:'#fff'},options.list[options.index]);
		}
		var img = new Image();
		$(img).load(function(){
			$(document).find('body').css({"background-position":"top center", "background-attachment":"fixed", "background-repeat":bg.repeat,"background-image":"url("+options.path+bg.img+")"});
			setInfo();
		}).attr('src',options.path+bg.img);
		if(options.play) timer = setTimeout(function(){ 
			if(++options.index >= options.list.length) options.index=0;
			loadBackground();
		},(options.interval*1000));	
		
		if (bg.img == "1.jpg"){
			$('#homeBackground1').show();
			$('#homeBackground2,#homeBackground3,#homeBackground4,#homeBackground5').hide();
		}
		else if (bg.img == "2.jpg"){
			$('#homeBackground1,#homeBackground3,#homeBackground4,#homeBackground5').hide();
			$('#homeBackground2').show()
		}
		else if (bg.img == "3.jpg"){
			$('#homeBackground1,#homeBackground2,#homeBackground4,#homeBackground5').hide()
			$('#homeBackground3').show()
		}
		else if (bg.img == "4.jpg"){
			$('#homeBackground1,#homeBackground2,#homeBackground3,#homeBackground5').hide()
			$('#homeBackground4').show()
		}
		else if (bg.img == "5.jpg"){
			$('#homeBackground1,#homeBackground2,#homeBackground3,#homeBackground4').hide()
			$('#homeBackground5').show()
		}
		
	}
	
	function setInfo(){
		if(options.list[options.index].title){
			$(".ui-backgroundmanager .ui-backgroundmanager-title").html(options.list[options.index].title);
		}else{
			$(".ui-backgroundmanager .ui-backgroundmanager-title").html('<i>geen titel</i>');

		}
		if(options.list[options.index].info){
			$(".ui-backgroundmanager .ui-background-content").html(options.list[options.index].info);
		}else{
			$(".ui-backgroundmanager .ui-background-content").html(options.container);
		}
	}
	
})(jQuery);
