﻿(function($){

	function topMenuEnter(event){

		var	li = $(this)
				liW = li.outerWidth()
				liL = li.position().left

		li.siblings(".menu-hover")
			.show()
			.animate({
				left: liL  + (liW - 50)/2
			}, "fast");

		if(li.has('ul')){
			var ul = $("ul",li).stop(true, true).fadeIn(400)
				ulW = ul.outerWidth()
				ulL = ul.offset().left
				dX = (ulW + ulL) - (boneW + boneL)
			if( dX > 0 ){
				ul.css({"left":- dX})
			}
		}

	}

	function viewItem(anim){
		if( gDir ){
			gCur == (gCount-1) ? gCur=0 : gCur++
		} else{
			gCur == 0 ? gCur=(gCount-1) : gCur--
		}

		anim == 1 ? (gItems.filter(":visible").fadeOut("fast"), $(gItems[gCur]).fadeIn("fast")) : (gItems.filter(":visible").hide(), $(gItems[gCur]).show());

		gCaption.html( $(gItems[gCur]).find("span").html() )
	}

	function topMenuLeave(event){
		var li = $(this)
		/*li.siblings(".menu-hover").hide()*/
		if(li.has('ul')) $("ul",li).stop(true, true).fadeOut(400)
	}

	/* topMenu */
	$.fn.topMenu = function() {
		$(this)
			.find("ul")
			.hover(
				function(){
					$(this).addClass("ul_hover")
				},function(){
					$(this).removeClass("ul_hover")
				}
			)
			/*.append("<span class='menu-hover'></span>")*/
			.find("li")
			.mouseover(function(){ $(this).addClass("li_hover") })
			.mouseout(function(){ $(this).removeClass("li_hover") })
			/* .each(function(){
				$(this) */
					.bind("mouseenter", topMenuEnter)
					.bind("mouseleave", topMenuLeave)
			/* }) */
			$(this).find("li ul").each(function(){
				$(this).css("margin-left", 28 )
			})
		//console.log( $(this).find("li ul").width() - $(this).find("li ul").parents("li").width() ) / 2 )
	};
	/* /topMenu */

	/* productsBlock */
	$.fn.productsBlock = function() {

		$.fn.fancyShow = function(dept) {
			return this.each(function() {
				anim == 1 ? $(this).fadeIn(dept) : $(this).show();
			});
		}
		$.fn.fancyHide = function(dept) {
			return this.each(function() {
				anim == 1 ? $(this).fadeOut(dept) : $(this).hide();
			});
		}

		var	block = $(this).hide()
				blockShadow = $("<div class='block-shadow'></div>").appendTo(block)
				blockParent = $(".middle")
				menu = $(".main-nav")

		function menuBind(){
			$("li",menu).each(function(){
				$(this).bind("mouseleave", {li:$(this)} ,topMenuLeave)
			})
		}
		function blockShow(){
			block.show()
				.find(".block-shadow")
				.css({"width":blockParent.width(),"height":blockParent.height()})
				.fancyShow(200)

		}
		function blockHide(evTraget){
			if( evTraget.className == "block-shadow" || evTraget.className == "product-blocks" ){
				block.fancyHide(400).find(".block-shadow").hide();

				menu
					.find(".menu-hover")
					.hide()
					.end()
					.find("ul ul:visible")
					.fadeOut(300)
			}
		}

		block
			.bind("mouseenter",function(event){
				blockHide(event.target)
			})
			.find(".product-block")
				.bind("mouseenter",function(event){

					menu
						.find("ul")
						.addClass("ul_hover")
						.find("[rel*="+$(this).attr("id")+"]")
						.parents("li")
						.addClass("li_hover")

				})
				.bind("mouseleave",function(event){
					$(this).hide()
					blockHide(event.relatedTarget)
					if (event.relatedTarget.className == "block-shadow"){
						menuBind()

						menu
							.find(".ul_hover")
								.removeClass("ul_hover")
							.find(".li_hover")
								.removeClass("li_hover")

					} else if( event.relatedTarget.tagName == "A" ){
						menu
							.find("ul ul .li_hover")
								.removeClass("li_hover")
					}
				})

		$("ul ul",menu)
			.mouseenter( function(event){
				blockShow(event)
				if( event.relatedTarget.className != "product-block" ){
					$("li",menu).unbind("mouseleave")
				}
			})
			.mouseleave( function(event){
				if( event.relatedTarget.className != "product-block" ){

					block.trigger("mouseenter")
						.find(".product-block:visible")
						.fancyHide(400)

					menuBind()
				}
			})
			.find("a")
				.mouseenter(function(event){

					var blc = $("#" + $(this).attr('rel'))

					$(".product-block:visible",block).fancyHide(200)

					blc.oneTime(200, 'blockViewTimer', function(){
						blc.fancyShow(300)
					});
				})
				.mouseleave(function(){
					$("#" + $(this).attr('rel')).stopTime('blockViewTimer')
				})
				.click(function(){return false})

	};
	/* /productsBlock */

	/* productsGallery */
	$.fn.productsGallery = function() {

		var	gallery = $(this)
				//galleryNav = $("<div class='gallery-nav'><a href='#' class='nav-btn btn-prev'><i></i></a><span class='nav-caption'></span><a href='#' class='nav-btn btn-next'><i></i></a></div>").appendTo(gallery)
				galleryNav = $("<div class='gallery-nav'><a href='#' class='nav-btn btn1'>1</a><a href='#' class='nav-btn btn2'>2</a><a href='#' class='nav-btn btn3'>3</a><a href='#' class='nav-btn btn4'>4</a><a href='#' class='nav-btn btn-play'>&#9658;</a><a href='#' class='nav-btn btn-pause'>&#8214;</a></div>").appendTo(gallery)
				gItems = $("li",gallery)
				gCaption = $(".nav-caption",galleryNav)
				gStart = -1
				gCur = gStart
				gCount = gItems.length
				gDir = 1
				gTimeCount = 16000
				isStopped = false;

		viewItem(1)

		gallery.everyTime(gTimeCount, 'productsGalleryTimer', function(){ viewItem(1) });

		$(".nav-btn",galleryNav)
			.mouseenter(function(){
				//$(this).find("i").addClass("hover")
				//gallery.stopTime('productsGalleryTimer');
			})
			.mouseleave(function(){
				//$(this).find("i").removeClass("hover")
				//gallery.everyTime(gTimeCount, 'productsGalleryTimer', function(){ viewItem(1) });
			})
			.click(function(){
			  shouldView = false;
				$(this).hasClass("btn-prev") ? gDir = 0 : gDir = 1;
				if ($(this).hasClass("btn1") ) { gCur = -1; $(this).addClass('sel'); shouldView = true; }
				if ($(this).hasClass("btn2") ) { gCur = 0; $(this).addClass('sel'); shouldView = true; }
				if ($(this).hasClass("btn3") ) { gCur = 1; $(this).addClass('sel'); shouldView = true; }
				if ($(this).hasClass("btn4") ) { gCur = 2; $(this).addClass('sel'); shouldView = true; }
				if ($(this).hasClass("btn-pause") ) { isStopped = true; gallery.stopTime('productsGalleryTimer'); }
				if ($(this).hasClass("btn-play") ) {
				  if(isStopped==true){
				    gallery.everyTime(gTimeCount, 'productsGalleryTimer', function(){ viewItem(1) });
				    isStopped = false;
				  }
				}
				 if(shouldView == true) { viewItem(1); }
        return false;
			})

	};
	/* /productsGallery */

	$.fn.seatSelectorGallery = function() {

		var	gallery = $(this)
				//galleryNav = $("<div class='gallery-nav'><a href='#' class='nav-btn btn-play'>&#9658;</a><a href='#' class='nav-btn btn-pause'>&#8214;</a></div>").appendTo(gallery)
				gItems = $("li", gallery)
				//gCaption = $(".nav-caption", galleryNav)
				gStart = 0
				gCur = gStart
				gCount = gItems.length
				gDir = 0
				gTimeCount = 5000
				isStopped = (gItems.length == 1);

		if (!isStopped) {
			gallery.everyTime(gTimeCount, 'seatSelectorTimer', function(){ viewItem(0) });
		}
	};

	/* formDialer */
	$.fn.formDialer = function() {

		$(this)
			.hover(
				function(){
					$(this).addClass("find-dialer-form_hover")
				},function(){
					$(this).removeClass("find-dialer-form_hover")
				}
			)
			.find("input[type='text']")
				.queue(function(){
					var	inpt = $(this)
							valDef = inpt.attr("value")
					inpt
						.val( valDef )
						.focus(function(){
							if ( inpt.val() == valDef ){
								inpt.val("")
							}
						})
						.blur(function(){
							if ( inpt.val() == "" ){
								inpt.val(valDef)
							}
						})
				})
	};
	/* /formDialer */

	/* logoHighlight */
	$.fn.logoHighlight = function() {

		var obj = $(this)

		obj
			.find('a')
			.append('<span class="logo-highlight"></span>')
			.queue(function () {
					moveHighlight();
					obj.everyTime(6000, 'logoHighlight', function(){ moveHighlight() });
					$(this).dequeue();
				});

		function moveHighlight(){
			obj
				.find(".logo-highlight")
				.css({left:"-110px"})
			 	.animate({ left: "236px" }, 2000);
		}
	};
	/* /logoHighlight */

})(jQuery);

