$j = jQuery.noConflict();

jQuery(window).load(function(){
	initNav({
		menuId: "nav",
		hoverClass: "hover",
		dropExistenceClass: "has-drop-down",
		sideClasses: true,
		cleverMode: true,
		flexibility: true,
		menuPaddings: 10
	});
	var _f = (jQuery.browser.msie && jQuery.browser.version < 8) ? false : true;
	
	jQuery("#nav a.inactive")
		.css({
			cursor: "default"
		})
		.click(function(){
			return false;
		});
	
	jQuery('#nav > li').each(function(){
		var link = jQuery(this);
		jQuery(this).data('w', jQuery(this).find('.drop').outerWidth(true));
		jQuery(this).data('wContent', jQuery(this).find('.drop .content').outerWidth(true));
		jQuery(this).data('wCol', jQuery(this).find('.drop .col').outerWidth(true));
		
		if(jQuery(this).find('.drop').length && jQuery(this).find('.subHold').length) {
			jQuery(this).find('.drop').css({width:jQuery(this).data('w')-223});
			jQuery(this).find('.drop .content').css({
				width:jQuery(this).data('wCol')+5,
				'float':'left'
			});
			jQuery(this).find('.subHold').css({width:0});
		}
	});
	
	jQuery('#nav li').each(function(){
		var _this = jQuery(this);
		_this.removeClass('hover');
		_this.mouseenter(function(){
			_this.addClass('hover');
		}).mouseleave(function(){
			_this.removeClass('hover');
		});
	});
	
	jQuery("#nav .sub-category").hide();

	if(!_f){
		jQuery('#nav > li').each(function(){
			var li = jQuery(this);
			var drop = li.find('.drop');
			var dropW = drop.find('.content').width();
			drop.find('.top, .bottom').css({width:dropW});
			drop.find('.right, .top-right, .bottom-right').css({left:dropW+22});
			li.find('li').mouseenter(function(){
				if(jQuery(this).find('> .sub-drop').length) {
					jQuery(this).find('.sub-drop').show();
					drop.find('.top, .bottom').css({width:dropW+223});
					drop.find('.right, .top-right, .bottom-right').css({left:dropW+245});
					drop.find('.content').css({width:dropW+223});
					jQuery(this).parents('.content').find('.subHold').width(223);
				}
			}).mouseleave(function(){
				if(jQuery(this).parent().parent().hasClass('col') && jQuery(this).find('> .sub-drop').length){
					jQuery(this).find('.sub-drop').hide();
					drop.find('.top, .bottom').css({width:dropW});
					drop.find('.right, .top-right, .bottom-right').css({left:dropW+22});
					drop.find('.content').css({width:dropW});
					jQuery(this).parents('.content').find('.subHold').width(0);
				}
			});
		});
	}
	else{
		var _t;
		jQuery('#nav > li li').each(function(){
			var _this = jQuery(this);
			jQuery(this).css('position', 'static');
			_this.mouseenter(function(){
				if(jQuery(this).find('> .sub-drop').length) {
					_this.parents('.drop').css({width:_this.parents('li').data('w')});
					_this.parents('.drop .content').css({width:_this.parents('li').data('wContent')+5});
					_this.parents('li').find('.subHold').stop().css({width:223});
					_this.find('.sub-drop').show();
					jQuery("#nav .sub-category").show();
				}
			}).mouseleave(function(){
				if(_this.parent().parent().hasClass('col') && jQuery(this).find('> .sub-drop').length){
					_this.find('.sub-drop').hide();
					_this.parents('.drop').css({width:_this.parents('li').data('w')-223});
					_this.parents('.drop .content').css({width:_this.parents('li').data('wCol')+5});
					_this.parents('li').find('.subHold').css({width:0});
					jQuery("#nav .sub-category").hide();
				}
			});
		});
	}
});

jQuery(function(){
	initDrop();
	initFancyHoverNav();
	initFancyHover();
	initFixNav();
	initHover('.form-search .input-text, .controls ul.switcher li');
	initGallery({
		holder:'.promo',
		list:'ul.slide-list',
		switcher:'ul.switcher > li',
		autoRotation:7000,
		prev:'a.btn-prev',
		next:'a.btn-next'
	});
	initGallery({
		holder:'.catalogs .content',
		list:'ul.covers',
		switcher:'ul.magazine-list > li',
		autoRotation:false,
		prev:false,
		next:false,
		event:'mouseenter'
	});
});

function initDrop(){
	jQuery('#nav > li').each(function(){
		var _this = jQuery(this);
		var drop = _this.find('> div.drop');
		var hDrop = drop.find('.body .content .col').outerHeight(true);
		var subdrop = _this.find('div.sub-drop');
		var hSDrop = 0;
		subdrop.each(function(){
			if(jQuery(this).outerHeight(true) > hSDrop) hSDrop = jQuery(this).outerHeight(true);
		});

		if(hDrop < hSDrop){
			drop.find('.body .content .col').height(hSDrop)
			drop.find('.body .sub-drop').height(hSDrop)
			drop.find('.body .content .sub-category .frame').height(hSDrop+10)
		}
		else{
			drop.find('.body .content .sub-category .frame').height(hDrop)
		}
	});
}

