var JQnewsObj;

NewsPack = function() 
{
	var xmlAjax;
	var idCategory;
	var idSubCategory;
	var numberItem;
	var pagination;
	var objectForHtml;	
	var htmlType;	//variabile che indica l'html da applicare agli <li>
	var domResult;
	var numberItemNews;
	var numberItemVideo;
	var counterVideo;
	var counterNews;	
	var uniqueName;
	var typeForFilter = "";
	
	this.xmlAjax = "";
	this.idSubCategory = "";
	this.numItemForPage = 12;//Serve soprattutto per la paginazione 
	
	this.numberItemNews = 0;//
	this.counterNews = 0;
	
	this.numberItemVideo = 0;//
	this.counterVideo = 0;
	
	this.numberItem = 0;
	this.pagination = false;
	this.objectForHtml = new ItemXML();
	this.htmlType = 1;
	this.uniqueName = "";
	
	this.DOM_RESULT_GENERIC = "#Searchresult";
	this.DOM_RESULT_VIDEO = "#SearchresultVideo";
	this.CLASS_OTHER_NEWS = ".tit_AltreNotizie";
	this.CLASS_OTHER_VIDEO = ".tit_GuardaAnche";
	this.PATH_IMG_TAPPO = "/dl/RaiSport/images/mask_nofoto.gif";
	this.PAGINAZIONE_ID ="#Paginazione2";
	this.PAGINAZIONE_ANCHOR ="#AltreNotizie";
	
	this.typeForFilter="";
	
	this.domResult = this.DOM_RESULT_GENERIC;
	
}

