$(document).ready(function(){
	
	var l = $("#anteprime_wrapper li").length;
	if (l > 3) {
		$('#anteprime_wrapper').carousel({
			direction: "vertical",
			dispItems: 1,
			pagination: false,
			loop: true,
			autoSlide: true,
			btnsPosition: "outside"
		});
	}
	
	$('#mainArticle ul li a').live("click", function(e) {
		e.preventDefault();
		var article = $(this).attr('rel');
		if ($(this).attr('class') != 'active') {
			$(this).parent().siblings().children('a').removeClass('active');
			$(this).addClass('active');	
			$('#articleContent > div').css('display', 'none');
			$('#'+article).css('display', 'block');
		};
	});
  
	$('#projects a img').hover(
		function () {
			$(this).animate({
			    opacity: 0
			}, 300);
		}, 
		function () {
			$(this).animate({
			    opacity: 1
			}, 300);
		}
	);
	
	
  if ($("#google_map").length>0) {
    //initialize();
  }
  
  toggleText('search_text', 'cerca nel sito');
  toggleText('search_text', 'search');
  $(document).pngFix();
  $('.pressList').accordion({ header: 'h3' , active: 50, autoHeight: false });

    
  $('.pressList_internal').accordion({
			header: "h3",
			active: 50,
			alwaysOpen: false,
			autoHeight: false
		});   

  $("#banner li").hover(function() { //On hover...

    var thumbOver = $(this).find("img").attr("src"); //Get image url and assign it to 'thumbOver'

    //Set a background image(thumbOver) on the &lt;a&gt; tag 
    $(this).find("a.thumb").css({'background' : 'url(' + thumbOver + ') no-repeat center bottom'});
    //Animate the image to 0 opacity (fade it out)
    $(this).find("span").stop().animate({opacity: 0}, 300);
    } , function() { //on hover out...
      //Animate the image back to 100% opacity (fade it back in)
      $(this).find("span").stop().animate({opacity: 1}, 300);
    });
    
    $("#banner li.vertical").hover(function() { //On hover...

      var thumbOver = $(this).find("img").attr("src"); //Get image url and assign it to 'thumbOver'

      //Set a background image(thumbOver) on the &lt;a&gt; tag 
      $(this).find("a.thumbVertical").css({'background' : 'url(' + thumbOver + ') no-repeat center bottom'});
      //Animate the image to 0 opacity (fade it out)
      $(this).find("span").stop().animate({opacity: 0}, 300);
      } , function() { //on hover out...
        //Animate the image back to 100% opacity (fade it back in)
        $(this).find("span").stop().animate({opacity: 1}, 300);
      });
    // function uno() { 
      //    $("#topics.article").toggle() ;
      //    $("#main.section").css('borderTop', '3px solid #2d0b01');  
      //    $("a.forTopics").addClass('selectedForTopics'); 
      //    $("a.forStore").removeClass('selected'); 
      //    $("a.forElearning").removeClass('selected');  
      //  }
      // 
      //  function due() { 
        //    $("#elearning.article").toggle() ;
        //    $("#main.section").css('borderTop', '3px solid #444');  
        //    $("a.forTopics").removeClass('selectedForTopics'); 
        //    $("a.forStore").removeClass('selected'); 
        //    $("a.forElearning").removeClass('selected');
        //    $("#topics.article").toggle() ;
        //    
        //  }
        // 
        //  function tre() { 
          //    $("#store.article").animate({height: "290px"}, {queue: false, duration: 700, easing: 'swing'});
          //    $("#elearning.article").animate({height: "0"}, {queue: false, duration: 700, easing: 'swing'});
          //    $("#topics.article").animate({height: "0"}, {queue: false, duration: 700, easing: 'swing'});
          //    $("#main.section").css('borderTop', '3px solid #282828');  
          //    $("a.forStore").addClass('selected');
          //    $("a.forTopics").removeClass('selectedForTopics'); 
          //    $("a.forElearning").removeClass('selected');
          //  }
          //  function quattro() { 
            //    $("#store.article").animate({height: "0"}, {queue: false, duration: 700, easing: 'swing'});
            //    $("#elearning.article").animate({height: "0"}, {queue: false, duration: 700, easing: 'swing'});
            //    $("#topics.article").animate({height: "0"}, {queue: false, duration: 700, easing: 'swing'});        
            //    $("#main.section").css('borderTop', '3px solid #444');  
            //    $("a.forStore").removeClass('selected');
            //    $("a.forTopics").removeClass('selectedForTopics'); 
            //    $("a.forElearning").removeClass('selected');
            //  }
            //  function cinque() { 
              //    $("#elearning.article").animate({height: "290px"}, {queue: false, duration: 700, easing: 'swing'});
              //    $("#topics.article").animate({height: "0"}, {queue: false, duration: 700, easing: 'swing'});
              //    $("#store.article").animate({height: "0"}, {queue: false, duration: 700, easing: 'swing'});
              //    $("#main.section").css('borderTop', '3px solid #282828'); 
              //    $("a.forElearning").addClass('selected');  
              //    $("a.forStore").removeClass('selected');
              //    $("a.forTopics").removeClass('selectedForTopics');       
              //  }
              //  function sei() { 
                // $("#elearning.article").animate({height: "320px"}, {queue: false, duration: 700, easing: 'swing'});
                //     $("#topics.article").animate({height: "320px"}, {queue: false, duration: 700, easing: 'swing'});
                //    $("#store.article").animate({height: "320px"}, {queue: false, duration: 700, easing: 'swing'});
                //    $("#main.section").css('borderTop', '3px solid #444'); 
                // }

                $("#store.article").hide();
                $("#topics.article").hide();
                $("#elearning.article").hide();
                $("#moreInfoSlide").css('position', 'static');
                $("a.forElearning").live("click", function() { 
                  if($('a.forTopics').hasClass('selectedForTopics') || $('a.forStore').hasClass('selected')) {
                  } else if($("a.forElearning").hasClass('selected')) {
                    $("a.forElearning").removeClass('selected');
                    $("#elearning.article").slideUp("slow");
                    $("#main.section").css('borderTop', '3px solid #444');
                    $("a.forTopics").fadeTo("slow", 1);
                    $("a.forStore").fadeTo("slow", 1);
                  }  
                  else {
                    $("#elearning.article").slideDown("slow");
                    $("#main.section").css('borderTop', '3px solid #282828'); 
                    $("a.forElearning").addClass('selected');  
                    $("a.forStore").removeClass('selected');
                    $("a.forTopics").removeClass('selectedForTopics'); 
                    $("a.forTopics").fadeTo("slow", 0.4);
                    $("a.forStore").fadeTo("slow", 0.4);
                  }    
               
                  });
                  $("a.forTopics").live("click", function() { 
                     if($('a.forElearning').hasClass('selected') || $('a.forStore').hasClass('selected')) {
                      } else if($('a.forTopics').hasClass('selectedForTopics')) {
                        $("a.forTopics").removeClass('selectedForTopics');
                        $("#topics.article").slideUp('slow');
                        $("#main.section").css('borderTop', '3px solid #444'); 
                        $("a.forElearning").fadeTo("slow", 1);
                        $("a.forStore").fadeTo("slow", 1);
                    }    
                    else {
                  $("#topics.article").slideDown("slow");
                  $("#main.section").css('borderTop', '3px solid #2d0b01'); 
                  $("a.forElearning").removeClass('selected');  
                  $("a.forStore").removeClass('selected');
                  $("a.forTopics").addClass('selectedForTopics');   
                  $("a.forElearning").fadeTo("slow", 0.4);
                  $("a.forStore").fadeTo("slow", 0.4);
                  }
                  
                    });

                    $("a.forStore").live("click", function() {
                      if($('a.forTopics').hasClass('selectedForTopics') || $('a.forElearning').hasClass('selected')) {
                      } 
                      else if($('a.forStore').hasClass('selected')) {
                         $("#store.article").slideUp("slow");
                          $("a.forStore").removeClass('selected');
                          $("#main.section").css('borderTop', '3px solid #444');   
                          $("a.forTopics").fadeTo("slow", 1);
                          $("a.forElearning").fadeTo("slow", 1);   
                      }
                      else {
                        $("#store.article").slideDown("slow");
                        $("#main.section").css('borderTop', '3px solid #282828'); 
                        $("a.forElearning").removeClass('selected');  
                        $("a.forStore").addClass('selected');
                        $("a.forTopics").removeClass('selectedForTopics');     
                        $("a.forTopics").fadeTo("slow", 0.4);
                        $("a.forElearning").fadeTo("slow", 0.4);  
                      }
                    
                    
                      });

                      $(".close a").click(function() { 
                        $("#store.article").slideUp("slow");
                        $("#elearning.article").slideUp("slow");
                        $("#topics.article").slideUp("slow");			
                        $("#main.section").css('borderTop', '3px solid #444');
                        $("a.forTopics").removeClass('selectedForTopics'); 
                        $("a.forStore").removeClass('selected');
                        $("a.forElearning").removeClass('selected'); 
                        $("a.forTopics").fadeTo("slow", 1);
                        $("a.forStore").fadeTo("slow", 1); 
                        $("a.forElearning").fadeTo("slow", 1);                      
                      });

                      function onAfter() { 
                        /*$('.linkTo').html("") .append('<a href="index.php?m=progetti">' + this.alt + '</a>'); */
                        //$('.linkTo').html("") .append(this.alt); 
                      }
                      $('#slideShow') 
                      .before('<div id="nav">') 
                      .cycle({ 
                        fx:     'scrollRight', 
                        speed:  'slow', 
                        after:   onAfter,
                        timeout: 6000, 
                        pager:  '#nav' 
                      });
                      $('.forTabs .tabStarter').tabs();
                      $('a[rel*=facebox]').facebox({
                        //next_image    : 'img/icon/next.gif',
                        //play_image    : 'http://famspam.com/images/icons/small/fast_forward.png',
                        //pause_image   : 'http://famspam.com/images/icons/small/fast_forward.png',
                        //prev_image    : 'img/icon/prev.gif'

                      });

                      $("#primary.nav li").hover(
                        function () {
                          $(this).addClass("hover");
                        }, 
                        function () {
                          $(this).removeClass("hover");
                        }
                      );

                      // customizzazione del validate
                      $('#contributor').validate({
                        errorPlacement: function(error, element) {
                          error.appendTo( element.parents('p').addClass('error') );
                        },
                        highlight: function( element, errorClass ) {
                          $( element ).addClass( errorClass ).parents('p').removeClass('success').addClass('error');
                        },
                        unhighlight: function( element, errorClass ) {
                          $( element ).removeClass( errorClass ).parents('p').removeClass('error').addClass('success');
                        }
                      });
											$('.galleria').addClass('gallery_demo'); // adds new class name to maintain degradability

											
											$('ul.gallery_demo').galleria({
												history   : true, // activates the history object for bookmarking, back-button etc.
												clickNext : true, // helper for making the image clickable
												insert    : '.galleriaContainer', // the containing selector for our main image
												onImage   : function(image,caption,thumb) { // let's add some image effects for demonstration purposes

													// fade in the image & caption
													if(! ($.browser.mozilla && navigator.appVersion.indexOf("Win")!=-1) ) { // FF/Win fades large images terribly slow
														image.css('display','none').fadeIn(1000);
													}
													caption.css('display','none').fadeIn(1000);

													// fetch the thumbnail container
													var _li = thumb.parents('li');

													// fade out inactive thumbnail
													_li.siblings().children('img.selected').fadeTo(500,0.5);

													// fade in active thumbnail
													thumb.fadeTo('fast',1).addClass('selected');

													// add a title for the clickable image
													image.attr('title','Next image >>');
												},
												onThumb : function(thumb) { // thumbnail effects goes here

													// fetch the thumbnail container
													var _li = thumb.parents('li');

													// if thumbnail is active, fade all the way.
													var _fadeTo = _li.is('.active') ? '1' : '0.5';

													// fade in the thumbnail when finnished loading
													thumb.css({display:'none',opacity:_fadeTo}).fadeIn(1500);

													// hover effects
													thumb.hover(
														function() { thumb.fadeTo('fast',1); },
														function() { _li.not('.active').children('img').fadeTo('fast',0.5); } // don't fade out if the parent is active
													)
												}
											});
											


                    }); 


                    $(".addToCart a").click(function(){ $(function dosubmit() {
                      $(".qta-versioni").submit();

                    });

                  });


                  window.onload=intercetta;
                  function intercetta() {
                    for (var i=0; i<document.links.length; i++)
                    if(document.links[i].rel=="blank"){
                      document.links[i].target="_blank";
                    }
                  }

	 function toggleText(p1, p2){
    $('input'+'#'+p1).focus(function () {
        if (this.value == p2) {this.value = '';}
    });
    $('input'+'#'+p1).blur(function () {
        if (this.value == '') {this.value = p2;}
    });
}

