var banners = new Array();
function initBannerRotator(c_id, w, h, res, str_images, str_urls, str_titles)
{
  var nelementi = str_images.split('|').length;
  if(nelementi > 1) {
    banners[c_id] = {
      container_id: c_id,
      images: str_images.split('|'),
      urls: str_urls.split('|'),
      titles: str_titles.split('|'),
      width: w,
      heigth: h,
      resize: res,
      next: 0,
      nelements: nelementi - 1
    };
         
    setTimeout('nextBanner(\'' + c_id + '\');', 13000);
  }
}

function nextBanner(c_id)
{
  var banner = banners[c_id];
  var content = '';
  
  content += '<img alt="' + banner.titles[banner.next] + '" src="';
  if(banner.resize)
    content += '/phpthumb/phpThumb.php?src=' + banner.images[banner.next] + ((banner.heigth> 0) ? '&amp;h= "' + banner.heigth : '') + ((banner.width> 0) ? '&amp;w=' + banner.width : '');
  else
    content += banner.images[banner.next];
  content += '"' + ((banner.heigth> 0) ? ' height= "' + banner.heigth + '"' : '') + ((banner.width> 0) ? ' width= "' + banner.width + '"' : '');
  content += ' />';

  if(banner.urls[banner.next] != '') {
    content = '<a href="' + banner.urls[banner.next] + '" title="' + banner.titles[banner.next] +'">' + content + '</a>';
  }
    
  $('#' + c_id + ' div').hide();
  $('#' + c_id).append('<div>' + content + '</div>');
  
  banner.next++;
  if(banner.next == banner.nelements) {
    $('#' + c_id).cycle({
      fx: 'none', // choose your transition type, ex: fade, scrollUp, shuffle, etc...
      //speed: 2000,
      timeout: 13000
      //delay: -16000
    });
  } else {
    setTimeout('nextBanner(\'' + c_id + '\');', 13000);
  }
}

/* Modal FROMS Player */
function showModalBox(src, w, h)
{
  $.modal('<iframe src="' + src + '" height="' + h + '" width="' + w + '" style="border:0">', {
  	closeHTML:"",
  	containerCss:{
  		height:h,
  		padding:0,
  		width:w
  	},
  	overlayClose:true
  });
}

function sendemail(){
    var emailRegEx = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
    
    str =$.trim($('#userfromemail').val());
    if(!str.match(emailRegEx)){
        alert('Il vostro indirizzo email non è stato inserito correttamente.');
        return;
    }
    
    str =$.trim($('#emailto').val());
    if(str.match(emailRegEx)){
        //$('#send_by_email').modal.close();
        //$.modal.close();
        //$('#message').val('http://www.stopandgo.tv/invia_email_video?' + $('#shareform').serialize());
        $.getJSON('http://www.stopandgo.tv/invia_email_video?' + $('#shareform').serialize(),
            function(data){
              if(data && data.stato == 'ok'){
                //$.modal('<div align="center"><h2>Email inviata con successo</h2></div>');
                alert('Email inviata con successo');
              }
              else{
                //$.modal('<div align="center"><h2>Si &egrave; verificato un errore</h2></div>');
                alert('Si è verificato un errore');
              }
            }
        );
    }
    else
    {
        alert('L\'indirizzo email del destinatario non è stato inserito correttamente.');
    }
}


function showSendMail(){
  $('#videourl').val(playerobj.getClip().url);
  $('#send_by_email').modal({containerCss:{height: 230}});
}

function showEmbed(){
  $.modal('<div><div style="text-align: center"><span style="color: #FFF; font-weight: bold;">Copia il codice riportato qui sotto e inseriscilo nel tuo sito:</span></div><div align="center"><textarea readonly="readonly" id="embedtxt" style="width: 580px; height: 300px;">' + playerobj.embed().getEmbedCode(true) + '</textarea></div></div>');
}

