var $j = jQuery.noConflict();
var _ie6 = ($j.browser.msie && $j.browser.version == 6);

// toolbar
_toolbar = function(){
	_init = function(){
		var _w = $j('#informer').width();
		$j('#toolbar').width(_w + 20);
	}

	return {
		_init: _init
	}
}();

// filial
_filial = function(){
	var _height = 299;
	var _timeout = false;
	var _interval = false;

	_init = function(){
		$j('#filial span a').each(function(i){
			if(i == 0) $j(this).addClass('first');
		});
		
		_h = $j('#filial span').height();
		if(_h > _height){
			$j('#filial_panel div.bg').height(_height);
			$j('#filial div.bg').height(_height);
			$j('#filial div.up a').addClass('off');
			$j('#filial div.down a').addClass('on');

			$j('#filial div.up a').mousedown(function(){
				_interval = setInterval('_filial._move(0)', 30);
			}).mouseup(function(){
				clearInterval(_interval);
			});
			$j('#filial div.down a').mousedown(function(){
				_interval = setInterval('_filial._move(1)', 30);
			}).mouseup(function(){
				clearInterval(_interval);
			});	
		}else{
			$j('#filial_panel div.bg').height(_h);
			$j('#filial div.bg').height(_h);
		}
		
		$j('#filial_panel div').ifixpng();
	}

	_open = function(_type){
		_menu._close();
		_timeout = clearTimeout(_timeout);
		if(_type){
			$j('#filial_panel').css({visibility: 'visible'});
			$j('#filial').css({visibility: 'visible'});
		}else{
			_timeout = setTimeout('_filial._close()', 700);
		}
	}

	_close = function(){
		$j('#filial').css({visibility: 'hidden'});		
		$j('#filial_panel').css({visibility: 'hidden'});	
	}

	_move = function(_type){
		var _h = $j('#filial span').height();
		var _scroll = $j('#filial div.bg')[0].scrollTop;
		if(_scroll == 0 && _type == 0) { clearInterval(_interval); return false; }
		if(_scroll >= _h - _height && _type == 1) { clearInterval(_interval); return false; }
		
		var _shift = (_type)? 5 : -5;
		$j('#filial div.bg')[0].scrollTop += _shift;
		_scroll = $j('#filial div.bg')[0].scrollTop;
		
		$j('#filial div.up a')[0].className = (_scroll > 0)? 'on' : 'off';
		$j('#filial div.down a')[0].className = (_scroll < _h - _height)? 'on' : 'off';
	}

	return {
		_init: _init,
		_open: _open,
		_close: _close,
		_move: _move
	}
}();

