/*---- clear inputs ---*/
function clearInputs(){
	jQuery('input:text, input:password, textarea').each(function(){
		var _el = jQuery(this);
		_el.data('val', _el.val());
		_el.bind('focus', function(){
			if(_el.val() == _el.data('val')) _el.val('');
		}).bind('blur', function(){
			if(_el.val() == '') _el.val(_el.data('val'));
		});
	});
}
jQuery(function(){
	clearInputs();
	imgHide();
	initNavigation();
	gallery1 = jQuery('div.cite-hold').fadeGallery({
		slides : 'div.cite-box',
		autoplay : false,
		switchTime : 2000,
		duration : 1000
	});
	initSuperSized();
});

jQuery(window).load(function(){
	imgShow();
})

function initNavigation(){
	jQuery('.navigation li').hover(function(){
		jQuery(this).addClass('hover');
	},
	function(){
		jQuery(this).removeClass('hover');
		setTimeout(function(){Cufon.refresh('ul.navigation > li > a')}, 10);
	});
}

function initSuperSized() {
	jQuery('#supersize.home').superPuperSized({
		activeClass:'active',
		slides:'>li',
		slideImages:'>img',
		transition: 1,
		pagerLinks: '.page-visual ul.numbers li',
		verticalAlign: false,
		autoSlide: true,
		switchTime: 7000,
		minWidth: 1000,
		animSpeed: 1000
	});
}

// stretch background plugin
jQuery.fn.superPuperSized = function(_options){
	var _options = jQuery.extend({
		loadingClass: 'loading',
		activeClass:'active',
		slides:false,
		slideImages:'>img',
		verticalAlign: true,
		pagerLinks: false,
		transition: 0,
		animSpeed: 600,
		switchTime: 5000,
		onChange: false,
		autoSlide: false,
		minWidth: false
	},_options);

	return this.each(function(){
		// options
		var _holder = jQuery(this);
		var _animSpeed = _options.animSpeed;
		var _verticalAlign = _options.verticalAlign;
		var _activeClass = _options.activeClass;
		var _loadingClass = _options.loadingClass;
		var _autoSlide = _options.autoSlide;
		var _switchTime = _options.switchTime;
		var _transition = _options.transition;
		var _onChange = _options.onChange;
		var _minWidth = (_options.minWidth ? _options.minWidth : 0);
		var _slides = (_options.slides ? jQuery(_options.slides, _holder) : _holder.children());
		var _slideImages = jQuery(_options.slideImages, _holder);
		if(!_slides.length) return;

		// wait until all images loaded
		function loadImages(cb) {
			var _loadFlag = false;
			// all images loaded (way1)
			_window.load(function(){
				if(!_loadFlag) {
					_loadFlag = true;
					clearInterval(_loadTimer);
					_slideImages.each(function(){
						if(this.complete) {
							jQuery(this).attr('ratio',jQuery(this).attr('width') / jQuery(this).attr('height'));
						}
					});
					if(typeof cb === 'function') cb();
				}
			})

			// all images loaded (way2)
			var _loadTimer = setInterval(function(){
				var _loadedCount = 0;
				_slideImages.each(function(){
					if(this.complete) {
						jQuery(this).attr('ratio',jQuery(this).attr('width') / jQuery(this).attr('height'));
						_loadedCount++;
					}
				});
				if(_loadedCount == _slideImages.length && typeof cb === 'function') {
					_loadFlag = true;
					clearInterval(_loadTimer);
					cb();
				}
			},500);
		}

		// resize slides function
		function resizeSlides() {
			var _windowWidth = _window.width()-7;
			var _windowHeight = _window.height();
			_holder.css({
				width: (_windowWidth > _minWidth ? _windowWidth : _minWidth),
				height: _page.height() -7,
				overflow: 'hidden'
			});
			
			if(_windowWidth < _minWidth) _windowWidth = _minWidth;
			
			// resize slides to proper ratio
			_slides.each(function(){
				var _slide = jQuery(this);
				var _image = jQuery(_options.slideImages, _slide);
				var _ratio = 1.315;
				var _slideWidth = _windowWidth;
				var _slideHeight = _slideWidth/_ratio;
				if(_slideHeight < _windowHeight) {
					_slideHeight = _windowHeight;
					_slideWidth = _slideHeight * _ratio;
				}
				_slide.css({
					width:_slideWidth,
					height:_slideHeight
				});
				_image.css({
					width:_slideWidth,
					height:_slideHeight
				});
				if (_verticalAlign){
					_image.css('left', (_windowWidth - _image.width())/2);
					_image.css('top', (_windowHeight - _image.height())/2);
				}
			});
		}

		// init supersize
		var _window = jQuery(window);
		var _page = jQuery('.page');
		var _defaultWidth, _defaultHeight;

		_holder.addClass(_loadingClass);
		loadImages(function(){
			_holder.removeClass(_loadingClass);
			_window.resize(resizeSlides);
			resizeSlides();
			autoSlide();
		})

		// gallery behavior
		var _pagerLinks = jQuery(_options.pagerLinks, _holder);
		var _slidesCount = _slides.length;
		var _currentIndex = 0;
		var _oldIndex = _currentIndex;
		var _timer;

		// init gallery
		if(!_pagerLinks.length) _pagerLinks = jQuery(_options.pagerLinks);
		_slides.hide().eq(_currentIndex).show();

		// gallery controls
		if(_pagerLinks.length) {
			_pagerLinks.each(function(ind){
				jQuery(this).click(function(){
					numSlide(ind);
					return false;
				});
			});
		}
		function numSlide(num) {
			if(_currentIndex != num) {
				_oldIndex = _currentIndex;
				_currentIndex = num;
				if(_slides.length > 1) switchSlide();
			}
		}
		function prevSlide() {
			_oldIndex = _currentIndex;
			if(_currentIndex > 0) _currentIndex--;
			else _currentIndex = _slidesCount-1;
			if(_slides.length > 1) switchSlide();
		}
		function nextSlide() {
			_oldIndex = _currentIndex;
			if(_currentIndex < _slidesCount-1) _currentIndex++;
			else _currentIndex = 0;
			if(_slides.length > 1) switchSlide();
		}

		// gallery animation
		if(_slides.length > 1){
			function switchSlide() {
			var _oldSlide = _slides.eq(_oldIndex);
			var _newSlide = _slides.eq(_currentIndex);
			gallery1.showNextSlide();
			
			
			switch (_transition) {
				// fade transition
				case 1:
					_oldSlide.fadeOut(_animSpeed);
					_newSlide.fadeIn(_animSpeed)
					break;
				default:
					_oldSlide.hide();
					_newSlide.show();
			}

			refreshStatus();
			autoSlide();
		}
		};
		function refreshStatus() {
			if(_pagerLinks.length) _pagerLinks.removeClass(_activeClass).eq(_currentIndex).addClass(_activeClass);
			if(typeof _onChange === 'function') _onChange(_holder, _slides, _currentIndex);
		}
		function autoSlide() {
			if(_timer) clearTimeout(_timer);
			if(_autoSlide) _timer = setTimeout(nextSlide,_switchTime);
		}
		refreshStatus();

	});
}