/* player video */
var current_pl = 0;
  var resizeTimer = null;
  var playerobj = null;
  var language = 'it';
  
	function initPage()
	{
		playerobj = flowplayer(
		  'div_player',
		  //{src: 'http://www.stopandgo.tv/swf/flowplayer.commercial-3.0.4.swf', width: 520, height: 299, wmode: 'transparent'},
		  {src: 'http://www.stopandgo.tv/swf/flowplayer.commercial-3.0.4.swf', width: 540, height: 296, wmode: 'transparent'},
		  {
			key: '$6c675f11163fb112b6d',
			canvas: { 
			  backgroundColor: '#000000', 
			  backgroundGradient: 'none', 
			  borderRadius: 0 
			},
			clip: { baseURL: 'http://www.stopandgo.tv' },
			plugins: { 
			  controls: { 
				  url: 'http://www.stopandgo.tv/swf/flowplayer.controls-3.0.3.swf',
				  bufferGradient: 'none',
				  sliderGradient: 'none',
				  durationColor: '#ffffff',
				  timeColor: '#01DAFF',
				  borderRadius: '0',
				  backgroundColor: '#000000',
				  bufferColor: '#6c9cbc',
				  progressColor: '#015B7A',
				  progressGradient: 'medium',
				  backgroundGradient: [0.6,0.3,0,0,0],
				  buttonOverColor: '#728B94',
				  sliderColor: '#000000',
				  buttonColor: '#5F747C',
				  opacity:1.0,
				  //width: '478px'
				  width: '498px'
			  } 
			}
		  }
		);
		
		/*getPLFor(
			'highlights',
			function() {
				eval($('.dock:eq(0) a:eq(0)').attr('href').replace('javascript:',''));
			}
		);*/

		//nextBanner();
		//setInterval("nextBanner();", 50000);
	}

	/*function nextBanner(){
		$('#banner_verticale').load(
			'http://www.stopandgo.tv/global/displayBanner?banner_dir=banner/verticali_player/&refresh=' + (new Date()).getMilliseconds(),
			function() {
				$('#banner_verticale a').attr('target', '_black');
			}
		);
	} */
	
	function playVideo(video_src){
	  playerobj.stop();
	  playerobj.setClip({   
        autoPlay: false,
        autoBuffering: true,
        scaling: 'scale',
		    baseURL: 'http://www.stopandgo.tv',
        url: video_src
      });
      playerobj.startBuffering();
	}
	
	function getPLFor(categoria, callback, page){
	  $.get(
  		'http://www.stopandgo.tv/play_list/' + language + '/' + categoria + '/' + page, 
  		//{data: (new Date()).getTime()},
  		function(strcontent) {
  		  $('#content_pl').html(strcontent);
  		  initPL(0);
  		  //$('#npagina').html('1');
  		  $('#npagina').html($('#pl_current_page').html());
  		  //$('#npagine').html($('.dock').length);
  		  $('#npagine').html($('#pl_tot_page').html());
  		  if(callback) callback();
  		}
	  );
	}
	
	/*function resetDock(){
	  $(document).mousemove(null);
	}*/
	  
	function initPL(id_pl){
	  /*$(window).unbind('resize');
	  $(document).unbind();
  
	  $(document).bind('mouseover', function() {    
		if (resizeTimer) clearTimeout(resizeTimer);    
		//resizeTimer = setTimeout(resetDock, 100);
	  });
	  
	  $(document).bind('mouseout', function() {
		if (resizeTimer) clearTimeout(resizeTimer);    
		resizeTimer = setTimeout(resetDock, 100);    
	  });*/
	  
	  var element = $('.dock:eq('+ id_pl + ')');
	  element.Fisheye(
		{
		  maxWidth: 82,
		  items: 'a',
		  itemsText: 'span',
		  container: '.dock-container',
		  itemWidth: 76,
		  proximity: 40,
		  alignment : 'left',
		  valign: 'bottom',
		  halign : 'center'
		}
	  );
	  var images_object = $('.dock:eq('+ id_pl + ') img');
	  /*images_object.hide();
	  element.show('fast',
		function() {
		  imageFadeIn(id_pl, images_object.length - 1, 0);
		}
	  );*/
	  
	  //element.show();
	  images_object.each(function(index) {
	    var element = $(this);
      element.load(function(){
        element.fadeIn(200);
      });
    });	  
	}

	/*function imageFadeIn(id_pl, nimages, current_image){
	 var element = $('.dock:eq('+ id_pl + ') img:eq(' + current_image + ')');
		element.fadeIn(
			200,
			function() {
				if(nimages > current_image)
					imageFadeIn(id_pl, nimages, current_image + 1);
			}
		);
	}*/	

	function prevPL(categoria){
	  /*if(current_pl > 0) {
		$('.dock:eq(' + current_pl + ')').hide();
		current_pl -= 1;
		initPL(current_pl);
		$('#npagina').html(current_pl + 1);
	  }*/
	  
	  var current_pl_page = parseInt($('#npagina').html());
	  if(current_pl_page > 1)
	     getPLFor(categoria, null, current_pl_page - 1);
	     
	  //function() {eval($('.dock:eq(0) a:eq(0)').attr('href').replace('javascript:',''));}
	}
	
	function nextPL(categoria){
	  /*if(current_pl < $('.dock').length - 1) {
		$('.dock:eq(' + current_pl + ')').hide();
		current_pl += 1;
		initPL(current_pl);
		$('#npagina').html(current_pl + 1);
	  }*/
	  
	  var current_pl_page = parseInt($('#npagina').html());
	  var tot_pl_page = parseInt($('#npagine').html());
	  if(current_pl_page < tot_pl_page)
	     getPLFor(categoria, null, current_pl_page + 1);
	}
	
	/*function changePL(categoria,nome_categoria){
	  var link_element = $('#link_cat_' + categoria);
	  if((link_element.attr('class') + '').indexOf('current') != -1)
		return;
	  getPLFor(nome_categoria);
	  current_pl = 0;
	  $('#nav a').removeClass('current');
	  link_element.addClass('current');
	}
	
	function changeLanguage(strlang){
	  language = strlang;
	  getPLFor('highlights');
	  current_pl = 0;
	  $('#nav a').removeClass('current');
	  $('#link_cat_1').addClass('current');
	}*/

var start_facebook = false;  
	
$(document).ready( function(){
  if($('#div_player').length > 0)
    initPage();
	  
	$().piroBox({
			my_speed: 600, //animation speed
			bg_alpha: 0.5, //background opacity
			radius: 4, //caption rounded corner
			scrollImage : false, // true == image follows the page, false == image remains in the same open position
			pirobox_next : 'piro_next', // Nav buttons -> piro_next == inside piroBox , piro_next_out == outside piroBox
			pirobox_prev : 'piro_prev',// Nav buttons -> piro_prev == inside piroBox , piro_prev_out == outside piroBox
			close_all : '.piro_close'// add class .piro_overlay(with comma)if you want overlay click close piroBox
			//slideShow : 'slideshow', // just delete slideshow between '' if you don't want it.
			//slideSpeed : 4 //slideshow duration in seconds(3 to 6 Recommended)
	});

  /*$('.slideshow').cycle({
		fx: 'none', // choose your transition type, ex: fade, scrollUp, shuffle, etc...
		speed: 2000,
		timeout: 13000
    //delay: -16000
	});*/
	$('.scroll_down').cycle({ 
    fx:    'scrollDown',
    timeout: 9000
    //delay: -16000
  });
  
  if(start_facebook){
    FB.init("8fe5e0ac77d325a3881bab56f17a6b8f","/xd_receiver.htm");
  }
});