function slide(dir){
	if(dir == 'next'){
		if(this.current < this.slides){
			//slides
			var moveBy = -Math.min(5, this.slides - this.current - 5);
			new Effect.Move(this, {
				x: moveBy * 110,
				y: 0,
				mode: 'relative',
				afterFinish: function(){
					this.current -= moveBy;
					enableLinks(this)
				}.bind(this)
			});
		}
	} else{
		if(this.current > 0){
			//slides
			var moveBy = Math.min(5, this.current);
			new Effect.Move(this, {
				x: moveBy * 110,
				y: 0,
				mode: 'relative',
				afterFinish: function(){
					this.current -= moveBy;
					enableLinks(this)
				}.bind(this)
			});
		}
	}
	return false;
}
function enableLinks(slideObj){
	if(slideObj.current == slideObj.slides - 5){
		slideObj.next.onclick = function(){return false;};
		slideObj.next.addClassName('disabled');
	} else{
		slideObj.next.onclick = slide.bind(slideObj, 'next');
		slideObj.next.removeClassName('disabled');
	}
	
	if(slideObj.current == 0){
		slideObj.prev.onclick = function(){return false;};
		slideObj.prev.addClassName('disabled');
	} else{
		slideObj.prev.onclick = slide.bind(slideObj);
		slideObj.prev.removeClassName('disabled');
	}
}

function rating(ratingBox, imgPath, videoUid){
	this.ratingBox		= ratingBox;
	this.rating			= ratingBox.down('img').src.split('rating_')[1].split('.png')[0];
	this.imgPath		= imgPath;
	this.videoUid		= ratingBox.down('img').id.split('-no')[1];
	this.ratingImages	= new Array();
	
	this.setOrigRating	= function(e){
		this.ratingImages.each(function(el, index){
			el.src	= (index <= this.rating) ? this.imgPath+"ro.png" : this.imgPath+"no.png";
		}, this);
	}

	this.setRating		= function(e,i){
		this.ratingImages.each(function(el, index){
			el.src	= (index <= i) ? this.imgPath+"ro.png" : this.imgPath+"no.png";
		}, this);
	}
	
	this.rateNow		= function(e,i){
		new Ajax.Request(
			'index.php?type=802&tx_twmeinlpercussion_videolist[videoUid]='+this.videoUid+'&tx_twmeinlpercussion_videolist[action]=rating&tx_twmeinlpercussion_videolist[controller]=Video&tx_twmeinlpercussion_videolist[rating]='+i+'&no_cache=1',
			{
				method: 'get',
				onSuccess: function(transport,json){
					var response = transport.responseText.evalJSON(true);
					var newRating = new Element('img', {src: videoRating.imgPath+response.item['object'].ratingsum+".png"});
					videoRating.ratingBox.update('Thanks for rating! ');
					videoRating.ratingBox.appendChild(newRating);
				},
				onFailure: function(){
					alert('Error while rating. Sorry! Your vote wasn\'t counted!');
				}
			}
		);
	}
	
	ratingBox.down('img').remove();
	for(var i = 1; i < 6; i++){
		var ratingImg	= new Element('img', {src: ((i <= this.rating) ? this.imgPath+"ro.png" : this.imgPath+"no.png")});
		ratingImg.addClassName('act');
		
		ratingBox.appendChild(ratingImg);
		
		ratingImg.onmouseover	= this.setRating.bindAsEventListener(this,i - 1);
		ratingImg.onmouseout	= this.setOrigRating.bindAsEventListener(this);
		ratingImg.onclick			= this.rateNow.bindAsEventListener(this, i);
		
		this.ratingImages.push(ratingImg);
	}
}

