var main_menu;
var ie= '\v' == 'v';
//

function vIE(){return (navigator.appName=='Microsoft Internet Explorer')?parseFloat((new RegExp("MSIE ([0-9]{1,}[.0-9]{0,})")).exec(navigator.userAgent)[1]):999;}

function Moo$(id) {
	return document.id(id);
}

function toggleMainMenu() {
	if (!main_menu) {
		main_menu = Moo$('main_menu');
		main_menu.set('morph', {duration: 400, transition: Fx.Transitions.Quad.easeInOut});
	}
	if (main_menu.toggle != 1) {
		main_menu.toggle = 1;
		main_menu.morph({marginTop: -33, opacity:1});
		Moo$('main_menu_toggle').getElements('a').set('class', 'slider_a');
	} else {
		main_menu.toggle = 0;
		main_menu.morph({marginTop: 0, opacity:1});
		Moo$('main_menu_toggle').getElements('a').set('class', 'slider_b');
	}
	return false;
}

function galSlideDown(el) {
	el.getElement('div.desc').morph({marginTop: 0});
}

function random_color(format)
{
 var rint = Math.round(0xffffff * Math.random());
 switch(format)
 {
  case 'hex':
   return ('#0' + rint.toString(16)).replace(/^#0([0-9a-f]{6})$/i, '#$1');
  break;
  
  case 'rgb':
   return 'rgb(' + (rint >> 16) + ',' + (rint >> 8 & 255) + ',' + (rint & 255) + ')';
  break;
  
  default:
   return rint;
  break;
 }
}

function galKostil(el) {
	alert(el);
	el.getElement('div.desc').morph({marginTop: 97});	
	el.kostil = false;
}

function initGalSliders(as)
{
	as.getElement('div.desc').set('morph', {duration: 500, transition: Fx.Transitions.Quad.easeInOut});	
	as.addEvents({
					'mouseleave': function(){		
												 this.getElement('div.desc').morph({marginTop: 97});	
												 },
					'mouseenter' : function(){
												 this.getElement('div.desc').morph({marginTop: 0});
												 }
				 })

}

function initGallery()
{
	var gal_list = Moo$('content').getElements('div.gal_list');
	if (gal_list.length > 0) {
		for (var i=0; i<gal_list.length; i++) {
			initGalSliders(gal_list[i].getElements('a'));
		}
	}
}

function initMenuSlider()
{
	if (Moo$('main_menu_toggle')) {		
		Moo$('main_menu_toggle').getElements('a').addEvent('click', toggleMainMenu);
	}
}

function initNewsSelects()
{
	var drop_list = Moo$('content').getElements('div.news_dropdown');
	
	if (drop_list.length > 0) {
		for (var i=0; i<drop_list.length; i++) {
			bindNewsDropDown(drop_list[i])
		}
	}
}

function bindNewsDropDown(el)
{
	el.mh = el.getElement('ul').offsetHeight
	el.addEvents({
				   'mouseleave': function(){		
				   							this.getElement('div.sub').set('morph', {duration: 750, transition: Fx.Transitions.Quart.easeIn});
											this.getElement('.title').set('morph', {duration: 500});
											this.getElement('.title').morph({borderColor:'#72acc0'});
											el.getElement('div.sub').morph({height:0});
										     },
					'mouseenter' : function(){
											this.getElement('div.sub').set('morph', {duration: 500, transition: Fx.Transitions.Quart.easeOut});
											this.getElement('.title').set('morph', {duration: 250});
											this.getElement('.title').morph({borderColor:'#18364d'});
											el.getElement('div.sub').morph({height:this.mh});
											 }
				 })
}

function initScroller()
{
	var scroll_speed = 40;
	var sow = Moo$('playing_scroller').getElement('span').offsetWidth;
	if (sow > 246) {
		Moo$('playing_scroller').getElement('div').set('morph', {duration: (sow-246)*scroll_speed,  transition: Fx.Transitions.linear});
		Moo$('playing_scroller').getElement('div').speed = (sow-246)*scroll_speed;

		setTimeout(scrollerGoLeft, 4000);
	}
}

function scrollerGoLeft()
{		
		Moo$('playing_scroller').getElement('div').morph({marginLeft: -(Moo$('playing_scroller').getElement('span').offsetWidth-246)});	
		setTimeout(scrollerGoRight, Moo$('playing_scroller').getElement('div').speed+2000);
}

function scrollerGoRight()
{
		Moo$('playing_scroller').getElement('div').morph({marginLeft: 0});
		setTimeout(scrollerGoLeft, Moo$('playing_scroller').getElement('div').speed+2000);
}


var main_menu_items;
var main_menu_shown = -1;
var main_menu_minterval;
var main_menu_classto;

function attachSubMenu(subi)
{	
	var item_submenu = new Element('div', {'class':'submenu'});;
	
	var tmp_html = '<div class="content"><ul>';
	
	for (sub_item in main_sub_menu[subi]) {
		tmp_html += '<li><a href="'+main_sub_menu[subi][sub_item]+'">'+sub_item+'</a></li>';
	}
	tmp_html += '</ul></div><div class="clear"></div><div class="bottom"></div>';
	item_submenu.set('html', tmp_html);
	if (main_menu_items[subi]) {
		item_submenu.inject(main_menu_items[subi].getParent(), 'top');
		main_menu_items[subi].io = subi;
		main_menu_items[subi].getParent().getElement('.submenu').io = subi;		
		main_menu_items[subi].fh = main_menu_items[subi].getParent().getElement('.submenu').offsetHeight;
		main_menu_items[subi].getParent().getElement('.submenu').setStyles({'opacity': (ie ? 1 : 0), 'height':0});
		//BDSM {
		if (Moo$('top').getElement('#main_menu')) {
			if (subi == 9) { main_menu_items[subi].getParent().getElement('.submenu').addClass('last'); }
			if (subi == 8) { main_menu_items[subi].getParent().getElement('.submenu').addClass('prelast'); }
		} else {
			if (subi == 11) { main_menu_items[subi].getParent().getElement('.submenu').addClass('last'); }
		}		
		// } END BDSM		
		
		if (vIE() < 8) {
			main_menu_items[subi].getParent().getElement('.submenu').setStyle('marginLeft', main_menu_items[subi].offsetWidth*-1-4);
		}
		main_menu_items[subi].addEvents({
										'mouseenter': function(){
											main_menu_items[this.io].addClass('item_hover');
											showSubMenu(this.io, this.fh);
										},
										'mouseleave': function(){
											main_menu_minterval = setTimeout("hideSubMenu("+this.io+");", 100);
										}
										});
		main_menu_items[subi].getParent().getElement('.submenu').addEvents({
										'mouseenter': function(){
											main_menu_items[this.io].addClass('item_hover');
											showSubMenu(this.io, this.fh);
										},
										'mouseleave': function(){
											main_menu_minterval = setTimeout("hideSubMenu("+this.io+");", 100);
										}
										});
	}
}

function showSubMenu(sid, sfh) {
	if (main_menu_minterval) {
		clearTimeout(main_menu_minterval);
	}
	if (main_menu_classto) {
		clearTimeout(main_menu_classto);
	}
	if (main_menu_shown > -1 & main_menu_shown != sid) {
		hideSubMenu(main_menu_shown);
	}
	main_menu_items[sid].getParent().getElement('.submenu').set('morph', {duration: 500, transition: Fx.Transitions.Quart.easeOut});
	main_menu_items[sid].getParent().getElement('.submenu').morph({'height': sfh, 'opacity': 1});	
	main_menu_shown = sid;
}

function hideSubMenu(sid) {
	main_menu_items[sid].getParent().getElement('.submenu').set('morph', {duration:600, transition: Fx.Transitions.Quart.easeIn});
	main_menu_items[sid].getParent().getElement('.submenu').morph({'height': 0, 'opacity': 	(ie ? 1 : 0)});	
	main_menu_classto = setTimeout("main_menu_items["+sid+"].removeClass('item_hover');", 600);
}


function initSubMenu()
{	
	if (Moo$('main_menu').getElement('.borders') || true) {
		main_menu_items = Moo$('main_menu').getElements('a.item');
		for (var subi in main_sub_menu) {
			attachSubMenu(subi);
		}
	}
}


function initActions()
{	
  var els = Moo$('content').getElements('.past h3');
	if (els.length > 0) {
		els.each(function(el) {			
			 el.getParent().setStyles({'height': 30});
			 el.setStyles({'cursor' : 'pointer'/*, 'text-decoration' : 'underline'*/});
			 el.hidden = true;
     		 el.getParent().getElements('.month_group').each(function(el){el.setStyles({'display': 'none'});})
			 el.addEvents({'click': function(){openCloseYear (this);}});
		})
	}
}

function openCloseYear (el) {
	if (el.hidden) {
   		 el.getParent().getElements('.month_group').each(function(el){el.setStyles({'display': 'block'});})
		 el.getParent().setStyles({'height': 'auto'});
		 el.hidden = false;
	} else {
		 el.hidden = true;
   		 el.getParent().getElements('.month_group').each(function(el){el.setStyles({'display': 'none'});})
		 el.getParent().setStyles({'height': 30});
	}
}

window.addEvent('domready', function()
{	
	initSubMenu();
	initMenuSlider();
	initGallery();
	initNewsSelects();	
	initScroller();		
	initActions();
	initPlayer();
}
);

/*** PLAYER FUNCTIONS ***/

var player_for_player;
var player_overlay;
var player_add_block;
var player_add_block_radio1;
var player_add_block_radio2;
var jsReady;
var player_add_block_playlists;

function initPlayer() {
	if (Moo$('player_overlay')) {
		Moo$('player_overlay').set('morph', {duration: 500, transition: Fx.Transitions.Quart.easeOut});
		Moo$('player_add_block').set('morph', {duration: 500, transition: Fx.Transitions.Quart.easeOut});
		Moo$('player_add_block').getElement('.add_to_player').addEvents({'click':function(){doAdd(); return false;}});		
		Moo$('player_add_block').getElement('.cancel_add').addEvents({'click':function(){endAdd();return false;}});		
		player_for_player = Moo$('player_for');
		player_overlay = Moo$('player_overlay');
		player_add_block = Moo$('player_add_block');
		player_message = Moo$('player_overlay_message');
		showMessage('Синхронизация...');
		jsReady = true;
		player_add_block_radio1 = player_add_block.getElement('input[value=1]');
		player_add_block_radio2 = player_add_block.getElement('input[value=2]');
		player_add_block_playlists = Moo$('player_playlists');
		Moo$('player_newplaylist').addEvents({'focus':function(){this.getParent().getParent().getElement('input[value=2]').checked=true;if (this.value=='Новый плейлист') {this.value=''}}, 'blur':function(){if (this.value=='') {this.value='Новый плейлист'}}})
		player_add_block_playlists.addEvents({'focus':function(){this.getParent().getParent().getElement('input[value=1]').checked=true;}})
	}	
}

function showOverlay() {
	player_overlay.setStyles({'width':player_for_player.offsetWidth-3, 'height':player_for_player.offsetHeight, 'opacity':0, 'display':'block'});
	player_overlay.morph({'opacity':0.7});
}

function showAddBlock() {
	player_add_block.setStyles({'margin-left':(player_for_player.offsetWidth/2)-120, 'margin-top':(player_for_player.offsetHeight/2)-55, 'display':'block', 'opacity':1});
//	player_add_block.morph({'opacity':1});
	if (player_add_block_playlists.options.length > 0) {
		player_add_block_radio1.disabled = false;
		player_add_block_playlists.disabled = false;
		player_add_block_radio1.checked = true;
	} else {
		player_add_block_radio1.disabled = true;
		player_add_block_playlists .disabled = true;
		player_add_block_radio2.checked = true;
	}
	

	Moo$('player_newplaylist').value = 'Новый плейлист';
	
}

function showMessage(str) {
	showOverlay();
	player_message.setStyles({'width':player_for_player.offsetWidth, 'margin-top':(player_for_player.offsetHeight/2)-15, 'display':'block', 'opacity':0});
	player_message.set('text', str);	
//	player_message.morph({'opacity':1});
	player_message.setStyles({'opacity':1});
}

function hideMessage() {
	hideOverlay();
	player_message.morph({'opacity':0});
}

function hideOverlay() {
	player_overlay.setStyles({'opacity':0});
}

function hideAddBlock() {
	player_add_block.setStyles({'opacity':0});
}

function playerIsReady() {
	hideMessage();
}

var adding_songs;
var cur_selected_song;

function PlayerAddSongs(link_pointer, songs_array) {
	showOverlay();
	showAddBlock();		
	cur_selected_song = $(link_pointer).parent();
	adding_songs = songs_array;
	if ($(cur_selected_song)[0].tagName == 'LI') {
		$(cur_selected_song).addClass('selected');
		player_add_block.setStyles({'margin-left':115, 'margin-top':120}/*(getBounds(cur_selected_song).y-getBounds(player_for_player).y)-75}*/);
	} else {
		player_add_block.setStyles({'margin-left':115, 'margin-top':120}/*(getBounds(cur_selected_song).y-getBounds(player_for_player).y)-75}*/);
	}
}

function PlayerDeletePlaylist(playlist_id) {
	deletePlaylistStart();
	thisMovie('alpha_player').ToAS_DeletePlaylist(playlist_id);	
}

function doAdd() {
	if (player_add_block_radio1.checked) {
		thisMovie('alpha_player').ToAS_AddTracksExist(player_add_block_playlists.options[player_add_block_playlists.selectedIndex].value, adding_songs);	
	} else {
		thisMovie('alpha_player').ToAS_AddTracksNew(Moo$('player_newplaylist').value, adding_songs);	
	}	
	endAdd();
}

function playerJsIsReady() { 
	playerIsReady();
	return jsReady;
}

function FromAS_PlaylistSelect (newid) {
	for (var i=0; i<player_add_block_playlists.options.length; i++) {
		if (player_add_block_playlists.options[i].value == newid) {
			player_add_block_playlists.selectedIndex = i;
		}
	}
}

function FromAS_PlaylistChange(arr) {
	player_add_block_playlists.options.length = arr.length;
	for (var i=0; i<arr.length; i++) {
		var vals = arr[i].split('|');
		player_add_block_playlists.options[i].value = vals[0];
		player_add_block_playlists.options[i].text = vals[1];
	}
}

function endAdd() {
	hideOverlay();
	hideAddBlock();
	$(cur_selected_song).removeClass('selected');
}

var getBounds = function (obj) {
	var left, top;
	var ObjWidth = obj.offsetWidth;
	var ObjHeight = obj.offsetHeight;
    left = top = 0;
    if (obj.offsetParent) {
        do {
            left += obj.offsetLeft;
            top  += obj.offsetTop;
        } while (obj = obj.offsetParent);
    }

    return {		
        x : left,
        y : top,
		width: ObjWidth,
		height: ObjHeight
    };
}

function thisMovie(movieName) {
	if (navigator.appName.indexOf("Microsoft") != -1) {
		return window[movieName];
	} else {
		return document[movieName];
	}
}