
	/// SETTINGS
	var idleOpacity = 			1;
	var hoverOpacity = 			0.8;
	var filteredOpacity = 		0.2;
	var selectBoxClassesFilter = Array('item','active','filtered','lastPic','firstPic',' corporate',' editorial',' grafik',' web',' event',' dialog',' ');

	var selectBox;
	var imageViewer;
	var slickBox;
	var filter;

	// DOCUMENT READY
	$(document).ready(function() {
		var location = $.address.baseURL().replace('http://', '').split("/");
		location = location[location.length-1];
	
		var subFolderName = false;
		if(location == "home")								subFolderName = "projekte";
		else if(location == "agentur")						subFolderName = "team";
		else if(location == "kontakt")						subFolderName = "infos";
		else if(location == "aktion-jahres-schluss")		subFolderName = "aktion";
		else if(location == "aktion-jahres-schluss-zwei")	subFolderName = "aktion2";
		else if(location == "aktion-jahres-schluss-drei")	subFolderName = "aktion3";
			
		if($('body').hasClass('home')) {
			if($.browser.msie && $.browser.version < 7) {
				// DO NOTHING
			}
			else {
				getSlickBox();
			}
		}
		else {
			selectBox = 	new SelectBox();
			slickBox = 		new SlickBox(subFolderName);
			imageViewer = 	new ImageViewer();
			//filter = 		new Filter();
		}

	
		// JQUERY ADDRESS
		$.address.init(function(event) {
		}).change(function(event) {
			checkAddress();
		});

		// KONTAKT
		$('#right').children('a.impressum').click(function(e) {
			e.preventDefault();
			slickBox.showByName('impressum');
		});
		$('#right').children('a.agb').click(function(e) {
			e.preventDefault();
			slickBox.showByName('agb');
		});
		$('#right').children('a.datenschutz').click(function(e) {
			e.preventDefault();
			slickBox.showByName('datenschutz');
		});
		$('.slickKontakt').children('a.kontakt_offensive_eins').click(function(e) {
			e.preventDefault();
			slickBox.showByName('kontakt_offensive_eins');
		});
		$('.slickKontakt').children('a.kontakt_offensive_zwei').click(function(e) {
			e.preventDefault();
			slickBox.showByName('kontakt_offensive_zwei');
		});
		$('.slickKontakt').children('a.kontakt_offensive_drei').click(function(e) {
			e.preventDefault();
			slickBox.showByName('kontakt_offensive_drei');
		});
	
		// AGENTUR
		$('#selectBox.ansprechpartner').children().hover(function() {
			var name = $(this).attr('className');
			$.each(selectBoxClassesFilter, function(i, v) {
				name = name.replaceAll(v, '');
			});
			$('#right').children('.ansprechpartner').children('li').children('a.'+name).addClass('active');
			Cufon.refresh('#right > ul > li > a');
		}, function() {
			$('#right').children('.ansprechpartner').children('li').children('a').removeClass('active');
			Cufon.refresh('#right > ul > li > a');
		});
		$('#right').children('.ansprechpartner').children('li').children('a').hover(function() {
			var title = $(this).attr('title');
			selectBox.items.each(function() {
				if ($(this).attr('title') == title) {
					selectBox.mouseOver($(this), true);
					return false;
				}
			});
		}, function() {
			var title = $(this).attr('title');
			selectBox.items.each(function() {
				if ($(this).attr('title') == title) {
					selectBox.mouseOut($(this));
					return false;
				}
			});
		}).click(function(e) {
			e.preventDefault();
			var show = $(this).attr('className');
			slickBox.showByName(show);
		});
		
		
		// web-check:
		$('.webcheckYes').click(function() {
			updateWebcheckStatus();
		});
		$('.webcheckNo').click(function() {
			updateWebcheckStatus();
		});
	});
	
	function updateWebcheckStatus() {
		var numberOfWebcheckYes = $('.webcheckYes:checked').size();
		$('#webcheckResult').val((70/10*numberOfWebcheckYes)+"%");
	}

	// CHECK ADDRESS
	function checkAddress() {
		var curPath = $.address.pathNames();
		// CHECK SLICKBOX
		if(slickBox) {
			if(curPath[0] == slickBox.folderName && curPath[1] && curPath[1] != "" && (curPath[1] != slickBox.activeName || (curPath[1] == slickBox.activeName && !slickBox.visible))) {
				slickBox.showByName(curPath[1], true);
			}
			else if((curPath[0] == slickBox.folderName || !curPath[0] || curPath[0] == "filter") && (curPath[1] == "" || !curPath[1] || curPath[0] == "filter") && slickBox.visible) {
				slickBox.hide(true);
			}
		}
		// CHECK FILTER
		if(filter) {	
			if(curPath[0] == "filter" && curPath[1] && curPath[1] != "" && curPath[1] != filter.active) {
				filter.apply(curPath[1]);
			}
			else if(curPath[0] == "filter" && !curPath[1]) {
				filter.reset(true);
			}
		}
	}

	// GET SLICKBOX
	function getSlickBox() {
		$.ajax({
			url: 		"view/slickboxProjekte.php",
			cache: 		false,
			success: function(html) {
				onGetSlickBox(html);
			}
		});

	}

	function onGetSlickBox(html) {
		$('#items').empty().append(html);
		selectBox = 	new SelectBox();
		slickBox = 		new SlickBox("projekte");
		imageViewer = 	new ImageViewer();
		filter = 		new Filter();
		checkAddress();
		Cufon.refresh('h3');
	}

	// SELECTBOX
	function SelectBox() {
		this.info = 			$('#selectBoxInfo');
		this.items = 			$('#selectBox').children('a');
		this.itemsPerRow = 		6;
		this.itemCount = 		this.items.length;
	
		var _ref = this;
	
		this.items.click(function(e) {
			e.preventDefault();
			_ref.click($(this));
		})
		.hover(function() {
			_ref.mouseOver($(this));
		}, function() {
			_ref.mouseOut($(this));
		}).css('opacity', idleOpacity);
	};
	SelectBox.prototype.click = function(element) {
		slickBox.showItem(element);
	};
	SelectBox.prototype.mouseOver = function(element, noInfo) {
		if($.browser.msie)		element.data('opacity', element.css('opacity')).css('opacity', hoverOpacity);
		else 					element.data('opacity', element.css('opacity')).fadeTo(50, hoverOpacity);
		if(!noInfo)	this.showInfo(element);
	};
	SelectBox.prototype.mouseOut = function(element) {
		if($.browser.msie)		element.css('opacity', element.data('opacity'));
		else 					element.fadeTo(20, element.data('opacity'));
		this.hideInfo();
	};
	SelectBox.prototype.showInfo = function(element) {
		var title = element.attr('title');
		if (title != "") {
			this.info.empty().append(title);
			//this.info.show();
		}
	};
	SelectBox.prototype.hideInfo = function() {
		this.info.empty();
	};

	// SLICKBOX
	function SlickBox(folderName) {
		this.slickBox = 		$('#slickBox');
		this.overlay = 			$('#overlay');
		this.itemContainer = 	this.slickBox.children('.items');
		this.items = 			this.itemContainer.children();
		this.active = 			0;
		this.activeName = 		false;
		this.visible = 			false;
		if(folderName)			this.folderName = folderName;
		else 					this.folderName = "projekte";
	
		this.itemWidth = 		960;
		this.totalWidth = 		this.items.length * this.itemWidth;
	
		this.init();
	}
	SlickBox.prototype.setFolderName = function(folderName) {
		this.folderName = folderName;
	};
	SlickBox.prototype.init = function() {
		var _ref = this;
		this.itemContainer.css('width', this.totalWidth);
	
		// CHECK/SET ADDRESS
		var curPath = $.address.pathNames();
		if(curPath.length >= 1) {
			if(curPath[0] == this.folderName) 	if(this.showByName(curPath[1]));
			else								this.setPosition(0, true);
		}
		else 									this.setPosition(0, true);

		/*this.overlay.click(function() {
			_ref.hide();
		});*/
	
		// SLICKBOX CONTROL
		this.slickBox.children('.control').children('.close').click(function(e) {
			e.preventDefault();
			_ref.hide();
		});
		this.slickBox.children('.control').children('.prev').click(function(e) {
			e.preventDefault();
			_ref.prev();
		});
		this.slickBox.children('.control').children('.next').click(function(e) {
			e.preventDefault();
			_ref.next();
		});
	
		// PROJECT LINKS
		this.items.children('.content').children('p').children('.projectLink').click(function(e) {
			e.preventDefault();
			var jumpTo = $(this).attr('className').replaceAll('projectLink' ,'').replaceAll(' ','');
			_ref.showByName(jumpTo, false);
		});
	};
	SlickBox.prototype.setPosition = function(num, dontUpdateAddress) {
		this.active = num;
		var activeName = $(this.items[num]).attr('className');
		if (activeName) {
			$.each(selectBoxClassesFilter, function(i, v){
				activeName = activeName.replaceAll(v, '');
			});
		}
		else	return;
		this.activeName = (activeName && activeName != "") ? activeName : false;
	
		if (!dontUpdateAddress && this.activeName) { 
			$.address.value(this.folderName + "/" + this.activeName);
		}
	
		if(this.active == 0 || this.slickBox.hasClass('scrollable')) 					$('#slickBox').children('.control').children('.prev').hide();
		else																			$('#slickBox').children('.control').children('.prev').show();
	
		if(this.active == this.items.length-1 || this.slickBox.hasClass('scrollable')) 	$('#slickBox').children('.control').children('.next').hide();
		else 																			$('#slickBox').children('.control').children('.next').show();
	};
	SlickBox.prototype.showItem = function(element) {
		var lookForClassName = element.attr('className');
		$.each(selectBoxClassesFilter, function(i, v) {
			lookForClassName = lookForClassName.replaceAll(v, '');
		});

		if(this.showByName(lookForClassName)) {
			return;
		}
		else {
			this.reset();
			this.show();
		}
	};
	SlickBox.prototype.show = function(){
		this.overlay.show();
		if ($.browser.msie) {
			this.slickBox.show();
			$('#right').hide();
			$('#logoBanner').children('div').hide();
			$('#label').hide();
		}
		else {
			this.slickBox.fadeIn(300);
			$('#right').hide(); //fadeOut(200);
			$('#logoBanner').children('div').hide(); //fadeOut(200);
			$('#label').hide();
		}
		this.visible = true;
	};
	SlickBox.prototype.hide = function(dontChangeAddress) {
		var _ref = this;
		this.overlay.hide();
		if ($.browser.msie) {
			this.slickBox.hide();
			this.reset();
			$('#right').show();
			$('#logoBanner').children('div').show();
			$('#label').show();
		}
		else {
			this.slickBox.fadeOut(100, function(){
				_ref.reset();
			});
			$('#right').show(); //fadeIn(100);
			$('#logoBanner').children('div').show(); //fadeIn(100);
			$('#label').show();
		}
		this.visible = false;
		this.activeName = false;
		if(!dontChangeAddress)	$.address.value(_ref.folderName);
		imageViewer.reset();
	};
	SlickBox.prototype.prev = function() {
		this.slideTo(this.active - 1);
	}
	SlickBox.prototype.next = function() {
		this.slideTo(this.active + 1);
	};
	SlickBox.prototype.slideTo = function(num, noSlide, dontUpdateAddress) {
		if(isNaN(num))	return;
	
		var curPos = parseInt(this.itemContainer.css('marginLeft'));
		var newPos = num * this.itemWidth * -1;
	
		var limitLeft = 0;
		var limitRight = (this.totalWidth) * -1;
	
		if(newPos > limitLeft) 		return;
		if(newPos <= limitRight) 	return;
			
		if (!noSlide) {
			this.itemContainer.animate({
				marginLeft: newPos
			});
		}
		else {
			this.itemContainer.css('marginLeft', newPos);
		}
		this.setPosition(num, dontUpdateAddress);
	};
	SlickBox.prototype.reset = function(num) {
		this.slideTo(0, true, true);
		this.activeName = false;
	};
	SlickBox.prototype.showByName = function(name, slide) {
		var _ref = this;
		var found = false;
		this.items.each(function(i, v) {
			if($(this).hasClass(name)) {
				if(slide)	_ref.slideTo(i);
				else 		_ref.slideTo(i, true);
			
				if(!_ref.visible) 	_ref.show();
				found = true;
				return false;
			}
		});
		return found;
	};


	// IMAGE VIEWER
	function ImageViewer() {
		this.items = $('#slickBox').children('.items').children('.item');
		var _ref = this;
		this.items.each(function() {
			var item = $(this);
			item.data('images', 		item.children('.images'));
			item.data('imageBoxes', 	item.children('ul.preview').children('li'));
			item.data('count', 			item.children('.images').children('img').length);
			item.data('active', 		0);
		
			item.data('images').children().click(function() {
				_ref.next(item, $(this));
			});
		
			item.data('imageBoxes').children('a').each(function(i, v) {
				$(this).click(function(e) {
					e.preventDefault();
					_ref.show(item, i);
				}).hover(function() {
					$(this).fadeTo(50, hoverOpacity);
				}, function() {
					if($(this).hasClass('active')) 	$(this).css('opacity', idleOpacity);
					else 							$(this).css('opacity', filteredOpacity);
				});
			});
			item.data('images').hover(function() {
				if ($.browser.msie) 	$('#slickBox .items .item .images .overlay').show();
				else 					$('#slickBox .items .item .images .overlay').fadeIn(150);
			}, function() {
				if ($.browser.msie) 	$('#slickBox .items .item .images .overlay').hide();
				else 					$('#slickBox .items .item .images .overlay').fadeOut(150);
			});
		});
		_ref.reset($(this));
	};
	ImageViewer.prototype.show = function(item, num) {
		if(num >= item.data('count'))	num = 0;
	
		if(num == item.data('active'))	return;
			
		item.data('images').children('.active').fadeOut(200, function() {
			$(this).removeClass('active');
		});
		item.data('images').children(':eq('+num+')').fadeIn(200, function() {
			$(this).addClass('active');
		});
	
		this.setActive(item, num);
		item.data('active', num);
	};
	ImageViewer.prototype.setActive = function(item, num) {
		item.data('imageBoxes').children('.active').fadeTo(50, filteredOpacity).removeClass('active');
		item.data('imageBoxes').children(':eq('+num+')').addClass('active').fadeTo(50, idleOpacity);
	};
	ImageViewer.prototype.next = function(item, image) {
		this.show(item, item.data('active') + 1);
	};
	ImageViewer.prototype.prev = function(item, image) {
		this.show(item, item.data('active') - 1);
	};
	ImageViewer.prototype.reset = function() {
		var _ref = this;
		this.items.each(function() {
			_ref.show($(this), 0);
		
			$(this).data('imageBoxes').children('a').each(function(i, v){
				if ($(this).hasClass('active')) {
					$(this).css('opacity', idleOpacity);
				}
				else {
					$(this).css('opacity', filteredOpacity);
				}
			});
		});
	};


	// FILTER
	function Filter() {
		this.items = 		$('#selectBox').children();
		this.filter = 		$('#filter').children('a');
		this.resetButton = 	$('#filter').children('.reset');
		this.active = 		false;
	
		var _ref = this;
		this.filter.each(function() {			
			var filter = $(this).attr('className').replace('active', '').replace(' ', '');
			$(this).click(function(e) {
				e.preventDefault();
				_ref.apply(filter);
			})
		});
	
		this.resetButton.click(function() {
			_ref.reset();
		});
		
	
	
		// CHECK/SET ADDRESS
		var curPath = $.address.pathNames();
		if(curPath.length >= 1) {
			if (curPath[0] == "filter" && curPath[1] != "" && curPath[1]) {
				this.apply(curPath[1]); 
			}
		}
	};
	Filter.prototype.apply = function(filter) {
		if (filter == this.active) {
			this.active = false;
			this.reset();
			return;
		}

		this.items.each(function() {
			if(!$(this).hasClass(filter)) 		$(this).addClass('filtered').fadeTo(250, filteredOpacity);
			else								$(this).removeClass('filtered').fadeTo(250, idleOpacity);
		});
		this.filter.removeClass('active');
		this.filter.each(function() {
			if($(this).hasClass(filter))	$(this).addClass('active');
		});
		$.address.value('filter/'+filter);
		Cufon.refresh('#right > .filter > a');
		this.active = filter;
		this.resetButton.fadeIn(500);
	};
	Filter.prototype.reset = function(dontChangeAddress) {
		this.items.removeClass('filtered').each(function() {
			$(this).fadeTo(50, idleOpacity);
		});
		$.address.value('filter/');
		if(!dontChangeAddress)	this.filter.removeClass('active');
		Cufon.refresh('#right > .filter > a');
		this.active = false;
		this.resetButton.hide();
	};


	String.prototype.replaceAll = function(find, replace) {
		var strText = this;
		var regex = new RegExp(find, 'g');
		strText = strText.replace(regex, replace)
		return strText;
	};
