$(document).ready(function() {
    
    $('a.print').live('click', function(){
       window.print();
       return false;
    });
    
    $('a.back').live('click', function(){
       history.go(-1); return false;
    });
    
    $(".show_tooltip").each(function (){
        $(this).tooltip({ 
            position: "top left", 
            tip: $(this).attr('rel')/*,
            opacity: 0.9*/
        });
    });

    $("ul.nav > li").hover (function () {
        $(this).find("ul").show();
    },
    function () {
        $(this).find("ul").hide();
    });

    $("ul#menu_language li a").live ('click', 
        function () {
            
            var self = this;
            
            $.post("/internals/change_language/", 
            {
                id: $(self).attr("rel")
            },
            function (link) {
                //redirect
                document.location.href = link;
                });
            
            return false;
            
        });
    
    $('.confirm').live('click', function(){
        if (!confirm($(this).attr('rel'))) 
            return false; 
    });
    
    $('form').each(function(){
       $(this).attr('action', $(this).attr('rel')); 
       if ($(this).is('.validate'))
           $(this).validate({
               errorElement: 'legend',
               onkeyup: false
           });
    });
   
   if ($('div.players').length){
       init_flowplayer();
   }
   
   
   var swf_width = 970;
   var swf_height = 600;
   
   var ratio = swf_width / swf_height;
   
   if ($(window).width()/$(window).height() < ratio){
       if ($(window).width()*0.8 < swf_width){
           swf_width = Math.round($(window).width()*0.8);
           swf_height = Math.round(swf_width/ratio);
       }
   }
   else {
       if ($(window).height()*0.8 < swf_height){
           swf_height = Math.round($(window).height()*0.8);
           swf_width = Math.round(swf_height*ratio);
       }
   }

   $("a.open_swf").fancybox({
       width: swf_width, /*$(window).width()*0.8,*/
       height: swf_height, /*$(window).height()*0.8,*/
       type: 'swf'
    });
    
    $("a.open_dialog").fancybox({
//        width: 500,
//        height: 300,
//        autoDimensions: false,
        onComplete: function(){      
                        
            var form = $('#fancybox-content').find('form');
            
            if (form){
                
                if (form.is('.validate')) form.validate({errorElement: 'legend', onkeyup: false});
                form.attr('action', $('#fancybox-content').find('form').attr('rel'));
                form.live('submit', function() {
                    $(this).ajaxSubmit({
                        type: 'post',
                        success: function(response){
                            $('#fancybox-content').html(response);
                        }
                    });
                    return false; 
		});
                
            }
            
        }
    });
    
    
    /**
     * ANALYTICS
     */
    
    $("div.newsletter > form > input[type=submit]").click(function () {
        _gaq.push(['_trackEvent', 'Interazione', 'Newsletter', 'Step1']);
    });
    
});

var init_flowplayer = function (){

    try{

        flowplayer('div.players', {
            src: "/js/flowplayer/flowplayer-3.2.7.swf",
            wmode: 'transparent'
        }, {
            clip:  {
                autoBuffering: false
            },
            plugins: {			// load one or more plugins
                controls: {			// load the controls plugin
                    url: '/js/flowplayer/flowplayer.controls-3.2.5.swf' //,	// always: where to find the Flash object
                }
            }

        });

    } catch(e) {console.log(e);}




}