// menu
_menu = function(){
	var _id = false;
	var _sid = false;
	var _timeout = false;
	var _timeout2 = false;
	var _depth = 0;
	var _count = 0;

	_create = function(_container, _show){
		var _html = '<ul level="1" class="raMenuLeft">';
		$j.each(_map, function(i){
			if(parseInt(this.depth) > _depth){
				_html += '<li id="li_' + _id + '_menu" class="menu" style="display: none"><ul level="' + this.depth + '">';
				_count++;
			}
			if(parseInt(this.depth) < _depth){
				_level = _depth - parseInt(this.depth);
				for(var i = 0; i < _level; i++){
					_html += '</ul></li>';
					_count--;
				}
			}
			
			_depth = parseInt(this.depth);
			_id = this.id;
      if (this.visible != 'false') {
				_html += '<li tree="' + this.treenum + '" id="li_' + _id + '"><a href="' + this.href + '">' + this.title + '</a></li>';
			}	
		});
		for(var i = 0; i < _count; i++) _html += '</ul></li>';
		_html += '</ul>';
		
		$j(_container).html(_html);
		
		$j(_container + ' li').each(function(){
			if(document.getElementById(this.id + '_menu')){
				_html = '<a class="tree" onclick="_menu._open(this)"></a>' + $j(this).html();
				$j(this).addClass('close');
				$j(this).html(_html);
			}
		});
		
		var _num = $j('#li_' + _show).attr('tree');
		var _arr = _num.split('.');
		var _tree = '';
		var _firstId = false;
		var _lastId = false;
		for(var i = 0; i < _arr.length; i++){
			if(_tree != '') _tree += '.';
			_tree += _arr[i];
			$j(_container + ' li[tree=' + _tree + ']').each(function(){
				if(!_firstId) _firstId = this.id;
				_lastId = this.id;
				$j('#' + this.id + '_menu').css({display: 'block'});
				_class = $j('#' + this.id).attr('class');
				if(_class == 'close') $j('#' + this.id)[0].className = 'open';
			});
		}
		
		if(_lastId) $j('#' + _lastId).addClass('on');
		if(_firstId){
			_html = $j('#' + _firstId + '_menu ul').html();
			if(_html != '') $j(_container + ' ul').html(_html);
		}
		
		_id = false;
	}

	_init = function(_selectedId){
		// level parse
		var _level1 = 0;
		var _level = new Array();
		var _selected_id = 0;
		$j(_menuJson).each(function(){
			var _id = '';
			var _parent = '';
			var _arr = this.treenum.split('.');
			$j(_arr).each(function(i){
				if(i) _id += '_';
		 		_id += this;
		 		if(i < _arr.length -1){
		 			if(i) _parent += '_';
		 			_parent += this;
		 		}
			});				

			if (this.id == _selectedId) {
				_selected_id = _id;
			}

			_level[_level.length] = {id: _id, parent: _parent, level: this.depth, href: this.href, title: this.title, visible: this.visible};
			if(this.depth == 1) _level1++;
		});

		// level create
		var _html1 = '';
		var _html2 = '';
		var _html3 = '';

		var _html1tmp = '<table><tr>';
		var _count1 = 0;
		$j(_level).each(function(i){
    
    //-----------------
       if (this.visible!='false'){
			if(this.level == 1){
				_count1++;
				_id1 = this.id;
				_class = (_selected_id.indexOf(_id1) == 0 ? ' class="on" ' : '');	
				_html1tmp += '<td';
				if(_count1 == 1) _html1tmp += ' class="l"';
				if(_count1 == _level1) _html1tmp += ' class="r"';
				_html1tmp += ' id="menu' + _id1 + '"><a href="' + this.href + '"' + _class + '>' + this.title + '</a></td>';

				var _html2tmp = '<div id="submenu' + _id1 + '" class="raSubMenu"><span>';
				var _count2 = 0;
				$j(_level).each(function(){
					if(this.level == 2 && this.parent == _id1){
						_count2++;
						_id2 = this.id;
						_html2tmp += '<a';
						if(_count2 == 1) _html2tmp += ' class="first"';

                        var _html3tmp = '<div id="submenu' + _id2 + '" class="raSubMenu2"><span>';
                        var _count3 = 0;
                        $j(_level).each(function(){
                            if(this.level == 3 && this.parent == _id2){
                                _count3++;
                                _html3tmp += '<a';
                                if(_count3 == 1) _html3tmp += ' class="first"';
                                _html3tmp += ' rel="menu" id="menu' + this.id + '" href="' + this.href + '">' + this.title + '</a>';
                            }
                        });
                        _html3tmp += '</span></div>';
                        if(_count3) _html3 += _html3tmp;

                        _class = (_count3)? 'menu' : '';
                        _html2tmp += ' rel="menu" id="menu' + _id2 + '" href="' + this.href + '"><div class="' + _class + '">' + this.title + '</div></a>';

					}
				});
				_html2tmp += '</span></div>';
				if(_count2) _html2 += _html2tmp;
			}		
		}
    //-------------------
    
    });
		_html1tmp += '</tr></table>';
		if(_count1) _html1 += _html1tmp;
		
		$j('#menu').html(_html1 + _html2 + _html3);
	
	
		$j('#menu td').each(function(){
			$j(this).mouseover(function(){
				_menu._show(this, 1);
			}).mouseout(function(){
				_menu._show(this, 0);			
			});
			
			var _sid = this.id;
			$j('#sub' + _sid).mouseover(function(){
				_menu._show(null, 1);
			}).mouseout(function(){
				_menu._show(null, 0);			
			});
		});
		
		$j('.raSubMenu span a').each(function(){
			$j(this).mouseover(function(){
				_menu._show2(this, 1);
			}).mouseout(function(){
				_menu._show2(this, 0);			
			});			

			var _sid = this.id;
			$j('#sub' + _sid).mouseover(function(){
				_menu._show2(null, 1);
			}).mouseout(function(){
				_menu._show2(null, 0);			
			});
		});
	}
	
	_show = function(_obj, _type){
		_filial._close();
		_timeout = clearTimeout(_timeout);
		if(_type){
			if(_obj == null) return;
			_menu._close();
			_id = _obj.id;
			var _left = _obj.offsetLeft;
			if(_left > 755) _left = 755;
			$j('#sub' + _id).css({visibility: 'visible', left: _left});
			$j('#' + _id + ' a').addClass('sel');
		}else{
			_timeout = setTimeout('_menu._close()', 700);
		}
	}

	_show2 = function(_obj, _type){
		_timeout2 = clearTimeout(_timeout2);
		_timeout = clearTimeout(_timeout);
		if(_type){
			if(_obj == null) return;
			_menu._close2();
			if($j(_obj).attr('rel') != 'menu') return;
			_sid = _obj.id;
			var _offset = $j('#menu').offset();
			var _offset2 = $j(_obj).offset();
			var _top = _offset2.top - _offset.top;
			var _left = _offset2.left - _offset.left;
			var _width = _obj.offsetWidth;
			var _width2 = $j('#sub' + _sid).width();
			_left = (_left + _width > 755)? (_left - _width2) : (_left + _width);
			$j('#sub' + _sid).css({visibility: 'visible', top: _top, left: _left});
			$j('#' + _sid).addClass('sel');
		}else{
			_timeout2 = setTimeout('_menu._close2()', 700);
			_timeout = setTimeout('_menu._close()', 700);
		}
	}
	
	_close2 = function(){
		if(!_sid) return false;	
		$j('#sub' + _sid).css({visibility: 'hidden'});
		$j('#' + _sid).removeClass('sel');
		_sid = false;
	}

	_close = function(){
		if(!_id) return false;
		$j('#sub' + _id).css({visibility: 'hidden'});
		$j('#' + _id + ' a').removeClass('sel');
	}

	_open = function(_obj){
		var _id = $j(_obj).parent().attr('id');
		$j('#' + _id).removeClass('open');
		$j('#' + _id).removeClass('close');

		var _display = $j('#' + _id + '_menu').css('display');
		if(_display == 'none'){
			$j('#' + _id + '_menu').css({display: 'block'});
			$j('#' + _id).addClass('open');
		}else{
			$j('#' + _id + '_menu').css({display: 'none'});
			$j('#' + _id).addClass('close');
		}
	}

	return {
		_create: _create,
		_init: _init,
		_show: _show,
		_show2: _show2,
		_close: _close,
		_close2: _close2,
		_open: _open
	}
}();

