var poolCanali =  null;
var startCounterPoolCanali =  0;
var sezioneCorrente = 'offertaCinema';
var oldVisore = "";

var videoHash = new Hash({"offertaCinema": "46508643001", 
													"offertaCalcio": "46500266001",
													"offertaSport": "46500265001", 
													"offertaSerietv": "46505126001",
													"offertaMondi": "46500268001",
													"offertaBambini": "46505127001",
													"offertaMusica": "46505128001",
													"offertaNews": false
													});

var trackingHash = new Hash({"offertaCinema": "entry-pack-cinema", 
													"offertaCalcio": "entry-pack-calcio",
													"offertaSport": "entry-pack-sport", 
													"offertaSerietv": "entry-pack-intrattenimento",
													"offertaMondi": "entry-pack-documentari",
													"offertaBambini": "entry-pack-bambini",
													"offertaMusica": "entry-pack-musica",
													"offertaNews": "entry-pack-news"
													});


window.addEvent('domready', function ()
{
	sezione = getURLParam('sezione');
	if (sezione!="")
	{
		changeSection(sezione);	
	}
	else
	{
		changeSection('offertaCinema');
	}	
});


function inizializzaPackOfferta()
{
	// LEGGE I CANALI DA VISUALIZZARE...
	
	readChannels()
	
	/*-----------------------------------------*/
	/* accende il layer dettagli*/
	/*-----------------------------------------*/
	$$('.offertaMenuItem').each(function(el){
		
			el.addEvent('click', function(listID){

				changeSection(listID);
				
			}.pass(el.id)
		);
		
	});
	
	
	setupVideo();
		
		
}

function setupVideo()
{

	
	$('offertaVisoreVideo').removeEvents();
		// AVVIA IL VIDEO....
	$('offertaVisoreVideo').addEvent('click', function(listID){

				setVisore();
				
			}
		);
}


function setVisore()
{
	videoId=videoHash.get(sezioneCorrente);
	if (videoId!=false)
	{
		$('offertaVisoreVideo').innerHTML='<object id="flashObj" width="490" height="248" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,47,0"><param name="movie" value="http://c.brightcove.com/services/viewer/federated_f9/40190652001?isVid=1&publisherID=1445083406" /><param name="bgcolor" value="#FFFFFF" /><param name="flashVars" value="playerID=40190652001&domain=embed&@videoPlayer='+videoId+'" /><param name="base" value="http://admin.brightcove.com" /><param name="seamlesstabbing" value="false" />	<param name="allowFullScreen" value="true" />	<param name="swLiveConnect" value="true" />	<param name="allowScriptAccess" value="always" />	<embed src="http://c.brightcove.com/services/viewer/federated_f9/40190652001?isVid=1&publisherID=1445083406" bgcolor="#FFFFFF" flashVars="playerID=40190652001&domain=embed&&@videoPlayer='+videoId+'" base="http://admin.brightcove.com" name="flashObj" width="490" height="248" seamlesstabbing="false" type="application/x-shockwave-flash" @videoPlayer="'+videoId+'" allowFullScreen="true" swLiveConnect="true" allowScriptAccess="always" pluginspage="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash"></embed></object>';
	}
}

// legge l'elenco dei canali della categoria corrente
function readChannels()
{
	var req = new Request({
	    method: 'get',
	    url: 'pacchettoOfferta/descChannels/'+$('offertaSezione').className+'/description.txt',
	    onComplete: function(response) 
	    {
						poolCanali = eval('(' + response + ')'); 
						
						startCounterPoolCanali = 0;
						assegnaCanali();
			}	
					    	
					    	
			//offertaCanale		    		
	    		
	}).send();				
}

