$(document).ready(function() 
{

    var teaserContentText = $("#teaserContentText");
	var teaserPlayer = $("#teaserPlayer");
	var teaserContent = $("#teaserContent");
	var teaserButtonWrapper = $("#teaserButtonWrapper");
	var imgTeaser = $("#imgTeaser");
	var imgTeaserBack = $("#imgTeaserBack");
	var ivTeaserIntervalStart = new Object();	
	var teaserstarted = true;
    
    // current visible image: front or back
    var frontimg = 1;
	
	// current teaser number
	var current = 0;
	
	var durationinc = 0;	
	
	var getTeaserOrderSearch = function(search)
	{
		var order = new Array();
		
		order = getTeaserOrder();
		
		for (var i = 0; i < order.length; i++) 
		{
			if(order[i] == search.toLowerCase())
			{
				return i;
			}
		}
		
		return -1;
	};
    
    // Animate Teaser Buttons & Set click option
    var teaserButtons = function()
	{
        var teaserimages = teaserContent.children("img");
		
		var teaserButtons = teaserButtonWrapper.children(".teaserButton");
		
		var tbwWidth = '204px';
		var tbwWidthold = teaserButtonWrapper.css('width');
		
		var tbTop = '-16px';
		var tbTopold = '0px';
		
		var tbbWidth = '112px';
		var tbbWidthold = '80px';
		
		var tbbHeight = '54px';		
		var tbbHeightold = '38px';
	
		var tbbtHeight = '12px';		
		var tbbtHeightold = '0px';
		
		teaserButtons.hover
		(
			function () 
			{
				teaserButtonWrapper.css(
				{
					'width' : tbwWidth				
				});
				
				$(this).css(
				{
					'top' : tbTop				
				});

				$(this).children(".teaserButtonImg").css(
				{
					'width' : tbbWidth,
					'height' : tbbHeight				
				});
				
				
				$(this).children(".teaserButtonSel").css(
				{
					'width' : tbbWidth				
				});
				
				
				$(this).children(".teaserButtonText").css(
				{
					'visibility' : 'visible',
					'width' : tbbWidth,
					'height' : tbbtHeight				
				});
			}, 
			function () 
			{
				teaserButtonWrapper.css(
				{
					'width' : tbwWidthold				
				});
				
				$(this).css(
				{
					'top' : tbTopold				
				});
				
				$(this).children(".teaserButtonImg").css(
				{
					'width' : tbbWidthold,
					'height' : tbbHeightold				
				});				
				
				$(this).children(".teaserButtonSel").css(
				{
					'width' : tbbWidthold		
				});
				
				$(this).children(".teaserButtonText").css(
				{
					'visibility' : 'hidden',
					'width' : tbbWidthold,
					'height' : tbbtHeightold				
				});
			}
		);
		
		teaserButtons.click(function()
		{
			
			// get the title of the clicked button
			var title = $(this).attr('data-tb');
						
			// get the cfg for the clicked button
			var cfg = getTeaserCFG(title);		

			var currTeaserNr = getTeaserOrderSearch(title);
			
			if(currTeaserNr >= 0)
			{
				current = currTeaserNr;
			}
			
			teaserMovie(title,cfg[1],cfg[2],cfg[3],cfg[4]);
		});
		

	};
	
	var setTeaserButtonSel = function(title)
	{
		var teaserButtons = teaserButtonWrapper.children(".teaserButton");
		
		// if visible set for one teaserButtonSel set visibility to hidden
		$(".teaserButtonSel").css(
		{
			'visibility' : 'hidden'			
		});
		
		teaserButtons.each(function(index) 
		{
			var eltitle = $(this).attr('data-tb');
			if(title.toLowerCase() == eltitle.toLowerCase())
			{
				// set visibility to visible for the clicked button
				$(this).children(".teaserButtonSel").css(
				{
					'visibility' : 'visible'			
				});
			}
		});
	}
	
	
	
	var teaserMovie = function(title,movie,image,text,link)
	{
		// stop the teaser interval
		clearInterval(ivTeaserIntervalStart);
		
		// set the movie attribute
		if($("#popupPlayer").length < 1)
        {
            setTeaserButtonSel(title);
            
            // remove ContentText elements if present
            teaserContentText.fadeOut("fast");
                    
            teaserPlayer.fadeIn("fast");
            
            // set the movie attribute
            teaserPlayer.attr
            ({
              href: movie
            });
		
            // start the player
            flowplayer
            (	"teaserPlayer",{src: "./movies/flowplayer.commercial-3.2.7.swf", wmode: 'transparent'},			 
                {
                    key: '#$f016f8d8e636cbf1381',
                    showErrors: false,
                    plugins: 
                    {
                       // disable autoloading of the controlbar plugin
                       controls: null
                    },
                    // disable the "overlay play button"
                    play:  
                    {
                        url: ''
                    },
                    // prevent pausing
                    onBeforePause: function() 
                    {
                        return false;
                    },
                    clip:  
                    {
                        autoPlay: true,
                        image:true,
                        autoBuffering: true,
                        onStart: function () 
                        {
                            // reset opacity when clip is played again
                            this.getPlugin("play").css({opacity: 1});
                        },
                        onLastSecond: function()
                        {
                            
                            imgTeaserBack.attr
                            ({
                                src: image
                            });
                            
                            imgTeaser.attr
                            ({
                                src: image
                            });
                            // do not show "Play again"
                            this.getPlugin("play").css({opacity: 0});
                        },
                        onFinish: function () 
                        {
                            // show teaser text elements
                            showTeaserText(text,link);
                            
                            // proceed the interval
                            startTeaserInterval();
                            imgTeaser.fadeIn();  
                            teaserPlayer.fadeOut();	
                        }
                    }
                }
            );
        }
        else
        {
            startTeaserInterval();
        }
	};
	
	// Teaser Image Function
	var teaserImage = function(title,image)
	{
		// stop the teaser interval
		clearInterval(ivTeaserIntervalStart);
		
		setTeaserButtonSel(title);
		
		// set the image attribute
		
        if(frontimg == 1)
        {
            imgTeaserBack.attr
            ({
                src: image
            });
            
            imgTeaser.fadeOut();
            frontimg = 0;
        }
        else
        {
            imgTeaser.attr
            ({
                src: image
            });
            
            imgTeaser.fadeIn();        
            frontimg = 1;
        }
        
		//
		//imgTeaser.fadeIn("fast");
		
		// remove ContentText and teaserPlayer elements if present
        teaserContentText.fadeOut("fast");
		teaserPlayer.fadeOut();	
		
		// proceed the interval
		startTeaserInterval();
	};
	
	var showTeaserText = function(text,link)
	{	
        teaserContentText.children().remove();
		teaserContentText.css('left',"852px");
		teaserContentText.css('top',"200px");
		
		teaserContentText.append("<div id='tctText'>"+ text +"</div>");
	
		teaserContentText.append("<div id='tctInfoBtn'><a class=\"btn\" href=\""+link+"\" title=\""+text+"\" >Find out more</a></div>");
		
		teaserContentText.fadeIn(100, function () 
		{
            $("#tctText").fadeIn("slow");
            $("#tctInfoBtn").fadeIn("slow");
        });

		teaserTextHidden = false;
	};
	
	var stopTeaserInterval = function()
	{
		// stop the teaser interval
		clearInterval(ivTeaserIntervalStart);
	}
	
	var startTeaserInterval = function()
	{
	
		teaserstarted = true;
		var currName='';
		var nextName='';
		
		var currTeaser=new Array();
		var nextTeaser=new Array();
		
		// default duration
		var dur = 20000;
		
		// get the teaser configuration array
		//var cfg = getTeaserCFG("0");
		var order = getTeaserOrder();
		
		// count the configuration elements
		var countTeaser = order.length;
		// next teaser element number
		var next = current+1;
		
		// if there's no next element begin with element 0
		if(next >= countTeaser)
		{
			next = 0;
		}
		
		// do we have more than one teaser elements?
		if(countTeaser > 1)
		{
			// get the current and next teaser names
			currName = order[current];
			nextName = order[next];
			
			currTeaser = getTeaserCFG(currName);
			nextTeaser = getTeaserCFG(nextName);
			
			// set the duration of the current element
			if(currTeaser[5] > 0)
			{
				dur = currTeaser[5]+durationinc;
			}
			
			// is the next teaser element a movie?
			if(nextTeaser[1].length > 0)
			{
				ivTeaserIntervalStart = setTimeout(function ()
										{
											teaserMovie(nextName,nextTeaser[1],nextTeaser[2],nextTeaser[3],nextTeaser[4])
										},dur);
			}
			else // show Image
			{
				ivTeaserIntervalStart = setTimeout(function ()
										{
											teaserImage(nextName,nextTeaser[2])
										},dur);
			}
			
			// start from the beginning if current element is last element
			if(next == 0)
			{
				current=0;
				durationinc = durationinc + 10000;
			}
			else
			{
				current++;
			}
		}		
	};
    
    teaserButtons();
	startTeaserInterval();
	
	$(window).blur(function()
	{
		if(teaserstarted)
		{
			stopTeaserInterval();
			teaserstarted = false;
		}
	});
	$(window).focus(function()
	{
		if(!teaserstarted)
		{
			startTeaserInterval();
			teaserstarted = true;
		}
	});
    
});