// banner
_banner = function(){
	var _page;
	var _max;
	var _lock = false;

	_init = function(_num, _end){
		_page = _num;
		_max = _end;
		$j('#banner' + _num).css({visibility: 'visible'});
		$j('#banner_arrow').ifixpng();
		if(_end < 2) return false;
		
		var _html = '<a type="next" class="next" onclick="return _banner._move(1)"><spacer /></a>';
		for(var i = _end; i > 0; i--){
			_html += '<a type="' + i + '" ';
			if(_num == i) _html += 'class="on" ';
			_html += 'onclick="return _banner._list(' + i + ')">' + i + '</a>';
		}
		_html += '<a type="prev" class="prev" onclick="return _banner._move(-1)"><spacer /></a>';
		$j('#banner_list').html(_html);
		$j('#banner_list a').ifixpng();
	}

	_open = function(_obj){
		if(_lock) return false;
		var _class = _obj.className;
		_h = (_class == 'open')? 296 : 166;
		_title = (_class == 'open')? 'Close' : 'Open';
		_class = (_class == 'open')? 'close' : 'open';
		_lock = true;
		$j('#banner').animate({height: _h}, 500, function(){
			_lock = false;
		});
		if(_ie6){
			$j('#banner_arrow').remove();
			$j('#banner').append('<div id="banner_arrow" class="' + _class + '" title="' + _title + '" onclick="_banner._open(this)"><spacer /></div>');
			$j('#banner_arrow').ifixpng();
		}else{
			$j('#banner_arrow')[0].className = _class;
			$j('#banner_arrow').attr('title', _title);
		}
	}
	
	_list = function(_num){
		if(_page == _num) return false;
		_banner._move(_num - _page);
	}
	
	_move = function(_shift){
		var _num = _page + _shift;
		if(_num < 1 || _num > _max) return false;
		
		$j('#banner_list a').removeClass('on');
		$j('#banner_list a[type = ' + _num + ']').addClass('on');
		
		_lock = true;
		if(_shift > 0){
			$j('#banner' + _num).css({visibility: 'visible', left: 948});
			$j('#banner' + _page).animate({left: -948}, 500);
			$j('#banner' + _num).animate({left: 0}, 500, function(){
				_lock = false;
			});
		}else{
			$j('#banner' + _num).css({visibility: 'visible', left: -948});
			$j('#banner' + _page).animate({left: 948}, 500);
			$j('#banner' + _num).animate({left: 0}, 500, function(){
				_lock = false;
			});	
		}
		_page = _num;
	
		return false;
	}

	return {
		_init: _init,
		_open: _open,
		_list: _list,
		_move: _move
	}
}();

