$(function() {
	var data = $("#bannerHome").metadata({type:'elem',name:'script'});
	
    $('img.on_off', "#pulsantiSlide").live('mouseover', function(event){ 
        $(event.target).attr("src", $(event.target).attr("src").replace(/_OFF/g, "_ON"));
    });
    
    $('img.on_off', "#bannerHome").live('mouseout', function(event){
    	$(event.target).attr("src", $(event.target).attr("src").replace(/_ON/g, "_OFF"))
    });
    
    $('#slideshow').before('<div id="nav" class="nav">').cycle({
        fx:     'fade',
        speed:  'slow',
        timeout: data.timeout ? parseInt(data.timeout, 10) : 3000,
        pager:  '#navigation ul',
        pagerAnchorBuilder: function(idx, slide) {
            return '<li><a href="#" title="Thumbnail"><img class="on_off" src="'+data.numeri+'/' + (idx+1) + '_OFF.png"></img></a></li>';
        },
        updateActivePagerLink: function(pager, currSlide, clsName) {
        	$('#slideshow').css({visibility:'visible'});
        	
        	$(pager).find('img').each(function(i, v){
                $(this).attr('src', data.numeri + '/' + (i+1) + '_OFF.png').attr("class", "");
            });
            $(pager).find('img:eq('+currSlide+')').attr('src', data.numeri + '/' + (currSlide+1) + '_ON.png').attr("class", "activeSlide");
        }
    });
    
    $("#pulsantiSlide a.playpause").toggle(function(){
        $('#slideshow').cycle("pause");
        
        var src = $("img", this).attr('src');
        var end = src.match("_.*$");
        
        $("img", this).attr('src', data.pulsanti + '/play' + end);
    }, function(){
        $('#slideshow').cycle("resume");
        
        var src = $("img", this).attr('src');
        var end = src.match("_.*$");
        
        $("img", this).attr('src', data.pulsanti + '/pause' + end);
    });
    // POPIN HOME
    
	if($("#popinHome").length && !$.cookie("popinHome")){
		$('#popinHome').dialog({
	    	autoOpen: true,
	    	resizable: false,
			height:  parseInt($('#popinHome').metadata().h),
			width:   parseInt($('#popinHome').metadata().w),
			modal: true,
			position: 'center'
		});
		$.cookie("popinHome","1");
	}

	// POPIN MOBILE
	/*
	if($('#mobilepopin').attr('data-show') == '1'){
		$('#mobilepopin').dialog({
	    	autoOpen: true,
	    	resizable: false,
			height:  parseInt($('#mobilepopin').metadata().h),
			width:   parseInt($('#mobilepopin').metadata().w),
			modal: true,
			position: 'center'
		});
	}
	
	$('.chiuditi').click(function(){
		$('#mobilepopin').dialog('close');
		return false;
	});
	*/
});
