$(document).ready(function(){
	$('div.center-inner').width($('div.flash-area').outerWidth());
	
	$(window).resize(function(){
		$('div.center-inner').width($('div.flash-area').outerWidth());
		//$('div.flash-area').width($('html').outerWidth());
	})
	
	$('textarea').autogrow({minHeight: '300px'});
	
	$('div#gallery').gallSlide({
		duration: 700
	});
});

function initArea(){
	$('textarea.textarr').each(function(){
		var hold = $(this);
		var _h = hold.outerHeight(true);
		$('body').append('<div id="jquery-overlay"> </div>');
		var _fader = $('#jquery-overlay');
		_fader.css({
			position: 'absolute',
			top: 0,
			left: '-9999px',
			width: hold.outerWidth(),
			overflow: 'hidden',
			fontSize: '14px',
			lineHeight: '19px',
			fontFamily: 'Arial,Helvetica,sans-serif'
		});
		hold.keypress(function(){
			_fader.text(hold.attr('value'));
			alert(hold.attr('value'));
			if (_h < _fader.outerHeight(true)){
				
				hold.height(_fader.outerHeight(true));
				_h = hold.outerHeight(true);
			}
		})
	});
}
/*Gallery (slide-on-click) */
jQuery.fn.gallSlide = function(_options){
	// defaults options	
	var _options = jQuery.extend({
		duration: 700,
		autoSlide: 5000
	},_options);

	return this.each(function(){
		var _hold = $(this);
		var _speed = _options.duration;
		var _speedIMG = $.browser.msie ? 0 : _options.duration;
		var _timer = _options.autoSlide;
		var _wrap = _hold.find('div.holder > ul');
		var _el = _hold.find('div.holder > ul > li');
		var _next = _hold.find('a.link-next');
		var _prev = _hold.find('a.link-prev');
		var _holdIMG = $('div.visual > div.img > ul > li');
		var _count = _el.index(_el.filter(':last'));
		var _w = _el.outerWidth();
		var _wrapHolderW = Math.ceil(_wrap.parent().width()/_w);
		var _ost = 0;
		if ((Math.floor((_count+1) / _wrapHolderW)) < ((_count+1) / _wrapHolderW)) var _ost = _count + 1 - (Math.floor(_count/_wrapHolderW) * _wrapHolderW)
		if (_count == _wrapHolderW) _ost = 1;
		var _active = 0;
		var _activeIMG = _holdIMG.index(_holdIMG.filter('.active'));
		if (_activeIMG <0) _activeIMG = 0;
		var _tmp = _activeIMG;
		var _text = _hold.find('div.links span.mark');
		
		_holdIMG.removeClass('active').css({opacity: 0}).eq(_activeIMG).addClass('active').css({opacity: 1}).css('opacity','auto');
		
		function scrollEl(){
			_wrap.eq(0).animate({
				marginLeft: -(_w * _active) + "px"
			}, {queue:false, duration: _speed});
		}
		function fadeEl(){
			//_holdIMG.eq(_tmp).css('background-color','#000');
			_holdIMG.eq(_tmp).animate({
				opacity: 0
			}, {queue:false, duration: _speedIMG});
			//_holdIMG.eq(_tmp).fadeOut();
			//_holdIMG.eq(_activeIMG).fadeIn();
			_holdIMG.eq(_activeIMG).animate({
				opacity: 1
			}, {queue:false, duration: _speedIMG, complete: function(){
				$(this).css('opacity','auto');
			}});
			
			_holdIMG.removeClass('active').eq(_activeIMG).addClass('active');
			_el.removeClass('active').eq(_activeIMG).addClass('active');
			_text.text(_activeIMG+1);
			_tmp = _activeIMG;
		}
		_el.click(function(){
			_activeIMG = _el.index($(this));
			if ((_activeIMG - _active) == 4){
				_active++;
				if (_active <= _count - _wrapHolderW + 1) scrollEl()
				else _active = _count - _wrapHolderW + 1
			}
			if ((_activeIMG - _active) == 0){
				_active--;
				if (_active >= 0) scrollEl()
				else _active = 0;
			}
			fadeEl();
			return false;
		});
		_next.click(function(){
			if (_active == (_count - _wrapHolderW + 1)) _active = 0 - _wrapHolderW;
			for (var i = 0; i < _wrapHolderW; i++){
				if (_active < (_count - _wrapHolderW + 1)) _active ++;
			};
			scrollEl();
			return false;
		});
		
		_prev.click(function(){
			if (_active == 0) _active = (_count+1);
			for (var i = 0; i < _wrapHolderW; i++){
				if (_active > 0) _active --;
			};
			scrollEl();
			return false;
		});
	});
}