$(document).ready( function() {
	
	$("#popup").tabs();
	$("#popup").center();
    //
	$("#navigation ul li a").tooltip({position: "bottom, center"});
	
	$("#google-map-api a.map-icon").hover(function() {
		$("#google-map-api .balloon").css({ 'top':$(this).position().top-130,'left':$(this).position().left+20 }).show();
	}, function (e) {
		dialog_object = $(this);
		$(document).bind('mouseout', monitor_close_balloon);
	});
	
	function monitor_close_balloon(e) {
		if ($(e.relatedTarget).hasClass('balloon')) {
			return;
		}
		if ($(e.relatedTarget).parents('.balloon').length !== 0) {
			return;
		}
		$('.balloon').hide();
	}
	
	$("#google-map-api a.map-icon").live('click', function(){
		$("#google-map-api .balloon").css({ 'top':$(this).position().top-130,'left':$(this).position().left+30 }).toggle(); 
		return false;
	});
	$("#google-map-api .balloon .close-button").live('click', function(){
		$("#google-map-api .balloon").hide();
	});
	
	
	$("#tabs").tabs();
	/*
	$("#tabs li").live('click', function(){ 
		if( $(this).hasClass("li-list")) {
			$("ul#tab-menu li.view-results").show();
		}
		else {
			$("ul#tab-menu li.view-results").hide();
		}
	});
	*/
	
	$('#mycarousel').jcarousel({
		scroll: 1,
		wrap: 'both'
	});
	
	//accordion
	$(".accordion").accordion({ active: 0 });
	
	$('.vertical-slider .arrow').live('click', function() {
        var slider = $(this).parents('.vertical-slider:eq(0)');
        var fragments = slider.find('.fragment');
        var container = slider.find('.fragment-mover');
        
        var currentPage = slider.data('position');
        if (!currentPage) {
            currentPage = 1;
        }
        var fragments_count = fragments.length;
        var fragmet_height = fragments.eq(0).height();
        var perPage = 1;
        var numPages = Math.ceil(fragments_count/perPage);
        var stepMove = fragmet_height*perPage;
        var firstPosition = 0;
        var lastPosition = -1*((numPages-1)*stepMove);
        
        if ($(this).hasClass('arrow-bottom')) {
            currentPage ++;
            if (currentPage > numPages) {
                slider.data('position', 1);
                container.animate({'top': firstPosition});
                return false;
            };
            container.animate({'top': -((currentPage - 1)*stepMove)});
        };
        if ($(this).hasClass('arrow-top')) {
            currentPage --;
            if (currentPage < 1) {
                slider.data('position', numPages);
                container.animate({'top': lastPosition});
                return false;
            };
            container.animate({'top': -((currentPage-1)*stepMove)});
        };
        slider.data('position', currentPage);
        
        return false;
    });
	
});

jQuery.fn.center = function(loaded) {
    var obj = this;
    body_width = parseInt($(window).width());
    body_height = parseInt($(window).height());
    block_width = parseInt(obj.width());
    block_height = parseInt(obj.height());
    
    left_position = parseInt((body_width/2) - (block_width/2)  + $(window).scrollLeft());
    if (body_width<block_width) { left_position = 0 + $(window).scrollLeft(); };
    top_position = parseInt((body_height/2) - (block_height/2) + $(window).scrollTop());
    if (body_height<block_height) { top_position = 0 + $(window).scrollTop(); };
    
    if(!loaded) {
        
        obj.css({'position': 'absolute'});
        obj.css({'left': left_position, 'top': top_position});
        $(window).bind('resize', function() { 
            obj.center(!loaded);
        });
        
    } else {
        obj.stop();
        obj.css({'position': 'absolute'});
        obj.animate({'left': left_position, 'top': top_position}, 200, 'linear');
    }
}