﻿/** Namespace sichern */
var eve = eve || {};

jQuery(function() {

	/** Die eve.slideShow-Klasse um ein paar Plugins erweitern, bevor sie instanziert wird. */
	
	/**
	 * Diese Methode belegt die Media-Buttons (Music, Video, Images) mit den richtigen Links,
	 * blendet notfalls Buttons aus oder ein. Die Informationen dafuer sind im aktuellen SlideItem
	 * hinterlegt.
	 *
	 * @member	eve.Slideshow
	 * @param		{object}		e		Event-Objekt.
	 */
	eve.Slideshow.prototype.mediaButtons = function(e) {
	
		/** Wenn noch nicht verfuegbar, dann die jeweiligen Links ermitteln */
		if ((this.mediaBtns || null) == null) {
			
			this.mediaBtns = {
			
				music : jQuery('#' + this.containerId + ' .iconMusic').get(0) || false,
				video : jQuery('#' + this.containerId + ' .iconVideo').get(0) || false,
				image : jQuery('#' + this.containerId + ' .iconImages').get(0) || false
			};
			
			for (var indize in this.mediaBtns) {
			
				this.mediaBtns[indize].isVisible = jQuery(this.mediaBtns[indize]).hasClass('visible');
				this.mediaBtns[indize].size = { height : jQuery(this.mediaBtns[indize]).height(), width : jQuery(this.mediaBtns[indize]).width() };
			}
			
			this.mediaBtns.music.linkTemplate = this.getRawLink(this.mediaBtns.music.href, 'artistid', '%26', '%3D');
			if((this.mediaBtns.music.linkTemplate || false)) if ((this.mediaBtns.music.linkTemplate.prefix + this.mediaBtns.music.linkTemplate.postfix).split('artistid').length > 2) this.mediaBtns.music.linkTemplate = this.getRawLink(this.mediaBtns.music.href, 'artistid');
			
			this.mediaBtns.video.linkTemplate = this.getRawLink(this.mediaBtns.video.href, 'bclid');
			this.mediaBtns.image.linkTemplate = this.getRawLink(this.mediaBtns.image.href, 'kuid');
		}
		
		/** Aus dem aktuellem Item die Link-Informationen zusammenstellen... */
		if (this.mediaBtns.music) {
			var artistid = this.actualItem.artistid || false;
			this.buttonFadeInOrOut(this.mediaBtns.music, artistid, this.mediaBtns.music.size.width, this.mediaBtns.music.size.height);
			if (artistid) this.mediaBtns.music.href = this.mediaBtns.music.linkTemplate.prefix + artistid + this.mediaBtns.music.linkTemplate.postfix;
		}
		
		if (this.mediaBtns.video) {
			var bclid = this.actualItem.bclid || false;
			this.buttonFadeInOrOut(this.mediaBtns.video, bclid, this.mediaBtns.video.size.width, this.mediaBtns.video.size.height);
			if (bclid) this.mediaBtns.video.href = this.mediaBtns.video.linkTemplate.prefix + bclid + this.mediaBtns.video.linkTemplate.postfix;
		}
		
		if (this.mediaBtns.image) {
			var kuid = this.actualItem.kuid || false;
			this.buttonFadeInOrOut(this.mediaBtns.image, kuid, this.mediaBtns.image.size.width, this.mediaBtns.image.size.height);
			if (kuid) this.mediaBtns.image.href = this.mediaBtns.image.linkTemplate.prefix + kuid + this.mediaBtns.image.linkTemplate.postfix;
		}
	};
	
	eve.Slideshow.prototype.buttonFadeInOrOut = function(el, fadeIn, width, height) {
	
		var isVisible = el.isVisible || false;
		var isFadeIn = fadeIn || false;
		
		if (!isFadeIn && isVisible) {
		
			if (this.isOldIE) {
			
				jQuery(el).hide();
				
			} else {
				
				jQuery(el).stop();
				jQuery(el).animate({ width:"0px", height:"0px" });
			}
			
			el.isVisible = false;
			
		} else if (isFadeIn && !isVisible) {
				
			if (this.isOldIE) {
			
				jQuery(el).show();
				
			} else {
				
				jQuery(el).stop();
				jQuery(el).animate({ width:width+"px", height:height+"px" });
			}
			
			el.isVisible = true;
		}
	};
	
	eve.Slideshow.prototype.getRawLink = function(string, param, and, equal) {
		
		var and = and || '&';
		var equal = equal || '=';
		var string = string || '';
		
		if (string == '') return { prefix : '', postfix : '' };
		
		var tmpArray = string.split(and + param + equal);
		var preLink = tmpArray[0] + and + param + equal;
		
		var backLink = (tmpArray[1] || '').split(and);
		backLink.shift();
		backLink = backLink.join(and);
		
		return { prefix : preLink, postfix : backLink };
	};
	
	/**
	 * Diese Methode belegt den Ticket-Button, sowie das aktuelle SlideItem aus der
	 * Slideshow mit einem Link zur Detail-Seite ...
	 *
	 * @member	eve.Slideshow
	 * @param		{object}		e		Event-Objekt.
	 */
	eve.Slideshow.prototype.ticketButton = function(e) {
	
		/** Wenn noch nicht verfuegbar, dann den TicketLink ermitteln ... */
		if ((this.ticketBtn || null) == null) {
		
			this.ticketBtn = jQuery('#' + this.containerId + ' .iconMore').get(0) || false;
			this.ticketBtn.size = { height : jQuery(this.ticketBtn).height(), width : jQuery(this.ticketBtn).width() };
			this.ticketBtn.isVisible = jQuery(this.ticketBtn).hasClass('visible');
		}
		
		if ((this.ticketBtnLastClass || null) != null) {
		
			if (jQuery(this.ticketBtn).hasClass(this.ticketBtnLastClass)) jQuery(this.ticketBtn).removeClass(this.ticketBtnLastClass)
			this.ticketBtnLastClass = null;
		}
		
		if ((this.actualItem.ticketBtnCls || null) != null) {
			
			if (!jQuery(this.ticketBtn).hasClass(this.actualItem.ticketBtnCls)) jQuery(this.ticketBtn).addClass(this.actualItem.ticketBtnCls)
			this.ticketBtnLastClass = this.actualItem.ticketBtnCls;	
		}
		
		/** Aus dem aktuellem Item die Link-Informationen zusammenstellen... */
		var tmpLink = this.actualItem.origLinkEl.getAttribute('href') || false;
		var tmpName = this.actualItem.origLinkEl.getAttribute('name') || false;
		var tmpClass = this.actualItem.origLinkEl.className || false;
		var tmpHTML = jQuery(this.actualItem.origLinkEl).find('span').get(0).innerHTML || "";
		
		var oldClass = this.ticketBtn.className || false;
		var oldWidth = ((typeof this.ticketBtn.oldWidth != "undefined") ? this.ticketBtn.oldWidth : jQuery(this.ticketBtn).width() + 3 );
		
		if(tmpHTML != "") jQuery(this.ticketBtn).find('span').get(0).innerHTML = tmpHTML || "";
		
		if(tmpClass) {
			
			this.ticketBtn.className = tmpClass + ((this.ticketBtn.isVisible) ? " visible" : "" );
			
			if (!this.isOldIE) {
				
				this.ticketBtn.parentNode.style.width = "";
				
				var newWidth = jQuery(this.ticketBtn).width() + 3;
				
				this.ticketBtn.parentNode.style.width = oldWidth + 'px';
				this.ticketBtn.parentNode.style.overflow = 'hidden';
				
				jQuery(this.ticketBtn.parentNode).stop();
				
				var newWidth = ((tmpHTML == "") ? 0 : newWidth );
				
				jQuery(this.ticketBtn.parentNode).animate({ width:newWidth +'px' });
				
				this.ticketBtn.oldWidth = newWidth;
			}
		}
		
		this.buttonFadeInOrOut(this.ticketBtn, tmpLink, this.ticketBtn.size.width, this.ticketBtn.size.height);
		
		if (tmpLink) {
			
			this.ticketBtn.href = tmpLink;
			this.ticketBtn.setAttribute('title', this.actualItem.origLinkEl.getAttribute('title') || '');
		}
		
		// Wenn name gesetzt ist, dann kann es sein, dass HBX-Code darauf liegt
		if (tmpName) {
			
			this.ticketBtn.setAttribute('name', tmpName);			
		}
		

		/** Den Link auch auf das SlideItem setzen */
		// IE - Firefox Methode (normal auf das Bild) / Opera (im Opera gehts nur auf dem Container)
		var tmpPlakat = (!jQuery.browser.opera) ?  this.actualItem.el : this.container;
		var tmpRealBtn = this.ticketBtn;
		
		tmpPlakat.isTicketBtnLink = tmpLink;
		tmpPlakat.style.cursor = 'pointer';
		
		// Funktion vorbereiten und Scope auf das Bild setzen...
		var tmpClickFunction = function() {

			trackAndOpenLink({ el : tmpRealBtn });

			return false;
		};

		tmpPlakat.onclick = tmpClickFunction;
		this.ticketBtn.onclick = tmpClickFunction;
	};
	
	var tmp = new eve.Slideshow({ container : 'bigTeaser' });
});
