
/*########################################################*/
/*-------------- MultiMessageBillboard --------------------*/
/*
 * function for handling of multimessagebillboards on homepage
 */
(function ($) {
    $.fn.MultiMessageBillboard = function() {

		var highestPosition = 0;
		var currentPosition = 0;
		var billboardWidth = 990;
		var animationDuration = 350;
		var billboardRotationInterval = 8; //in seconds
		var rotateInterval;

		var setup = function() {
			highestPosition = $('.multi-message-billboard .billboard').size();

			if (highestPosition > 1) {
				$('.multi-message-billboard .billboard').each(function(index){
					var t = '<a href="#">X</a>';
					$('.multi-message-billboard-controls').append(t);
				});
				
				$('.multi-message-billboard-controls a').eq(currentPosition).addClass('on');
				
				$('.multi-message-billboard .show-billboard-video').click(onOpenVideoClick);
				$('.billboard .ui-btn-close').live('click', onCloseVideoClick);
				
				setMouseActions();
				
				$('.multi-message-billboard-controls a').click(function(){
					scrollBy(this);
					return false;
				});
				
				$('.multi-message-billboard-left a').click(function(){
					scrollBy(-1);
					return false;
				});
				
				$('.multi-message-billboard-right a').click(function(){
					scrollBy(1);
					return false;
				});
				
				//set position of billboard based 
				$('.billboards').css({
					'left': billboardWidth * -currentPosition
				});
				
				//rotateBillboards();
			} else {
				$('.multi-message-billboard-controls').hide();
			}
        };
		
		var onOpenVideoClick = function() {
			clearInterval(rotateInterval);
			clearMouseActions();
			fadeOutNav();
			$('.multi-message-billboard-controls').hide();
			$this = $(this);
			$teaser = $this.parents('.billboard-video-teaser').hide();
			$videoWrapper = $teaser.siblings('.billboard-video').show();
			$teaser.siblings('.ui-btn-close').show();
			
			//mediaelement was initialized with jquery 1.43
			$videoWrapper_jQuery143 = jQuery143(this).parents('.billboard-video-teaser').siblings('.billboard-video');
			$videoPlayer_jQuery143 = $videoWrapper_jQuery143.find('video').data('mediaElementPlayer');
			$videoPlayer_jQuery143.setControlsSize();
			$videoWrapper_jQuery143.find('div.mejs-play').click();
			
			return false;
        }
		
		var onCloseVideoClick = function() {
			//mediaelement was initialized with jquery 1.43
			$videoPlayer_jQuery143 = jQuery143(this).parents('.billboard').find('video');
			var mediaElementPlayer = $videoPlayer_jQuery143.data('mediaElementPlayer');
			mediaElementPlayer.pause();
					
			setMouseActions();
			fadeInNav();
			$('.multi-message-billboard-controls').show();
			$this = $(this);
			$videoElem = $this.parents('.billboard').find('.billboard-video').hide();
			$videoElem.siblings('.billboard-video-teaser').show();
			$this.hide();
			
			return false;
		}
		
		var clearMouseActions = function(){
			$('.multi-message-billboard').unbind('mouseenter');
			$('.multi-message-billboard').unbind('mouseleave');
		}
		
		var setMouseActions = function() {
			$('.multi-message-billboard').mouseenter(function(){
				clearInterval(rotateInterval);
				fadeInNav();
			});
			$('.multi-message-billboard').mouseleave(function(){
				rotateBillboards();
				fadeOutNav();
			});
		}

		var fadeInNav = function(){
			$('.multi-message-billboard-left, .multi-message-billboard-right').stop(true, true).each(function(){
				$this = $(this);
				if ($.browser.msie == true) {
					$this.show();
				}
				else {
					$this.fadeIn();
				}
			});
		}

		var fadeOutNav = function(){
			$('.multi-message-billboard-left, .multi-message-billboard-right').stop(true, true).each(function(){
				$this = $(this);
				if ($.browser.msie == true) {
					$this.hide();
				}
				else {
					$this.fadeOut();
				}
			});
		}

		var rotateBillboards = function() {
            rotateInterval = setInterval(function() {
                scrollBy(1);
            }, billboardRotationInterval * 1000);
        };

        var scrollBy = function(elem) {
			$('.multi-message-billboard-controls a').eq(currentPosition).removeClass('on');
			if($.isNumeric(elem)) {
				currentPosition += elem;
				currentPosition = ((currentPosition > highestPosition - 1) ? 0 : ((currentPosition < 0) ? highestPosition - 1 : currentPosition))
			} else {
				currentPosition = $('.multi-message-billboard-controls a').index(elem);
			}
			$('.multi-message-billboard-controls a').eq(currentPosition).addClass('on');
            $('.billboards').stop(true, true).animate({'left':billboardWidth * -currentPosition},{duration:animationDuration});
		}
		
		setup();

    }
})(jQuery);

$(document).ready(function () {
	$.fn.MultiMessageBillboard();
});

jQuery143(document).ready(function () {
	jQuery143('.billboard-video video').mediaelementplayer({
		//force flash player is all browsers
		//mode: 'shim',
		// shows debug errors on screen
		enablePluginDebug: false,
		plugins: ['flash'],
		// path to Flash plugin
		pluginPath: 'flash/',
		// name of flash player file
		flashName: 'flashmediaelement.swf'
	});
});