// gallery
_gallery = function(){
	var _width = 500;
	var _widthSmall = 156;
	var _interval = false;
	var _click = false;

	_init = function(_id, _size){
		if (_size == undefined) {
			_size = 1;
		}
		var _w = $j(_id + ' div b').width();
		$j(_id + 'Left').click(function(){
			_gallery._move(_id, 0, _size);
		});
		$j(_id + 'Right').click(function(){
			_gallery._move(_id, 1, _size);
		});			
		$j(_id + 'Right')[0].className = 'rightOn';
	}

	_move = function(_id, _type, _size){
		if(_click) return false;
		var _s = (_size)? _width : _widthSmall;
		var _w = $j(_id + ' div b').width();
		var _scroll = $j(_id + ' div')[0].scrollLeft;
		if(_scroll == 0 && _type == 0) return false;
		if(_scroll >= (_w - _s) && _type == 1) return false;

		_click = true;
		var _shift = (_type)? _widthSmall : 0 - _widthSmall;
		if(_size) _shift = (_type)? _width : 0 - _width;
		$j(_id + ' div').animate({scrollLeft: _scroll + _shift}, 300, function(){
			$j(_id + 'Left')[0].className = ((_scroll + _shift) > 0)? 'leftOn' : 'left';
			$j(_id + 'Right')[0].className = ((_scroll + _shift) < (_w - _s))? 'rightOn' : 'right';
			_click = false;
		});
	}

	return {
		_init: _init,
		_move: _move
	}
}();

// calendar
_calendar = function(){
	var _timeout = false;
	var _open = false;

	_init = function(_id){
		$j(_id + ' span').mouseover(function(){
			_calendar._event(this, 1);
		});
		$j('#calendarEvent div').ifixpng();
	}

	_event = function(_obj, _type){
		if(_type){
			clearTimeout(_timeout);
			if(_obj == null) return;

			_open = _obj.id;
			_close();
			
			var _text = '';
			if(document.getElementById(_open + '_text')){
				_text = $j('#' + _open + '_text').html();
			}else{
				return;
			}

			var _offset = $j('#' + _open).offset();
			var _w = $j('#' + _open).width();
			var _h = 0;
			if(_ie6){
				$j('#calendarEventTextIe6').html('<span><ul>' + _text + '</ul></span>');
				_h = $j('#calendarEventTextIe6').height();
				$j('#calendarEventText').height(_h);
				_h = $j('#calendarEventIe6').height();			
			}else{
				$j('#calendarEventText').html('<span><ul>' + _text + '</ul></span>');
				_h = $j('#calendarEvent').height();
			}
		
			$j('#calendarEvent').css({visibility: 'visible', top: (_offset.top + 5) - _h, left: (_offset.left + (_w / 2)) - 256});
			if(_ie6) $j('#calendarEventIe6').css({visibility: 'visible', top: (_offset.top + 5) - _h, left: (_offset.left + (_w / 2)) - 256});
		}else{
			_timeout = setTimeout('_calendar._close()', 500);
		}
	}
	
	_close = function(){
		$j('#calendarEvent').css({visibility: 'hidden'});
		if(_ie6) $j('#calendarEventIe6').css({visibility: 'hidden'});
	}
	
	return {
		_init: _init,
		_event: _event,
		_close: _close
	}
}(); 

