$(document).ready(function(){
	runIntroduction();
	jQuery('div.main-frame-wrapper').fadeGallery({
		listSelector: '> div.main-frame',
		navHolder:		'div.switcher',
		navCreate:		true,
		thumbsSelector: 'li',
		swichTime:		10000,
		delay:			1000
	});
	initAction();
});

/*fadeGallery*/
jQuery.fn.fadeGallery = function(_options){
	var _options = jQuery.extend({
		listSelector: '> li',
		navHolder:		false,
		navCreate:		false,
		thumbsSelector: 'li',
		prev:			'a.prev',
		next:			'a.next',
		swichTime:		false,
		delay:			900,
		fadeIEfix:		false,
		onChange:		null
	},_options);
	return this.each(function(){
		var _swichTime = _options.swichTime;
		var _d = (_options.fadeIEfix) ? ($.browser.msie ? 0 : _options.delay) : (_options.delay);
		var _this = $(this);
		this.pause = false;
		var _list = $(_options.listSelector, _this);
		var _linksHold = $(_options.navHolder, _this);

		if(_options.navCreate){
			var _htmlNav ='<ul>';
			for(var i=0; i<_list.length; i++) {
				_htmlNav += '<li><a href="#">'+(i+1)+'</a></li>';
			}
			_htmlNav +='</ul>';
			_linksHold.html(_htmlNav);
		}
		var _links = $(_options.thumbsSelector, _linksHold);
		var _btnPrev = $(_options.prev , _this);
		var _btnNext = $(_options.next , _this);
		var _a = _list.index(_list.filter('.active:eq(0)'));
		if(_a == -1) _a = 0;
		var _t;
		_list.find('.intro').each(function(i){
			if(i!=_a){
				$(this).css({
					left: -$(this).width()
				});
			};
		});
		_list.removeClass('active').css({display: 'none', opacity: 0}).eq(_a).addClass('active').css({display: 'block', opacity: 1}).each(function(){
			if($.browser.msie) $(this).css('opacity', 'auto');
		});
		_links.eq(_a).addClass('active');
		autoSwitch();
		function autoSwitch(){
			if (_swichTime && !this.pause){
				_t = setTimeout(function(){
					if(_a < _list.length - 1) changeEl(_a + 1);
					else changeEl(0);
				}, _swichTime);
			}
		}

		this.stop = function(){
			if(_t) clearTimeout(_t);
			this.pause = true;
		};
		this.play = function(){
			this.pause = false;
			autoSwitch();
		};

		if (_btnPrev){
			_btnPrev.click(function(){
				var _prevItem = 0;
				if (_a > 0) _prevItem = _a-1;
				else _prevItem = _list.length-1;
				changeEl(_prevItem);
				return false;
			})
		}
		if (_btnNext){
			_btnNext.click(function(){
				var _nextItem = 0;
				if (_a < _list.length - 1) _nextItem = _a+1;
				else _nextItem = 0;
				changeEl(_nextItem);
				return false;
			})
		}

		if(_links){
			_links.click(function(){
				var _ind = _links.removeClass('active').index($(this).addClass('active'));
				changeEl(_ind);
				return false;
			})
		}
		function changeEl(_ind){
			if(_t) clearTimeout(_t);
			if(_list.is(':animated')) _list.stop(true, true);
			if(_ind != _a){
				_links.removeClass('active').eq(_ind).addClass('active');
				//text
				_list.eq(_a).find('.intro').animate({left: -_list.eq(_a).find('.intro').width()},{duration: _d/2,complete: function(){
					_list.eq(_ind).find('.intro').animate({left: 0},{duration: _d/2});
				}});
				//fade
				_list.eq(_ind).css({opacity: 0, zIndex: 10,display:'block'}).animate({opacity: 1}, {queue:false, duration:_d,complete:function(){
					_list.eq(_a).removeClass('active').css({opacity: 0,display:'none'});
					if($.browser.msie) $(this).css('opacity', 'auto');
					$(this).css({
						zIndex: ''
					}).addClass('active');
					_a = _ind;
					autoSwitch();
					if (jQuery.isFunction(_options.onChange)) {
						_options.onChange.apply(_this);
					};
				}});
			}
		}
	});
};

