/*
 *  core.js
 */
 
$(function(){

	galleryInit();
	expandInit();

	// Отрывать новые окна с картинкой в "красивом" окошке :)
	$('a[target=_blank][href$=.jpg]').click( function(e) {

		var $t = $(this);

		WND.openHtml( {
			wnd: {
				url: '/blank.xml', 
				w: 500,
				h: 500
			}, 
			img: {
				title: $t.attr('title') || '', 
				src: $t.attr('href')
			}
		} );

		return false;

	});

});


/**
 * Gallery
 */
function galleryInit() {
	var main = $('#gacenter');

	if( !main ) {
		return;
	}

	var g = $('.b-gallery'),
		list = $('.b-gallery_items', g),
		elements = $('a', list),
		ctrl_prev = $('.i_prev', g),
		ctrl_next = $('.i_next', g),
		t = null;

	var bAnimationLock = false;

	// pre-init
	var nRealH = 0;
	elements.each(function(i) {
		//console.log($(this).parent().get(0));
		nRealH += $(this).parent().get(0).offsetHeight;
	});

	//console.log( nRealW );

	list.height( nRealH );

	ctrl_prev.hide();
	if( elements.size() < 4 ) {
		ctrl_next.hide();
	}

	var oList = list.get(0);

	function move( prev ) {
		prev ? movePrev() : moveNext();
	};

	function movePrev() {
		if ( bAnimationLock ) {
			return;
		}

		bAnimationLock = true;

		list.animate( {"top" : "+=130px"}, 300, function(){
			if( oList.offsetTop >= 0 ) {
				ctrl_prev.hide();
			}

			if( -oList.offsetTop + 130 <= nRealH ) {
				ctrl_next.show();
			}

			bAnimationLock = false;
			//console.log(-oList.offsetLeft + 410);
		});
	};

	function moveNext() {
		if ( bAnimationLock ) {
			return;
		}

		bAnimationLock = true;

		list.animate( {"top" : "-=130px"}, 300, function(){
			if( oList.offsetTop < 0 ) {
				ctrl_prev.show();
			}

			if( -oList.offsetTop + 135 > nRealH ) {
				ctrl_next.hide();
			}

			bAnimationLock = false;

			//console.log(-oList.offsetLeft + 415);
		});
	};

	elements.click(function(e) {
		e.preventDefault();

		t = $(this);
		//var i = (new Image()).src = t.attr('href');

		main.attr('src', t.attr('href')).removeAttr('height');

		elements.removeClass('current');
		t.addClass('current');
	});

	ctrl_prev.click(function(){
		move( 1 );
	});

	ctrl_next.click(function(){
		move();
	});
}


/**
 * Expanded block
 */
function expandInit() {
	var bd = $('body');

	// Expanded blocks initialisation
	var expBlocks = $('.expblock', bd);

	if( !expBlocks ) {
		return;
	}

	expBlocks.each( function(i){
		var _that = $(this);

		/*var expControl = _that.find('.i_toggle'),
			expBody = _that.find('.expbody')*/
		var expControl = _that.children('.h_toggle'),
			expBody = _that.children('.expbody');

		//console.log(expControl);

		// Collapse all blocks with no class=expanded on init
		if( !_that.hasClass('expanded') ) {
			expControl.removeClass('ico-minus').addClass('ico-plus');

			expBody.hide();
			_that.expand = false;
		} else {
			_that.expand = true;
		}

		expControl.click( function(){
			if( _that.expand ){
				_that.expand = false;
				expBody.slideUp('fast');
				$(this).removeClass('ico-minus').addClass('ico-plus');
			} else {
				_that.expand = true;
				expBody.slideDown('fast');
				$(this).removeClass('ico-plus').addClass('ico-minus');
			}

			return false;
		});

	});
}

/**
 * WND
 */
var WND = (function() {

	// HTML шаблон нового окна
	var _sWinTpl = ' \
		<html> \
			<head> \
				<title>{{ WND_TITLE }}</title> \
				<link href="/styles/common.css" rel="stylesheet" type="text/css"> \
			</head> \
			<body> \
				<div class="window" id="main"> \
					<div id="gallery"> \
						<div class="container"> \
							<div class="header"> \
								<div style="position:relative;height:41px;background:url(/images/img/squarebracket.gif) no-repeat;"> \
									<div id="sub_title" style="position:relative;top:10px;padding-left:25px;margin-bottom:0px;"> \
										<b>{{ WND_TITLE }}</b> \
									</div> \
								</div> \
							</div> \
							<div class="image"> \
								<img alt="{{ WND_TITLE }}" id="gacenter" src="{{ IMG_SRC }}" /> \
							</div> \
						</div> \
					</div> \
				</div> \
				<script type="text/javascript"> \
					var wndimage = document.getElementById("gacenter"); \
					function resizewnd() { \
						w = (document.getElementsByTagName("img").length > 1) ? 247 : 35; \
						window.resizeTo(wndimage.width + w, wndimage.height + 150); \
					}; \
					resizewnd(); \
				</script> \
			</body> \
		</html>';

	// new window object
	var _wndObject = null;

	/**
	 * _processTpl
	 *
	 * @param {Object} oParam
	 * @return {String}
	 */
	function _processTpl( oParam ) {
		var sHTML = _sWinTpl,
			oTpl = {
				'WND_TITLE': oParam.title,
				'IMG_SRC': oParam.src
			};

		// меняем {{ PATTERN_NAME }} на соответствующие данные
		return sHTML.replace(/\{\{ ([^\}]*) \}\}/g, function( s, m1 ) {
			//console.log(oTpl[m1], m1);
			return oTpl[m1];
		});
	}

	/**
	 * _open
	 *
	 * @param {Object} oParam
	 */
	function _open( oParam ) {
		var sUrl = oParam.url,
			nWindth = oParam.w || 640,
			nHeight = oParam.h || 480,
			sName = oParam.name || 'WND_NewWindow',
			sFeatures = oParam.features || 'toolbar=no,menubar=yes,location=no,resizable=no,scrollbars=no,status=no';

		var size = 'width=' + nWindth + ',height=' + nHeight;

		if( _wndObject ) {
			_wndObject.close();
		}

		_wndObject = window.open( sUrl, sName, size + sFeatures );

		return _wndObject;
	}

	/**
	 * _openHtml
	 *
	 * @param {Object} oParam
	 */
	function _openHtml( oParam ) {
		
		// 
		_open( oParam.wnd );

		if( !_wndObject ) {
			return false;
		}

		//console.log( _processTpl(oParam.img) );
		//console.log( _wndObject );
		_wndObject.document.write( _processTpl(oParam.img) )

	}

	return {
		open: _open,
		openHtml: _openHtml
	};

})();

$(function(){

	$('#showbig').facebox();
	$('map[name=plots] area').live('click', function(e){
		$('.plot').removeShadow().hide();
		$plid = $(this).attr('href').substr(3);
		$facebox_dim = $('#facebox').offset();
		if (e.clientX+460 > $facebox_dim.left+900)
			$x = e.clientX-460;
		else
			$x = e.clientX+60;
		$y = e.clientY+100;
		//alert($x+','+$y);
		$('#pl'+$plid).css({'left':$x+'px', 'top':$y+'px'}).show();
		$('#pl'+$plid).dropShadow({left:0,top:0});
		return false;
	});
	$('.close').click(function(){
		$(this).parent().removeShadow().hide();
	});
	$('.thumbs img').click(function(){
		$(this).parents('.plimgs').children('.bimg').attr('src', $(this).attr('src'));
	});
});