/**
 * Return an Object cursor{x, y} with the current cursor-position 
 * for all current browsers
 * 
 * @e event
 * @return cursor-object 
 */


Prototype.Browser = {
	IE:     !!(window.attachEvent && !window.opera),
	Opera:  !!window.opera,
	WebKit: document.childNodes && !document.all && !navigator.taintEnabled,
	Gecko:  (document.getBoxObjectFor != null),
	MobileSafari: !!navigator.userAgent.match(/Apple.*Mobile.*Safari/)
};

Object.extend(Prototype.Browser, {
     WebKit419: Prototype.Browser.WebKit && (Prototype.BrowserFeatures.XPath),
     WebKit420: Prototype.Browser.WebKit && (!Prototype.BrowserFeatures.XPath),
     IE6:      Prototype.Browser.IE && (typeof window.XMLHttpRequest == "undefined"),
     IE7:      Prototype.Browser.IE && (typeof window.XMLHttpRequest == "object")
});


var ImageSlide = Class.create({
	
	//currently active item, count from 0
	items: [],
	currentItem: 0,
	navigation: [],
	pe: false,
	inProgress: false,
	
	
	/**
	 * Initialize class
	 *
	 * @container: html-parent-container, usually with class fce-imageslide
	 */
	initialize: function(container) {
		this.container  = container;
		this.height = 0;
		this.items = this.container.select('ul.images>li');
		this.itemCount = this.items.size();
		
		this.items.each(function (item, index) {
			imgTag = item.select('img').first();
			//preload all images
			preloadImage = new Image();
			preloadImage.src = imgTag.readAttribute('src');
			
			//search for image with greatest height
			if (Prototype.Browser.IE) {
				regExp = new RegExp(/\<img.*height[^\d]+(\d+)/i);
				match = regExp.exec(item.innerHTML);
				tempHeight = match[1];
			} else {
				tempHeight = imgTag.readAttribute('height');
			}
			this.height = (tempHeight > this.height) ? tempHeight : this.height;
			
			//display first image
			if (index == this.currentItem) {
				item.show();
			}
		}.bind(this));
		//FG, this is a hotfix, please change 
		if (this.container.hasClassName('imageslider')) {
			this.container.setStyle({height: this.height + 'px'});
		}

		if (this.itemCount > 1) {
			this.navigation = this.container.select('ul.navigation>li');
			this.navigation.each(function (item, index) {
				item.observe('click', function(event) {
					elementClicked = event.element().tagName.toLowerCase() == 'li' ? event.element() : event.element().up('li');
					this.currentItem = this.navigation.indexOf(elementClicked);
					this.pe.stop();
					this.showItem(this.currentItem);
				}.bindAsEventListener(this));
			}.bind(this));
			
			//startPeriodicalExecuter
			this.pe = new PeriodicalExecuter(function(pe) {
				this.nextItem();
			}.bind(this), 5);
		}
	},
	
	nextItem: function () {
		this.items[this.currentItem].fade();
		this.currentItem = (this.currentItem + 1) % this.itemCount;
		this.items[this.currentItem].appear();
		
		if (this.navigation.size() > 0) {
			this.navigation.invoke('removeClassName', 'act');
			this.navigation[this.currentItem].addClassName('act');
		}
	},
	
	stopRotation: function () {
		this.pe.stop();
	},
	
	destroy: function () {
		this.navigation.each( function (item) {
			item.stopObserving('click');
		});
		if(this.pe){
			this.pe.stop();
		}
	},
	
	showItem: function (itemPos) {
		inProgress = false; 
		this.items.each(function (item) {
			if (item.inProgress) {
				inProgress = true;
			}
		});
		
		if (! inProgress) {
			this.items.each(function (item, index) {
				item.inProgress = true;
				if (index == itemPos) {
					item.appear({
						afterFinish: function() {
							item.inProgress = false;
						}.bind(item)
					});
				} else {
					item.fade({
						afterFinish: function() {
							item.inProgress = false;
						}.bind(item)
					});
				}
			}.bind(this));
			
			if (this.navigation.size() > 0) {
				this.navigation.invoke('removeClassName', 'act');
				this.navigation[itemPos].addClassName('act');
			}
		}
	}
});

var MediaPlayerNavReverse = Class.create({
	
	//currently active item, count from 0
	items: [],
	currentItem: 0,
	navigation: [],
	pe: false,
	inProgress: false,
	autoplay: false,
	
	/**
	 * Initialize class
	 *
	 * @container: html-parent-container
	 */
	initialize: function(container) {
		this.container  = container;
		this.items = this.container.select('ul.jwplayer-media-nav>li');
		this.navigationList = this.container.down('ul.jwplayer-media-nav');
		this.itemCount = this.items.size();

		if (this.itemCount > 1) {
			
			this.navigation = this.container.select('ul.jwplayer-media-nav>li');
			tempUlContainer = new Element('ul', {'class': 'jwplayer-media-nav'});
			this.navigation.each(function (item) {
				tempUlContainer.insert({top: item});
			}.bind(this));
			this.navigationList.replace(tempUlContainer);
			
		}
	}
});