NewsPack.prototype =
{
	readXmlContent : function(page_id) {
			
		//startLoading(JQnewsObj.DOM_RESULT_GENERIC,'#SearchresultLoading');
		//$(JQnewsObj.PAGINAZIONE_ID).hide();
		
		var countItem = 1;
		JQnewsObj.counterVideo = 1;
		JQnewsObj.counterNews = 1;
		$(JQnewsObj.DOM_RESULT_GENERIC).html("");
		$(JQnewsObj.DOM_RESULT_VIDEO).html("");
		var categoriaVisualizzata = "";
		
		var filterItem = "elemento";
		if(JQnewsObj.idSubCategory!="")
			filterItem += "[sottocategoria*='|"+JQnewsObj.idSubCategory+"|']";
		if(JQnewsObj.typeForFilter!="")
			filterItem += "[type*='|"+JQnewsObj.typeForFilter+"']";
			//filterItem += "[type='"+JQnewsObj.typeForFilter+"']";
		
		$(JQnewsObj.xmlAjax).find(filterItem).filter(function (indexSecond) {
			indexSecond++;
			if(JQnewsObj.pagination) { //Qui se c'è la paginazione
				return  indexSecond >= ((page_id*JQnewsObj.numItemForPage)+1) && indexSecond <= ((page_id*JQnewsObj.numItemForPage)+JQnewsObj.numItemForPage);
			}
			else if(JQnewsObj.numItemForPage == 0) {//Qui se vogliamo visualizzarli tutti senza paginazione		
				if(JQnewsObj.uniqueName != "") {
					return $(this).attr('id') != JQnewsObj.uniqueName;
				}
				else
					return indexSecond;
			}
			else //qui se vogliamo visualizzarne un certo numero
				return indexSecond <= JQnewsObj.numItemForPage;
		}).each(function(){
				var categoriaVisualizzata = '';
				JQnewsObj.objectForHtml.setId($(this).attr('id'));
				JQnewsObj.objectForHtml.setIdSubCategory($(this).attr('sottocategoria'));
				JQnewsObj.objectForHtml.setType($(this).attr('type'));
				JQnewsObj.objectForHtml.setTitolo($(this).find('titolo').text());
				JQnewsObj.objectForHtml.setTestoLancio($(this).find('testolancio').text());
				JQnewsObj.objectForHtml.setLink($(this).find('link').text());
				JQnewsObj.objectForHtml.setImg($(this).find('img').text());
				//alert($(this).find('datapubblicazione').text());
				var data = $(this).find('datapubblicazione').text();

				if(data.indexOf("-") > -1)
					data = data.replace(/-/g, "/");

				if(JQnewsObj.objectForHtml.type.search(/articolo/) > 0)					
					JQnewsObj.objectForHtml.setDate(data,"dd/MM/yyyy HH:mm","dd/MM HH:mm");
				else 
					JQnewsObj.objectForHtml.setDate(data,"dd/MM/yyyy","dd/MM/yyyy");

				if (JQnewsObj.idSubCategory!= "" )
					categoriaVisualizzata = JQnewsObj.idSubCategory;
				else {
					var catArray=$(this).attr('sottocategoria').split("|");
					categoriaVisualizzata = catArray[2];
				}
				JQnewsObj.objectForHtml.setCategoriaView(categoriaVisualizzata);
				
				JQnewsObj.drawHTML(JQnewsObj.htmlType,countItem,page_id);

				countItem++;
		}); //close each(
		JQnewsObj.applyPngFix();
		//finishLoading('#SearchresultLoading',1500,JQnewsObj.DOM_RESULT_GENERIC,0);
		//setTimeout("JQnewsObj.__showIdAfterTimeout('"+JQnewsObj.PAGINAZIONE_ID+"','0')", 2000);
	}		
	,
	drawHTML: function(type,countItem,page_id) 
	{
		var classe = '';
		var imgHtml = ''

		switch(parseInt(type)) {
			case 1 : //HomePage per Sport
				var linkVideo = '';
				var linkVideoSimple = '';
				$(JQnewsObj.CLASS_OTHER_NEWS).show();
				
				var data = '<span style="font-weight:normal;">'+JQnewsObj.objectForHtml.datapubblicazione+'</span>';
				//alert(JQnewsObj.objectForHtml.type);
				if(JQnewsObj.objectForHtml.type.search(/video/) > 0) {
					linkVideo = '<a href="'+JQnewsObj.objectForHtml.link+'" title="Vai..."><img class="btnVai" src="/dl/RaiSport/images/btn_vai.png" height="20" width="21"/></a>';
					//data = '';
					linkVideoSimple = '<span><img class="btnVai" src="/dl/RaiSport/images/btn_vai_small.png" height="14" width="15" style="margin-bottom:-2px;margin-left:0px;"/></span>&nbsp;&nbsp;';
				}
						
				if(countItem <= 4 && page_id == 0) {	
					classe = (countItem % 2) ? '': 'last';
					if(JQnewsObj.objectForHtml.img == "")
							JQnewsObj.objectForHtml.img = JQnewsObj.PATH_IMG_TAPPO;
							
					html = '<div class="ImgLancio" style="background: url('+JQnewsObj.objectForHtml.img+') center top no-repeat;"><a href="'+JQnewsObj.objectForHtml.link+'"><img src="/dl/RaiSport/images/mask_lancetti.png" width="89" height="73" /></a></div><h3>'+JQnewsObj.objectForHtml.categoriaView+'<br />'+data+'</h3>'+linkVideo+'<div class="TestoLancetto"><a href="'+JQnewsObj.objectForHtml.link+'"><h2>'+JQnewsObj.objectForHtml.titolo+'</h2></a>'+JQnewsObj.objectForHtml.testolancio+'</div>';
				} else {
					classe='Semplice';
					html = '<a href="'+JQnewsObj.objectForHtml.link+'"><h2>'+linkVideoSimple+JQnewsObj.objectForHtml.titolo+'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'+data+'</h2>'+JQnewsObj.objectForHtml.testolancio+"</a>";			
				}
				$('<li></li>').addClass(classe).html(html +'\n').appendTo(JQnewsObj.domResult);
				return;
			case 2 : // Pagine Articoli Small e Video
				JQnewsObj.numberItemNews ="4"; //Per ora li cablo qui...
				JQnewsObj.numberItemVideo = "2";//Per ora li cablo qui...
				var data = '<span style="font-weight:normal;">'+JQnewsObj.objectForHtml.datapubblicazione+'</span>';
				
				if(JQnewsObj.objectForHtml.type.search(/video/) > 0 && JQnewsObj.counterVideo <=  JQnewsObj.numberItemVideo){
					JQnewsObj.domResult = JQnewsObj.DOM_RESULT_VIDEO;
					if(JQnewsObj.counterVideo <= 2) {	
						$(JQnewsObj.CLASS_OTHER_VIDEO).show();
						classe = (JQnewsObj.counterVideo % 2) ? '': 'last';
						if(JQnewsObj.objectForHtml.img == "")
								JQnewsObj.objectForHtml.img = JQnewsObj.PATH_IMG_TAPPO;	
								
						html = '<div class="ImgLancio" style="background: url('+JQnewsObj.objectForHtml.img+') center top no-repeat;"><a href="'+JQnewsObj.objectForHtml.link+'"><img src="/dl/RaiSport/images/mask_lancetti.png" width="89" height="73" /></a></div><h3>'+JQnewsObj.objectForHtml.categoriaView+'<br /><span>'+JQnewsObj.objectForHtml.datapubblicazione+'</span></h3><a href="'+JQnewsObj.objectForHtml.link+'" title="Vai..."><img class="btnVai" src="/dl/RaiSport/images/btn_vai.png" height="20" width="21" /></a><div class="TestoLancetto"><a href="'+JQnewsObj.objectForHtml.link+'"><h2>'+JQnewsObj.objectForHtml.titolo+'</h2></a>'+JQnewsObj.objectForHtml.testolancio+'</div>';
						
						$('<li></li>').addClass(classe).html(html +'\n').appendTo(JQnewsObj.domResult);
					} 
					JQnewsObj.counterVideo++;
				} else if(JQnewsObj.objectForHtml.type == "|articolo" && JQnewsObj.counterNews <=  JQnewsObj.numberItemNews){
					JQnewsObj.domResult = JQnewsObj.DOM_RESULT_GENERIC;
					if(JQnewsObj.counterNews <= 2) {	
						$(JQnewsObj.CLASS_OTHER_NEWS).show();
						classe = (JQnewsObj.counterNews % 2) ? '': 'last';
						
						if(JQnewsObj.objectForHtml.img == "")
							JQnewsObj.objectForHtml.img = JQnewsObj.PATH_IMG_TAPPO;
						
						html = '<div class="ImgLancio" style="background: url('+JQnewsObj.objectForHtml.img+') center top no-repeat;"><a href="'+JQnewsObj.objectForHtml.link+'"><img src="/dl/RaiSport/images/mask_lancetti.png" width="89" height="73" /></a></div><h3>'+JQnewsObj.objectForHtml.categoriaView+'<br /><span>'+JQnewsObj.objectForHtml.datapubblicazione+'</span></h3><div class="TestoLancetto"><a href="'+JQnewsObj.objectForHtml.link+'"><h2>'+JQnewsObj.objectForHtml.titolo+'</h2></a>'+JQnewsObj.objectForHtml.testolancio+'</div>';
					} else {
						classe='Semplice';
						html = '<a href="'+JQnewsObj.objectForHtml.link+'"><h2>'+JQnewsObj.objectForHtml.titolo+'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'+data+'</h2>'+JQnewsObj.objectForHtml.testolancio+"</a>";			
					}	
					JQnewsObj.counterNews++;
					
					$('<li></li>').addClass(classe).html(html +'\n').appendTo(JQnewsObj.domResult);
				}				
				
				return;	
			case 3 : // Pagine Articoli Large
				JQnewsObj.numberItemNews ="3"; //Per ora li cablo qui...
				JQnewsObj.numberItemVideo = "2";//Per ora li cablo qui...	
				var data = '<span style="font-weight:normal;">'+JQnewsObj.objectForHtml.datapubblicazione+'</span>';				
				
				if(JQnewsObj.objectForHtml.type.search(/video/) > 0 && JQnewsObj.counterVideo <=  JQnewsObj.numberItemVideo){
						$(JQnewsObj.CLASS_OTHER_VIDEO).show();
						JQnewsObj.domResult = JQnewsObj.DOM_RESULT_VIDEO;
						classe = (JQnewsObj.counterVideo % 2) ? '': 'last';
						if(JQnewsObj.objectForHtml.img == "")
							JQnewsObj.objectForHtml.img = JQnewsObj.PATH_IMG_TAPPO;
							
						html = '<div class="ImgLancio" style="background: url('+JQnewsObj.objectForHtml.img+') center top no-repeat;"><a href="'+JQnewsObj.objectForHtml.link+'"><img src="/dl/RaiSport/images/mask_lancetti.png" width="89" height="73" /></a></div><h3>'+JQnewsObj.objectForHtml.categoriaView+'<br />'+data+'</h3><a href="'+JQnewsObj.objectForHtml.link+'" title="Vai..."><img class="btnVai" src="/dl/RaiSport/images/btn_vai.png" height="20" width="21" /></a><div class="TestoLancetto"><a href="'+JQnewsObj.objectForHtml.link+'"><h2>'+JQnewsObj.objectForHtml.titolo+'</h2></a>'+JQnewsObj.objectForHtml.testolancio+'</div>';
						
						$('<li></li>').addClass(classe).html(html +'\n').appendTo(JQnewsObj.domResult);

						JQnewsObj.counterVideo++;
				} else if(JQnewsObj.objectForHtml.type == "|articolo" && JQnewsObj.counterNews <=  JQnewsObj.numberItemNews){
					$(JQnewsObj.CLASS_OTHER_NEWS).show();
					JQnewsObj.domResult = JQnewsObj.DOM_RESULT_GENERIC;
					classe='Semplice';
					html = '<a href="'+JQnewsObj.objectForHtml.link+'"><h2>'+JQnewsObj.objectForHtml.titolo+'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'+data+'</h2>'+JQnewsObj.objectForHtml.testolancio+"</a>";			

					JQnewsObj.counterNews++;
					
					$('<li></li>').addClass(classe).html(html +'\n').appendTo(JQnewsObj.domResult);
				}				
				
				return;		
		}
	}
	,	
	paginationJQuery: function() {
		// Create pagination element
		$(JQnewsObj.PAGINAZIONE_ID).pagination(this.numberItem, {
			num_edge_entries: 5,
			num_display_entries: this.numItemForPage,
			items_per_page : this.numItemForPage,
			prev_text:" ",
			next_text:" ",
			link_to: JQnewsObj.PAGINAZIONE_ANCHOR,
			callback: JQnewsObj.pageselectCallback
		 });	
		JQnewsObj.readXmlContent(0);
	}
	,
	pageselectCallback : function(page_id, jq){
		var url = location.href.substring(0,location.href.indexOf(".html")+5)+"/pag="+page_id;
		//alert(url);
		$("#nielsen img").attr("src","");
		$("#nielsen img").attr("src","//secure-it.imrworldwide.com/cgi-bin/m?ci=rainet-it&cg=0&si="+url);
		JQnewsObj.readXmlContent(page_id);
	}
	,
	setNewsItem: function(idSubCat, type) 
	{
		this.idSubCategory = idSubCat;
		this.typeForFilter = type;
		
		$(JQnewsObj.CLASS_OTHER_VIDEO).hide();
		$(JQnewsObj.CLASS_OTHER_NEWS).hide();
		
		var filterItem = "elemento";
		if(JQnewsObj.idSubCategory!="")
			filterItem += "[sottocategoria*='|"+JQnewsObj.idSubCategory+"|']";
		if(JQnewsObj.typeForFilter!="")
			filterItem += "[type*='|"+JQnewsObj.typeForFilter+"']";
			//filterItem += "[type='"+JQnewsObj.typeForFilter+"']";
		
		this.numberItem =  $(JQnewsObj.xmlAjax).find(filterItem).length;
		
		if(JQnewsObj.pagination)
			JQnewsObj.paginationJQuery(JQnewsObj.idSubCategory);
		else
			JQnewsObj.readXmlContent(0);
	}	
	,
	applyPngFix : function()
	{
		var arVersion = navigator.appVersion.split("MSIE")
		var version = parseFloat(arVersion[1])

		if ((version >= 5.5) && (version < 7) && (document.body.filters)) 
		{
		   for(var i=0; i<document.images.length; i++)
		   {
		      var img = document.images[i]
		      var imgName = img.src.toUpperCase()
		      if (imgName.substring(imgName.length-3, imgName.length) == "PNG")
		      {
		         var imgID = (img.id) ? "id='" + img.id + "' " : ""
		         var imgClass = (img.className) ? "class='" + img.className + "' " : ""
		         var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' "
		         var imgStyle = "display:inline-block;" + img.style.cssText 
		         if (img.align == "left") imgStyle = "float:left;" + imgStyle
		         if (img.align == "right") imgStyle = "float:right;" + imgStyle
		         if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle
		         var strNewHTML = "<span " + imgID + imgClass + imgTitle
		         + " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"
		         + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
		         + "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>" 
		         img.outerHTML = strNewHTML
		         i = i-1
		      }
		   }
		}
	},	
	__showIdAfterTimeout : function(el,millisec)
	{
		$(el).show(millisec);
	}
	,	
	/** SET ****************************************************************************************************************/
	setNumItemForPage : function(number)
	{
		this.numItemForPage = number;	
	}
	,
	setXmlAjax : function(xml)
	{
		this.xmlAjax = xml;	
	}
	,
	setIdCategory : function(cat)
	{
		this.idCategory = cat;	
	}
	,
	setIdSubCategory : function(subcat)
	{
		this.idSubCategory = subcat;	
	}	
	,
	setPagination : function(pagination)
	{
		this.pagination = pagination;	
	}	
	,
	setHtmlType : function(htmlType)
	{
		this.htmlType = htmlType;	
	}
	,
	setDomResult : function(domResult)
	{
		this.domResult = domResult;	
	}
	,
	setUniqueName : function(uniqueName)
	{
		this.uniqueName = uniqueName;	
	}
	,
	setNumItemForPage : function(numItemForPage)
	{
		this.numItemForPage = numItemForPage;	
	}
	,	
	setTypeForFilter : function(typeForFilter)
	{
		this.typeForFilter = typeForFilter;	
	}
	,	
	initNews : function()
	{
		JQnewsObj = this;
		JQnewsObj.setNewsItem(this.idSubCategory,this.typeForFilter);
	}	
}
