swfobject.embedSWF('/public/flash/swfHeader.swf', 'swfHeader', '100%', '115', '9.0.0', '/public/flash/expressInstall.swf', false, {wmode: 'opaque'});

var mdlAudioVideoInit = function()
{
	$('.mdlAudio').each(function()
	{
		var id = 'mdlAudio_'+Math.floor(Math.random()*1000000);
		this.firstChild.id = id;
		var params = eval('('+$(this).attr('name')+')');
		swfobject.embedSWF('/public/flash/player.swf', id, params.w, params.h || '20', '9.0.0', '/public/flash/expressInstall.swf', params, {wmode: 'opaque'});
	});
};

var toggleSitemap = function()
{
	$('.mdlSitemap').slideToggle();
	$('.mdlOverlay').toggle();
};

var selectDate = function(elem)
{
	$(elem).datepicker('dialog' , $(elem).attr('name'),
		function(txt, dp)
		{
			var l = document.location;
			l.href = 'http://'+l.hostname+l.pathname+'?year='+dp.selectedYear+'&month='+(dp.selectedMonth+1);
		}, {changeMonth: true, changeYear: true});
	return false;
};



// *** mdlShare ***
var share = function(elem, url, title, desc)
{
	if(elem.isShare) return true;
	
	var cont   = $(elem).closest('.mdlShare');
	var surl   = cont.find('.url').html();
	var stitle = cont.find('.title').html();
	var sdesc  = cont.find('.desc').html();
	
	
	var href   = '';
	if(url)   href+='&' + url   + '=' + encodeURIComponent(surl);
	if(title) href+='&' + title + '=' + encodeURIComponent(stitle);
	if(desc)  href+='&' + desc  + '=' + encodeURIComponent(sdesc);
	
	elem.href	 = elem.href.indexOf('?')==-1 ? elem.href + '?' + href : elem.href + href;
	elem.isShare = true;
	return true;
};


// *** darkbox ***
var darkbox_cb = function()
{
	var link = $(this);		
	if(!$('div.darkbox-frame').length) {					
		darkbox = $('<div class="darkbox-frame"><div class="darkbox-shadow"></div><div class="darkbox-canvas"><div class="darkbox-button"></div></div></div>').appendTo('body');
	}
	var frame = darkbox.clone().appendTo('body').addClass('darkbox-frame-on');
	var shadow = frame.find('div.darkbox-shadow').animate({opacity:0.6},300);
	var canvas = frame.find('div.darkbox-canvas');
	var button = frame.find('div.darkbox-button');
	var image = $('<img src="'+ link.attr('href') +'" alt="'+ link.attr('title') +'"/>');

	image.appendTo(canvas);
	var protect = $('<img class="protect" src="/public/images/blank.gif" alt=""/>');
	protect.appendTo(canvas);
	image.load(function(){

		var imageWidth = image.width();
		protect.width(imageWidth);
		var imageHeight = image.height();
		protect.height(imageHeight);
		protect.css('marginTop', '-'+imageHeight+'px');
		var frameWidth = frame.width()-40;
		var frameHeight = frame.height()-40;

		if(imageWidth > frameWidth) {
			imageWidth = frameWidth;
			image.width(imageWidth);
			protect.width(imageWidth);
			while(image.height() > frameHeight) {
				image.width(imageWidth);
				protect.width(imageWidth);
				imageWidth--;
			}

			imageHeight = image.height();
		}

		if(imageHeight > frameHeight) {
			imageHeight = frameHeight;
			image.height(imageHeight);
			protect.height(imageHeight);
			protect.css('marginTop', '-'+imageHeight+'px');
			while(image.width() > frameWidth) {
				image.height(imageHeight);
				protect.height(imageHeight);
				protect.css('marginTop', '-'+imageHeight+'px');
				imageHeight--;
			}

			imageWidth = image.width();
		}

		canvas.addClass('darkbox-canvas-load').animate({
			width:imageWidth,
			marginLeft:-imageWidth/2,
			height:imageHeight,
			marginTop:-imageHeight/2
		},500,function() {
			canvas.addClass('darkbox-canvas-done');
			button.addClass('darkbox-button-on');
			button.addClass(navigator.platform.toLowerCase().indexOf('mac')+1?'darkbox-button-left':'darkbox-button-right');

			image.animate({opacity:1},500,function() {
				shadow.click(closer);
				button.click(closer);
			});
		});
	});

	var closer = function() {
		canvas.remove();
		shadow.animate({opacity:0},300,function() {
			frame.remove();
		});
	}

	$(document).keydown(function(e) {
		if(e.which==27) closer();
	});

	return false;
};


/* Russian (UTF-8) initialisation for the jQuery UI date picker plugin. */
/* Written by Andrew Stromnov (stromnov@gmail.com). */
jQuery(function($){
	$.datepicker.regional['ru'] = {
		closeText: 'Закрыть',
		prevText: '&#x3c;Пред',
		nextText: 'След&#x3e;',
		currentText: 'Сегодня',
		monthNames: ['Январь','Февраль','Март','Апрель','Май','Июнь',
		'Июль','Август','Сентябрь','Октябрь','Ноябрь','Декабрь'],
		monthNamesShort: ['Янв','Фев','Мар','Апр','Май','Июн',
		'Июл','Авг','Сен','Окт','Ноя','Дек'],
		dayNames: ['воскресенье','понедельник','вторник','среда','четверг','пятница','суббота'],
		dayNamesShort: ['вск','пнд','втр','срд','чтв','птн','сбт'],
		dayNamesMin: ['Вс','Пн','Вт','Ср','Чт','Пт','Сб'],
		weekHeader: 'Не',
		dateFormat: 'dd.mm.yy',
		firstDay: 1,
		isRTL: false,
		showMonthAfterYear: false,
		yearSuffix: ''};
	$.datepicker.setDefaults($.datepicker.regional['ru']);
});


$(function()
{
	mdlAudioVideoInit();
	$('a.darkbox').click(darkbox_cb);
	$("a.colorbox").colorbox({transition:'fade', photo:'true'});
	$("a.colorbox-inline").colorbox({inline:true});
	$("a.colorbox-iframe").colorbox({iframe:true, width:'90%', height:'90%'});
});
