var TID = null;

function initAll() {
	initMenu();
	initVideoBoxTabs();
	initImageRotatorTabs();
	initImageRotator();
	initUpdater();
	initTopMenuUpdater();
}

function initMenu() {
	var list = $('part_menu');
	if (list) {
		list.childElements().each(function(el) {
			setMenuBackground(el);
		});
	}
}

function setMenuBackground(el){
	el.style.backgroundPosition = '-'+el.offsetLeft+'px bottom';
}

/*======= image ritator ===========*/
function initImageRotator(){
	stopImageRotator();

	var list = $('image_rotator');

	if (list != 'undefined' && list != null) {
		TID = setInterval(function(){
			var found = false;
			var i = 0;

			list.childElements().each(function(li){
				i++;

				if(li.className == 'active' && found == false){
					found = true;
					li.off()

					if(i>3){
						var first = li.parentNode.firstChild;
						first.hide();
						li.parentNode.appendChild(first)
					}

					if(li.next()){
						li.next().show();
					}
				}

			});

		}, 1500);
	}
}

function stopImageRotator(){
	if(TID)clearInterval(TID);
}


function initImageRotatorTabs() {
	var list = $('image_rotator');

	if(list != 'undefined' && list != null) {
		list.childElements().each(function(tab) {
			tab.pureId = tab.id.sub('imageTab_', '');
			tab.image = $('boxImage_'+tab.pureId);
			tab.notice = $('boxImageNotice_'+tab.pureId);

			tab.on = function(){
				this.className = 'active';
				if(this.image)this.image.style.display='block';
				if(this.notice)this.notice.style.display='block';
			};

			tab.off = function(){
				this.className = '';
				if(this.image)this.image.style.display='none';
				if(this.notice)this.notice.style.display='none';
			};

			tab.show = function(){
				this.on();
				this.style.display='block'
			}

			tab.hide = function(){
				this.off();
				this.style.display='none'
			}

			tab.observe('mouseover', function(e){
				stopImageRotator();
				list.childElements().each(function(el){
					switchTab(el, 0);
				});
				switchTab(tab, 1);
			});

			tab.observe('mouseout', function(e){
				initImageRotator();
			});

		});
	}
}
/*======= Video box ===========*/
function initVideoBoxTabs() {
	var list = $('video_box');
	var i=0;
	if(list != 'undefined' && list != null) {
		list.childElements().each(function(tab) {
			tab.selected = i==0 ? true : false;
			tab.content = $(tab.id+'_content');
			tab.text = $(tab.id+'_text');
			tab.player = $(tab.id+'_player');

			tab.select = function(){
				this.selected = true;
				if(this.content)this.content.style.display='block';
				if(this.text)this.text.style.display='block';
			}

			tab.unSelect = function(){
				this.selected = false;
				if(this.content)this.content.style.display='none';
				if(this.text)this.text.style.display='none';
			}

			tab.on = function(){
				this.className = 'active';
			};

			tab.off = function(){
				this.className = '';

			};

			tab.observe('mouseover', function(e){

				list.childElements().each(function(el){
					var tab = getTab(el);
					if (tab) {
						if (tab.selected == false) tab.off();
					}
				});
				switchTab(e.target, 1);
			})

			tab.observe('mouseout', function(e){
				var tab = getTab(e.target);
				if (tab && tab.selected == false){
					tab.off();
				}
			})

			tab.observe('click', function(e){
				list.childElements().each(function(el){
					var tab = getTab(el);
					if (tab) {
						tab.off();
						tab.unSelect();
					}
				});

				var sTab = getTab(e.target);

				if (sTab) {
					sTab.on();
					sTab.select();
				}
			})

			if (i==0){
				tab.on();
				tab.select();
			}
			i++;
		});

	}
}

function switchTab(el, state){
	var tab = getTab(el);
	if (tab != null) {
		if (state){
			tab.on();
		} else {
			tab.off();
		}
	}
}

function getTab(el){
	if(el.tagName != 'LI') {
		var i = 0;
		do {
			el = el.parentNode;
			i++;
		} while(i<4 && el!=undefined && el.tagName != 'LI');
	}

	return (el != undefined && el.tagName == 'LI') ? el : null;
}

/* Updater */
function initUpdater() {

	$$('a[rel=updater]').each(function(a) {
		a.stopObserving('click');
		a.observe('click', updater);
	});
}

/* Top menu */
function initTopMenuUpdater() {

	$$('a[rel=topMenu]').each(function(a) {

		a.observe('click', function(e){

			$$('a[rel=topMenu]').each(function(a) {
				a.className='';
			});
			a.className = 'active_top';

			updater(e);
		});
	});
}

function updater(e){
	var a = e.target;
	stopImageRotator();
	e.preventDefault();

	dropVideo();

	new Ajax.Updater('part_menu', a.getAttribute('href'), {
		method:'get',
		parameters: { view: 'updater', context:'menu' },
		onComplete: function() {
			initMenu();
			initUpdater();
		}
	});

	if($('gmap')){
		$('gmap').update('');
	}

	var tmp = new Element('div', { 'id': 'tmp_update_container', 'class':'effect'});
	$('center').appendChild(tmp);

	new Ajax.Updater('tmp_update_container', a.getAttribute('href'), {
		method:'get',
		parameters: { view: 'updater', context:'center' },
		onComplete: function(response) {

			//initMenu();
			initUpdater();

			new Effect.SlideUp(
				'updated',
				{
					duration:0.7,
					afterFinish:function(){
						$('updated').remove();
						$('tmp_update_container').id = 'updated';

						initImageRotatorTabs();
						initImageRotator();
						initVideoBoxTabs();

						if($('gmap')){
							$('gmap').show();
						}
						showVideo();
					}
				});
		}
	});
}

function dropVideo(){
	var box = $('video_content');
	if (box){
		box.update('');
	}
}
function showVideo(){
	var box = $('video_content');
	if (box){
		box.show();
	}
}