$(function(){
	$('.popup, #simple-gallery a').fancybox({
		'titleShow': true,
		'transitionIn': 'elastic',
		'transitionOut': 'elastic'
	});

	// table zebra
	$('table tr:odd td').css('background','#f3f9ff');
	
	// text-shadow
	if ($.browser.msie) {
		$("#info").textShadow({x:1,y:1,radius:0,color:"#104e96"});
		$("#header h2").textShadow({x:1,y:1,radius:0,color:"#000"});
	}
	
	// kamchatka block menu
	$('#lnews h5').each(function(){
		$(this).click(function(){
			$('#lnews h5').removeClass('bl-cur');
			$('.sec').slideUp();
			var sec = $("#sec-" + $(this).attr('id').replace(/^cat\-/, '') );
			if (sec.css('display') == 'none') {
				$(this).addClass('bl-cur');
				sec.slideDown();
			}
		});
	});
	
	// feedback
	$('#message').keyup(function(){
		var isValueEmpty = ( $(this).attr('value') == '' );
		var url = location.href.replace(/\#.+$/, '').replace(/\/+$/, '') + (isValueEmpty ? '/error' : '/post');
		$('#feedback input[type="submit"]').attr('disabled', isValueEmpty);
		$('#feedback #submit span').css('display', (isValueEmpty ? 'inline' : 'none') ) ;
		$('#feedback form').attr('action', url);
	});
	
	// notice
	$('#top').prepend('<div class="notice"><div></div><p></p></div>');
	$('#lang li, #ico li').each(function(){
		$(this).mouseover(function(){
			$('.notice').stop().show().css('opacity','1');
			var alt = $(this).find('img').attr('alt');
			try {
				var href = $(this).find('a').attr('href').toString();
				alt = '<a href="'+ href +'">' + alt + '</a>';
			} catch(e) {
				var href = null;
			}
			var msgWidth = $('.notice p').html(alt).width();
			var icoWidth = $(this).width();
			var right = -10 - Math.floor(msgWidth/2);
			var parentRight = parseInt( $(this).parent().css('right') );
			var blRight = 0;
			var cur = $(this);
			while (cur.next().html() != null) {
				blRight += parseInt(cur.width()) + parseInt(cur.css('margin-left'))
						+ parseInt(cur.css('padding-left')) + parseInt(cur.css('padding-right'));
				cur = cur.next();
			}
			right += parentRight + Math.floor(icoWidth/2) + blRight;
			$('.notice').css('right', right+'px')
		});
		$(this).mouseout(function(){
			$(".notice").fadeOut('normal');
		});
		$('.notice').mouseover(function(){
			$(this).stop().show().css('opacity','1');
			
		});
		$('.notice').mouseout(function(){
			$(this).fadeOut('normal');
		});
	});
	
	
	// Tours and hunt info
	if ( $('#tours').text() != '' ) {
		// build menu
		var infoMenu = $('<ul id="tour-menu" />');
		$('.infoblock').each(function() {
			$('<li />').text( $(this).find('h2:first').text() ).appendTo(infoMenu);
		});
		infoMenu.prependTo('#tours').show();
		$('.infoblock').blox( $('#tour-menu li'), 'cur' );
	
		// build side gallery
		if ( $("#photos li").length != 0 ) {
			var photos = $('<div id="ph-prev"><h4><span>Фотогалерея тура</span></h4><ul></ul></div>');
			for (var i=0; i<3; i++) {
				$("#photos li").eq(i).clone(true).appendTo( photos.find('ul') );
			}
			$("#photos li a").attr('rel', 'smgal');
			photos.appendTo('#lnews');
		}
		$('#tour-menu li').click(function() { 
			$('#ph-prev').fadeIn(100);
		});
		$('#tour-menu li:contains("Фотогалерея")').click(function(){
			$('#ph-prev').fadeOut(100);
			
		});
		$('#ph-prev h4').click(function(){
			$('#tour-menu li').removeClass('cur');
			$('#tour-menu li:contains("Фотогалерея")').addClass('cur');
			$('#ph-prev, .infoblock').fadeOut(100);
			$("#photos").fadeIn(100);
		});
	}
	
	// excursions info
	if ( $('.ex-info').text() != '' ) {
		$('.ex-info').blox( $('#tour-menu li'), 'cur' );
		$('#tour-menu').show();
	}
	
	
	if ( $('.section').text() != '' && window.location.href.toString().indexOf('/excursion') != -1) {
		// build menu
		var listMenu = $('<ul id="tour-menu" />');
		$('.section').each(function() {
			$('<li />').text( $(this).find('h2:first').text() ).appendTo(listMenu);
		});
		
		listMenu.insertAfter($('#content h1'));
		$('<div style="clear:both; height:20px"></div>').insertAfter($('#tour-menu'));
		
		$('.section').blox( $('#tour-menu li'), 'cur' );
		
	}
});



$.fn.blox = function(link, cssClass) {
	if (cssClass == undefined) { cssClass = 'cur'; }
	var block = this;
	this.hide().eq(0).show();
	link.eq(0).addClass(cssClass);
	link.click(function(){
		link.removeClass(cssClass);
		$(this).addClass(cssClass);
		var index = $(this).parent().find(this.tagName).index(this);
		if (!block.eq(index).is(':visible')) {
			block.hide().eq(index).fadeIn(400);
		} 
		return false;
	});
	return this;
}