function bigSlide(){
	this.slides			= new Array();
	this.container		= new Element('div', {'class': 'slideshow-big'});
	this.slideContainer	= new Element('div', {'class': 'slider-big'});
	this.slider			= new Element('div', {'class': 'inner-big'});
	this.index			= 0;
	this.next			= new Element('a', {'href': '#'}).update('next');
	this.prev			= new Element('a', {'href': '#'}).update('prev');
	this.next.addClassName('next-big');
	this.prev.addClassName('prev-big');
	this.prev.addClassName('disabled-big');
	this.maxHeight		= 0;
	
	this.checkLinks		= function(){
		this.next.removeClassName('disabled-big');
		this.prev.removeClassName('disabled-big');
		
		this.next.onclick = this.slide.bindAsEventListener(this,-1);
		this.prev.onclick = this.slide.bindAsEventListener(this,1);
		
		if(this.index == this.slides.length - 1){
			this.next.addClassName('disabled-big');
			this.next.onclick = function(){return false;};
		} else if(this.index == 0){
			this.prev.addClassName('disabled-big');
			this.prev.onclick = function(){return false;};
		}
	}
	
	this.slide			= function(e, dir){
		var moveBy = 960 * dir;
		if(dir == 1){
			--this.index;
		} else{
			++this.index;
		}
		this.slider.setStyle({zIndex: 8001});
		new Effect.Move(this.slider, {
			x: moveBy,
			y: 0,
			mode: 'relative',
			afterFinish: function(){
				this.checkLinks();
				this.slider.setStyle({zIndex: 7999});
			}.bind(this)
		});
		return false;
	}
	
	if($$('.mp-stage').length > 1){
		$$('.mp-stage').each(function(single, index){
			this.maxHeight = Math.max(this.maxHeight, single.getHeight());
			if(index == 0){
				single.insert({before: this.container});
				this.container.appendChild(this.slideContainer);
				this.slideContainer.appendChild(this.slider);
			}
			single.addClassName('sliding');
			this.slider.appendChild(single);
			this.slides.push(single);
		}, this);
		
		if(this.slides.length > 1){
			this.slider.setStyle({width: (this.slides.length * 960)+'px', height: this.maxHeight+'px'});
			this.container.setStyle({height: this.maxHeight+'px'});
			this.slideContainer.setStyle({height: this.maxHeight+'px'});
			this.slides.each(function(el){
				var margin = parseInt(this.maxHeight) - parseInt(el.getHeight());
				el.setStyle({marginTop: margin+'px'});
			}, this);
			this.container.insert({before: this.prev});
			this.container.insert({before: this.next});
			this.next.onclick = this.slide.bindAsEventListener(this,-1);
			this.prev.onclick = this.slide.bindAsEventListener(this,1);
			
			this.next.setStyle({marginTop: (this.maxHeight - 350 + 120)+'px'});
			this.prev.setStyle({marginTop: (this.maxHeight - 350 + 120)+'px'});
			
			this.checkLinks();
		}
	}
}

function initFavs(){
	var favContainer = $('favourites-container');
	if(favContainer.hasClassName('keepopen')){
		favContainer.removeClassName('keepopen');
		new Effect.Highlight('favourites-count', {
			startcolor: '#1b8be2',
			endcolor: '#E2001A',
			afterFinish: function(){
				new Effect.Highlight('favourites-count', {
					startcolor: '#E2001A',
					endcolor: '#1b8be2',
					afterFinish: function(){
						new Effect.Highlight('favourites-count', {
							startcolor: '#1b8be2',
							endcolor: '#E2001A'
						});
					}
				});
			}
		});
	}
	favContainer.setStyle({display: 'none'});
	
	
	$('favourites').down('h3').onclick = function(){
		if($('browse-index') && $('browse-index').getStyle('display') != 'none'){
			new Effect.toggle('browse-index', 'blind', {
				duration: 0.5,
				afterFinish: function(){
					new Effect.toggle('favourites-container', 'blind', {duration: 0.5});
				}
			});
		} else{
			new Effect.toggle('favourites-container', 'blind', {duration: 0.5});
		}
	}
}