jQuery(document).ready(function($){

	anim = 1
	if( ($.browser.msie && $.browser.version.split('.')[0]<=8) || ($.browser.opera && $.browser.version.split('.')[0] <= 9)) { anim = 0 }

	boneW = $(".bone").width()
	boneL = $(".bone").offset().left

	//$('.logo').logoHighlight();

  if ($('.main-nav').length > 0){
		$('.main-nav').topMenu();
	}

	if ($('.product-blocks').length > 0){
		$('.product-blocks').productsBlock();
	}

	if ($('.products-gallery').length > 0){
		$('.products-gallery').productsGallery();
	}

	if ($('.seat-selector-gallery').length > 0){
		$('.seat-selector-gallery').seatSelectorGallery();
	}

	if ($('.find-dialer-form').length > 0){
		$('.find-dialer-form').formDialer();
	}

	$('[rel="print"]').click(function() {
		window.print();
		return false;
	});

	if ($('.tabs-content').length > 0){
		$(".tabs-content").tabs();
	};

	if ($('.photo-gallery').length > 0){
		$(".photo-gallery a").lightBox({
			imageLoading:			'/images/lightbox/lightbox-ico-loading.gif',
			imageBtnPrev:			'/images/lightbox/lightbox-btn-prev.gif',
			imageBtnNext:			'/images/lightbox/lightbox-btn-next.gif',
			imageBtnClose:			'/images/lightbox/lightbox-btn-close.gif',
			imageBlank:				'/images/lightbox/lightbox-blank.gif'
		});
	};
});

