/**
 * 
 * @author Coolmen
 */

/*
if ( typeof console == 'undefined' ) {
	if ( typeof window.loadFirebugConsole == 'function' ) {
		console = window.loadFirebugConsole();
	}
}

*/

var ballon_z_index = 2;

/**
 * Класс баллона
 * @param {} el 
 */
function Ballon ( el, options ) {

	var show_speed = 'fast';
	var hide_speed = 'slow';
	
	var ballon		= $(el);
	var controller	= ballon.children('.controller');
	var container	= ballon.children('.ballon-container'); 
	var link		= ballon.find('.wrapper > a').attr('href');
	var image_box	= $('#hat-image');
	
	/**
	 * Статус баллон:
	 *		true	-- открыт
	 *		false	-- закрыт
	 * @type bullean 
	 */
	var status		= null;
	/**
	 * Перегружать при клике?
	 *		true	-- да	(нет блока #contacts-content-box)
	 *		false	-- нет 	(есть блок #contacts-content-box)
	 * @type bullean
	 */
	var reloaded	= null;
	/**
	 * Показывать картинку?
	 *		true	-- да	(есть блока #hat-image)
	 *		false	-- нет 	(нет блока #hat-image)
	 */
	var images		= null;

	
	/**
	 * создание баллона
	 */
	this.init = function () {
//		ballon.css( 'border', '1px dashed red' );
//		controller.css( 'border', '1px dashed black' );
//		controller.css( 'background-color', 'orange' );
//		container.css( 'border', '1px dashed green' );
//		ballon.children('.point').css( 'border', '1px dashed blue' );
//		ballon.children('.point').css( 'background-color', '#fff' );

		status		= ! ballon.hasClass('hidden');
		reloaded	= ! $("#contacts-content-box").is("#contacts-content-box");
		images		= image_box.is('#hat-image');

		if ( status ) {
			ballon.css( 'z-index', ballon_z_index++ );
		}
		if ( images ) {
			ballon.find('.wrapper > a').hover( this.show_img, this.hide_img );
		}
		else {
			controller.hover( this.show, this.hide );
			controller.click( this.click );
		}
	};
	

	/**
	 * Показываем изображеение точки
	 * 
	 */
	this.show_img = function () {
		// Подгружаю картинку
		var src = container.find('img').attr('src');
		// Если есть картинка
		if ( typeof(src) != 'undefined' ) {
			image_box.find('.img-box img').attr('src', src );
			var img_width	= image_box.show().find('.img-box img').width() + parseInt( ballon.css('margin-left') );
			if ( ballon.hasClass('left') ) {
				img_width -= container.width();
			}
			var img_height	= image_box.find('.img-box img').height();
			image_box.hide();
			image_box.stop().show().children('.img-box').css({
				'top': ( container.offset().top - img_height ) + 'px',
				'left': ( container.offset().left - img_width ) + 'px'
			});
			image_box.stop().fadeTo(0,1).fadeIn('fast');
		}
	}

	
	/**
	 * Прячим изображения 
	 */
	this.hide_img = function () {
//		console.info( 'hide_img' );
		image_box.stop().fadeOut('slow').hide();
	}

	
	/**
	 * Показываем баллон
	 * @return {Boolean}
	 */
	this.show = function () {
		// Если баллон спрятал показываем его
		if ( !status ) {
			hide_all();
			status = true;
			ballon.removeClass('hidden');
			ballon.css( 'z-index', ballon_z_index++ );
			container.stop().fadeTo(0,1).hide().fadeIn( show_speed,
				function() {
					container.show();
				});
		}
		return false;
	};
	
	/**
	 * Прячим баллон
	 * @return {Boolean}
	 */
	this.hide = function () {
		if ( status ) {
			status = false;
			container.stop().fadeOut( hide_speed,
				function() {
					ballon.addClass('hidden');
				});
		}
		return false;
	};

	/**
	 * Обработчик клика
	 */
	this.click = function () {
		window.location = link;
		return false;

		if ( status && link && reloaded ) {
			window.location = link;
		}
		else {
			$.get("/contacts/", { link: link }, function (data) {
				$("#contacts-content-box").slideUp( 'fast', function () {
					$(this).html(data).slideDown('fast', function () {
						if ( $.jcarousel ) {
//							$.jcarousel.reset();
							$('.gallery-box > ul').jcarousel({ scroll: 1 });
						}
						tb_init('a.thickbox, area.thickbox, input.thickbox');
					});
				});
			});
		}
	};

	/**
	 * Прячим все открытые баллоны
	 */
	function hide_all () {
		$('.ballon').each( function () {
				this.Ballon.hide();
			});
	}

	this.init();
}

/**
 * Морда jQuery
 * @param {} options
 */
jQuery.fn.ballons = function( options ) {
	this.each( function () {
			this.Ballon = new Ballon( this );
		});
};


var tb_pathToImage = "/img/progress.gif";

/**
 * Карта
 */
$(document).ready( function () {
	// Баллоны
	$('.ballon').ballons();
	// Галлерея
	if ( $.jcarousel ) {
		$('.gallery-box > ul').jcarousel({ scroll: 1 });
	}
});


