/** 
 *    Common scripts file
 *    @version: 1.0 
 */

window.addEvent('domready', function() {     
    //loadAccordion('accordion', 'h3.domanda', 'div.risposta');    
    //loadGallery('gallery', 570);
    emailProtection('.email');
    //printButtons('.print');
    //loadNavbar('#nav ul.main li.item');
    //new SmoothScroll({ duration:700 }, window);
    //var myMenu = new UvumiDropdown("drop_down_menu");
    loadSqueeze('a.boxed');    
});


window.addEvent('load', function() {
    if ($chk($('gallery'))) {
        var nS = new noobSlide({
            box: $('imglist'),
            
            size:570,
            items: $$('#imglist span'),                       
            button_event: 'click',            
            handles: $$('#handles span'),
            onWalk: function(currentItem,currentHandle){
                this.handles.removeClass('active');
                currentHandle.addClass('active');
            }
        });
    }
});


/**
 *    Funzione per la gestione dello scrolling
 *    @require mootools core
 *    @version 1.0
 */
function loadScroller(el)
{
    if ($(el)) {
        if ($(el).getScrollHeight() <= $(el).getHeight()) {
            $('scrollUp').addClass('hidden');
            $('scrollDown').addClass('hidden');
        } else {        
            var myScroller = new Scroller(el, {velocity: 0.2});
            myScroller.start(); 
        }
    }
}

/**
 *    Funzione per la gestione del mouseover/mouseout
 *  sulla navbar (compatibilità con ie6)
 *    @require mootools core
 *    @version 1.0
 */
function loadNavbar(els)
{
    $$(els).addEvents({
        'mouseover': function(e){
            e = new Event(e).stop();
            this.addClass('hover');
        },
        'mouseout': function(e){
            e = new Event(e).stop();
            this.removeClass('hover');
        }
    }); 
}

/**
 *    Funzione per lo zoom delle foto 
 *    @require mootools core/more, squeezebox  
 *    @version 1.0
 */
function loadSqueeze(elClass)
{
    SqueezeBox.assign($$(elClass), { 
        //parse: 'rel'
        handler: 'iframe',
        size: {x: 500, y: 500}
    });
}

/**
 *    Funzione per l'offuscamento degli indirizzi eMail
 *    @require mootools core 
 *    @version 1.1
 */
function emailProtection(elClass)
{
    $each($$(elClass), function(el) {
        var mail = el.get('text').replace('[AT]','@');
        var text = el.get('title') || mail;
        var a = (new Element('a').setProperty('href', 'mailto:' + mail).addClass(elClass.substring(1)).set('text', text)).replaces(el);
    });
}

/**
 *    Funzione per l'aggiunta dei pulsanti di stampa 
 *    @require mootools core
 *    @version 1.0
 */
function printButtons(elClass)
{
    $each($$(elClass), function(el) {
        el.addEvent('click', function(e){
            e = new Event(e).stop();
            window.print();
        });
    });
}

/**
 *    Funzione per la gestione degli accordion 
 *    @require mootools core/more
 *    @version 1.0
 */
function loadAccordion(id, togglers, stretchers) 
{
    if ($(id)) {
        var myAccordion = new Accordion($(id), togglers, stretchers, {
            opacity: true,
            show: false,
            alwaysHide: true,
            wait: true,
            onActive: function(toggler, element){
                toggler.setStyle('color', '#02390C');
                element.setStyle('border-bottom', '1px dotted #AEAEAE');
                element.setStyle('padding-bottom', '10px');
            },
            onBackground: function(toggler, element){
                toggler.setStyle('color', '#967117');
                element.setStyle('padding-bottom', '0');
                element.setStyle('border-bottom', '0 none');
            }
        });    
    }
}

/**
 *    Funzione per l'aggiunta della gallery 
 *    @require mootools core/more, noobslide 
 *    @version 1.0
 */
function loadGallery(el, w) 
{
    if ($(el)) {
        
        var box = $(el).getFirst();
        var items = $(el).getNext().getElements('span');
        var handles = items;
        
        if (items.length > 0) {        
            var hs = new noobSlide({
                box: box,
                size: w,
                items: items,
                handles: handles,
                interval: 10000,
                autoPlay: false,
                onWalk: function(currentItem,currentHandle){
                        $$(this.handles).removeClass('active');
                        $(currentHandle).addClass('active');
                }
            });
            hs.walk(0)    
        }
    }
}

/**
 *    Funzione per l'aggiunta di una mappa google
 *    @require mootools core/more, noobslide 
 *    @version 1.0
 */
function loadGmap1(div, kmlUrl, mapPointStr) {
    if (GBrowserIsCompatible()) {
      mapZoom = parseInt('13');
      mapType = parseInt('1');
      mapScale = parseInt('1');
      mapControl = 'large';
      mapOverview = parseInt('1');      
      mapPoint = mapPointStr.split(",");
      mapDiv = $(div);

      map = new GMap2(mapDiv);
        if(mapType) {
          map.addControl(new GMapTypeControl());
        }
        if(mapScale) {
          map.addControl(new GScaleControl());
        }
        if(mapControl == 'small') {
          map.addControl(new GSmallMapControl());
        } else {
          map.addControl(new GLargeMapControl());
        }
        if(mapOverview) {
          map.addControl(new GOverviewMapControl());
        }
        
        lat = parseFloat(mapPoint[0]);
        lng = parseFloat(mapPoint[1]);
        center = parseFloat(mapPoint[2]) ? parseFloat(mapPoint[2]) : mapZoom;
        
        map.setCenter(new GLatLng(lat, lng), center);
        
        if(kmlUrl != '') {
          var geo = new GGeoXml(kmlUrl);
          map.addOverlay(geo);
        } else {
          var marker = new GMarker(new GLatLng(lat, lng));
          map.addOverlay(marker);
        }
        
    }
}

function condividi(via)
{
	var url,
		pageUrl = encodeURIComponent(window.location),
		pageTitle = encodeURIComponent(document.title);
		
	switch(via){
		case 'facebook':
			url = 'http://www.facebook.com/sharer.php?u=' + pageUrl + '&amp;t=' + pageTitle;
		break;
		
		case 'tweet':
			url = 'http://twitter.com/home?status=' + pageTitle + ':+' + pageTitle;
		break;

		case 'buzz':
			url = 'http://www.google.com/reader/link?url='+ pageUrl +'&title='+ pageTitle;
		break;
		
		case 'delicious':
			url = 'http://del.icio.us/post?url='+ pageUrl +'&title='+ pageTitle;
		break;
	
		case 'friendfeed':
			url = 'http://friendfeed.com/share/bookmarklet/frame#title=' + pageTitle + '&url=' + pageUrl;
		break;
					
		case 'oknotizie':
			url = 'http://oknotizie.virgilio.it/post?url='+ pageUrl +'&title='+ pageTitle;
		break;
	}
	//console.log(url);
	window.open(url, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no');	
}


