//dojo.require('dojo.cookie');
//dojo.require('dojo.fx');
//dojo.require('dojo.NodeList-fx');
//dojo.require('dijit._base');
//dojo.require('dojox.image.Lightbox');
//dojo.require('dojox.av.FLAudio');

var mySound;

dojo.addOnLoad(function() {
	resizeContent();
	dojo.connect(dojo.byId('header-lft'), 'onclick', function() {
		document.location.href = '/' + current_lang + '/home/';
	});
	dojo.connect(window, 'resize', 'resizeContent');
	mySound = new dojox.av.FLAudio({initialVolume:.5, autoPlay:true, isDebug:false, statusInterval:500});
	mySound.load({url:'http://www.zhiguli.com/img/main.mp3', id:'start'});
	if ('' == dojo.cookie('sound') || 'on' == dojo.cookie('sound')) {
		setTimeout(function() {
			mySound.doPlay({index:0});
		}, 2000);
	}
})

function resizeContent()
{
	vp = dijit.getViewport();
	var content_width = Math.round(vp.w / 2.52);
	var content_height = null;
	if (null != (content_node = dojo.byId('content_body'))) {
		content_height = vp.h - 270;
		content_margin = 0;
		if (null != (node = dojo.byId('home_invitation'))) {
			if (1.05 < content_width / content_height) {
				old_content_width = content_width;
				content_width = Math.round(content_height * 1.05);
				content_margin = old_content_width - content_width;
			}
			dojo.style(node, 'width', (content_width - 20) + 'px');
		}
		if (null != (node = dojo.byId('home_restaurant'))) {
			dojo.style(node, 'width', (content_width / 3 - 8) + 'px');
		}
		if (null != (node = dojo.byId('home_fastfood'))) {
			dojo.style(node, 'width', (content_width / 3 - 8) + 'px');
		}
		if (null != (node = dojo.byId('home_news'))) {
			dojo.style(node, 'width', (content_width / 3 - 8) + 'px');
		}
		dojo.style(content_node, 'width', content_width + 'px');
		dojo.style(content_node, 'height', content_height + 'px');
		if (0 < content_margin) {
			dojo.style('content_block', 'marginLeft', Math.round(content_margin / 2) + 'px');
		}
		if (null != (node = dojo.byId('content_scroller'))) {
			dojo.style(node, 'width', content_width + 'px');
			dojo.style(node, 'height', (content_height - 40) + 'px');
		}
		dojo.style('content_left_block', 'height', (content_height - 70) + 'px');
		dojo.style('content_right_block', 'height', (content_height - 70) + 'px');
	}
	else {
		if (null != (node = dojo.byId('label_img'))) {
			dojo.style(node, 'height', (vp.h * 0.55) + 'px');
		}
		if (null != (node = dojo.byId('decor2_img'))) {
			dojo.style(node, 'height', vp.h + 'px');
		}
	}
	if (null != (node = dojo.byId('decor_img'))) {
		dojo.style(node, 'width', Math.round(content_width / 2) + 'px');
	}
}

function createGallery()
{
	dojo.query('ul.gallery_thumbs li a').forEach(function(node) {
		var lb = new dojox.image.Lightbox({title:node.alt, group:'gallery', href:node.href}, node);
		lb.startup();
	});
}

function createMenu()
{
	dojo.query('ol#menu_content li dl').style({display: 'none'});
	dojo.query('ol#menu_content li h3').connect('onclick', function() {
		var index = dojo.query('ol#menu_content li h3').indexOf(this);
		console.debug(index);
		dojo.query('ol#menu_content li dl').forEach(function(item, idx) {
			if (idx != index && 'none' != dojo.style(item, 'display')) {
				dojo.query(item).wipeOut().play();
			}
		});
		var content = dojo.query('dl', this.parentNode);
		if (dojo.hasClass(this, 'active')) {
			dojo.removeClass(this,'active'); // удаляем класс active
			content.wipeOut().play(); // прячем  <p>
		}
		else {
			dojo.addClass(this, 'active'); // добавляем класс active
			content.wipeIn().play(); // показываем  <p>
		}
	});
}

function doPlayPause(evt, element)
{
	if (dojo.hasClass(element, 'stopped')) {
		dojo.cookie('sound', 'on', {path: '/'});
		dojo.removeClass(element, 'stopped');
		mySound.doPlay({index:0});
	}
	else {
		dojo.cookie('sound', 'off', {path: '/'});
		dojo.addClass(element, 'stopped');
		mySound.stop({});
	}
	dojo.stopEvent(evt);
}

