
/* vc init */

var vc = {
	
	init: function() 
	{
		this.setMainMenu();
		this.setSliders();
		this.setCarousels();
        if (jQuery.browser.msie) this.setLinksOnTables();
        try{
          var links=document.getElementsByTagName("A");
          
          for (var i=0; i<links.length; i++){
            if (/^http:.*_podcast.xml/.test(links[i].href)){
                var href=links[i].href.replace("http://", "itpc://");
                links[i].href=href;
            }
          }
        } catch (e){}
	},
    
    setLinksOnTables: function(){
        jQuery.each($('a > table'), function(i,item){
            $(item).click(function(){
                $(this).css('cursor', 'pointer');
                this.parentNode.click();
            });
        });
    },
	
	setMainMenu: function() 
	{
    
        jQuery.each($('.main_menu td:not(.active)'), function(i,item){
            $(item).mouseenter(function(){
                $(this).addClass("hover");
            }).mouseleave(function(){
                $(this).removeClass("hover");
            });
        });
    
		jQuery.each($('#contentHeaderMenu > li > a'), function(i, item) {
			
			var masterLi = $(item).parent();
			$(item).hover(
				function(){ masterLi.addClass('hover'); }, 
				function(){
					setTimeout(function() {
						if(!masterLi.hasClass('keep')) masterLi.removeClass('hover');
					}, 50);
				}
			);
			
			jQuery.each($('ul.subMenu a'), function(i, e) {
				$(e).hover(
					function() { $(this).parents('li.hover:first').addClass('keep') },
					function() { $(this).parents('li.hover:first').removeClass('keep') }
				);
			});
			
			jQuery.each($('ul.subMenu'), function(i, e) {
				$(e).hover(
					function() { $(this).parents('li.hover:first').addClass('keep') },
					function() { if(!masterLi.hasClass('keep')) masterLi.removeClass('hover'); }
				);
			});
		});
		// Sottomenù
		jQuery.each($('.main_menu_sub'), function(i, item) {
			$(item).wrap('<div class="main_menu_sub_container"></div>');
			var td = $(item).parents('td:first');
			var link = td.find('a:first');
			var subcont = td.find('.main_menu_sub_container:first');
			$(link).hover(
				function() { subcont.addClass('on'); },
				function() { 
					setTimeout(function() {
						if (!subcont.hasClass('keepon')) { subcont.removeClass('on'); }
					}, 50);
				}
			);
			$(subcont).hover(
				function() { td.addClass('active'); subcont.addClass('keepon'); },
				function() { td.removeClass('active'); subcont.removeClass('keepon').removeClass('on'); }
			);
			if (i == ($('.main_menu_sub').length - 1)) {
				item.style.left = 'auto';
				item.style.right = '0'
			}
		});
	},
	
	setCarousels: function() 
	{
		jQuery.each($('.carousel'), function(e, obj) { vc.setCarouselProps(e, obj); });
	},
	
	setCarouselProps: function(j, obj)
	{
		var slider = $(obj);
		slider.orientation = ($(obj).hasClass('horizontal') ? 'h' : 'v');
		slider.container = slider.find('ul:first');
		slider.step = (slider.orientation == 'h' ? $(slider.container).width() : $(slider.container).height());
		slider.items = slider.find('li');
		slider.itemHeight = slider.find('li:first').height();
		slider.itemWidth = slider.find('li:first').width();
		slider.tot = slider.items.length;
//		slider.totHeight = slider.find('li:first').height() * slider.tot;
//		slider.totWidth = slider.find('li:first').width() * slider.tot;
		slider.windowItems = (slider.orientation == 'h' ? Math.round(slider.container.width() / slider.itemWidth) : Math.round(slider.container.height() / slider.itemHeight));
		slider.index = (slider.orientation == 'h' ? Math.round(slider.container.width() / slider.step) : Math.round(slider.container.height() / slider.step)) - 1;
		// display settings
		slider.container.wrap('<div class="slider_clip"></div>');
		slider.find('.slider_clip').height(slider.container.height())
		slider.find('.slider_clip').width(slider.container.width());
		slider.container.addClass('sliderize');		
		if(slider.orientation == 'h')
			slider.container.width(slider.tot * slider.step);
		slider.find('a.previous:first').click(function() { slider.prev(); }).removeAttr('href');
        slider.find('a.previous:first').addClass("disabled");
		slider.find('a.next:first').click(function() { slider.next(); }).removeAttr('href');
        if (slider.windowItems>=slider.tot) slider.find('a.next:first').addClass("disabled");
		slider.prev = function(offset) {
			var obj = this;
            if (offset) obj.index-=offset;
			var pos = (obj.index - obj.windowItems > 0) ? (obj.index - obj.windowItems) : 0;
			if (!obj.animating && obj.index > 0) {
				if(obj.orientation == 'v'){
					var setup = { top: -($(obj.items[pos]).position().top) + 'px'};
				} else {
					var setup = { left: -($(obj.items[pos]).position().left) + 'px'};
                }
				$(obj.container).animate(setup, 500, function() { 
                    
                    //enable next
                    slider.find('a.next:first').removeClass("disabled");
					
                    obj.animating = false;
					obj.index = pos;
                    if(obj.index == 0){
                        //disable prev
                        slider.find('a.previous:first').addClass("disabled");
                    } 
				});
			}
		}
		slider.next = function(offset) {
			var obj = this;
            if (offset) obj.index+=offset;
			if (!obj.animating && (obj.index + obj.windowItems) < obj.tot) {
                if ((obj.index + obj.windowItems*2) > obj.tot) {
                    obj.index = obj.tot-obj.windowItems*2;
                }
				if(obj.orientation == 'v'){
					var setup = { top: -($(obj.items[obj.index + obj.windowItems]).position().top) + 'px'};
				} else {
					var setup = { left: -($(obj.items[obj.index + obj.windowItems]).position().left) + 'px'};
                }
                $(obj.container).animate(setup, 500, function() { 
                
                    //enable prev
                    slider.find('a.previous:first').removeClass("disabled");

					obj.animating = false;
					obj.index += obj.windowItems;
                    if((obj.index + obj.windowItems) == obj.tot){
                        //disable next
                        slider.find('a.next:first').addClass("disabled");
                    } 
				});
			}
		}
        
        var found = slider.find('li.carousel-start:first');
        try{
            if (slider.find('li.carousel-start').length > 0) {
                
                found.each(function(){
                    $(this).show();//for ie6 bug!!!
                });
                
                var startIndex=found.prevAll().length;

                if (slider.windowItems>3 && startIndex>0){
                    for(i=1; i<=slider.windowItems/2; i++) found=found.prev();
                }

                if(obj.orientation == 'v'){
                    var setup = { top: -(found.position().top) + 'px'};
                }else {
                    var setup = { left: -(found.position().left) + 'px'};
                }
                if (startIndex>0){
                    $(slider.container).animate(setup, 500, function(){
                        slider.animating = false;
                        slider.index = startIndex;
                        slider.find('a.previous:first').removeClass("disabled");
                    });
                }
            }
        } catch (exception){
            //if (console) console.error(exception);
        }

	},
	
	setSliders: function()
	{
		jQuery.each($('.slider'), function(e, obj) { vc.setSliderProps(e, obj); });
	},
	
	setSliderProps: function(j, obj)
	{
		if ($(obj).attr('scroll')) {
			var cont = $('#' + $(obj).attr('scroll'));
			var setup = {
				slide: function(j, ui) {
					var maxScroll = $(cont).attr("scrollHeight") - $(cont).height();
                    var localValue=-ui.value * (maxScroll / 10000);
                    localValue=localValue.toFixed(0);
					$(cont).attr({
						scrollTop: localValue
					});
				},
				value: -10000,
				min: -10000,
				max: 0,
				orientation: 'vertical'
			};
			// Elemento iniziale
            try{
                var found = $(cont).find('li.slider-start');
                if (found.length > 0) {

                    found.each(function(){
                        $(this).show();//for the ie6 bug!!!
                    });

                    var localValue=500-($(cont).find('li.slider-start:first').position().top / ($(cont).attr("scrollHeight") - $(cont).height()) * 10000);
                    localValue=localValue.toFixed(0);
                    setup.value = localValue;
                }
            } catch (exception){
                //if (console) console.error(exception);
            }
			$(obj).slider(setup);
            var localValue=-0-100-setup.value * (($(cont).attr("scrollHeight") - $(cont).height()) / 10000);
            localValue=localValue.toFixed(0);
			$(cont).attr({scrollTop: localValue });
			$(obj).css('margin-top', '42px')
			$(obj).height($(cont).height() - 50).wrap('<div class="slider_scroll"></div>');
			setup.slide(0, setup);
			
		}
	}
	
}

function handleChange(e, ui) {
    

}

function handleSlide(e, ui) {
    var maxScroll = $("#scroller")
      .attr("scrollHeight") -
      $("#scroller").height();
    $("#scroller")
      .attr({ scrollTop: -ui.value
        * (maxScroll / 100)   });

}

jQuery(document).ready(function() {
    vc.init();
});