// this tells jquery to run the function below once the DOM is ready
$(document).ready(function() {

// choose text for the show/hide link - can contain HTML (e.g. an image)
var showText='<img src="theme/arrow_blue_small.png" /> leggi tutto';
var hideText='<img src="theme/arrow_blue_small.png" /> nascondi';

// initialise the visibility check
var is_visible = false;

// append show/hide links to the element directly preceding the element with a class of "toggle"
$('.toggle').prev().append('<a href="#" class="toggleLink">'+showText+'</a>');

// hide all of the elements with a class of 'toggle'
$('.toggle').hide();

// capture clicks on the toggle links
$('a.toggleLink').click(function() {

// switch visibility
is_visible = !is_visible;

// change the link depending on whether the element is shown or hidden
$(this).html( (!is_visible) ? showText : hideText);

// toggle the display - uncomment the next line for a basic "accordion" style
$('.toggle').hide();$('a.toggleLink').html(showText);
$(this).parent().next('.toggle').slideToggle('slow');

// return false so any link destination is not followed
return false;

});
});

/*
Print Page
*/
var addPrintLink = {
	init:function(sTargetEl,sLinkText) {
		if (!document.getElementById || !document.createTextNode) {return;} // Check for DOM support
		if (!document.getElementById(sTargetEl)) {return;} // Check that the target element actually exists
		if (!window.print) {return;} // Check that the browser supports window.print
		var oTarget = document.getElementById(sTargetEl);
		var oLink = document.createElement('a');
		oLink.id = 'print-link'; // Give the link an id to allow styling
		oLink.href = '#'; // Make the link focusable for keyboard users
		oLink.appendChild(document.createTextNode(sLinkText));
		oLink.onclick = function() {window.print(); return false;} // Return false prevents the browser from following the link and jumping to the top of the page after printing
		oTarget.appendChild(oLink);
	},
/*
addEvent function included here for portability. Replace with your own addEvent function if you use one.
*/
	addEvent:function(obj, type, fn) {
		if (obj.addEventListener)
			obj.addEventListener(type, fn, false);
		else if (obj.attachEvent) {
			obj["e"+type+fn] = fn;
			obj[type+fn] = function() {obj["e"+type+fn](window.event);}
			obj.attachEvent("on"+type, obj[type+fn]);
		}
	}
};
addPrintLink.addEvent(window, 'load', function(){addPrintLink.init('article','Print this page');});

/*
Mini Modal Window
*/
// Plugin created by Daenu Probst http://codebrewery.blogspot.com/
(function ($) {
    $.fn.microModal = function (options) {
        var opts = $.extend(true, {}, $.fn.microModal.defaults, options);
        return this.each(function () {
			var obj = $(this);
			var target = '#' + getLast('target-', obj);
			var overlay = $('<div style="width:100%;height:100%;position:fixed;margin:0;padding:0;z-index:99999;top:0px;left:0px;right:0px;bottom:0px" />');
			overlay.css('background-color', opts.overlay.color);
			overlay.css('opacity', opts.overlay.opacity);
			$(target).css('position', 'absolute').css('z-index', 999999);
			var dlg = null;
			
			if(opts.overlay.show)
				dlg = new dialog($(target).clone(true), overlay.clone(true), opts.autoPositioning);
			else
				dlg = new dialog($(target).clone(true), null, opts.autoPositioning);
			$(target).remove();
			overlay.remove();
			
			$.fn.microModal.dialogs[target] = dlg;
			
			obj.click(function(e) {
				$.fn.microModal.dialogs[target].open();
				return false;
			});
        });
    };
	
	function getLast(part, element) {
        var classes = element.attr('class').split(' ');
        for (var i = 0; i < classes.length; i++) {
            if (classes[i].indexOf(part) > -1) {
                var parts = classes[i].split('-');
                return parts[parts.length - 1];
            }
        }
        return null;
    };
	
	var dialog = function(_content, _overlay, _autoPositioning) {
		this.isVisible = false;
		var content = _content;
		var overlay = _overlay;
		var autoPositioning = _autoPositioning;
		var id = '#' + content.attr('id');
		
		this.close = function() {
			$(id).remove();
			$(id + '-overlay').remove();
			this.isVisible = false;
		};
		this.open = function() {
			if(overlay != null) {
				var newOverlay = overlay.clone(true);
				newOverlay.attr('id', content.attr('id') + '-overlay');
				$('body').append(newOverlay);
			}
			var newContent = content.clone(true);
			
			$('body').append(newContent);
			
			if(autoPositioning) {
				position(newContent);
				$(window).resize(function() {
					position(newContent);
				});
				
				$(window).scroll(function() {
					position(newContent);
				});
			}
			
			this.isVisible = true;
		};
		
		var position = function(element) {
			element.css("top", ( $(window).height() - element.outerHeight() ) / 2 + $(window).scrollTop() + "px");
			element.css("left", ( $(window).width() - element.outerWidth() ) / 2 + $(window).scrollLeft() + "px");
		};
	};
	
    $.fn.microModal.defaults = {
		autoPositioning: true,
		overlay: {
			show: true,
			color: '#fff',
			opacity: 0.8
		}
    };
	
	$.fn.microModal.dialogs = new Object();
})(jQuery);