function flash(path,wid,hei){
	document.write('<object id="filmato" type="application/x-shockwave-flash" data="'+path+'" width="'+wid+'" height="'+hei+'"><param name="movie" value="'+path+'" /><param name="menu" value="false" /><param name="quality" value="best" /><p>Per una visione ottimale della testata scaricare il plug-in flash</p></object>');
}
function popUpLink(urlPop) {
  newWin = window.open(urlPop,"nuovafinestr");
}

function credits(){
	if(document.getElementById('nascar')){
	document.getElementById('nascar').href="javascript:popUpLink('http://www.nascar.it')";
	}
}
function openmenu(idiv,n){
	if(document.getElementById(idiv).style.display=="none"){
		closeallmenu(n);
		document.getElementById(idiv).style.display="block";
	}else{
		closeallmenu(n);
	}
}

function closeallmenu(n){
	var uls = document.getElementsByTagName("ul");
	nmax=2;
	for(z=n;z<=nmax;z++){
		for (i=0;i<uls.length;i++){
			if(uls[i].className=="submenu-"+z){uls[i].style.display = "none";}
		}
	}
}

function attivaeditor(){
	if(document.getElementById("text")){
		var oFCKeditor = new FCKeditor( 'text' ) ;
		oFCKeditor.BasePath	= 'admin/css/';
		oFCKeditor.ToolbarSet	= 't-store' ;
		oFCKeditor.ReplaceTextarea() ;	
	}
}

