﻿$(document).ready(function(){
	ieHover("#nav li");
	clearInputs();
	initGallery();
	initTabs();
	colorGallery();
	initPopup();
});
function ieHover(_selector, _class){
	if(_class == null) _class = 'hover';
	if ($.browser.msie && $.browser.version < 7) {
		$(_selector).each(function(){
			$(this).mouseenter(function(){
				$(this).addClass(_class)
			}).mouseleave(function(){
				$(this).removeClass(_class)
			})
		})
	}
}
function initGallery(){
	$('div.gallery').each(function(){
		var gallerySet = $(this);
		var galleryItem = gallerySet.find('div.wrap > ul > li', gallerySet);
		var switcher = gallerySet.find('.switcher', gallerySet);
		var durTime = 500;
		var link = switcher.find('>li');
		var ind = link.index( galleryItem.filter('.active:eq(0)') );
		if (ind == -1) ind=0;
		
		var itemH=galleryItem.eq(ind).height();
		galleryItem.css({
			position: 'absolute',
			top: 0,
			left: 0
		});
		galleryItem.parents('ul').css({
			position : 'relative',
			height : itemH
		});
		galleryItem.hide().eq(ind).show();
		function slideList(){
			galleryItem.stop(false, true).fadeOut({ queue: false, duration: durTime }).eq( ind ).fadeIn({ queue: false, duration: durTime });
			switcher.find('>li').removeClass('active').eq(ind).addClass('active');
			itemH=galleryItem.eq(ind).height();
			galleryItem.parents('ul').animate({ height : itemH}, {duration: 500});
		}
		// Switcher
		link.click(function(){
			if( !$(this).hasClass('active') ){
				ind = link.index($(this));
				slideList();
			}
			return false;
		});
	});
}
function initTabs(){
	$('ul.tabset').each(function(){
		var _list = $(this);
		var _links = _list.find('li >a');
		_links.each(function() {
			var _link = $(this);
			var _href = _link.attr('href');
			var _tab = $(_href);
			if(_link.hasClass('active')) _tab.show();
			else _tab.hide();
			_link.click(function(){
				_links.filter('.active').each(function(){
					$($(this).removeClass('active').attr('href')).hide();
				});
				_link.addClass('active');
				_tab.show();
				return false;
			});
		});
	});
}
function initPopup(){
	if($('#overlay').length == 0) $('body').append('<div id="overlay"></div>');
	var _fader = $('#overlay').css({
		position: 'absolute',
		top: 0,
		left: 0,
		backgroundColor: '#000'
	});
	_fader.hide();
	var _popup = -1;
	$('a.link-popup').each(function(){
		var _el = this;
		if (_el.hash && _el.hash.length > 1) {
			_el._popup = $(_el.hash);
			if(_el._popup.length){
				_el.onclick = function(){
					if(_el._popup){
						_el._popup.hide();
						_popup = _el._popup;
						showPopup();
					}
					return false;
				}
				_el._popup.find('strong.close').click(function(){
					if($.browser.msie){
						_popup.hide();
						_fader.fadeOut(200);
						_popup = -1;
					} else {
						_popup.fadeOut(400, function(){
							_fader.fadeOut(200);
							_popup = -1;
						});
					}
					return false;
				});
			}
		}
	});
	_fader.click(function(){
		if(_popup != -1){
			if ($.browser.msie) {
				_popup.hide();
				_fader.fadeOut(200);
				_popup = -1;
			} else {
				_popup.fadeOut(400, function(){
					_fader.fadeOut(200);
					_popup = -1;
				});
			}
		}
		//Show select in IE6
		if ($.browser.msie && $.browser.version < 7) {
			$('#wrapper').find('select').show();
		}
		return false;
	});
	function showPopup(){
		_fader.css({
			opacity: 0,
			width: $('body').width(),
			height: initH(),
			display:'block'
		});
		_fader.fadeTo(400, 0.4, function(){
			if(_popup != -1){
				if($.browser.msie) {
					_popup.show() 
				} else _popup.fadeIn(200, function(){
					if(initH() < _popup.outerHeight()+_popup.offset().top) _fader.height(_popup.outerHeight()+_popup.offset().top);
				});
				//Hide select under overlay in IE6
				if ($.browser.msie && $.browser.version < 7) {
					$('#wrapper').find('select').hide();
				}
				if (_popup.height() < $('body').height() ) {
					_popup.css({
						top: $(window).scrollTop()+ ($(window).height() - _popup.outerHeight())/2,
						left: ($(window).width() - _popup.outerWidth())/2
					});
				}
				if(initH() < _popup.outerHeight()+_popup.offset().top) _fader.height(_popup.outerHeight()+_popup.offset().top);
			}
		});
		// $(window).scroll(function(){
			// _popup.animate({ top: $(window).scrollTop()+ ($(window).height() - _popup.outerHeight())/2 }, { queue: false, duration: 250 })
		// })
	}
	$(window).resize(function(){
		if( $(window).width() > $('#wrapper').outerWidth() ){
			_popup.css({ left: ($(window).width() - _popup.outerWidth())/2 })
		}
		_fader.css({ width: $('body').width() });
	})
	function initH(){
		var _h = $('#wrapper').outerHeight();
		if(_h < $(window).height()) _h = $(window).height();
		if(_h < $('html').height()) _h = $('html').height();
		return _h;
	}
}
function colorGallery(){
	$('div.lightbox-gallery').each(function(){
		var _hold=$(this);
		var slide=_hold.find('div.lightbox-gallery-holder > ul > li');
		var switcher = _hold.find('ul.switcher');
		var switchLink = switcher.find('li > a')
		var _radio = switcher.find('input:radio')
		var ind = _radio.index( _radio.filter(':checked') );
		if( ind == -1 ) ind = 0;
		function checker(){
			_radio.each(function(){
				if( $(this).attr('checked') ) {
					ind = _radio.index($(this));
					slideList();
				}
			});
		}
		_radio.change(checker)
		switchLink.bind('click', function(){
			ind = switchLink.index($(this));
			slideList();
			_radio.removeAttr().eq(ind).attr('checked', 'checked');
			return false;
		})
		var slideH=slide.eq(ind).height();
		var _active=ind;
		slide.css({
			position: 'absolute',
			top: 0,
			left: 0
		});
		slide.parents('ul').css({
			position : 'relative',
			height : slideH
		});
		function slideList(){
			slide.stop(false, true).hide().eq( ind ).show();
			slideH=slide.eq(ind).height();
			slide.parents('ul').css({ height : slideH }, {duration: 500});
		}
		slideList();
	})
}
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'));
		});
	});
}
