function dettaglio(url,id)
{
	window.open(url,'dettaglio'+id,'width=750,height=520,scrollbars=yes');
}

function nuovoCv(url,id)
{
	window.open(url,'cv'+id,'width=750,height=600,scrollbars=yes');
}

function imposeMaxLength(Object, MaxLen)
{
  return (Object.value.length <= MaxLen);
}







function in_array(the_needle, the_haystack){
	 //alert(the_haystack.length)
	 // alert(the_haystack)
	 matched = false
	 for(c=0; c<=the_haystack.length; c++){
	 	  if(the_needle == the_haystack[c])
	 	  {
	 	  	matched = true
	 	  }
	 }
   return matched;
} 

$(document).ready(function() {
	
	  initZoom();
	  initZoomSwapImage();
	  initChangeLook();
	  
 $("div#scrollProdotti").scrollable({ 
 
    size: 2 
}); 
	  
 $("div#scrollTeam").scrollable({ 
 
    size: 4 
}); 
	  
 $("div#scrollDjokovic").scrollable({ 
 
    size: 4 
}); 
	
 
 
		renderMenu('#menuTop>ul>li>a','gif');
		//renderMenu("#menuLiv1Collection>li>a",'png');
		renderMenu("#menuCollection>ul>li>a",'png');
		$(document).pngFix(); 
    //$('img[@src$=.png]').ifixpng(); 
    //$('img').ifixpng(); 
    //alert($('body').html());
    
})


function renderMenu(selector,type) {
   $(selector).each(function(i) {
      var item_title  = $(this).text();
      //alert(item_title+" "+$(this).attr('id'))
      if (item_title != '') {
       	      
  	      
          var item_label  = escape($(this).text());
          var item_id     = $(this).attr('id');
          var status      = $(this).attr('class');
          var width       = $(this).attr('width');
          var height      = $(this).attr('height');
          //alert(height)
          var img_tag = "<img id=\""+item_id+"\" src=\""+wwwRoot+"/"+wwwLang+"/img/menu/"+item_id+"_"+status+"."+type+"\" width=\""+width+"\" height=\""+height+"\" alt=\""+item_title+"\" border=\"0\"/>";
					//img_tag = '';
          //alert(img_tag)
          $(this).text("");
			   	$(this).html(img_tag);
      }
   });
   $(document).pngFix(); 
}



function renderTitles(parentObj) {
   $(parentObj+" h1:not(.alt),"+parentObj+" h2:not(.alt),h3:not(.alt)").each(function(i) {
      var item_title  = $(this).text();
      
      if (item_title != '') {
      
   	   var alternativeStyle  = $(this).attr("class");
   	   if(alternativeStyle){
   	     var item_level  = alternativeStyle;
   	   }
   	   else {
   	     var item_level  = this.tagName;
   	   }
   	      
  	      
          var item_label  = escape($(this).text());
   	      var item_width  = $(this).css("width");
   	      if (item_level == 'H1') {
   	         var item_height= '21px';
   	      }
   	      else if (item_level == 'H2') {
   	         var item_height= '21px';
   	      }
   	      else if (item_level == 'H3') {
   	         var item_height= '10px';
   	      }
   	      
   	      //var item_height  = $(this).css("height");
   	      var item_id     = item_level+"_"+i;
   	      //alert(item_width+" || "+item_height);
   	      var img_tag = "<img id=\""+item_id+"\" style=\"/*width:"+item_width+";height:"+item_height+"*/\" src=\""+wwwRoot+"/dynamic/img/titles/"+item_level+"/"+item_width+"/"+item_label+".gif\" alt=\""+item_title+"\" border=\"0\"/>";
   	      
   	      $(this).text("");
   	      $(this).html(img_tag);
      }
   });
   if ($.browser.msie) { $(document).pngFix(); /*$(".png").ifixpng();*/ }
}

function renderMenu2Liv() {
   $(".menuLiv2 a").each(function(i) {
      var item_title  = $(this).text();
      if (item_title != '') {
   
   	      
  	      
          var item_label  = escape($(this).text());
          var item_status  = $(this).attr('class');
          //alert(item_status)
   	      //var item_id     = item_level+"_"+i;
   	      var img_tag = "<img style=\"\" src=\""+wwwRoot+"/dynamic/img/titles/liv2_"+item_status+"/166px/"+item_label+".png\" alt=\""+item_title+"\" border=\"0\"/>";
   	      
   	      $(this).text("");
   	      $(this).html(img_tag);
      }
   });
   if ($.browser.msie) { $(document).pngFix(); /*$(".png").ifixpng();*/ }
}

function stampaImmagine(item_id,status,item_title)
{
	//global wwwRoot;
 $.ajax({
   type: "GET",
   url: wwwRoot+'/ajax/calcolaDimensioni.php?img='+wwwRoot+"/comuni/img/menu/"+item_id+"_"+status+".png",
   success: function(msg){
     return ("<img id=\""+item_id+"\" src=\""+wwwRoot+"/comuni/img/menu/"+item_id+"_"+status+".png\" "+msg+" alt=\""+item_title+"\" border=\"0\"/>");

   }
 });
}

var imgIndex = 1;