function initFancyHover(){
	var link = jQuery('a.fancy-btn');
	link.each(function(){
		var _this = jQuery(this);
		var def = _this.find('.def');
		var hvr = _this.find('.hvr').css('opacity', 0);
		_this.mouseover(function(){
			hvr.stop().animate({opacity:1}, {queue:false, duration:300})
		}).mouseleave(function(){
			hvr.stop().animate({opacity:0}, {queue:false, duration:300})
		});
	});
}

function initGallery(option){
	var hold = jQuery(option.holder);
	var duration = option.autoRotation;
	var switcher = false;
	var event = option.event || 'click';
	hold.each(function(){
		var _this = jQuery(this);
		var list = _this.find(option.list),
			count = list.children().length,
			w = list.parent().width(),
			_t,
			a = 0,
			r = a;

		if(option.prev && option.next){
			var prev = _this.find(option.prev).attr('rel', 'prev').click(animateSlide);
			var next = _this.find(option.next).attr('rel', 'next').click(animateSlide);
		}
		if(option.switcher){
			switcher = _this.find(option.switcher);
			switcher.eq(a).addClass('active').find('.bg').css('opacity', 1);
			switcher.bind(event, function(){
				var ind = switcher.index(jQuery(this));
				animateSlide(ind);
				return false;
			});
		}
		if(option.autoRotation) runTimer();

		list.children().css('opacity', 0);
		list.children().eq(a).css('opacity', 1).addClass('active');

		if(option.stopOnHover && _t){
			list.mouseenter(function(){
				clearTimeout(_t);
			}).mouseleave(runTimer);
		}
		function runTimer(){
			_t = setTimeout(function(){
				animateSlide('next');
			}, duration);
		}
		
		function animateSlide(e){
			r = a;
			if(typeof e == 'string' && e == 'next') a++;
			else if(typeof e == 'number') a=e;
			else{
				if(e.currentTarget.rel == 'next') a++;
				else if(e.currentTarget.rel == 'prev') a--;
			}
			if(_t) clearTimeout(_t);
			if(a == count) a=0;
			else if(a == -1) a=count-1;
			list.children().eq(r).removeClass('active');
			list.children().eq(a).addClass('active');
			if(option.switcher){
				switcher.eq(r).removeClass('active').find('.bg').css({opacity:0});
				switcher.eq(a).addClass('active').find('.bg').css({opacity:1});
			}
			list.children().eq(r).stop().animate({opacity:0}, {queue:false, duration:700});
			list.children().eq(a).stop().animate({opacity:1}, {queue:false, duration:700, complete:function(){
				if(option.autoRotation) runTimer();
			}});
			return false;
		}
	});
};

function initFancyHoverNav(){
	jQuery('.promo .controls a').each(function(){
		var _this = jQuery(this);
		var bg = jQuery('<em class="bg"></em>').css('opacity', 0);
		jQuery(this).append(bg);
		_this.mouseenter(function(){
			if(!bg.parents('li').hasClass('active')) bg.stop().animate({opacity:1}, {queue:false, duration:300});
		}).mouseleave(function(){
			if(!bg.parents('li').hasClass('active')) bg.stop().animate({opacity:0}, {queue:false, duration:300});
		});
	});
}

function initHover(_obj){
	jQuery(_obj).each(function(){
		jQuery(this).mouseenter(function(){
			jQuery(this).addClass('hover');
		}).mouseleave(function(){
			jQuery(this).removeClass('hover');
		});
	});
}

function initFixNav(){
	var t = jQuery('.navbar').offset().top;
	var win = jQuery(window);
	var f = true;
	if(win.scrollTop() >= t) {
		jQuery('a.scroll-to-top').show();
	}
	else {
		jQuery('a.scroll-to-top').hide();
	}
	win.scroll(function(){
		if(jQuery.browser.msie && jQuery.browser.version < 7){
			if(win.scrollTop() >= t) {
				jQuery('.navbar').css('top', win.scrollTop());
				jQuery('a.scroll-to-top').show();
				if(f) scrollPageToTop();
			}
			else {
				jQuery('.navbar').css('top', t);
				jQuery('a.scroll-to-top').unbind().hide();
			}
		}
		else{
			if(win.scrollTop() >= t) {
				jQuery('.navbar').addClass('fixed');
				jQuery('a.scroll-to-top').show();
				if(f) scrollPageToTop();
			}
			else {
				jQuery('.navbar').removeClass('fixed');
				jQuery('a.scroll-to-top').unbind().hide();
				f = true;
			}
		}
	});
	function scrollPageToTop(){
		f = false;
		jQuery('a.scroll-to-top').click(function(){
			if (jQuery.browser.safari) {
				jQuery('body').animate({
					'scrollTop':0
				}, 750)
			}
			else jQuery('html').animate({
				'scrollTop':0
				}, 750)
			return false;
		});
	}
}
