var showAll = false;

(function($) {
    $.fn.tipsy = function(opts) {

        opts = $.extend({fade: false, gravity: 's'}, opts || {});
        var tip = null, cancelHide = false;

        this.hover(function(event) {
            $.data(this, 'cancel.tipsy', true);

            var tip = $.data(this, 'active.tipsy');
            if (!tip) {
                tip = $('<div class="tipsy"><div class="tipsy-inner">' + $(this).attr('title') + '</div></div>');
                tip.css({position: 'absolute', zIndex: 100000});
                $(this).attr('title', '');
                $.data(this, 'active.tipsy', tip);
            }
            var mouseX = event.pageX;
            var mouseY = event.pageY;
            var pos = $.extend({}, $(this).offset(), {width: this.offsetWidth, height: this.offsetHeight});
                       
            
            if ($(this)[0].tagName.toLowerCase() == 'area')	
			{
			
				var coords = $(this).attr("coords").split(","); 
				var max_l = 0, min_l = 9999, max_t = 0, min_t = 9999;
				for(i=0; i<coords.length; i++){
					if(i%2 == 0){					
						max_l = (max_l < Number(coords[i]))?Number(coords[i]):max_l;
						min_l = (min_l > Number(coords[i]))?Number(coords[i]):min_l;
					}
					else {
						max_t = (max_t < Number(coords[i]))?Number(coords[i]):max_t;
						min_t = (min_t > Number(coords[i]))?Number(coords[i]):min_t;
					}
				}
				var posLeft = Math.floor((max_l+min_l)/2);
				var posTop = Math.floor(min_t); 				
			}      
			
			 tip.remove().css({top: posTop+'px', left: posLeft+'px', visibility: 'hidden', display: 'block'}).appendTo($('#wrapper'));
            var actualWidth = tip[0].offsetWidth, actualHeight = tip[0].offsetHeight;
            
            switch (opts.gravity.charAt(0)) {
                case 'n':
                    tip.css({top: max_t+5, left: posLeft-actualWidth / 2}).addClass('tipsy-north');
                    break;
                case 's':
                    tip.css({top: posTop - actualHeight-5, left: posLeft-actualWidth/2}).addClass('tipsy-south');
                    break;
                case 'e':
                    tip.css({top: Math.floor((max_t+min_t)/2) + actualHeight / 2, left: min_l-actualWidth}).addClass('tipsy-east');
                    break;
                case 'w':
                    tip.css({top: Math.floor((max_t+min_t)/2) - actualHeight / 2, left: max_l}).addClass('tipsy-west');
                    break;
            }

            if (opts.fade) {
                tip.css({opacity: 0, display: 'block', visibility: 'visible'}).animate({opacity: 1});
            } else {
                tip.css({visibility: 'visible'});
            }

        }, function() {
            $.data(this, 'cancel.tipsy', false);
            var self = this;
            setTimeout(function() {
                if ($.data(this, 'cancel.tipsy')) return;
                var tip = $.data(self, 'active.tipsy');
                if (opts.fade) {
                    tip.stop().fadeOut(function() { $(this).remove(); });
                } else {
                    tip.remove();
                }
            }, 100);
            
        });

    };
})(jQuery);




function showAllTips(link) {
	if (!showAll) {
		showAll = true;
		$(link).text('Приховаті всі підказки');
		$('area').mouseover();
	} else {
		showAll = false;
		$(link).text('Показати всі підказки');
		$('area').mouseout();	
	}
}

$(function () {
	var img = new Image();
	var originalImage = $('img.preload')[0];

  	$(img).attr('src', $(originalImage).attr('src'))
  	.load(function () {
    	//place in the middle and show
    	if (($(document).height() - img.height)/2 > 0)
			$('#wrapper').css('margin-top', ($(document).height() - img.height) / 2);
		$('#wrapper').fadeIn();
		$('body').css('background-image','none');

    })
    // if there was an error loading the image, react accordingly
    .error(function () {
      // notify the user that the image could not be loaded
      alert('Виникла помилка під час завантаження зображення');
    })

    ;
});



		
		
	