$(document).ready(function(){
	videoPopup();
	initSounds();
	initMenu();
	initGall();
	$('ul.accordion').acc({
		speed: 300,
		active: 'active',
		opener: 'a.opener',
		slide: 'div.slide'
	});
	$('#sidebar select').customSelect();
});
/*--- sounds ---*/
soundManager.url = '/swf/soundmanager2_flash9.swf';
soundManager.debugMode = false;
soundManager.consoleOnly = false;
function initSounds(){
	
	var menu_volume = 50;
	var song_volume = 100;
	
	soundManager.onload = function(){
		/*--- in menu ---*/
		$('#nav li[title$=".mp3"]').each(function(i){
			var _el = $(this);
			var _obj = new Object();
			_obj._id = 'menuSound'+i;
			_obj._song = soundManager.createSound({
				id: _obj._id,
				url: _el.attr('title'),
				autoLoad: true,
				volume: menu_volume
			});
			_el.data('sound', _obj).removeAttr('title');
			
		});
		/*--- in music list ---*/
		var _current = false;
		$('div.music-box ul a').each(function(){
			this._song = soundManager.createSound({
				id: 'song_'+Math.round(Math.random()*1000000),
				url: this.href,
				volume: song_volume
			});
			
			this.onclick = function(){
				if($(this).parent().hasClass('play')){
					$(this).parent().removeClass('play');
					if(this._song) this._song.stop();
					_current = false;
				}
				else{
					if(_current){
						_current._song.stop();
						$(_current).parent().removeClass('play');
					}
					$(this).parent().addClass('play');
					this._song.play();
					_current = this;
				}
				return false;
			}
		});
	}
}
/*--- video popup ---*/
function videoPopup(){
	if($('#fader').length == 0) $('body').append('<div id="fader"></div>');
	var _popup = $('<div id="video-popup"><strong class="title"></strong><div class="holder"><div id="video-box"></div><a href="#" class="close">close</a></div></div>').hide().appendTo('body');
	var _fader = $('#fader').hide();
	var f_popup = false;
	/*--- ---*/
	$('a.btn-video').click(function(){
		_init(this.href, this.rel);
		return false;
	});
	_popup.find('a.close').click(function(){
		_hide();
		return false;
	});
	_fader.click(function(){
		_hide();
		return false;
	});
	$(document).keydown(function(e){
		e = e || window.event;
		if(e.keyCode == 27) _hide();
	});
	
	/*--- ---*/
	function _init(_url, _title){
		flowplayer('video-box', '/swf/flowplayer-3.1.5.swf', _url);
		_popup.find('.title').text(_title);
		f_popup = true;
		_fader.css({opacity:0, height: _h(), display:'block'}).fadeTo(200, 0.5, function(){
			_popup.css('top', -9999).show().css('top', $(window).scrollTop()+ ($(window).height() - _popup.outerHeight())/2);
		});
	}
	function _hide(){
		if(f_popup){
			_popup.hide();
			_fader.fadeOut(200);
			f_popup = false;
			flowplayer('video-box').stop();
			$('#video-box').empty();
		}
	}
	function _h(){
		var _h = $('#wrapper').outerHeight();
		if(_h < $(window).height()) _h = $(window).height();
		if(_h < $('body').height()) _h = $('body').height();
		return _h;
	}
}
/*--- gallery ---*/
function initGall(){
	var stay_time = 5000; //in ms or 'false' if not needed
	var change_speed = 500; //in ms 
	$('div.gallery-hold').each(function(){
		var _hold = $(this);
		var _list = _hold.find('div li');
		var _btn = _hold.find('a.arrow');
		var _f = true, _t;
		var _a = (_list.index(_list.filter('.acitve:eq(0)')) != -1)?(_list.index(_list.filter('.acitve:eq(0)'))):(0);
		_list.removeClass('active').css('opacity', 0).eq(_a).addClass('active').css('opacity', 1);
		
		_hold.mouseenter(function(){
			_f = false;
			if(_t) clearTimeout(_t);
		}).mouseleave(function(){
			_f = true;
			if(_f && stay_time){
				_t = setTimeout(function(){
					if(_a < _list.length - 1) changeEl(_a + 1);
					else changeEl(0);
				}, stay_time);
			}
		});
		_btn.click(function(){
			if(_a < _list.length - 1) changeEl(_a + 1);
			else changeEl(0);
			return false;
		});
		if(_f && stay_time){
			_t = setTimeout(function(){
				if(_a < _list.length - 1) changeEl(_a + 1);
				else changeEl(0);
			}, stay_time);
		}
		function changeEl(_ind){
			if(_t) clearTimeout(_t);
			if(_ind != _a){
				_list.eq(_a).removeClass('active').animate({opacity: 0}, {queue:false, duration:change_speed});
				_list.eq(_ind).addClass('active').animate({opacity: 1}, {queue:false, duration:change_speed});
				_a = _ind;
			}
			if(_f && stay_time){
				_t = setTimeout(function(){
					if(_a < _list.length - 1) changeEl(_a + 1);
					else changeEl(0);
				}, stay_time+change_speed);
			}
		}
	});
}
/*--- main menu ---*/
function initMenu(){
	var slide_speed = 300; //in ms
	$('#nav li').each(function(){
		var _el = $(this);
		var _box = _el.children('div.drop-hold');
		var box_f = false, _t;
		if(_box.length){
			box_f = true;
			var sub_box = _box.children('div.drop');
			_box.hide();
		}
		_el.mouseenter(function(){
			if(_t) clearTimeout(_t);
			_t = setTimeout(function(){
				if(box_f){
					_el.addClass('hover');
					if(_box.is(':hidden')){
						sub_box.css('marginTop', -9999);
						_box.show();
						sub_box.css('marginTop', -sub_box.outerHeight());
					}
					sub_box.stop().animate({marginTop:0}, slide_speed);
				}
				if(_el.data('sound')) _el.data('sound')._song.play();
			}, 200);
		}).mouseleave(function(){
			if(box_f){
				if(_t) clearTimeout(_t);
				sub_box.stop().animate({marginTop:-sub_box.outerHeight()}, slide_speed, function(){
					_box.hide();
					_el.removeClass('hover');
				});
			}
			if(_el.data('sound')) _el.data('sound')._song.stop();
		});
	});
}
/*--- accordion ---*/
jQuery.fn.acc = function(_options){
	var _options = jQuery.extend({
		speed: 400,
		active: 'active',
		list: '.children()',
		opener: 'a.opener',
		slide: 'div.slide'
	}, _options);
	return this.each(function(){
		var _list = eval('jQuery(this)' + _options.list);
		var _active = _options.active;
		var _speed = _options.speed;
		var _a = _list.index(_list.filter('.' + _active + ':eq(0)'));
		if(_a != -1) _list.removeClass(_active).eq(_a).addClass(_active);
		for(var i = 0; i < _list.length; i++){
			_list.eq(i).data('btn', _list.eq(i).find(_options.opener).eq(0));
			_list.eq(i).data('box', _list.eq(i).find(_options.slide).eq(0));
			if(i == _a) _list.eq(i).data('box').css('display', 'block');
			else _list.eq(i).data('box').css('display', 'none');
			_list.eq(i).data('btn').data('ind', i);
			_list.eq(i).data('btn').click(function(){
				changeEl(jQuery(this).data('ind'));
				return false;
			});
		}
		var anim_f = true;
		var a_h, ind_h, _k;
		function changeEl(_ind){
			if(anim_f){
				anim_f = false;
				if(_a == _ind){
					_list.eq(_a).removeClass(_active).data('box').animate({height: 0}, {
						duration: _speed,
						complete: function(){
							jQuery(this).css({display:'none', height:'auto'});
							_a = -1;
							anim_f = true;
						}
					});
				}
				else{
					_list.eq(_ind).data('box').css('display', 'block');
					ind_h = _list.eq(_ind).data('box').outerHeight();
					_list.eq(_ind).data('box').height(0);
					if(_a != -1){
						a_h = _list.eq(_a).removeClass(_active).data('box').outerHeight();
						_k = a_h/ind_h;
					}
					_list.eq(_ind).addClass(_active).data('box').animate({height: ind_h}, {
						duration: _speed,
						step: function(t_h){
							if(_a != -1) _list.eq(_a).data('box').height(a_h - t_h*_k);
						},
						complete: function(){
							_list.eq(_ind).data('box').height('auto');
							if(_a != -1) _list.eq(_a).data('box').css({display:'none', height: 'auto'});
							_a = _ind;
							anim_f = true;
						}
					});
				}
			}
		}
	});
}
/*--- custom select's ---*/
jQuery.fn.customSelect = function(_options){
var _options = jQuery.extend({
	selectStructure: '<div class="selectArea"><div class="left"></div><div class="center"></div><a href="#" class="selectButton">&nbsp;</a><div class="disabled"></div></div>',
	selectText: '.center',
	selectBtn: '.selectButton',
	selectDisabled: '.disabled',
	optStructure: '<div class="selectOptions"><ul></ul></div>',
	optList: 'ul'
}, _options);
return this.each(function() {
	var select = jQuery(this);
	if(!select.hasClass('outtaHere')) {
		if(select.is(':visible')) {
			var replaced = jQuery(_options.selectStructure);
			var selectText = replaced.find(_options.selectText);
			var selectBtn = replaced.find(_options.selectBtn);
			var selectDisabled = replaced.find(_options.selectDisabled).hide();
			var optHolder = jQuery(_options.optStructure);
			var optList = optHolder.find(_options.optList);
			if(select.attr('disabled')) selectDisabled.show();
			select.find('option').each(function() {
				var selOpt = jQuery(this);
				var _opt = jQuery('<li><a href="#">' + selOpt.html() + '</a></li>');
				if(selOpt.attr('selected')) {
					selectText.html(selOpt.html());
					_opt.addClass('selected');
				}
				_opt.children('a').click(function() {
					optList.find('li').removeClass('selected');
					select.find('option').removeAttr('selected');
					jQuery(this).parent().addClass('selected');
					selOpt.attr('selected', 'selected');
					selectText.html(selOpt.html());
					select.change();
					optHolder.hide();
					return false;
				});
				optList.append(_opt);
			});
			replaced.width(select.outerWidth());
			replaced.insertBefore(select);
			optHolder.css({
				width: select.outerWidth(),
				display: 'none',
				position: 'absolute'
			});
			jQuery(document.body).append(optHolder);
			
			var optTimer;
			replaced.hover(function() {
				if(optTimer) clearTimeout(optTimer);
			}, function() {
				optTimer = setTimeout(function() {
					optHolder.hide();
				}, 200);
			});
			optHolder.hover(function(){
				if(optTimer) clearTimeout(optTimer);
			}, function() {
				optTimer = setTimeout(function() {
					optHolder.hide();
				}, 200);
			});
			selectBtn.click(function() {
				if(optHolder.is(':visible')) {
					optHolder.hide();
				}
				else{
					optHolder.children('ul').css({height:'auto', overflow:'hidden'});
					optHolder.css({
						top: replaced.offset().top + replaced.outerHeight(),
						left: replaced.offset().left,
						display: 'block'
					});
					if(optHolder.children('ul').height() > 100) optHolder.children('ul').css({height:100, overflow:'auto'});
				}
				return false;
			});
			select.addClass('outtaHere');
		}
	}
});
}
