(function() {
	
	var shop = {
		init: function() {
			$('.article-view').each(function() {
				$(this).click(function() {
					$('#viewer').html('');
					$('#viewer').addClass('article-viewer-loading');
					$('#viewer').slideDown();
					$('#main-block').slideUp();
					$.ajax({
						url: this.href,
						cache: false,
						dataType: 'html',
						success: function(html){
							$('#viewer').removeClass('article-viewer-loading');
							$('#viewer').addClass('article-viewer');
							$('#viewer').html(html);
							articleHelper.load();
						}
					});
					return false;
				})	
			});
		}
	};
	
	var articleHelper = {
		images: [],
		load: function() {
			this.colorChangeListener();
			$('#close-article-view').click(function() {
	    		$('#viewer').slideUp();
	    		$('#main-block').slideDown();
	    		return false;
			});
			$('.closeup_preview').each(function() {
				$(this).mouseover(function() {
					$("#closeup_src").attr({src: document.images[this.name+'_full'].src});
				});
			});
		},
		
		colorChangeListener: function() {
			$('#color-select').change(function() {
				var color = this.options[this.selectedIndex].value;
				for(var i = 0; i < document.images.length; i ++) {
					var currentImage = document.images[i];
					if (
						currentImage.name == 'default_preview' ||
						currentImage.name == 'closeup_big'
						)
						replaceColor( currentImage, color );
					for (var j = 1; j <= 4; j++) {
						if (currentImage.name.indexOf('closeup_'+j) != -1)
							replaceColor( currentImage, color );
					}
				}	
			});
			// set the src attrib of the current image to new src
			function replaceColor( image, color ) {
				var cs = image.src.lastIndexOf('_');
				var ce = image.src.lastIndexOf('.');
				var rs = image.src.substr(0, cs);
				var re = image.src.substr(ce, image.src.length);
				image.src = rs.concat('_'+color).concat(re);
			}
		}
	};
	
	var navigation = {
		entry: [
			{ id :'dmenu', prefix:'_a', imgOn :[], imgOff :[]},
			{ id :'sub_1', prefix:'_a', imgOn :[], imgOff :[]},
			{ id :'sub_2', prefix:'_a', imgOn :[], imgOff :[]},
			{ id :'sub_3', prefix:'_a', imgOn :[], imgOff :[]},
			{ id :'sub_4', prefix:'_a', imgOn :[], imgOff :[]}
		],
		getKey: function(id, type) {
			for (var i = 0 ; i < this.entry.length; i++ ) {
				if (id == this.entry[i].id )
					return this.entry[i][type];
			}
		},
		handleMouseOver: function() { this.src = navigation.getKey(this.menuName, 'imgOn')[this.number].src; },
		handleMouseOut: function() { this.src = navigation.getKey(this.menuName, 'imgOff')[this.number].src; }
	}
	
	var app = {
		safari: navigator.userAgent.indexOf('AppleWebKit/') > -1,
		timer:  500,
		fademenu: true,
		prehide: function() {
			// hide menu
			$('#dmenu').children('li').each(function(i, item) {
				$(item).hide();
			});
			// hide custome elements
			var elements = ['#style-intro','#pre-intro','.hidden'];
			$(elements).each(function(i, item) {
				$(item).hide();
			});
		},
		
		postview: function() {
			// get elements by generic class an show again 
			var elements = ['.fadein'];
			$(elements).each(function(i, item) {
				var o = $(item);
				if (app.safari)
					o.show('slow');
				else
					o.fadeIn(1500);
			});
		},
		
		init: function() {
			if (typeof navigation == 'undefined') return;
			for (var i=0; i < navigation.entry.length; i++) {
				var current = navigation.entry[i];
				var nav = document.getElementById(current.id);
				if (!nav) continue; 
				var imgs = nav.getElementsByTagName('img');
				for (var j=0;j<imgs.length;j++) {
					var suffix = imgs[j].src.substring(imgs[j].src.lastIndexOf('.'));
					current.imgOff[j] = new Image();
					current.imgOff[j].src = imgs[j].src;
					current.imgOn[j] = new Image();
					current.imgOn[j].src = imgs[j].src.substring(0,imgs[j].src.lastIndexOf('.')) + current.prefix + suffix;
					imgs[j].number = j;
					imgs[j].menuName = current.id;
					$(imgs[j]).bind("mouseover", navigation.handleMouseOver);
					$(imgs[j]).bind("mouseout", navigation.handleMouseOut);
				}
			}
			
			$(window).load(function() {
				if (document.getElementById('pre-intro')) {
					if (!app.safari)
						$('#pre-intro').fadeIn(2000);
					else
						$('#pre-intro').show(2000);
					app.cookie.erase('bettinavisit');
				}
				var cpage = document.getElementById('swap-contact');
				if (cpage) {				
					var contact = new swapper(document.getElementById('swap-contact'), { prefix: '_a', type: 'img' } );
					var senden = new swapper(document.createElement('div'), { prefix: '_a', id: 'contact-senden' } );
				}
				if (!app.fademenu)
					app.showStyle(500);
				
			});
		},
		
		cookie: {
		
			get: function(name) {
				var nameEQ = name + "=";
				var ca = document.cookie.split(';');
				for(var i=0;i < ca.length;i++) {
					var c = ca[i];
					while (c.charAt(0)==' ') c = c.substring(1,c.length);
					if (c.indexOf(nameEQ) == 0) 
						return c.substring(nameEQ.length,c.length);
				}
				return null;
			},
			
			set: function (name, value, days){
				if (days) {
					var date = new Date();
					date.setTime(date.getTime()+(days*24*60*60*1000));
					var expires = "; expires="+date.toGMTString();
				}
				else var expires = "";
				document.cookie = name+"="+value+expires+"; path=/";
			},
			
			erase: function(name) {
				this.set(name,"",-1);
			},
			
			check: function() {
				var ends = 1000 * 60 * 30; // 30 minutes
				var coo = this.get('bettinavisit');
				if (!coo) {
					app.fademenu = true;
					this.set("bettinavisit", 'yes');	
				} else {
					app.fademenu = false;				
				}
			}
		},
		
		showStyle:function(delay) {
			window.setTimeout(function() {
				var style = document.getElementById('style-intro');
				if (style) {				
					if (app.safari)
						$(style).show('slow');
					else
						$(style).fadeIn(2000);
				}
			}, delay);
		},
		
		menu : {
			current: false,
			count: 0,
			animate: function() {
				$('#dmenu').children('li').each(function(i, item) {
					var li = $(item);
					if (app.fademenu) {
						app.timer+= 500;
						window.setTimeout(function() {
							app.menu.count++;
							if (app.safari)
								li.show('slow', function() {
									if (app.menu.count >= 7)
										app.showStyle(1500);
								});
							else
								li.fadeIn(1500, function() {
									if (app.menu.count >= 7)
										app.showStyle(1500);
								});
						}, app.timer);
					} else {
						li.show();
					}
					li.find('a').each(function(k, topmenulia) { 
						app.menu.markCurrent(topmenulia); 
					});
				});
			},
			
			markCurrent: function(node) {
				var matchstr = new RegExp('/\show\/'.concat(bs_pagename).concat('(?!_)'));
				var match = matchstr.exec(node.href);
				if ( match != null ) {
					var img = node.getElementsByTagName('img')[0];
					$(node).children('img').each(function(j, img) {
						var src = img.src.split('/img/');
						var name = src[1].split('.');
						img.src = '/img/' + name[0] + '_a.gif';
						$(img).unbind("mouseover", navigation.handleMouseOver);
						$(img).unbind("mouseout", navigation.handleMouseOut);
						if ($(node).parent().hasClass('submenuli'))
							$(node).parent().parent().addClass('open');
						
						$(node).parent().find('ul').each(function(c, obj) {
							$(obj).addClass('open');
						});
					});
				}
			},
			
			helper: function() {
				var navRoot = document.getElementById("dmenu");
				if (navRoot) {
					for (var i=0; i < navRoot.childNodes.length; i++) {
						node = navRoot.childNodes[i];
						if (node.nodeName=="LI") {
							node.onmouseover = function() { this.className+=" over"; }
							node.onmouseout = function() { this.className=this.className.replace(" over", "");	}
						}
					}
				}
			}
		}
	};
	
	var simpleGallery = {
		init: function() {
			var screen = $('#image-holder').get(0);
			var defaultSrc = screen.src;
			
			$('#images-row img').each(function() {
				// preload
				var image = document.createElement('img')
				image.src = $(this).attr('rel');
				
				$(this).mouseover(function() {
					screen.src = $(this).attr('rel');
				});
				$(this).mouseout(function() {
					screen.src = defaultSrc;
				});
			});
		}
	}
	
	var swapper = function(element, params) {
			
		this.element = element;
		this.params = params;
		this.init();
	}
		
	swapper.prototype = {
		
		init: function() {
			
			if ( !this.params.id || !this.element ) {
				var imgs = this.element.getElementsByTagName('img');
				for (var i = 0; i < imgs.length; i++) {
					$(imgs[i]).bind("mouseover", this, this.over);
					$(imgs[i]).bind("mouseout", this, this.out);
				}
			}
			else if ( this.params.id ) {
				$('#'+this.params.id).bind("mouseover", this, this.over);
				$('#'+this.params.id).bind("mouseout", this, this.out);
			}
		},
		// switch to new img path with given prefix@params, save old path		
		over: function(obj) {
			obj.data.off = this.src;
			this.src = this.src.substr(0, (this.src.length - 4)).concat(obj.data.params.prefix).concat(this.src.substr((this.src.length - 4), this.src.length));
		},
		// restore old img path
		out: function(obj) {
			this.src = obj.data.off;
		}
			
	};
	
	$(document).ready(function() {
		app.prehide();
		app.postview();
		// make menu items hoverable
		app.init();
		shop.init();
		var menu = document.getElementById('dmenu');
		if (menu) {
			app.cookie.check();
			app.menu.helper();
			app.menu.animate();
		}
		var sg = document.getElementById('simple-gallery');
		if (sg)
			simpleGallery.init();
	});
})();