function aggphoto(pos,lpos,xx,titolo) {
  document.getElementById('img_p').src = pos;
  document.getElementById('link_p').href = lpos;
  document.getElementById('img_p').title = titolo;
  document.getElementById('link_p').title = titolo;
  chan=getElementsByClassName('scelto');
  for(i=1;i<=chan.length;i++){
	  chan[i-1].className="";
  }
  
  document.getElementById(xx).className = "scelto";
}


//chiudi div modifiche
function chiudi() {
	node = document.getElementById("messaggio-conferma");
	if ((node)&&(node.parentNode)){
		removed=node.parentNode.removeChild(node); 
   }
}
function selpay(gg){
	mod_i=gg.seller.length;
	err=0;
	if(mod_i==undefined)mod_i=1; 
	for (i=0; i<mod_i; i++) {
		if (document.getElementById("seller"+(i)).checked){
			err++;
		}
	} 
	if(err==0){
		alert('selezionare una modalita di spedizione');            
		return false;	
	}
	mod_i=gg.payment.length;
	err=0;
	if(mod_i==undefined)mod_i=1; 
	for (i=0; i<mod_i; i++) {
		if (document.getElementById("payment"+(i+1)).checked){
			err++;
		}
	} 
	if(err==0){
		alert('selezionare un modalita di pagamento');            
		return false;	
	}
}
//redirect
function relink(rel){
	location.href=rel;	
}
//cerca classi
function getElementsByClassName(classname){
    var a = [];
    var re = new RegExp('\\b' + classname + '\\b');
    var els = document.all?document.all:document.getElementsByTagName("*");
    for(var i=0,j=els.length; i<j; i++)
        if(re.test(els[i].className))a.push(els[i]);
    return a;
}
//oggetti carrello
function controllcart(totale){
	errore=0;
	for(i=1;i<=totale;i++){
		qta=document.getElementById("qta"+i).value;
		if(isNaN(qta)){
			errore++;
		}
	}
	if(errore>0){
		alert("Il contenuto del campo [[Quantità]] nel carrello deve essere espresso in forma [[Numerica]]");
		return false;
	}else{
		return true;	
	}
}
function cart_form(myform,valore,single){
	totale_cart=document.getElementById("totprod").value;
	if(valore=='cart_refresh'){
		if(controllcart(totale_cart)==false){
			return false;
		}
	}
	
	document.getElementById("type_cart").value=valore;
	document.getElementById("single").value=single;
	document.getElementById("contributor").submit();
}
function controllqta(){
	qta=document.getElementById("qta").value;
	if(isNaN(qta)){
		alert("Il contenuto del campo [[Quantità]] nella scheda prodotto deve essere espresso in forma [[Numerica]]");
		return false;
	}else{
		return true;	
	}
}


