$.extend({URLEncode:function(c){var o='';var x=0;c=c.toString();var r=/(^[a-zA-Z0-9_.]*)/;
  while(x<c.length){var m=r.exec(c.substr(x));
    if(m!=null && m.length>1 && m[1]!=''){o+=m[1];x+=m[1].length;
    }else{if(c[x]==' ')o+='+';else{var d=c.charCodeAt(x);var h=d.toString(16);
    o+='%'+(h.length<2?'0':'')+h.toUpperCase();}x++;}}return o;},
URLDecode:function(s){var o=s;var binVal,t;var r=/(%[^%]{2})/;
  while((m=r.exec(o))!=null && m.length>1 && m[1]!=''){b=parseInt(m[1].substr(1),16);
  t=String.fromCharCode(b);o=o.replace(m[1],t);}return o;}
});

$.extend({
  getUrlVars: function(){
    var vars = [], hash;
    var myref = $.URLDecode(window.location.href);
    var hashes = myref.slice(myref.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
      hash = hashes[i].split('=');
      vars.push(hash[0]);
      vars[hash[0]] = hash[1];
    }
    
    return vars;
  },
  getUrlVar: function(name){
    return $.getUrlVars()[name];
  }
});

	
//==================================================
// hightlight voci delle ancore
//==================================================

function highlightSub(link) {
	$("ul.submenu1 li a").removeClass('on');
	$("ul.submenu1 li a").each(function(){
		if($(this).attr('href') == link) {
			$(this).addClass("on");
		}
	}); 
}

$(document).ready(function(){

	//rimuoviamo la freccina per le categorie nel menu
	$("ul.submenu1 li a span").parent().css('background-image', 'none');
	
	//$(document).pngFix();
    //==================================================
    // scroll della pagina in base alle ancore
    //==================================================
    var thisUrl = location.href;
    var thisRad = thisUrl.split("#");
	if(typeof(thisRad[1]) != 'undefined') {
		highlightSub("#"+thisRad[1]);
		var target_offset = $("#"+thisRad[1]).offset();
		var target_top = target_offset.top - 80;
		$('html, body').animate({scrollTop:target_top}, 500);
	}
    $(".submenu1 li a, .triplo p a, .imgProdotti ul li a").click(function(event){
        //get the full url - like mysitecom/index.htm#home
        var full_url = this.href;
        //split the url by # and get the anchor target name - home in mysitecom/index.htm#home
        var parts = full_url.split("#");
        var trgt = parts[1];
        // se il collegamento è relativo alla pagina attiva allora crea l'effetto
        if(thisRad[0] == parts[0]){
            //prevent the default action for the click event
            event.preventDefault();
            //get the top offset of the target anchor
            var target_offset = $("#"+trgt).offset();
            var target_top = target_offset.top - 80;
            //goto that anchor by setting the body scroll top to anchor top
            $('html, body').animate({scrollTop:target_top}, 500);
        } 
    });
	
	$("ul.submenu1 li a").click(function(){
		var link = $(this).attr('href');
		highlightSub(link);
	});

    //==================================================
    // caricamento immagini su visualizzazione
    //==================================================

   $("img").lazyload({
       placeholder: "fileadmin/templates/sys/img/grey.gif",
       effect: "fadeIn"
    });
	
	
    //==================================================
    // menu
    //==================================================
  $('.menu1 li ul').css({
    display: "none",
    left: "auto"
  });
  $('.menu1 li').hover(function() {
    $(this)
      .find('ul')
      .stop(true, true)
      .slideDown('fast');
  }, function() {
    $(this)
      .find('ul')
      .stop(true,true)
      .fadeOut('fast');
  });

  

    //==================================================
    // effetto mouseover su collegamenti
    //==================================================

    $(".triplo a, .imgProdotti a").mouseenter(function () {
      $(this).fadeTo("fast", 0.70);
    }).mouseleave(function(){
        $(this).fadeTo("slow", 1);
    });

    //==================================================
    // photogallery
    //==================================================

    /*$('.slider').nivoSlider({
		manualAdvance: true,
        effect:'fade', //Specify sets like: 'fold,fade,sliceDown'
        //slices:15,
        //animSpeed:500, //Slide transition speed
        //pauseTime:3000,
        directionNav:false,
        startSlide:0 //Set starting Slide (0 index)
    });*/
	$('.slider').nivoSlider({
		manualAdvance: true,
		effect:'fade'
	});

    //==================================================
    // box news accordion apertura a richiesta
    //==================================================

    $('.armonico').hide();
    $('.opclo').click(function(){
        $(this).next('.armonico').slideToggle('slow');
        if($(this).html() == 'CLOSE') {
               $(this).html('EXPAND');
               $(this).css({backgroundPosition: "4px -22px"});
        } else {
               $(this).html('CLOSE');
               $(this).css({backgroundPosition: "4px 3px"});
        }
    });

    //==================================================
    // popup flavours
    //==================================================
	
    $().piroBox_ext({
        piro_speed : 700,
        bg_alpha : 0.5,
        piro_scroll : true // pirobox always positioned at the center of the page
    });
	
	//==================================================
    // apre il box armonico se si arriva dalla home cliccando su una news
    //==================================================
    
    if($.getUrlVar('tx_ttnews[tt_news]')) {
        var newsId = 'n'+$.getUrlVar('tx_ttnews[tt_news]');
        $('#'+newsId).closest("div.armonico").show();
		var puls = $('#'+newsId).closest('div.armonico').prev();
		puls.html('CLOSE');
		puls.css({backgroundPosition: "4px 3px"});
        $.scrollTo($('#'+newsId).closest("div.armonico"), {
            offset:-200
        });
    }

}); //fine document ready

