(function( $ ){

	$.fn.theme = function(method, options) {

		var defaults = {
			tweenDuration: 500,
			sitebg: './img/bg/{theme}_high.jpg',
			rotationbg: './img/bg/slide_{theme}.jpg',
			/*navbg: './img/balken_{theme}.png',*/
			bgcolors: site.bgcolors,
			themes: ['blau','olive','bordeaux','orange','grau']
		};

		var o = $.extend(defaults, options); 
		
		var methods = {
			init: function() { 
				return this.each(function(){ 
					var $this = $(this);
					$this.prepend(
						$('<div class="bg-images-container">')
						.css({
						  position: 'absolute',
						  'z-index': -1,
						  top: 0,
						  left: 0,
						  width: '100%',
						  height: '100%',
						  overflow: 'hidden'
						})
						.append( 
							$('<div class="bg-images">')
							.css({position: 'relative'})
						)
					);
					$('[data-theme]').each(function() {
						var t = $(this).attr('data-theme'),
						bg = $(this).attr('data-sitebg') ? $(this).attr('data-sitebg') : o.sitebg.replace('{theme}', t);
						/*navbg = $(this).attr('data-navbg') ? $(this).attr('data-navbg') : o.navbg.replace('{theme}', t);*/ 
						$(this).attr({
							'data-bgimage': bg/*,
							'data-navbgimage': navbg*/
						})
						.next().find('a').each(function() {
							$(this).attr({
								'data-theme': $(this).attr('data-theme') || t,
								'data-sitebg': $(this).attr('data-sitebg') || bg/*,
								'data-navbg': $(this).attr('data-navbg') || navbg*/ 
							}); 
						});
					});
					methods.preload();
					return $this;
				});
			},
			preload: function() {
				for (c in o.bgcolors) {
					$('<img src="img/bg/'+c+'_high.jpg" style="position:absolute;top:0;left:0;display:none" onload="$(this).remove()"/>').appendTo(document.body);
					$('<img src="img/bg/slide_'+c+'.jpg" style="position:absolute;top:0;left:0;display:none" onload="$(this).remove()"/>').appendTo(document.body);
				}
			},
			load: function(name) { 
				var instant = false;
				if ($.isPlainObject(name)) {
					var params = $.extend({},name);
					name = params.name;
					instant = params.instant;
				}
				/*
				if ($.fn.theme.current && $.fn.theme.current == name) {
					return $(this);
				} 
				*/
				 
				// set body theme class
				var b = $(document.body);
				var bClass = b.attr('class');
				if (bClass.indexOf('theme-') != -1) {
					var tClass = 'theme-' + bClass.split('theme-')[1].split(' ')[0];
					b.attr('class', bClass.replace(tClass, 'theme-'+name));
				}
				else {
					b.addClass('theme-'+name);
				}
				
				$.fn.theme.current = name;  
				return this.each(function() {
					var $this = $(this); 
					//-------------------------------
					// sitebg 
					//------------------------------- 
					var src = $.fn.theme.rotation ? o.rotationbg.replace('{theme}', name) : o.sitebg.replace('{theme}', name);
					$('<img>')
			  		.css({
			  			position: 'absolute',
			  			top: 0,
			  			left: 0,
			  			opacity: 0,
			  			'-webkit-backface-visibility': 'hidden',
			  			'-webkit-transform': 'translate3d(0,0,0)',
			  			'-webkit-transform-style': 'preserve-3d'
			  		})
			  		.load(function() {  
			  			$(this)
			  	  		.appendTo($('.bg-images'))
			  	  		.animate({opacity :1}, instant ? 0 : o.tweenDuration, function() { 
			  				var removeOld = function() {
			  					if ($('.bg-images > .bg-element').length > 1) {
				  					$('.bg-images > .bg-element:eq(0)').remove();
				  				}
			  				}
			  				if (IS_MOBILE) {
			  					setTimeout(removeOld, o.tweenDuration)
			  				}
			  				else {
				  				removeOld();
			  				}
			  			});
			  			//$('.site').css('min-height', $(this).height())
			  		})
			  		.attr({
			  			'class': 'bg-element bg-image',
			  			'src': src
			  		});
			  		
					//-------------------------------
					// site bgcolor
					//------------------------------- 
					
					
					$('.bgcolor').animate({ backgroundColor: o.bgcolors[name] }, o.tweenDuration);
	 
					return $this;
				});
			},
			startRotation: function() {
				$.fn.theme.rotation = true;
				$.fn.theme.rotationInt = setInterval(function() {
					$('.site').theme('next')
				}, 2000);
			},
			stopRotation: function() {
				$.fn.theme.rotation = false;
				clearInterval($.fn.theme.rotationInt)
			},
			next: function() {
				var c = $.fn.theme.current;
				var c_id = $.inArray(c, o.themes); 
				var n_id = c_id+1 >= o.themes.length ? 0 : c_id+1; 
				var n = o.themes[n_id]; 
				$('.site').theme('load', n);
			}
		};
		
		if ( methods[method] ) {
			return methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 ));
		} else if ( typeof method === 'object' || ! method ) {
			return methods.init.apply( this, arguments );
		} else {
			$.error( 'Method ' +  method + ' does not exist on jQuery.vNavSubmenu' );
		} 
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	/*
	
	
	var updateBalken = function(url) {
 
  		var o = $.fn.sitebg.options;
  		// hack: improve and make theme plugin instead of sitebg plugin
  		var theme = url.split('bg_')[1].split('.')[0];
  		var balken = 'img/balken_'+theme+'.png';
  		
  		var navbg = $('<div>')
  		.css('opacity', 0)
  		.appendTo($('.vnav-bg-images'));
  		
  		$('<img>')
  		.load(function() {
  			$(document).pngFix(); 
  			navbg.animate({opacity:1},o.tweenDuration);
  			$('.vnav-bg-images *:eq(0)').animate({opacity:0},o.tweenDuration, function() {
  				$(this).remove();
  			});
  		})
  		.appendTo(navbg)
  		.attr('src', balken)
  		
	}
  var methods = { 
	init: function() { 
	  
	  // container for bg images
	  this.prepend(
		$('<div class="bg-images-container">')
		.css({
		  position: 'absolute',
		  'z-index': -1,
		  top: 0,
		  left: 0,
		  width: '100%',
		  height: '100%'
		})
		.append( 
			$('<div class="bg-images">')
			.css({position: 'relative'})
		)
	  );
	  
	  // data attributes
	  $('[data-theme]').each(function() {
		  var t = $(this).attr('data-theme'),
		  bg = './img/bg_'+$(this).attr('data-theme')+'.jpg',
		  navbg = './img/balken_'+$(this).attr('data-theme')+'.png';		  
		  $(this).attr({
			  'data-bgimage': bg,
			  'data-navbgimage': navbg
		  })
		  .next().find('a').attr({
			  'data-theme': t,
			  'data-bgimage': bg,
			  'data-navbgimage': navbg 
		  }); 
	  })
	  
	  // listen to hashchange event and set image
	  $(window).bind('hashchange.sitebg', $.proxy( function() {  
		  this.sitebg('load', $('a[data-path="/'+hash()+'"]').attr('data-bgimage'));
	  }, this));
	  return $(this);
  	},
  	
	load: function(url) { 
  		var force = false;
  		if ($.fn.sitebg.rotInterval) {
			$('.slideshow img').stop();
			clearInterval($.fn.sitebg.rotInterval);
			$.fn.sitebg.rotInterval = null; 
			force = true;
		}
	
  		var o = $.fn.sitebg.options;
		if (!force && url === $('.bg-images > img:eq(0)').attr('src')) {
  			return $(this);
  		}
  		var img = $('<img>')
  		.css({
  			position: 'absolute',
  			top: 0,
  			left: 0,
  			opacity: 0,
  			'z-index':999
  		})
  		.load(function() {  	  			 
  			$(this).animate({opacity:1}, o.tweenDuration, function() { 
  				if ($('.bg-images > .bg-element').length > 1) {
  					$('.bg-images > .bg-element:eq(0)').remove();
  				}
  			})
  	  		.appendTo($('.bg-images')); 
  		}).
  		attr({
  			'class': 'bg-element bg-image',
  			'src': url
  		});
  		
  		updateBalken(url);
  		
  		
  		
  		return $(this);
  	},
  	
  	rotation: function(value) {
  		if (value===false) {
  			if ($.fn.sitebg.rotIntervalerval) {
  				$('.slideshow img').stop();
  				clearInterval($.fn.sitebg.rotInterval);
  				$.fn.sitebg.rotInterval = null;
  			} 
  		}
  		else {
  			var o = $.fn.sitebg.options;
  			$('.bg-images').append(
  				$('<div class="bg-element slideshow">')
  				.css({
  					position:'relative',
  					width:'100%',
  					height:'100%'
  				})
  			);
  			var used_imgs = []; 
  			$('[data-bgimage]').each(function() {
  				var img_src = $(this).attr('data-bgimage');
  				if ($.inArray(img_src, used_imgs) == -1) {
  					used_imgs.push(img_src);
  					$('.slideshow').append(
  						$('<img>')
  						.css({
  							position: 'absolute',
	  						top: 0,
	  						left: 0
  						})
  						.attr({
 							 src: img_src,
 							 border: 0
 						})
  					);
  				}
  			}); 

  			 
  			var nextImg = function() {
	  		    var $active = $('.slideshow img.active');
	
	  		    if ( $active.length == 0 ) $active = $('.slideshow img:last');
	
	  		    // use this to pull the images in the order they appear in the markup
	  		    var $next =  $active.next().length ? $active.next()
	  		        : $('.slideshow img:first');
	
	  		    // uncomment the 3 lines below to pull the images in random order
	  		    
	  		    // var $sibs  = $active.siblings();
	  		    // var rndNum = Math.floor(Math.random() * $sibs.length );
	  		    // var $next  = $( $sibs[ rndNum ] );
	
	
	  		    $active.addClass('last-active');
  		  		updateBalken($next.attr('src'));
	
	  		    $next.css({opacity: 0.0})
	  		        .addClass('active')
	  		        .animate({opacity: 1.0}, o.tweenDuration, function() {
	  		            $active.removeClass('active last-active');
	  		        });
	  		}
  			nextImg();
  			$.fn.sitebg.rotInterval = setInterval( nextImg, o.rotationDuration );
  		}
  	}
  };

  $.fn.sitebg = function( method ) {
  
    if ( methods[method] ) {
      return methods[method].apply( this, Array.prototype.slice.call( arguments, 1 ));
    } else if ( typeof method === 'object' || ! method ) {
      return methods.init.apply( this, arguments );
    } else {
      $.error( 'Method ' +  method + ' does not exist on jQuery.tooltip' );
    }    
  
  };
  $.fn.sitebg.options = {
	  tweenDuration: 1500,
	  rotationDuration: 3000
	};
  */
})( jQuery );
