(function($) {
	var defaults = {
		play: true,
		list: [],
		index: 0,
		interval: 60,
		speed:1000,
		pause:10
	};
	var options = {};
	var selector;
	var timer;
	window.onorientationchange = function() {
		
		
			$("#bgFader1").css({"position":"absolute","left":"0","top":"0","background-attachment":"fixed","background-size":"100% 100%","-webkit-background-size":"100% 100%","width":$(window).width(),"height":$(window).height() ,"background-position":"left top","background-color":"transparent","background-repeat":"no-repeat"});
			$("#bgFader2").css({"position":"absolute","left":"0","top":"0","background-attachment":"fixed","background-size":"100% 100%","-webkit-background-size":"100% 100%","width":$(window).width(),"height":$(window).height() ,"background-position":"left top","background-color":"transparent","background-repeat":"no-repeat"});
		
	}
	$.fn.animatedbackground = 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);
		
		if ((options.interval*1000)<options.speed){
		options.interval=options.speed+1000;
		}
		if (options.index==0){
		options.index=Math.floor(Math.random()*options.list.length)
		}
		//Loop elements
		selector = this;
		this.each(function(){
			if(action=='init'){
				//init
				$(this).wrapInner("<div id=\"content\" style=\"background-color: transparent;position:absolute;width:"+$(window).width()+";height:"+$(window).height()+";top:0;left:0;z-index: 10;\"></div>");
				$(this).prepend("<div id=\"bgFader2\" class=\"faderbackground hidden\"></div>");
				$(this).prepend("<div id=\"bgFader1\" class=\"faderbackground show\"></div>");
				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]);
				}
				if(++options.index >= options.list.length) {
								options.index=0;
				}
				if(typeof(options.list[options.index])=='string'){
					var bg2 = {img:options.list[options.index],repeat:'no-repeat',color:'#fff'};
				}else if(typeof(options.list[options.index])=='object'){
					var bg2 = $.extend({repeat:'no-repeat',color:'#fff'},options.list[options.index]);
				}
				$("#bgFader1").css({"position":"absolute","left":"0","top":"0","z-index":"1", "background-attachment":"fixed","background-size":"100% 100%","-webkit-background-size":"100% 100%","width":$(window).width(),"height":$(window).height() ,"background-position":"right top","background-color":"transparent","background-repeat":"no-repeat","background-image":"url("+bg.img+")"});
				$("#bgFader2").css({"position":"absolute","left":"0","top":"0","z-index":"0", "background-attachment":"fixed","background-size":"100% 100%","-webkit-background-size":"100% 100%","width":$(window).width(),"height":$(window).height() ,"background-position":"right top","background-color":"transparent","background-repeat":"no-repeat","background-image":"url("+bg2.img+")"});
				if(++options.index >= options.list.length) {
				options.index=0;
				}
				//Events
				loadBackground();
			}
		});
		$(window).resize(function(){
				$("#content").css({"position":"absolute","left":"0","top":"0","width":$(window).width(),"height":$(window).height() });
				$("#bgFader1").css({"position":"absolute","left":"0","top":"0","background-position":"center center", "background-attachment":"fixed","background-size":"100% 100%","-webkit-background-size":"100% 100%","width":$(window).width(),"height":$(window).height() ,"background-position":"left top","background-color":"transparent","background-repeat":"no-repeat"});
				$("#bgFader2").css({"position":"absolute","left":"0","top":"0","background-position":"center center", "background-attachment":"fixed","background-size":"100% 100%","-webkit-background-size":"100% 100%","width":$(window).width(),"height":$(window).height() ,"background-position":"left top","background-color":"transparent","background-repeat":"no-repeat"});
		});
		return this;
	}
	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 objectvisible=$('.faderbackground.show').attr('id');
				var objecthidden=$('.faderbackground.hidden').attr('id');
				
				$("#"+objectvisible).css("z-index","1");
				$("#"+objecthidden).css("z-index","0");
				$("#"+objecthidden).show("fast",function(){
						$("#"+objectvisible).fadeOut( options.speed, function(){
						$("#"+objectvisible).css({"background-attachment":"fixed","background-color":"transparent","background-size":"100% 100%","-webkit-background-size":"100% 100%","width":$(window).width(),"height":$(window).height(),"background-repeat":"no-repeat","background-position":"left top","background-image":"url("+bg.img+")"});
						$("#"+objectvisible).removeClass('show').addClass("hidden");
						$("#"+objecthidden).removeClass('hidden').addClass("show");
					});
				});

		if(options.play) timer = setTimeout(function(){ 
			if(++options.index >= options.list.length) {
				options.index=0;
				}
			loadBackground();
		},(options.pause*1000));
	}
})(jQuery);



		
		

	