var videoShortList	= new Array();
var videoRating		= null;

document.observe('dom:loaded', function(){
	Cufon.replace(
		$('head').down('ul').select('a'),
			{
				fontFamily:	'Caecilia LT Bold',
				hover:		true
			}
		)(
			'h1',
			{
				fontFamily:	'Caecilia LT Bold',
				hover:		true
			}
		)(
			'h2',
			{
				fontFamily: 'j.d.'
			}
		)(
			$$('.day'),
			{
				fontFamily: 'Caecilia LT Bold'
			}
		)(
			$$('.caeciliastd'),
			{
				fontFamily: 'Caecilia LT Std'
			}
		)(
				$$('.cbold'),
				{
					fontFamily: 'Caecilia LT Bold'
				}
			);
	if(Prototype.Browser.IE){
		Cufon.now();
	}
	
	
	/*shop specific*/
	var shopNav			= $('main-shop-nav');
	if(shopNav){
		Cufon.replace(
			shopNav.select('a'),
			{
				fontFamily:	'Caecilia LT Bold',
				hover:		true
			}
		)
	}
	
	var productIndex	= $('product-index');
	if(productIndex){
		if($('browse-index')){
			$('browse-index').setStyle({display: 'none'});
			productIndex.onclick = function(){
				if($('favourites-container').getStyle('display') != 'none'){
					new Effect.toggle('favourites-container', 'blind', {
						duration: 0.5,
						afterFinish: function(){
							new Effect.toggle('browse-index', 'blind', {duration: 0.5});
						}
					});
				} else{
					new Effect.toggle('browse-index', 'blind', {duration: 0.5});
				}
			}
		}
	}
	if($('favourites')){
		initFavs();
	}
	
	var productBrowser		= $$('.product-browser');
	if(productBrowser.length){
		var browser			= new Element('div', {'class': 'grid-3 omega', 'id': 'product-browser-container'});
		$('content').appendChild(browser);
		productBrowser.each(function(el){
			browser.appendChild(el);
		});
		
		browser.select('a.anchor').each(function(a){
			a.onclick = function(){
				$('product-browser-container').select('a.anchor').each(function(aTemp){aTemp.removeClassName('act')});
				var element = this.href.split('#').pop();
				Effect.ScrollTo(element, {duration: 0.8});
				this.addClassName('act');
				return false;
			}
		});
	
		var maxHeight		= $('content').getHeight();
		var browserHeight	= browser.getHeight();
		var leftOffset		= browser.cumulativeOffset()[0] - 10;
		window.onscroll		= function(){
			var offset		= browser.cumulativeScrollOffset();
			if(offset[1] > 170){
				// FIX
				if((offset[1] + 100 + browserHeight) < maxHeight){
					if(Prototype.Browser.IE){
						browser.setStyle({position: 'fixed', top: (10)+'px', marginLeft: '765px', width: '200px'});
					} else{
						browser.setStyle({position: 'fixed', top: (10)+'px', left: leftOffset+'px'});
					}
				} else{
					browser.setStyle({position: 'absolute', top: (maxHeight - browserHeight - 100)+'px'});
				}
			} else{
				browser.setStyle({position: 'static'});
			}
		}
	}
	
	/*init slider*/
	$$('div.csc-textpic-imagewrap').each(function(el){
		
		if(el.select('img').length > 1){
			var imageCount 		= el.select('img').length;
			
			var container		= new Element('div');
			var slideContainer	= new Element('div');
			var slider			= new Element('div');
			
			slideContainer.appendChild(slider);
			container.appendChild(slideContainer);
			
			el.insert({before: container});
			
			el.select('img').each(function(img){
				var fullImg 	= (img.ancestors()[0].href) ? img.ancestors()[0] : img;
				var li			= document.createElement('li');
				li.appendChild(fullImg);
				slider.appendChild(li);
			});
			slider.imageCount	= imageCount; 
			var sliderWidth		= imageCount * 110;
			
			slider.setStyle({width: sliderWidth+'px'});
			
			if(imageCount < 6){
				//kein scrollen - deswegen zentrieren
				slider.setStyle({marginLeft: ((550 - sliderWidth) / 2)+'px'});
			} else{
				// scrollen
				slider.current	= 0;
				slider.slides	= imageCount;
				slider.next		= new Element('a');
				slider.prev		= new Element('a');
				
				slideContainer.insert({before: slider.prev});
				slideContainer.insert({after: slider.next});
				
				slider.setStyle({width: (sliderWidth + 50)+'px'});
				
				slider.next.update('next');
				slider.prev.update('prev');
				
				slider.next.onclick	= slide.bind(slider, 'next');
				
				slider.next.addClassName('next');
				slider.prev.addClassName('prev');
				slider.prev.addClassName('disabled');
			}
			el.remove();
			container.addClassName('slideshow');
			slideContainer.addClassName('slider');
			slider.addClassName('inner');
		}
	});
	
	// blog comment
	var formField = $('blog-form-data');
	if(formField){
		formField.value = 'Cajon';
		formField.up('div').setStyle({display: 'none'});
		$('blog-new-comment').onsubmit = function(){
			var error = false;
			if(!$('blog-form-comment').value.length || !$('blog-form-name').value.length){
				error = true;
			}
			if(error === true){
				if(!$('blog-comment-error')){
					$('blog-new-comment-section').insert({after:'<p class="error" id="blog-comment-error">Please fill out all required fields!</p>'});
				}
				return false;
			} else{
				return true;
			}
		};
	}
	
	// video comment
	var formField = $('video-form-data');
	if(formField){
		formField.value = 'Bongo';
		formField.up('div').setStyle({display: 'none'});
		$('video-new-comment').onsubmit = function(){
			var error = false;
			if(!$('video-form-comment').value.length || !$('video-form-name').value.length){
				error = true;
			}
			if(error === true){
				if(!$('video-comment-error')){
					$('video-new-comment-section').insert({after:'<p class="error" id="video-comment-error">Please fill out all required fields!</p>'});
				}
				return false;
			} else{
				return true;
			}
		};
	}
	
	// video shortlist
	$$('ul.video-shortlist').each(function(el, index){
		el.uid			= el.id.split('-no')[1];
		el.parent		= $('video-nav-no'+el.uid); 
		videoShortList.push(el);
		
		$('video-nav-no'+el.uid).onclick = function(){
			var newSec = this;
			videoShortList.each(function(section){
				if(section.parent.hasClassName('act')){
					section.parent.removeClassName('act');
					newSec.parent.addClassName('act');
					new Effect.Opacity(section, { from: 1.0, to: 0, duration: 0.3, afterFinish:function(){
						section.setStyle({display: 'none'});
						
						newSec.setOpacity(0);
						newSec.setStyle({display: 'block'});
						new Effect.Opacity(newSec, { from: 0, to: 1.0, duration: 0.3 });
					} });
				}
			});

		}.bind(el);
		
		if(!el.parent.hasClassName('act')){
			el.setStyle({display: 'none'});
		}
	});
	
	var ratingBox = $('rating');
	if(ratingBox){
		videoRating = new rating(ratingBox, "fileadmin/templates/css/img/video/rating_");
	}
	
	// video embed
	if($('embed-video')){
		$('embed-video').onclick = function(){
			this.focus();
			this.select();
		}
	}
	
	var slideshow = new bigSlide();
	
	//twitter
	if($('twitter_div')){
		$$('head')[0].appendChild(new Element('script', {src: "http://twitter.com/javascripts/blogger.js", type: "text/javascript"}));
		$$('head')[0].appendChild(new Element('script', {src: "http://twitter.com/statuses/user_timeline/Meinlpercussion.json?callback=twitterCallback2&count=3", type: "text/javascript"}));
	}
});