function nascostocp(){
	filtro = /^nascostocp\[([a-zA-Z0-9])+\]$/;
	filtrob = /^menucp\[([a-zA-Z0-9])+\]$/;
	anchors = document.getElementsByTagName('a');
	for (i=0; i<anchors.length; i++){
		asc = anchors[i];
		pipp=asc.getAttribute('rel');
		if (filtro.test(pipp)){
			asc.parentNode.style.display="none";
		}
		if(asc.getAttribute('id')=="cpprimo"){
			asc.parentNode.style.display="block";
		}
		if (filtrob.test(pipp)){
			asc.parentNode.style.cursor="pointer";
			asc.onclick= function () {menucp(this); return false;}	
		}
	}
}
function menucp(arc){
	if(!arc) return false;
	filtro = /^nascostocp\[([a-zA-Z0-9])+\]$/;
	filtro2 = /^menucp\[([a-zA-Z0-9])+\]$/;
	str=arc.getAttribute('rel');
	str=str.replace("menucp[","");
	id=str.replace("]","");
	anchors = document.getElementsByTagName('a');
	for (i=0; i<anchors.length; i++){
		asc = anchors[i];
		pipp=asc.getAttribute('rel');
		if(filtro.test(pipp)){
			asc.parentNode.style.display="none";
		}
		if(filtro2.test(pipp)){
			asc.className="";
		}
		if (pipp=="nascostocp["+id+"]"){
			asc.parentNode.style.display="block";
			asc.style.display="none";
		}
	}	
	arc.className="active";
}

function send_form(myform,valore,single){
	myform.type.value=valore;
	myform.single.value=single;
	myform.submit();
}
function chiudifaq(){
	arc=getElementsByClassName("risposta");
    for(i=0;i<arc.length;i++){
		arc[i].style.display="none";
	}
}
function openfaq(id){
	chiudifaq();
	document.getElementById("risposta-"+id).style.display="block";	
}

function chiudimenu(){
	arc=getElementsByClassName("submenu");
    for(i=0;i<arc.length;i++){
		arc[i].style.display="none";
	}
}

function selezione_negozio(id,form){
	if(id==3){
		for(i=0;i<form.seller.length;i++){
			form.seller[i].disabled=true;		
			form.seller[i].checked=false;
			if(form.seller[i].className=="nodisabb"){
				form.seller[i].disabled=false;	
				form.seller[i].checked=true;	
			}
		}
	}else{
		for(i=0;i<form.seller.length;i++){
			form.seller[i].disabled=false;		
			if(form.seller[i].className=="nodisabb"){
				form.seller[i].disabled=true;	
				form.seller[i].checked=false;	
			}
		}
	}
}