function runIntroduction(){  
	var marginChange = '';
	var randomNumber = 0;
		
	$('.introduction > .image-box').each(function() {
		var marginChange = '';
		var randomNumber = Math.floor(Math.random()*4)
		var randomDelay = 3000+(Math.floor(Math.random()*1501));
		switch (randomNumber)
		{
			case 0:
			  $(this).find("img").delay(randomDelay).animate({ 
					marginTop: '-150px'
				}, 1500 );
			  break;
			case 1:
			  $(this).find("img").delay(randomDelay).animate({ 
					marginTop: '150px'
				}, 1500 );
			  break;
			case 2:
			  $(this).find("img").delay(randomDelay).animate({ 
					marginLeft: '-192px'
				}, 1500 );
			  break;
			case 3:
			  $(this).find("img").delay(randomDelay).animate({ 
					marginLeft: '192px'
				}, 1500 );
			  break;
		}
	});
	
	setTimeout(function() {
		$('.introduction').css('display','none');
	}, 6000);
}

function initAction(){
	var speed = 700;
	var _startDalay = 1000;
	var _fader = $('#fader');
	var _t = null;
	var gal1 = $('div.main-frame-wrapper').get(0);
	$('#footer').each(function(){
		var _links = $(this).find('a.tab');
		var _tabs = $('.content', this);
		var _a = null;
		var _content = $('.scrollarea',_tabs);
		_tabs.each(function(){
			$(this).css({
				display: 'block',
				marginBottom: - $(this).height()
			});
		});
		var _winloc = window.location.href;
		if(_winloc.indexOf('#')) _winloc = _winloc.split('#')[0];
		if(window.location.hash && window.location.hash.length > 1){
			_t = setTimeout(function(){
				var _tabname = window.location.hash.substr(1);
				_a = _tabs.filter('[title='+_tabname+']');
				_links.removeClass('active').filter('[title='+_tabname+']').addClass('active').css('color','#fff');
				Cufon.refresh('ul.menu a');
				Cufon.refresh('ul.nav a');
				_fader.stop().fadeTo(speed/2, 0.5);
				_content.css('overflow','hidden');
				_a.animate({marginBottom : 0},{duration: speed, complete: function(){
					_content.css('overflow','auto');
				}});
			},_startDalay)
			gal1.stop();
		};
		_links.click(function(){
			if(_t) clearTimeout(_t);
			var _cur = $(this);
			var this_title = _cur.attr('title');
			var this_tab = _tabs.filter('[title *= ' + this_title + ' ]');
			if(!_a) _fader.stop().fadeTo(speed/2, 0.5);
			if($(this).hasClass('active')){
				closePopup();
			}else{
				gal1.stop();
				if(_a){
					_content.css('overflow','hidden');
					_a.stop().animate({
						marginBottom : -_a.height()
					},{duration: speed/2, complete: function(){
						_links.removeClass('active').css('color','#B2B2B2');
						_cur.addClass('active').css('color','#fff');
						Cufon.refresh('ul.menu a');
						Cufon.refresh('ul.nav a');
						_a = this_tab;
						this_tab.stop().animate({
							marginBottom : 0
						}, {duration: speed/2, complete: function(){
							_content.css('overflow','auto');
							window.location.href =_winloc+ _cur.attr('href');
						}});
					}});
				}else{
					_content.css('overflow','hidden');
					_a = this_tab;
					_links.removeClass('active').css('color','#B2B2B2');
					_cur.addClass('active').css('color','#fff');
					Cufon.refresh('ul.menu a');
					Cufon.refresh('ul.nav a');
					this_tab.stop().animate({
						marginBottom : 0
					}, {duration: speed, complete: function(){
						_content.css('overflow','auto');
						window.location.href =_winloc+ _cur.attr('href');
					}});
				};
				
			}
			return false;
		});
		$(this).find('div.close a').click(function(){
			closePopup();
			return false;
		});
		$('#fader').click(function(){
			closePopup();
			return false;
		});
		$('ul.menu a, ul.nav a').mouseenter(function(){
			$(this).css({color: '#fff'});
			Cufon.refresh('ul.menu a');
			Cufon.refresh('ul.nav a');
		}).mouseleave(function(){
			$(this).css({color: ''});
			Cufon.refresh('ul.menu a');
			Cufon.refresh('ul.nav a');
		});
		function closePopup(){
			_tabs.each(function(){
				_content.css('overflow','hidden');
				$(this).stop().animate({
					marginBottom : -$(this).height()
				}, {duration: speed, complete: function(){
					_fader.stop().fadeTo(speed/2, 0, function(){
						_fader.hide();
						gal1.play();
						_links.removeClass('active').css('color','#B2B2B2');
						Cufon.refresh('ul.menu a');
						Cufon.refresh('ul.nav a');
						_a = null;
						_content.css('overflow','auto');
						gal1.play();
						//window.location.href =_winloc+'#';
					});
				}});
			});
		}
	});
}
