var $j = jQuery.noConflict();

var larg = screen.width;
var alt = screen.height;






function altim(){
	var larg = $j(window).width();
	var alt = $j(window).height();
	if (alt < 850)
	{
		/*$j('.main-img').css("height", "400px");
		$j('.home-img').css("height", "400px");*/
		$j('.main-img').css({height: ((alt/3)+120) });
		$j('.home-img').css({height: ((alt/3)+120) });
		$j('.slide img').css({height: ((alt/3)+120) });
		
	} else {
		$j('.main-img').css("height", "");
		$j('.home-img').css("height", "");
		$j('.slide img').css("height", "");
	/*	$j('#conten-menu').css({height: ((alt/2)+365) });
		$j('#menu').css({height: ((alt/2)+125) });*/
		}	
}




$j.preLoadImages = function(imageList,callback) {
	var pic = [], i, total, loaded = 0;
	if (typeof imageList != 'undefined') {
	if ($j.isArray(imageList)) {
	total = imageList.length; // used later
	for (i=0; i < total; i++) {
	pic[i] = new Image();
	pic[i].onload = function() {
	loaded++; // should never hit a race condition due to JS's non-threaded nature
	if (loaded == total) {
	if ($j.isFunction(callback)) {
		callback();
	}
	}
	};
	pic[i].src = imageList[i];
	}
	} else {
	pic[0] = new Image();
	pic[0].onload = function() {
	if ($j.isFunction(callback)) {
	callback();
	}
	}
	pic[0].src = imageList;
	}
	}
	pic = undefined;
};


function handleSliderChange(e, ui)
{
  var maxScroll = $j("#slide").attr("scrollWidth") - $j("#slide").width();
  $j("#slide").animate({scrollLeft: ui.value * (maxScroll / 100) }, 10);
}

function handleSliderSlide(e, ui)
{
  var maxScroll = $j("#slide").attr("scrollWidth") - $j("#slide").width();
  $j("#slide").attr({scrollLeft: ui.value * (maxScroll / 100) });
  //$j('.ui-slider-handle').css('left', ui.value+'px');
}


// scroll menu +++++++++++++++++++++++++++++++++++++

function makeScrollable(wrapper, scrollable){
    // Get jQuery elements
    var wrapper = $j(wrapper), scrollable = $j(scrollable);
    
    // Hide images until they are not loaded
    scrollable.hide();
    var loading = $j('<div class="loading">Loading...</div>').appendTo(wrapper);
    
    // Set function that will check if all images are loaded
    var interval = setInterval(function(){
        var images = scrollable.find('img');
        var completed = 0;
        
        // Counts number of images that are succesfully loaded
        images.each(function(){
            if (this.complete) completed++;    
        });
        
        if (completed == images.length){
            clearInterval(interval);
            // Timeout added to fix problem with Chrome
            setTimeout(function(){
                
                loading.hide();
                // Remove scrollbars    
                wrapper.css({overflow: 'hidden'});                        
                
                scrollable.slideDown('slow', function(){
                    enable();    
                });                    
            }, 1000);    
        }
    }, 100);
    
    function enable(){
        // height of area at the top at bottom, that don't respond to mousemove
        var inactiveMargin = 99;                    
        // Cache for performance
        var wrapperWidth = wrapper.width();
        var wrapperHeight = wrapper.height();
        // Using outer height to include padding too
        var scrollableHeight = scrollable.outerHeight() + 2*inactiveMargin;
        // Do not cache wrapperOffset, because it can change when user resizes window
        // We could use onresize event, but it's just not worth doing that 
        // var wrapperOffset = wrapper.offset();
        
        // Create a invisible tooltip
        var tooltip = $j('<div class="sc_menu_tooltip"></div>')
            .css('opacity', 0)
            .appendTo(wrapper);
    
        // Save menu titles
        scrollable.find('a').each(function(){                
            $j(this).data('tooltipText', this.title);                
        });
        
        // Remove default tooltip
        scrollable.find('a').removeAttr('title');        
        // Remove default tooltip in IE
        scrollable.find('img').removeAttr('alt');    
        
        var lastTarget;
        //When user move mouse over menu            
        wrapper.mousemove(function(e){
            // Save target
            lastTarget = e.target;

            var wrapperOffset = wrapper.offset();
        
            var tooltipLeft = e.pageX - wrapperOffset.left;
            // Do not let tooltip to move out of menu.
            // Because overflow is set to hidden, we will not be able too see it 
            tooltipLeft = Math.min(tooltipLeft, wrapperWidth - 75); //tooltip.outerWidth());
            
            var tooltipTop = e.pageY - wrapperOffset.top + wrapper.scrollTop() - 40;
            // Move tooltip under the mouse when we are in the higher part of the menu
            if (e.pageY - wrapperOffset.top < wrapperHeight/2){
                tooltipTop += 80;
            }                
            tooltip.css({top: tooltipTop, left: tooltipLeft});                
            
            // Scroll menu
            var top = (e.pageY -  wrapperOffset.top) * (scrollableHeight - wrapperHeight) / wrapperHeight - inactiveMargin;
            if (top < 0){
                top = 0;
            }            
            wrapper.scrollTop(top);
        });
        
        // Setting interval helps solving perfomance problems in IE
        var interval = setInterval(function(){
            if (!lastTarget) return;    
                                        
            var currentText = tooltip.text();
            
            if (lastTarget.nodeName == 'IMG'){                    
                // We've attached data to a link, not image
                var newText = $j(lastTarget).parent().data('tooltipText');

                // Show tooltip with the new text
                if (currentText != newText) {
                    tooltip
                        .stop(true)
                        .css('opacity', 0)    
                        .text(newText)
                        .animate({opacity: 1}, 1000);
                }                    
            }
        }, 200);
        
        // Hide tooltip when leaving menu
        wrapper.mouseleave(function(){
            lastTarget = false;
            tooltip.stop(true).css('opacity', 0).text('');
        });            
        
        /*
        //Usage of hover event resulted in performance problems
        scrollable.find('a').hover(function(){
            tooltip
                .stop()
                .css('opacity', 0)
                .text($j(this).data('tooltipText'))
                .animate({opacity: 1}, 1000);
    
        }, function(){
            tooltip
                .stop()
                .animate({opacity: 0}, 300);
        });
        */            
    }
}
    
$j(function(){    
    makeScrollable("div.sc_menu_wrapper", "div.sc_menu");
});

