$(document).ready(function() {

//Overlay stuff

video_z = $('.video_otf').css("z-index");

$('#ilG_overlay').css({"z-index":video_z+1});

});

//get n. of comments on home articles

function get_xml_comments(news_ids) {

  $.ajax({
  		type: "GET",
  		url: "/ajax/ncomments.php?ID="+news_ids,
  		success: function(xml){
        for (var i=1;i <= news_ids.length;i++)
    	  if ($(xml).find('articolo').eq(i-1).find('ncomments').text() > 0){
          $('#a'+i+'_commenti > a > div').text( $(xml).find('articolo').eq(i-1).find('ncomments').text() );
          $('#a'+i+'_commenti').show();          
        } else {
          $('#a'+i+'_commenta').show();
        }
      },
  		error: function(error){
    		$('#log').html("\n<!--ncomments: got no xml-->\n");
  		}
  }); //eo ajax
 
} //eo get_xml_comments

//some extra functions

function html_entity_encode(str) {
  replaced = str.replace(/&quot;/g,"\"").replace(/&igrave;/g,"ì").replace(/&egrave;/g,"è").replace(/&eacute;/g,"é").replace(/&agrave;/g,"à").replace(/&ograve;/g,"ò").replace(/&ugrave;/g,"ù").replace(/&#039;/g,"\'");
  return replaced;
}

function prepareMovie() {	
	
	//mm globals
	var mm_xml_loaded = 0;
	
	var mmc_vid_title = new Array(6);
	var mmc_vid_desc = new Array(6);
	var mmc_vid_img = new Array(6);
	var mmc_vid_location = new Array(6);	
	
	var save_mm_title = "";
	var save_mm_description = "";
	
	//some page preparations
	
	//putting special icons on images
	
	//css hover may not work (IE6) so doing it the jquery way
	
	$('.play_large > span').hover(function(){
		$(this).addClass('hl');
	}, function(){
		$(this).removeClass('hl');	
	});
	
	$('.gallery_large > span').hover(function(){
		$(this).addClass('hl');
	}, function(){
		$(this).removeClass('hl');	
	});

	$('.photostory_large > span').hover(function(){
		$(this).addClass('hl');
	}, function(){
		$(this).removeClass('hl');	
	});
	
	$('.audio_large > span').hover(function(){
		$(this).addClass('hl');
	}, function(){
		$(this).removeClass('hl');	
	});
	
	$('.play_small > span').hover(function(){
		$(this).addClass('hl');
	}, function(){
		$(this).removeClass('hl');	
	});
	
	$('.gallery_small > span').hover(function(){
		$(this).addClass('hl');
	}, function(){
		$(this).removeClass('hl');	
	});

	$('.photostory_small > span').hover(function(){
		$(this).addClass('hl');
	}, function(){
		$(this).removeClass('hl');	
	});
	
	$('.audio_small > span').hover(function(){
		$(this).addClass('hl');
	}, function(){
		$(this).removeClass('hl');	
	});
/*
	$('#mm_close').livequery('click', function (){
		$('#mm_menu').slideToggle("fast");
	});
	
	$('#mm_scroller > span').livequery('click', function (){
		$('#mm_menu').fadeTo(1, 0.9).slideToggle("fast");
	});
*/
	//for blackbar:

	$('.play_small_bb').hover(function(){
		$(this).addClass('play_small_bb_hl');
	}, function(){
		$(this).removeClass('play_small_bb_hl');	
	});

	$('.gallery_small_bb').hover(function(){
		$(this).addClass('gallery_small_bb_hl');
	}, function(){
		$(this).removeClass('gallery_small_bb_hl');	
	});

	$('.photostory_small_bb').hover(function(){
		$(this).addClass('photostory_small_bb_hl');
	}, function(){
		$(this).removeClass('photostory_small_bb_hl');	
	});

	$('.audio_small_bb').hover(function(){
		$(this).addClass('audio_small_bb_hl');
	}, function(){
		$(this).removeClass('audio_small_bb_hl');	
	});


	//this is called after document.ready or IE is gonna die.
	
	//reading xml + filling divs
	$.ajax({
		type: "GET",
		url: "/mmc.xml",
		
		success: function(data)
		{
			//alert("success");
			mm_xml_loaded = 1;
							
			idx = 0;
								
			$(data).find('track').each(function() {
				
			var $track = $(this);
	
			mmc_vid_title[idx] = $track.find('title').text();
			mmc_vid_desc[idx] = $track.find('creator').text();
			mmc_vid_location[idx] = $track.find('location').text().replace(/\s*/g, "").replace(/http:\/\/www.ilgiornale.it\/web\/vid\//, "").replace(/http:\/\/ilgiornale.alice.cdn.interbusiness.it\/web\/vid\//, "").replace(".flv","");
			mmc_vid_img[idx] = $track.find('image').text().replace(/&X=400&Y=300/,"&X=53&Y=40");
	
			//alert("title:"+mmc_vid_title[idx]+"\ndescription:"+mmc_vid_desc[idx]+"\nlocation:"+mmc_vid_location[idx]+"\nimage:"+mmc_vid_img[idx]);
			
			if (idx == 0) {
				$('#mm_text_title').text(html_entity_encode(mmc_vid_title[idx]));
				$('#mm_text_content').text(html_entity_encode(mmc_vid_desc[idx]));
				$('#mmc_tn'+idx).attr("src", mmc_vid_img[idx]);
				$('#mmc_tn'+idx).attr("title", mmc_vid_title[idx]);
				$('#mmc_link'+idx).attr("href", "/video.pic1?ID="+mmc_vid_location[idx]+"&LINK=MMC");				
				$('#mmc_send_link').attr("href","/invia_video.pic1?ID="+mmc_vid_location[idx]+"&LINK=MMC_SEND");
				//alert("0! title:"+mmc_vid_title[idx]+"\ndescription:"+mmc_vid_desc[idx]);
			}
			
			else {
				$('#mmc_tn'+idx).attr("src", mmc_vid_img[idx]);
				$('#mmc_tn'+idx).attr("title", mmc_vid_title[idx]);
				$('#mmc_link'+idx).attr("href", "/video.pic1?ID="+mmc_vid_location[idx]+"&LINK=MMC");
			}
			
			idx++;
	
			});
			
			//time to show the mmc magic
			//alert("toggle!");
			$('#mm_loader').toggle();
		},
		
		error: function(data){
			//alert("failure");
			$("#mm_loader > p").html('<p style="color:red; margin-top:120px;">Errore di lettura, <a href="/?SS_ID=-1">ricaricare</a> la pagina</p>');
			$("#mm_loader > img").toggle();
		}
	});

	//making mm_tn fade out
	$('.mm_tn').fadeTo(5, 0.33);
	
	$(".mm_tn:first").fadeTo(5, 1);
	$(".mm_tn:first").css({ paddingBottom:"2px", borderBottom:"4px solid #fab212" });

	$('.mm_tn').hover(function(){
			
			save_mm_title = $('#mm_text_title').text();
			save_mm_description = $('#mm_text_content').text();
			
			$('#mm_text_title').text(html_entity_encode(mmc_vid_title[$(this).attr("id").replace("mmc_tn","")]));
			$('#mm_text_content').text(html_entity_encode(mmc_vid_desc[$(this).attr("id").replace("mmc_tn","")]));
			
			//caller = this;

			$(this).fadeTo(5, 1);

		}, 
		function(){
			$(this).fadeTo(5, 0.33);
			
			$('#mm_text_title').text(save_mm_title);
			$('#mm_text_content').text(save_mm_description);
			
		});
	
	$('.mm_link').hover(function(){
			$(this).addClass('highlight_fab212');
		}, function(){
			$(this).removeClass('highlight_fab212');
		});
	
	$('#mm_scroller > span').hover(function(){
			$(this).addClass('highlight_fab212');
		}, function(){
			$(this).removeClass('highlight_fab212');
		});
	
}