// assegna i canali alle immagini placeholder
function assegnaCanali()
{
	counter = startCounterPoolCanali;
	
	$$('.offertaCanale').each(function(el){
	
	
			
			canaleCorrente = poolCanali.canali[counter];
			if (canaleCorrente)
			{
					
					el.getChildren().set('src',"pacchettoOfferta/img/loghi/" + canaleCorrente.nome + ".gif");
		
					el.removeEvents();
					el.addEvent('click', function(canaleCorrenteInt){
			
						
						var req = new Request({
						    method: 'get',
						    url: 'pacchettoOfferta/descChannels/'+$('offertaSezione').className+'/' + canaleCorrenteInt.nome + '.html',
						    onComplete: function(response) 
						    {


										
						    		htmlInterno = "<div id='visualCanale' style=\"background-image:url('pacchettoOfferta/img/structure/fondoCanale" + sezioneCorrente + ".jpg');\"><div id='visualCanaleInt'>" + response + "</div></div>"
										$('offertaVisoreVideo').set('html',htmlInterno); 
										
										
										$('offertaVisoreVideo').removeEvents();
										$('headerCanale').addEvent('click', function(canaleCorrenteInt){
												  Event.preventDefault;
												  Event.stopPropagation;
												  Event.stop;		
													$('offertaVisoreVideo').set('html',oldVisore);
													
													if ((Browser.Engine.trident4) || (Browser.Engine.trident))
													{
															setupVideo();
													}
													else
													{
							    					$('offertaVisoreVideo').addEvent('click', function(){
															setupVideo();
														});
													}

											});
								}	
						    		
						}).send();		
									
						
					}.pass(canaleCorrente));
			}
			else
			{
				el.getChildren().set('src',"pacchettoOfferta/img/structure/void.gif");
				el.removeEvents();
			}
			
			counter ++;	
			
		
	});	
	
	if (poolCanali.canali.length > 10 + startCounterPoolCanali)
	{
		$('oCFrDx').set('src','pacchettoOfferta/img/structure/dx.gif');	
		$('oCFrDx').addEvent('click', function(){
			startCounterPoolCanali = startCounterPoolCanali + 10;
			assegnaCanali();
		});
	}
	else
	{
		$('oCFrDx').set('src','pacchettoOfferta/img/structure/void.gif');	
		$('oCFrDx').removeEvents();
	}	
	
	if (startCounterPoolCanali >= 10)
	{
		$('oCFrSx').set('src','pacchettoOfferta/img/structure/sx.gif');	
		$('oCFrSx').addEvent('click', function(){
			startCounterPoolCanali = startCounterPoolCanali - 10;
			assegnaCanali();
		});
	}
	else
	{
		$('oCFrSx').set('src','pacchettoOfferta/img/structure/void.gif');	
		$('oCFrSx').removeEvents();
	}

	
}

// cambia sezione bambini, cinema, ecc..
function changeSection(listID)
{

	sezioneCorrente = listID;
									
	// CHIAMATA AJAX

		var req = new Request({
		    method: 'get',
		    url: 'pacchettoOfferta/' + listID + '.html',
		    data: {},
		    onRequest: function() {},
		    onComplete: function(response) {
						//sovrascrivo HTML
		    		$('contenuto1').set('html',response); 
		    		
		    		// riassocio gli eventi..
						inizializzaPackOfferta();																				
						$('offertaSezione').setStyle('opacity',1);
						oldVisore = $('offertaVisoreVideo').get('html'); 
						
						lpSendData('page','Section',trackingHash.get(sezioneCorrente));
						    		
		   }
		}).send();														
													
	
}


function getURLParam(strParamName){
  var strReturn = "";
  var strHref = window.location.href;
  if ( strHref.indexOf("?") > -1 ){
    var strQueryString = strHref.substr(strHref.indexOf("?"));
    var aQueryString = strQueryString.split("&");
    for ( var iParam = 0; iParam < aQueryString.length; iParam++ ){
      if (
aQueryString[iParam].indexOf(strParamName + "=") > -1 ){
        var aParam = aQueryString[iParam].split("=");
        strReturn = aParam[1];
        break;
      }
    }
  }
  return unescape(strReturn);
} 
