
function getAdServerParams() {
	carousel.clearCarousel();
	return {
		preRollURL: "http://go.pol.bbelements.com/please/showit/5211/1/1/43/?detekovano=,&typkodu=nonstandard"
		}
}      	

function startTrailer(movie,imageUrl){
	carousel.clearCarousel();
	$('#sgCarouselFlash').flash({
                        swf: 'wideo/bbelements/VideoPlayer.swf',
                        width: '500px',
                        height: '308px',        
                        flashvars: {videoUrl : movie,trailerUrl : imageUrl,config: '/wideo/bbelements/config.xml',skin: '/wideo/bbelements/skin-sg.xml',playlist: '/wideo/bbelements/playlist/'+movie+';'+imageUrl+';true',	adverts: '/wideo/bbelements/adverts.xml'}
                    });
	
}
function sgCarousel(){

    /**
    * Private variables
    */
    var self = this;
    var carouselInUl = '#caruselInUi';  
    
    var upBtn  = '.arrUp';
    var downBtn = '.arrDown';   
    var items = '.item';
  
    var inFunctionBounce = 'easeOutBack';
    var outFunctionBounce = 'easeOutBack';  
    
    var elementCount = $(items).length;
    var elementHeight = 96;
    var blocked = false;
    var moveCarousel = true;
    var refreshIntervalId;
    /**
    * Constructor
    */
    var construct = function() 
    {
	
    }
    
    this.clearActive = function(item)
    {
        elementCount = $(items).length;
		if(item)
		{
			//if we get an item - clear it
			item.children('.bar').css('display','none');
			item.children('.bar').css({'height':'0px','width':'0px'});
			item.removeClass('active');	    
		}
		else
		{
			//if we dont know which item clear - clear all
			$(items).children('.bar').css('display','none');
			$(items).children('.bar').css({'height':'0px','width':'0px'});
			$(items).removeClass('active');	
		}
    }
    
    this.setActive = function(item)
    {
        
        $(upBtn+', '+downBtn).blur();
		item.children('.bar').css('display','block');
		item.children('.bar').css('background-color',item.children('h3').children('span').css('background-color'));

		item.addClass('active');
                
		item.children('.bar').animate({'height':'90px','width':'310px'},300,'easeOutQuint',function(){
                    blocked = false
                
		var movie = $(item).attr('rel');
		if(movie && movie.indexOf('.flv') == -1)
		{
		    movie = movie + '.flv';
		}	
		var imageUrl = $(item).attr('image');
                $('.sgCarouselFlash').hide();    
                if (movie && movie.length>4){    
                    $('#sgCarouselFlash').show().html("<img src='"+imageUrl+"' onclick=''/><img src='images/play.png' style='display:block;position:absolute;margin-top:-210px;margin-left:200px;cursor:pointer;' onclick='startTrailer(\""+movie+"\",\""+imageUrl+"\")'>");
					
					$('#sCarouselFlash').flash({
                        swf: 'wideo/bbelements/VideoPlayer.swf',
                        width: '500px',
                        height: '308px',        
                        flashvars: {videoUrl : movie,trailerUrl : imageUrl,config: '/wideo/bbelements/config.xml',skin: '/wideo/bbelements/skin-sg.xml',playlist: '/wideo/bbelements/playlist/'+movie+';'+imageUrl,	adverts: '/wideo/bbelements/adverts.xml'}
                    });/*
					$('#sgCarouselFlash').flash({
                        swf: 'swf/player.swf',
                        width: '504px',
                        height: '318px',        
                        flashvars: {videoUrl : movie,trailerUrl : imageUrl}
                    });*/
                }else{
                    $('#sgCarouselImg'+$(item).attr('box')).show();                        
                }
                });
    }    
    
    this.moveUp = function()
    {
		blocked = true;
		last = $('.last');
		$('.last').css('top','-' + elementHeight + 'px');

		if($('.first'))
		{
			this.setActive($('.first'));
		}

		$('.first').removeClass('first');

		last.removeClass('last');



			$('.item').animate({'top':'+=' + elementHeight + 'px'},300,inFunctionBounce,function(){
			if($(this).css('top') == '0px')
			{
			$(this).addClass('first');
			}	    
			if($(this).css('top') == (elementHeight*(elementCount-1) + 'px'))
			{
			$(this).addClass('last');
			}

		});

    }
    
    this.moveDown = function()
    {
		blocked = true;
		last = $('.last');
		first = $('.first');
		first.css('top', elementHeight*elementCount + 'px');


		last.removeClass('last');
		first.removeClass('first');


		$('.item').each(function() {if ($(this).css('top') == (elementHeight*2 + 'px')) self.setActive($(this))} );


			$('.item').animate({'top':'-=' + elementHeight + 'px'},300,inFunctionBounce,function(){
			if($(this).css('top') == '0px')
			{
			$(this).addClass('first');
			}
			if($(this).css('top') == (elementHeight*(elementCount-1) + 'px'))
			{
			$(this).addClass('last');
			}	  

		});

    }
    
    this.up = function(event)
    {
		if(blocked == false)
		{
		//get active item
		var item = $('.active');

		//clear all items
		self.clearActive();

		//move slider up
		self.moveUp();
		
		//reset invertal
		self.resetCarousel();

		}
		if(event)
			event.preventDefault();

	
    }
    
    
    this.down = function(event)
    {
		if(blocked == false)
		{
		//get active item
		var item = $('.active');

		//clear active item
		self.clearActive();

		//move down slider
		self.moveDown();
		
		//reset invertal
		self.resetCarousel();
		
		}
		if(event)
			event.preventDefault();
	    
	//	event.preventDefault();

	
    }
    

    
    this.itemClick = function (event)
    {
	
	if($(this).attr('link'))
	{
	    if ($(this).attr('link').length>0){
		if($(this).attr('target') == '_blank')
		    window.open($(this).attr('link'), '_blank');
		else
		    location.href = $(this).attr('link');
                return true;
            }
	}
	
	if(blocked == false)
	{
	    var item = $('.active');

	    self.clearActive();

	    //if we have element to move up or down check side to move
	    if($(this).hasClass('first'))
	    {
		    self.moveUp();
	    }
	    else if(!$(this).hasClass('active'))
		    self.moveDown();


	    //$(this).children('.itemBackground').css('background',$(this).children('h3').children('span').css('background-color'));
	    //$(this).children('.itemBackground').animate({'height':'90px','width':'400px'},500,'easeOutQuint');
	}

			event.preventDefault();
	
    }
    
    this.clearCarousel = function ()
    {
        
		clearInterval(self.refreshIntervalId);
    }
    
    this.startCarousel = function ()
    {
		self.refreshIntervalId = setInterval(self.down,3000);
    }  
    this.resetCarousel = function ()
    {
		self.clearCarousel();
		self.startCarousel();
    }       
    
    this.carousel = function ()
    {
        $(upBtn).live('click',self.up);
        $(downBtn).live('click',self.down);
        $(items).live('click',self.itemClick);
	self.startCarousel();
	
        var movieUrl = $('#caruselInUi .active').attr('rel');
	
	if(movieUrl && movieUrl.indexOf('.flv') == -1)
	{
	    movieUrl = movieUrl + '.flv';
	}
        var imageUrl = $('#caruselInUi .active').attr('image');
        
        $('.sgCarouselFlash').hide();        
        if (movieUrl && movieUrl.length>4){    
            $('#sgCarouselFlash').show();
            $('#sgCarouselFlash').flash({
                swf: 'swf/player.swf',
                width: '504px',
                height: '318px',   
                allowScriptAccess: 'always',
                flashvars: {videoUrl : movieUrl,trailerUrl : imageUrl}
            });
        }else{
            $('#sgCarouselImg'+$('#caruselInUi .active').attr('box')).show();                        
        }
    }    

}


var carousel = new sgCarousel();
carousel.carousel();

//$(document).ready(function(){
//    var trCarousel = new trailerCarousel();
//    trCarousel.trailerCarousel();
//});