// tree
_tree = function(){
	var _depth = 0;
	var _count = 0;
	var _parent = 0;

	_create = function(_class){
		var _html = '';
		if(_class == 'raTree') _html += '<table width="100%"><tr valign="top"><td width="50%">';
		_html += '<ul level="1" class="' + _class + '">';
		$j.each(_map, function(i){
			if(this.depth > 1 && this.depth > _depth){
				_html += '<li class="menu"><ul level="' + this.depth + '">';
				_count++;
			}
			
			if(this.depth < _depth){
                for(var i = 0; i < _depth - this.depth; i++){
                    _html += '</ul></li>';
                    _count--;
                }
            } 


			if(_class == 'raTree'){
				if(this.depth == 1){
					_parent++;
					if(_parent == 3) _html += '</ul></td><td><div style="width: 25px"><spacer /></div></td><td width="50%"><ul level="1" class="' + _class + '">';
				}
			}	
			
			_depth = this.depth;
			if (this.visible != 'false') {
				_html += '<li><a href="' + this.href + '">' + this.title + '</a></li>';
			}	
		});
		for(var i = 0; i < _count; i++) _html += '</ul></li>';
		_html += '</ul>';
		if(_class == 'raTree') _html += '</td></tr></table>';
		
		return _html;
	}
	
	return {
		_create: _create
	}
}();

// promo
_promo2 = function(){
	var _active = 0;
	var _count = 0;
	var _interval = false;
	var _speed = 10000;
    var _n = 0;
	_init = function(n){
		_count = 3;//$j('#promo .promo').length;

		_active = Math.floor(Math.random() * _count) + 1;

		
		$j('#promo2_' + _active).css({top: 0});

		$j('#promo2').mouseover(function(){
			_promo2._pause(0);			
		}).mouseout(function(){
			_promo2._pause(1);
		});
		
		_promo2._pause(1);
	}

	_move = function(){
		var _next = 0;
		if ( 1 < _count )
		{
			while(!_next){
				_next = Math.floor(Math.random() * _count) + 1;
				if(_next == _active) _next = 0;
			}
			$j('#promo2_' + _next).css({top: -70});
			$j('#promo2_' + _active).animate({top: 70}, 500);
			$j('#promo2_' + _next).animate({top: 0}, 500, function(){
				_active = _next;
			});
		}
	}
	
	_pause = function(_type){
		if(_type){
			_interval = setInterval('_promo2._move()', _speed);
		}else{
			clearInterval(_interval);
		}
	}

	return {
		_init: _init,
		_move: _move,
		_pause: _pause
	}
}();

_promo = function(){
	var _active = 0;
	var _count = 0;
	var _interval = false;
	var _speed = 10000;
    var _n = 0;
	_init = function(n){
		_count = 3;//$j('#promo .promo').length;

		_active = Math.floor(Math.random() * _count) + 1;

		
		$j('#promo_' + _active).css({top: 0});

		$j('#promo1').mouseover(function(){
			_promo._pause(0);			
		}).mouseout(function(){
			_promo._pause(1);
		});
		
		_promo._pause(1);
	}

	_move = function(){
		var _next = 0;
		if ( 1 < _count )
		{
			while(!_next){
				_next = Math.floor(Math.random() * _count) + 1;
				if(_next == _active) _next = 0;
			}
			$j('#promo_' + _next).css({top: -70});
			$j('#promo_' + _active).animate({top: 70}, 500);
			$j('#promo_' + _next).animate({top: 0}, 500, function(){
				_active = _next;
			});
		}
	}
	
	_pause = function(_type){
		if(_type){
			_interval = setInterval('_promo._move()', _speed);
		}else{
			clearInterval(_interval);
		}
	}

	return {
		_init: _init,
		_move: _move,
		_pause: _pause
	}
}();