function initZoom()
{
	//alert('init zoom');
  imgIndex++;
	var options =
	            {
	                zoomWidth: 424,
	                zoomHeight: 510,
	                zoomType:'reverse',
	                showEffect:'fadein',
	                fadeinSpeed:'slow',
	                hideEffect:'fadeout',
	                fadeoutSpeed: 'slow',
	                position : 'right',
	                yOffset :0,
	                xOffset :112,
	                title :false,
	                randId:imgIndex
	            }
	$(".jqzoom").jqzoom(options);
}

function initZoomSwapImage()
{
    $('.zoomSwapImage').click(function () {
      $('#zoomPreview').attr('src',wwwRoot+'/prodotti/big/'+this.rel+'.jpg');
      $('#zoomImage').attr('href',wwwRoot+'/prodotti/zoom/'+this.rel+'.jpg');

      $(jQuery("img", this)).attr('src',wwwRoot+'/prodotti/thumbs/'+$('#zoomPreview').attr('rel')+'.jpg');
      
      relThumb = this.rel;
      relBig =  $('#zoomPreview').attr('rel');
      
      this.rel = relBig;
      $('#zoomPreview').attr('rel',relThumb);
      
      
      //$(".jqZoomWindow").remove();
      $(".jqZoomWindow").html('pippo');
      
      
      
      initZoom();
      return false;
     });
}

function svuota()
{
	$(".jqZoomWindow").remove();
}
function riempi()
{
	initZoom();
}

function initChangeLook()
{
	    $('.changeLook').each(function () {
					var randId = 'bgImg_'+(Math.ceil((Math.random()*10000)));
					var divBgImage = document.createElement('div');
				 	divBgImage.setAttribute('id',randId);
				 	divBgImage.setAttribute('style','position:absolute;left:-99999px;width:1px;height:1px;clip:rect(0,1,1,0)');
					var imgBgImage = document.createElement('img');
				 	imgBgImage.setAttribute('style','width:1px;height:1px;');
				 	imgBgImage.setAttribute('src',this.href);
				  document.body.appendChild(divBgImage);
				  document.getElementById(randId).appendChild(imgBgImage);
     });
    $('.changeLook').click(function () {
    	$('.changeLook img').css('background-color','#707f95');
    	$(jQuery("img", this)).css('background-color','#ffffff');
    	//alert(this.href)
    	$('#sfondo_catalogo_intro').css('background-image','url('+this.href+')');
    	return false;
     })
}

function initChangeBgStatic(div)
{
	    $('.changeBg').each(function () {
					var randId = 'bgImg_'+(Math.ceil((Math.random()*10000)));
					var divBgImage = document.createElement('div');
				 	divBgImage.setAttribute('id',randId);
				 	divBgImage.setAttribute('style','position:absolute;left:-99999px;width:1px;height:1px;clip:rect(0,1,1,0)');
					var imgBgImage = document.createElement('img');
				 	imgBgImage.setAttribute('style','width:1px;height:1px;');
				 	imgBgImage.setAttribute('src',this.href);
				  document.body.appendChild(divBgImage);
				  document.getElementById(randId).appendChild(imgBgImage);
     });
    $('.changeBg').click(function () {
    	$('.changeBg img').css('background-color','#707f95');
    	$(jQuery("img", this)).css('background-color','#ffffff');
    	//alert(this.href)
    	$('#'+div).css('background-image','url('+this.href+')');
    	return false;
     })
}


var BgIndex = 0;

function ChangeBg(id)
{
    	$('.changeBg img').css('background-color','#707f95');
    	$('#'+id+" img").css('background-color','#ffffff');
    	$(".sfondo_changes:visible").fadeOut();
      $("#bg_"+id).fadeIn();	
}

function automaticChangeBg()
{
	     BgIndex++;
	     if(BgIndex>=elencoBg.length){BgIndex = 0;}
	     if(elencoBg.length==1){clearInterval(x);}
	     else
	     {
	     ChangeBg(elencoBg[BgIndex]);
	     }
}

function initChangeBg(div)
{     
var imgIndex = 0;


	    elencoBg = new Array();
	    
	    $('.changeBg').each(function () {
	    	
					var randPreloadId = 'bgImgPreload_'+(Math.ceil((Math.random()*10000)));
					var $preload = $("<div></div>").attr("id", randPreloadId).css({
						position: "absolute",
						left: "-1000px",
						top: "0",
						width: "100px",
						height: "100px"
					});
					$("<img />").attr("src", this.href).css({
						width: "100px",
						height: "100px"
					}).appendTo($preload);
					$preload.appendTo("body");
				  
					var divBgImage = document.createElement('div');
				 	divBgImage.setAttribute('id','bg_'+this.id);
				 	
				  elencoBg[imgIndex] = this.id;
				 	
				 	if(imgIndex==0)
				 	{
				 		imgDisplay = 'block';
				 	}
				 	else
				 	{
				 		imgDisplay = 'none';
				 	}
				 	$(divBgImage).addClass('sfondo_changes').css({
				 		position: "absolute",
				 		width: "100%",
				 		background: "url('" + this.href + "') center top no-repeat",
				 		height: $("#"+div).outerHeight(),
				 		zIndex: 1,
				 		display: imgDisplay
				 	});
				  document.body.appendChild(divBgImage);
				  imgIndex++;
     });
    $('.changeBg').click(function () {
    	 clearInterval(x);
       ChangeBg(this.id)
    	return false;
     })
     
     x = window.setInterval("automaticChangeBg()", 5000);


     
     
     
}