var MediaPlayerSelect = Class.create({
	
	//currently active item, count from 0
	items: [],
	currentItem: 0,
	navigation: [],
	pe: false,
	inProgress: false,
	autoplay: false,
	
	/**
	 * Initialize class
	 *
	 * @container: html-parent-container
	 */
	initialize: function(container) {
		this.container  = container;
		this.height = 0;
		this.items = this.container.select('ul.jwplayer-media-items>li');
		this.navigationList = this.container.down('ul.jwplayer-media-nav');
		this.itemCount = this.items.size();
		this.nlhight = this.itemCount*160;

		if (this.itemCount > 1) {
			this.navigationList.setStyle({
				'height': this.navigationList.getHeight()+'px'
			});
			
			this.navigation = this.container.select('ul.jwplayer-media-nav>li');
			
			this.navigation.each(function (item, index) {
				item.observe('click', function(event) {
					elementClicked = event.element().tagName.toLowerCase() == 'li' ? event.element() : event.element().up('li');
					this.currentItem = this.navigation.indexOf(elementClicked);
					
					this.showMediaItem(this.currentItem);
				}.bindAsEventListener(this));
			}.bind(this));
			
		}
	},
	
	showMediaItem: function (itemPos) {
		inProgress = false; 
		mediaItem = itemPos;
		
		this.items.each(function (item) {
			if (item.inProgress) {
				inProgress = true;
			}
		});
		
		if (! inProgress) {
			this.items.each(function (item, index) {
				item.inProgress = true;
				if (index == mediaItem) {
					item.appear({
						afterFinish: function() {
							item.inProgress = false;
						}.bind(item)
					});
				} else {
					item.fade({
						afterFinish: function() {
							item.inProgress = false;
						}.bind(item)
					});
				}
			}.bind(this));
			
			if (this.navigation.size() > 0) {
				this.navigation.invoke('removeClassName', 'act');
				this.navigation[itemPos].addClassName('act');
			}
		}
	}
});


var MediaNavigationScroller = Class.create({

	height: '230',
	container: null,
	index: null,
	viewport: null,
	content: null,
	arrowup: null,
	arrowdown: null,
	interval: 0.01,
	step: 5,
	range: null,
	pe: null,
	top: null,

	initialize: function(container, index) {
		this.container = container;
		this.viewport = this.container.down('.jwplayer-navigation-nav');
		this.content = this.viewport.down('ul.jwplayer-media-nav');
		this.arrowleft = this.container.down('.jwplayer-navigation-nav-arrowbar .jwplayer-navigation-nav-arrowleft');
		this.arrowleftimg = this.arrowleft.down('a img');
		this.arrowright = this.container.down('.jwplayer-navigation-nav-arrowbar .jwplayer-navigation-nav-arrowright');
		this.arrowrightimg = this.arrowright.down('a img');
		
		//check if scrollbuttons are needed.
		if (this.content.getHeight() > this.height) {
			
			this.content.setStyle({
				'position': 'relative'
			});
			
			this.arrowleftimg.show();
			this.arrowrightimg.show();
			
			this.range = this.content.getHeight()-this.height;
			this.left = 0;
			
			this.arrowleft.observe('mousedown', this.scrollLeftInit.bindAsEventListener(this));
			this.arrowleft.observe('mouseup', this.scrollStop.bindAsEventListener(this));
			this.arrowright.observe('mousedown', this.scrollRightInit.bindAsEventListener(this));
			this.arrowright.observe('mouseup', this.scrollStop.bindAsEventListener(this));
		}
	},
	
	scrollLeftInit: function(event) {
		this.arrowleft.addClassName('over');
		this.pe = new PeriodicalExecuter(this.scrollLeft.bind(this), this.interval);
	},
	
	scrollRightInit: function(event) {
		this.arrowright.addClassName('over');
		this.pe = new PeriodicalExecuter(this.scrollRight.bind(this), this.interval);
	},
	
	scrollStop: function(event) {
		this.arrowleft.removeClassName('over');
		this.arrowright.removeClassName('over');
		this.pe.stop();
	},

	scrollRight: function() {
		if(this.left < (this.range * -1)) {
			this.scrollStop();
			return;
		}
		
		this.left-=this.step;
		this.content.setStyle({
			'top': this.left+'px'
		});
	},
	
	scrollLeft: function() {
		if(this.left >= 0) {
			this.scrollStop();
			return;
		}
		
		this.left+=this.step;
		this.content.setStyle({
			'top': this.left+'px'
		});
	}
});


Event.observe(document, "dom:loaded", function (event) {
		//imageslider
	$$('.imageslider').each(function (item) {
		running = new ImageSlide(item);
	});
		//jwPlayer Navigation
	$$('.jwplayer .jwplayer-navigation-nav').each(function (item) {
		running = new MediaPlayerNavReverse(item);
	});
	$$('.jwplayer').each(function (item) {
		running = new MediaPlayerSelect(item);
	});
	$$('.jwplayer .jwplayer-navigation').each(function (item) {
		running = new MediaNavigationScroller(item);
	});
	
});


Event.observe(window, 'unload', function (event) {
});