jQuery.fn.fadeGallery = function(options) {return new fadeGallery(jQuery(this).get(0), options)}

function fadeGallery(element, options) {this.init(element,options)}	

fadeGallery.prototype = {
	init: function(element,_options){
		var _obj = this
		var options = jQuery.extend({
			prev : 'a.prev',
			next : 'a.next',
			slides : 'div.holder ul > li',
			switcher : 'ul.switcher li',
			createSwitcher : 'div.switcher',
			autoheight : false,
			autoplay : true,
			functionAfterSlide : null,
			functionAfterAll : null,
			switchTime : 2000,
			duration : 700
		}, _options);
		
		_obj.holder = jQuery(element);
		_obj.slides = jQuery(options.slides, _obj.holder);
		_obj.prev = jQuery(options.prev, _obj.holder);
		_obj.next = jQuery(options.next, _obj.holder);
		_obj.autoplay = options.autoplay;
		_obj.duration = options.duration;
		_obj.switchTime = options.switchTime;
		_obj.functionAfterAll = options.functionAfterAll;
		_obj.functionAfterSlide = options.functionAfterSlide;
		_obj.autoheight = options.autoheight;
		_obj.slider = _obj.slides.eq(0).parent();
		_obj.timer;
		
		_obj.createSwitcher = jQuery(options.createSwitcher, _obj.holder);
		if (_obj.createSwitcher.length) {
			var switcher = jQuery('<ul></ul>');
			for (var i = 0; i < _obj.slides.length; i++ ) {
				switcher.append('<li><a href="#">'+(i+1)+'</a></li>')
			}
			_obj.createSwitcher.empty().append(switcher);
			_obj.switcher = jQuery('ul > li', _obj.createSwitcher);
		} else {
			_obj.switcher = jQuery(options.switcher, _obj.holder);
		}
		
		//search active slide
		_obj.startSlide = _obj.slides.filter('.active');
		if (_obj.slides.index(_obj.startSlide) != -1) {
			_obj.current = _obj.startSlide.index();
		} else {
			_obj.current = 0;
			_obj.startSlide = 0;
			_obj.slides.eq(0).addClass('active');
		}
		
		//set default styles
		if (!jQuery.browser.msie) {
			_obj.slides.css({opacity:0, display:'none', position:'absolute'});
			_obj.slides.eq(_obj.current).css({opacity:1, display:'block'});
		} else {
			_obj.slides.css({display:'none', position:'absolute'});
			_obj.slides.eq(_obj.current).css({display:'block'});
		}
		
		_obj.switcher.eq(_obj.current).addClass('active');
		if (_obj.autoheight) _obj.slider.css('height', _obj.slides.eq(_obj.current).height());
		
		//events
		_obj.switcher.each(function(i){
			jQuery(this).click(function(e){
				if (_obj.current != i) {
					_obj.move(i);
				}
				e.preventDefault();
			});
		});
		
		_obj.next.click(function(e){
			_obj.move(_obj.nextElem());
			e.preventDefault();
		});
		
		_obj.prev.click(function(e){
			_obj.move(_obj.prevElem());
			e.preventDefault();
		});
		
		if (_obj.autoplay) {
			_obj.timer = setTimeout(function(){
				_obj.move(_obj.nextElem());
			}, _obj.switchTime);
		}
	},
	
	nextElem: function(){
		var _obj = this;
		var tmp = _obj.current;
		if (tmp <  _obj.slides.length-1) tmp++;
		else tmp = 0;
		return tmp;
	},
	
	prevElem : function(){
		var _obj = this;
		var tmp = _obj.current;
		if (tmp > 0) tmp--;
		else tmp = _obj.slides.length-1;
		return tmp;
	},
	
	move : function(_ind){
		var _obj = this
		clearTimeout(_obj.timer);
		
		if (!jQuery.browser.msie) {
			_obj.slides.eq(_obj.current).stop().removeClass('active').animate({opacity:0},_obj.duration, function(){
					jQuery(this).css({ display: 'none'});
					if (jQuery.isFunction(_obj.functionAfterSlide)) _obj.functionAfterSlide();
					if (_obj.autoplay) {
						_obj.timer = setTimeout(function(){
							if (jQuery.isFunction(_obj.functionAfterSlide)) _obj.functionAfterSlide();
							
							if ((_ind == _obj.slides.length-1) && jQuery.isFunction(_obj.functionAfterAll)) _obj.functionAfterAll();
							
							if (_obj.autoplay) _obj.move(_obj.nextElem());
						}, _obj.switchTime);
					}				
				})
			
			_obj.slides.eq(_ind).stop().addClass('active').css({display:'block'}).animate({opacity:1},_obj.duration);
			
		} else {
			_obj.slides.eq(_obj.current).removeClass('active').css({ display: 'none'});
			_obj.slides.eq(_ind).addClass('active').css({display:'block'});
		}
		
		_obj.switcher.eq(_obj.current).removeClass('active').end().eq(_ind).addClass('active');
		
		if (_obj.autoheight) _obj.slider.stop().animate({height : _obj.slides.eq(_ind).height()},_obj.duration);
		
		
		
		
		
		_obj.current = _ind;
		
	},
	
	showNextSlide : function(){
		var _obj = this;
		_obj.move(_obj.nextElem());
	},
	
	toggleAutorotation : function(){
		var _obj = this;
		clearTimeout(_obj.timer);
		if (_obj.autoplay) _obj.autoplay = false
		else {
			_obj.autoplay = true;
			_obj.timer = setTimeout(function(){
				_obj.move(_obj.nextElem());
			}, _obj.switchTime);
		}
	},
	
	showSlide : function(_ind){
		var _obj = this;
		_obj.move(_ind);
	}
	
}
function imgHide(){
	jQuery('img').css('opacity',0);
	
}
function imgShow(){
	jQuery('img').animate({opacity:1},800);
}
