jQuery(function(){
	ieHover('div.image, ul.products > li, #nav > li');
	initPageGallery();
	jQuery('ul.tabset').jqueryTabs();
})

// ieHover
function ieHover(_selector, _class){
	if(_class == null) _class = 'hover';
	if (jQuery.browser.msie && jQuery.browser.version < 7) {
		jQuery(_selector).each(function(){
			jQuery(this).mouseenter(function(){
				jQuery(this).addClass(_class)
			}).mouseleave(function(){
				jQuery(this).removeClass(_class)
			})
		})
	}
}

// initAction
function initAction(o){
	jQuery('ul.tabset', o).jqueryTabs();
}

// initPageGallery
function initPageGallery(){
	var _holder = jQuery('#page-slider'),
		_page = jQuery('> .slide-box', _holder),
		_btnNext = jQuery('#switcher-page .next'),
		_btnPrev = jQuery('#switcher-page .prev'),
		_has = window.location.hash.replace('#', ''),
		_speed = 1000,
		_activeIndex = 0,
		_oldIndex = -1,
		_patch = '',
		_loadingClass = 'loading';
	var _wSlB = _page.eq(0).outerWidth();
	_page.eq(0).clone().appendTo(_holder);
	
	_page = jQuery('> .slide-box', _holder);
	var _max = _page.length;
	var _wSl = _wSlB * _max;
	var _loader = _holder.siblings('.ajax-loader').hide();
	
	if(_page.length) {
		_page.each(function(){
			this.setAttribute('location', this.title);
			this.removeAttribute('title');
		})
		/*if(!_has){
			window.location.hash = _page.eq(0).attr('location');
			_activeIndex = 0;
		} else {
			_activeIndex = _page.index(jQuery('[location=' + _has + ']'));
			if(_activeIndex == _page.length - 1) _oldIndex = 0;
			_has = window.location.hash.replace('#', '');
		}
		_holder.css('marginLeft',-_activeIndex*_wSlB)*/

		_activeIndex = _page.index(jQuery('[class=slide-box active]'));
		if (_activeIndex == -1) _activeIndex = 0;

		var _f = false;
		var _t;

		function Slide(_new){ 
			//alert(_new);
			if(_t) clearTimeout(_t);
			_loader.show();
			_t = setTimeout(function(){
				_loader.hide();
				_holder.animate({marginLeft: -_new*_wSlB}, {queue:false, duration:_speed, complete:function(){
					_page.removeClass('active');
					_page.eq(_new).addClass('active');
				}});
			}, 400)
			
		};
		function loadContent(nextBox){
			_holder.css({
				height: _holder.height()
			});
			if (_page.eq(nextBox).data('completeload') || _page.eq(nextBox).attr('location') == ''){
				if (_page.eq(nextBox).attr('location') != '') {
					window.location.hash = _page.eq(nextBox).attr('location');
				}
				Slide(nextBox);
			} else {
				var _targetBox = _page.eq(nextBox);
				_holder.addClass(_loadingClass);
					jQuery.ajax({
						type: 'POST',
						dataType: 'html',
						data: 'ajax=1',
						url: _patch + _targetBox.attr('location'),
						success: function(obj){
							var _content = jQuery(obj);
							_targetBox.append(_content);
							_targetBox.data('completeload', 'true');
							initAction(_targetBox);
							_holder.css({
								height: 'auto'
							}).removeClass(_loadingClass);
							if(nextBox != 0) jQuery('body').eq(0).addClass('inner-page');
							
							else jQuery('body').eq(0).removeClass('inner-page');
							
							window.location.hash = _page.eq(nextBox).attr('location');
							if(_f){
								if (nextBox == _max - 1) {
									if (_page.eq(0).html() != '') {
										loadContent(0);
									}
								}
								if (nextBox != 0){Slide(nextBox);}
							}else{
								if (nextBox == 0) {
									if (_page.eq(_max-1).html() != '') {
										loadContent(_max-1);
									}
								}
								if (nextBox != _max-1){Slide(nextBox);}
							}
						},
						error: function(){
							alert('AJAX Error');
						}
					})
			}
		}
		loadContent(_activeIndex);
		

		_btnNext.click(function(){//alert(1)
			_f = true;
			if(!_holder.hasClass(_loadingClass)){
				if(_activeIndex < _page.length -1){
					_oldIndex = _activeIndex;
					_activeIndex++;
				} else {
					_holder.css('marginLeft',0);
					_activeIndex = 1;
				}
				loadContent(_activeIndex);
			}
			return false;
		})
		
		_btnPrev.click(function(){
			_f = false;
			if(!_holder.hasClass(_loadingClass)){
				if(_activeIndex > 0){
					_oldIndex = _activeIndex;
					_activeIndex--;
				} else {
					_holder.css('marginLeft',-(_max-1)*_wSlB)
					_activeIndex = _max-2;
				}
				loadContent(_activeIndex);
			}
			return false;
		})
	}
}

// jquery tabs plugin
jQuery.fn.jqueryTabs = function(_options){
	// default options
	var _options = jQuery.extend({
		addToParent:false,
		holdHeight:true,
		activeClass:'active',
		tabLinks:'a.tab',
		fadeSpeed:0,
		event:'click'
	},_options);

	return this.each(function(){
		var _holder = jQuery(this);
		var _fadeSpeed = _options.fadeSpeed;
		var _activeClass = _options.activeClass;
		var _addToParent = _options.addToParent;
		var _holdHeight = _options.holdHeight;
		var _tabLinks = jQuery(_options.tabLinks, _holder);
		var _tabset = (_addToParent ? _tabLinks.parent() : _tabLinks);
		var _event = _options.event;
		var _animating = false;

		// tabs init
		_tabLinks.each(function(){
			var _tmpLink = jQuery(this);
			var _tmpTab = jQuery(this.hash);
			var _classItem = (_addToParent ? _tmpLink.parent() : _tmpLink);
			if(_tmpTab.length) {
				if(_classItem.hasClass(_activeClass)) _tmpTab.show();
				else _tmpTab.hide();
			}
		});

		// tab switcher
		function switchTab(_switcher) {
			if(!_animating) {
				var _link = jQuery(_switcher);
				var _newItem = (_addToParent ? _link.parent() : _link);
				var _newTab = jQuery(_link.get(0).hash);
				if(_newItem.hasClass(_activeClass)) return;

				var _oldItem = jQuery(_addToParent ? _tabset : _tabLinks).filter('.'+_activeClass);
				var _oldTab = jQuery(jQuery(_addToParent ? _oldItem.children('a') : _oldItem).get(0).hash);
				if(_newTab.length) {
					_animating = true;
					if(_oldItem.length) {
						_newItem.addClass(_activeClass);
						_oldItem.removeClass(_activeClass);

						var _parent = _oldTab.parent();
						if(_holdHeight) _parent.css({height:_parent.height()});

						_oldTab.fadeOut(_fadeSpeed,function(){
							_newTab.fadeIn(_fadeSpeed,function(){
								_animating = false;
							});
							if(_holdHeight) _parent.css({height:'auto'});
						});
					} else {
						_newItem.addClass(_activeClass);
						_newTab.fadeIn(_fadeSpeed,function(){
							_animating = false;
						});
					}
				}
			}
		}

		// control
		_tabLinks.each(function(){
			jQuery(this).bind(_event,function(){
				switchTab(this);
				return false;
			});
		});
	});
}
