﻿jQuery(document).ready(function(){
	initHoverIE();
	initOpenBox();
	initTabs();

	$('.slideshow').cycle({
	    fx: 'fade',
	    random: 1
	});
	
	jQuery("#breadCrumb").jBreadCrumb({ easing: 'swing' });
	
	$("a[rel='videos']").colorbox({iframe:true, innerWidth:853, innerHeight:480});

	$('#gallery a').lightBox();

	// Use the each() method to gain access to each elements attributes
	$('area').each(function () {
	    $(this).qtip(
		  {
		      content: $(this).attr('alt'), // Use the ALT attribute of the area map
		      style: {
		          name: 'dark', // Give it the preset dark style
		          border: {
		              width: 0,
		              radius: 4
		          },
		          tip: true // Apply a tip at the default tooltip corner
		      }
		  });
	});
});
function initHoverIE(){
	jQuery('body').NVhoverIE({
		hoverItem:'div.list ul > li'
	});
}
jQuery.fn.NVhoverIE = function(_options){
	var _options = jQuery.extend({
		hoverItem:'div',
		hoverClass: 'hover'
		
	},_options);

	return this.each(function(){
		var _this = jQuery(this);
		var _hoverItem = jQuery(_options.hoverItem, _this);
		var _hoverClass = _options.hoverClass;
		if (jQuery.browser.msie && jQuery.browser.version < 7) {
			_hoverItem.each(function(){
				jQuery(this).hover(
					function(){
						jQuery(this).addClass(_hoverClass)
					},
					function(){
						jQuery(this).removeClass(_hoverClass)
					}
				)
			})
		}
	});
}
function initOpenBox(){
	jQuery('.slide-block').NVslideBlock({
		opener: '.open-close',
		slideBlock: '.slide-text',
		openClass:'active'
	});
}
jQuery.fn.NVslideBlock = function(_options){
	var _options = jQuery.extend({
		//Options plugin
		opener: '.opener',
		slideBlock: '.slide',
		openClass:'opened',
		animationSpeed: 500
	},_options);

	return this.each(function(){
		var _this = jQuery(this);
		var _opener = jQuery(_options.opener, _this);
		var _slideBlock = jQuery(_options.slideBlock, _this);
		var _openClass = _options.openClass;
		var _animationSpeed = _options.animationSpeed;
		if(!_slideBlock.length) return;
		if(_this.hasClass(_openClass)) _slideBlock.show();
		else _slideBlock.hide();
		_opener.bind('click', function(){
			if(_animationSpeed == 0){
				if(_this.hasClass(_openClass)){
					_slideBlock.hide();
					_this.removeClass(_openClass);
				}
				else {
					_slideBlock.show()
					_this.addClass(_openClass);
				};
			}
			else {
				if(_this.hasClass(_openClass)){
					_slideBlock.slideUp(_animationSpeed);
					_this.removeClass(_openClass);
				}
				else {
					_slideBlock.slideDown(_animationSpeed);
					_this.addClass(_openClass);
				};
			}
			return false;
		})
	});
}
function initTabs(){
	jQuery('.tabs-holder').NVTabs();
}
jQuery.fn.NVTabs = function(_options){
	var _options = jQuery.extend({
		opener: '.tabset > li > a',
		tabBox: 'div.tab-content',
		fadeTab: false,
		slideTab: false,
		autoRotate: false,
		swichTime: 5000,
		animationSpeed: 500,
		activeClass: 'active'
	},_options);

	return this.each(function(){
		var _this = jQuery(this);
		var _opener = jQuery(_options.opener, _this);
		var _tabBox = jQuery(_options.tabBox, _this);
		var _activeClass = _options.activeClass;
		var _fadeTab = _options.fadeTab;
		var _slideTab = _options.slideTab;
		var _swichTime = _options.swichTime;
		var _animationSpeed = _options.animationSpeed;
		var _autoRotate = _options.autoRotate;
		if(!_tabBox.length) return;
		var _tabCount = _tabBox.length;
		var ind = 0;
		var prevInd = ind;
		var _tabInfo, timer;
		_opener.each(function(i, el){
			if(_opener.eq(i).hasClass(_activeClass)) ind = i;
			prevInd = ind;
		})
		if(_fadeTab){
			_this.css({
				position: 'relative',
				height: _opener.parents('ul').outerHeight(true)+_tabBox.eq(ind).outerHeight(true)
			})
			_tabBox.css({
				position: 'absolute',
				top: _opener.parents('ul').outerHeight(true),
				left: 0,
				opacity: 0
			});
			_tabBox.eq(ind).css({
				opacity: 1
			})
		}
		else if(_slideTab){
			_tabInfo = jQuery('<div class="tabinfo-wrapper"></div>');
			_tabInfo.appendTo(_this);
			_this.css({
				width: _this.parent().width()
			});
			_tabBox.css({
				position: 'absolute',
				top: 0
			});
			_tabBox.show().each(function(i, el){
				jQuery(this).appendTo(_tabInfo);
				jQuery(this).css({
					left: jQuery(this).outerWidth(true)*i
				});
			});
			_tabInfo.css({
				position: 'relative',
				width: _tabBox.outerWidth(true)*_tabCount,
				left: -_tabBox.eq(ind).outerWidth(true)*ind,
				height: _tabBox.eq(ind).outerHeight(true)
			});
			_tabInfo.animate({
				height: _tabBox.eq(ind).outerHeight(true)
			}, { queue: false, duration: 100 });
		}
		else {
			_tabBox.hide().eq(ind).show();
		}
		function nextTab(){
			prevInd = ind;
			if( ind < _tabCount-1 ) ind++;
			else ind = 0;
			swichTab();
		}
		function prevTab(){
			prevInd = ind;
			if(ind > 0) ind--;
			else ind = _tabCount-1;
			swichTab();
		}
		function swichTab(){
			if(_fadeTab){
				_tabBox.eq(prevInd).animate({
					opacity: 0
				}, { queue: false, duration: _animationSpeed });
				_tabBox.eq(ind).show().animate({
					opacity: 1
				}, { queue: false, duration: _animationSpeed });
				_this.animate({
					height: _opener.parents('ul').outerHeight(true)+_tabBox.eq(ind).outerHeight(true)
				}, { queue: false, duration: 250 })
			}
			else if(_slideTab){
				_tabInfo.animate({
					left: -_tabBox.eq(ind).outerWidth(true)*ind,
					height: _tabBox.eq(ind).outerHeight(true)
				}, { queue: false, duration: _animationSpeed })
			}
			else {
				_tabBox.hide().eq(ind).show();
			}
			_opener.removeClass(_activeClass).eq(ind).addClass(_activeClass);
		}
		function autoSwich(){
			if(_autoRotate){
				if( timer ) clearTimeout(timer);
				timer = setTimeout(function(){
					nextTab();
					autoSwich();
				}, _swichTime)
			}
		}
		_opener.bind('click', function(){
			prevInd = ind;
			ind = _opener.index(this);
			if( !jQuery(this).hasClass(_activeClass) ){
				swichTab();
			}
			return false;
		})
		_this.hover(
			function(){
				if( timer ) clearTimeout(timer);
			},
			function(){
				if( timer ) clearTimeout(timer);
				autoSwich();
			}
		)
		autoSwich();
	});
}

