/* 
todo: close delay
todo: click button to open

http://www.sohtanaka.com/web-design/examples/drop-down-menu/#
http://www.noupe.com/tutorial/drop-down-menu-jquery-css.html
http://css-tricks.com/examples/DiggHeader/#
*/

(function($){
	$.fn.dropmenu = function(custom) {
		var defaults = {
		  	openAnimation: "slide",
			closeAnimation: "slide",
			openClick: false,
			openSpeed: 500,
			closeSpeed: 700,
			closeDelay:500,
			onHide: function(){},
			onHidden: function(){},
			onShow: function(){},
			onShown: function(){}
		  };
		var settings = $.extend({}, defaults, custom);
		
		var menu = this;
		var currentPage = 0;
		var delayTimer = "";
		
		// Trigger init
		init();
		
		/**
		 * Do preparation work
		 */
		function init(){

			// Add open button and class to parent of a submenu
			var items = menu.find(":has(li,div) > a").append('<span class="arrow"></span>');
			$.each(items, function(i, val) {
				if(items.eq(i).parent().is("li")){
					items.eq(i).next().addClass("submenu").parent().addClass("haschildren");
				}else{
					items.eq(i).parent().find("ul").show();
				}
			});
			
			if(settings.openClick){
				menu.find(".submenu").css("display", "none");
				menu.find(":has(li,div) > a").parent().bind("mouseleave",handleHover).bind("mouseenter",function(){ window.clearInterval(delayTimer); });
				menu.find("a span.arrow").bind("click", handleHover);
			}else{
				menu.find(":has(li,div) > a").bind("mouseenter",handleHover).parent().bind("mouseleave",handleHover).bind("mouseenter",function(){ window.clearInterval(delayTimer); });
			}
			
			
		}
		
		/**
		 * Handle mouse hover action
		 */
		function handleHover(e){
			if(e.type == "mouseenter" || e.type == "click"){
				window.clearInterval(delayTimer);
				var current_submenu = $(e.target).parent().find(".submenu:not(:animated):not(.open)");
				if(current_submenu.html() == null){
					current_submenu = $(e.target).parent().next(".submenu:not(:animated):not(.open)");
				}
				if(current_submenu.html() != null){
					settings.onShow.call(current_submenu);
					closeAllMenus();
					current_submenu.prev().addClass("selected");
					current_submenu.css("z-index", "90");
					current_submenu.stop().hide();
					openMenu(current_submenu);
				}
			}
			if(e.type == "mouseleave" || e.type == "mouseout"){
				current_submenu = $(e.target).parents(".submenu");
				if(current_submenu.length != 1){
					var current_submenu = $(e.target).parent().parent().find(".submenu");
					if(current_submenu.html() == null){
						var current_submenu = $(e.target).parent().find(".submenu");
						if(current_submenu.html() == null){
							var current_submenu = $(e.target).parent().parent().parent().find(".submenu");
						}
					}
				}
				if(current_submenu.html() != null){
					if(settings.closeDelay == 0){
						current_submenu.parent().find("a").removeClass("selected");
						closeMenu(current_submenu);
					}else{
						window.clearInterval(delayTimer);
						delayTimer = setInterval(function(){
							window.clearInterval(delayTimer);
							closeMenu(current_submenu);
						}, settings.closeDelay);	
					}
				}
			}
		}
		
		function openMenu(object){
			switch(settings.openAnimation){
				case "slide":
					openSlideAnimation(object);
					break;
				case "fade":
					openFadeAnimation(object);
					break;
				default:
					openSizeAnimation(object);
					break;
			}
		}
		
		function openSlideAnimation(object){
			object.addClass("open").slideDown(settings.openSpeed, function(){ settings.onShown.call(this); });
		}
		
		function openFadeAnimation(object){
			object.addClass("open").fadeIn(settings.openSpeed, function(){ settings.onShown.call(this); });
		}
		
		function openSizeAnimation(object){
			object.addClass("open").show(settings.openSpeed, function(){ settings.onShown.call(this); });
		}
		
		function closeMenu(object){
			settings.onHide.call(object);
			switch(settings.closeAnimation){
				case "slide":
					closeSlideAnimation(object);
					break;
				case "fade":
					closeFadeAnimation(object);
					break;
				default:
					closeSizeAnimation(object);
					break;
			}
		}
		
		function closeSlideAnimation(object){
			object.slideUp(settings.closeSpeed, closeCallback);
		}
		
		function closeFadeAnimation(object){
			object.fadeOut(settings.closeSpeed, function(){ $(this).removeClass("open"); $(this).prev().removeClass("selected"); });
		}
		
		function closeSizeAnimation(object){
			object.hide(settings.closeSpeed, function(){ $(this).removeClass("open"); $(this).prev().removeClass("selected"); });
		}
		
		function closeAllMenus(){
			var submenus = menu.find(".submenu.open");
			$.each(submenus, function(i, val) {
				$(submenus[i]).css("z-index", "1");
				closeMenu($(submenus[i]));
			});
		}
		
		function closeCallback(object){
			$(this).removeClass("open"); 
			if($(this).prev().attr("class") == "selected") settings.onHidden.call(this);
			$(this).prev().removeClass("selected");
		}
			
		// returns the jQuery object to allow for chainability.
		return this;
	}
	
})(jQuery);


/*******************************************************************************************/
// jquery.event.wheel.js - rev 1
// Copyright (c) 2008, Three Dub Media (http://threedubmedia.com)
// Liscensed under the MIT License (MIT-LICENSE.txt)
// http://www.opensource.org/licenses/mit-license.php
// Created: 2008-07-01 | Updated: 2008-07-14
// $(body).bind('wheel',function(event,delta){    alert( delta>0 ? "up" : "down" );    });
/*******************************************************************************************/
;(function($){$.fn.wheel=function(a){return this[a?"bind":"trigger"]("wheel",a)};$.event.special.wheel={setup:function(){$.event.add(this,b,wheelHandler,{})},teardown:function(){$.event.remove(this,b,wheelHandler)}};var b=!$.browser.mozilla?"mousewheel":"DOMMouseScroll"+($.browser.version<"1.9"?" mousemove":"");function wheelHandler(a){switch(a.type){case"mousemove":return $.extend(a.data,{clientX:a.clientX,clientY:a.clientY,pageX:a.pageX,pageY:a.pageY});case"DOMMouseScroll":$.extend(a,a.data);a.delta=-a.detail/3;break;case"mousewheel":a.delta=a.wheelDelta/120;if($.browser.opera)a.delta*=-1;break}a.type="wheel";return $.event.handle.call(this,a,a.delta)}})(jQuery);

/*
 * jQuery crawlLine v1.2.0
 * Copyright (c) 2008 Taranets Aleksey
 * email: aleks_tar@ukr.net
 * www: markup-javascript.com
 * Licensed under the MIT License:
 * http://www.opensource.org/licenses/mit-license.php
 */

jQuery.fn.crawlLine = function(_options){
	// defaults options
	var _options = jQuery.extend({
		speed:2,
		crawElement:'div',
		textElement:'p',
		hoverClass:'viewText'
	},_options);
	
	return this.each(function(){
		var _THIS = jQuery(this);
		var _el = $(_options.crawElement, _THIS).css('position','relative');
		var _text = $(_options.textElement, _THIS);
		var _clone = _text.css('whiteSpace','nowrap').clone();
		var _elWidth = 0;
		var _k = 1;
		
		// set parametrs *******************************************************
		var _textWidth = 0;
		_text.each(function(){
			_textWidth += $(this).outerWidth(true);
		});
		var _duration = _textWidth*50 / _options.speed;
		_el.append(_clone);
		_el.css('width',_textWidth*2);
		
	    var animate = function() {
			_el.animate({left:-_textWidth}, {queue:false, duration:_duration*_k, easing:'linear', complete:function(){
				_el.css('left','0');
				_k=1;
				animate();
			}})
	    }
	    animate();
		
	    _THIS.hover(function() {
			_el.stop();
			_THIS.addClass(_options.hoverClass);
	    }, function(){
			_THIS.removeClass(_options.hoverClass);
			_k = (_textWidth + parseInt(_el.css('left')))/_textWidth;
			animate();
	    })
		_THIS.bind('wheel',function(event,delta){
			var _marginScroll;
			if (delta<0) {
				_marginScroll = parseInt(_el.css('left')) - 20;
				_el.animate({left:_marginScroll}, {queue:false, duration:100, easing:'linear', complete:function(){
					_k = (_textWidth + parseInt(_el.css('left')))/_textWidth;
				}});
			} else {
				_marginScroll = parseInt(_el.css('left')) + 20;
				if (_marginScroll > 0) _marginScroll = 0;
				_el.animate({left:_marginScroll}, {queue:false, duration:100, easing:'linear', complete:function(){
					_k = (_textWidth + parseInt(_el.css('left')))/_textWidth;
				}});
			}
			return false;
		});
	});
}








/*
 * Easy Dialog
 * Copyright 2009 Pieterjan de Smet
 * www.paprikadesign.be
 *
 * Version 1.0   -   Created: Feb. 17, 2010
 *
 * This Easy Dialog jQuery plug-in is dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 */
// create an instance of the object
if (!DIALOG) var DIALOG = new Object();

// set the plugin
(function($){
	// init the plugin
	$.fn.eDialog = function(properties){
		DIALOG._init(properties, this);
	};
	// set the open functionality
	$.fn.eDialog.open = function(evt){
		// open the dialog
		DIALOG.open();
	};
	// set the close functionality
	$.fn.eDialog.close = function(evt){
		// close the dialog
		DIALOG.close();
	};
})(jQuery);

// the DIALOG object
DIALOG = {
	// set vars
	// the properties
	properties: null,
	// the animation
	animationTime: 500,
	// overlay
	overlay: 'eDialogOverlay',
	overlayBackground: '#555555',
	overlayOpacity: 0.5,
	// selectors
	closeSelector: '',
	openSelector: '',
	// callbacks
	closeCallback: '',
	openCallback: '',
	//jquery object
	object: null,

	// init
	_init: function(properties, object){
		// set the vars
		DIALOG.properties = properties;
		DIALOG.object = object;

		// hide the dialogbox
		DIALOG.object.hide();

		// set the properties
		DIALOG.setProperties();

		// bind if necessary
		if(DIALOG.openSelector != '') $(DIALOG.openSelector).bind('click', DIALOG.open);
		if(DIALOG.closeSelector != '') $(DIALOG.closeSelector).bind('click', DIALOG.close);;
	},

	// close the dialogbox
	close : function(){
		// check if the body has the modelshowing class
		if($('body').hasClass('dialogboxShowing'))
		{
			// remove the class
			$('body').removeClass('dialogboxShowing');

			// check if the browser is IE
			if (jQuery.browser.msie) {
				// hide the dialogbox
				DIALOG.object.css('display', 'none');
				// hide the overlay
				$('#' + DIALOG.overlay).css('display', 'none');
			} else{
				// fade the dialogbox and the overlay out
				DIALOG.object.fadeOut(DIALOG.animationTime);
				$('#' + DIALOG.overlay).fadeOut(DIALOG.animationTime);
			}

			// use the callback that is given
			if(DIALOG.closeCallback != '') setTimeout(DIALOG.closeCallback, DIALOG.animationTime);
			
			$(document.body).unbind();
		}

		// stop the bubbling
		return false;
	},

	// open the dialogbox
	open: function(){
		// parse the object in o
		var o = DIALOG.object;

		// position the box
		DIALOG.rePosition();

		// set the overlay
		DIALOG.setOverlay();

		// Detect the browser
		if (jQuery.browser.msie) {
			// show tagBox
			o.fadeIn(DIALOG.animationTime);
			$('#' + DIALOG.overlay).css('display', 'block');
			$('#' + DIALOG.overlay).fadeTo(DIALOG.animationTime, DIALOG.overlayOpacity);

			// only IE should use absolute
			if($.browser.msie && $.browser.version =='6.0') {
				$('#' + DIALOG.overlay).css('height', $(document).height());
			}
		} else {
			// show tagBox
			o.fadeIn(DIALOG.animationTime);
			$('#' + DIALOG.overlay).css('display', 'block');
			$('#' + DIALOG.overlay).fadeTo(DIALOG.animationTime, DIALOG.overlayOpacity);
		}
		// add the modalshowing class
		$('body').addClass('dialogboxShowing');

		// Make sure the hide code has no effect on the tagbox itself
		o.bind('click', function(evt) { evt.stopPropagation(); });

		// Hide dialogbox when clicking outside it
		$(document.body).bind('click', function(evt) {
			DIALOG.close();
		});

		// Hide dialog ox when pressing escape button
		$(document.body).bind("keypress", function(evt) {
			if (evt.keyCode == 27) {
				DIALOG.close();
			}
		});

		// use the callback that is given
		if(DIALOG.openCallback != '') setTimeout(DIALOG.openCallback, DIALOG.animationTime);

		return false;
	},

	// position the box
	rePosition: function() {
		// parse the object in o
		var o = DIALOG.object;

		// calculate offset
		var offsetLeft = parseInt(($(window).width() - o.width()) / 2);
		var offsetTop = parseInt(($(window).height() - o.height()) / 2);

		var modalWidth = parseInt(o.width());
		var modalHeight = parseInt(o.height());

		// set properties
		o.css('left', offsetLeft)
			.css('top', offsetTop)
			.css('width', modalWidth)
			.css('height', modalHeight)
			.css('z-index', '1200')
			.css('position', 'fixed');

		// only IE should use absolute
		if($.browser.msie && $.browser.version =='6.0') {
			// Set scroll to top to avoid have to use buggy fixed positioning
			$(window).scrollTop(0);
			// fix the position
			o.css('position', 'absolute')
				.css('top', parseInt(offsetTop + $(window).scrollTop()));
		}
	},

	// set the overlay
	setOverlay: function(){
		// add the overlay div to the body
		$('body').append('<div id="'+ DIALOG.overlay +'">&nbsp;</div>')

		// set styling
		$('#' + DIALOG.overlay).attr('style', 'z-index: 1000; height: 100%; width: 100%; position: fixed; _position: absolute; top: 0; left: 0;')
			// set bg color
			.css('background', DIALOG.overlayBackground)
			// fade to 0
			.fadeTo(0, 0)
			// hide it
			.hide();
	},

	// set the properties
	setProperties: function(){
		// set animation
		if(DIALOG.properties.animationTime != undefined) DIALOG.animationTime = DIALOG.properties.animationTime;
		//set selectors
		if(DIALOG.properties.openSelector != undefined) DIALOG.openSelector = DIALOG.properties.openSelector;
		if(DIALOG.properties.closeSelector != undefined) DIALOG.closeSelector = DIALOG.properties.closeSelector;
		//set overlay
		if(DIALOG.properties.overlay != undefined) DIALOG.overlay = DIALOG.properties.overlay;
		if(DIALOG.properties.overlayBackground != undefined) DIALOG.overlayBackground = DIALOG.properties.overlayBackground;
		if(DIALOG.properties.overlayOpacity != undefined) DIALOG.overlayOpacity = DIALOG.properties.overlayOpacity;
		// callbacks
		if(DIALOG.properties.closeCallback != undefined) DIALOG.closeCallback = DIALOG.properties.closeCallback;
		if(DIALOG.properties.openCallback != undefined) DIALOG.openCallback = DIALOG.properties.openCallback;
	}
};






/*
jquery.semantictabs.js
Creates semantic tabs from nested divs
Chris Yates

Inspired by Niall Doherty's jQuery Coda-Slider v1.1 - http://www.ndoherty.com/coda-slider

Usage:
$("#mycontainer").semantictabs({
  panel:'.mypanelclass',         //-- Selector of individual panel body
  head:'headelement',           //-- Selector of element containing panel header
  active:':first',              //-- Which panel to activate by default
  activate:':eq(2)'             //-- Argument used to activate panel programmatically
});

1 Nov 2007

Bug fixes 15 Dec 2009:
http://plugins.jquery.com/node/11834
http://plugins.jquery.com/node/8486
(thanks zenmonkey)

Feature update 4 Jan 2010:
Now works with arbitrary jQuery selectors, not just 'class' attribute.

*/

jQuery.fn.semantictabs = function(passedArgsObj) {
  /* defaults */
  var defaults = {panel:'.panel', head:'h3', active:':first', activate:false};

  /* override the defaults if necessary */
  var args = jQuery.extend(defaults,passedArgsObj);
  
  // Allow activation of specific tab, by index
	if (args.activate) {
	  return this.each(function(){
	    var container = jQuery(this);
			container.find(args.panel).hide();
			container.find("ul.tabs li").removeClass("active");
			container.find(args.panel + ":eq(" + args.activate + ")").show();
			container.find("ul.tabs li:eq(" + args.activate + ")").addClass("active");      
	  });
	} else {
    return this.each(function(){
  		// Load behavior
  		var container = jQuery(this);
      container.find(args.panel).hide();
  		container.find(args.panel + args.active).show();
  		container.prepend("<ul class=\"tabs semtabs\"></ul>");
  		container.find(args.panel).each( function() {
  		  var title = jQuery(this).find(args.head).text();
  		  this.title = title;
  			container.find("ul.tabs").append("<li><a href=\"javascript:void(0);\">"+title+"</a></li>");
  		});
  		container.find("ul li" + args.active).addClass("active");
  		// Tab click behavior
  		container.find("ul.tabs li").click(function(){
  			container.find(args.panel).hide();
  			container.find("ul.tabs li").removeClass("active");
  			container.find(args.panel + "[title='"+jQuery(this).text()+"']").show();
  			jQuery(this).addClass("active");
  		});                                
  		container.find("#remtabs").click(function(){
  			container.find("ul.tabs").remove();
  			container.find(args.container + " " + args.panel).show();
  			container.find("#remtabs").remove();
  		});
  	});
	}
		
};













$(document).ready(function(){
	$("#navigation ul").dropmenu();
	
	$('#scrollline').crawlLine({
            speed:2,
            crawElement:'div', // для примера div.move
            textElement:'p',
            hoverClass:'viewText'
    });
	
	$('#dialogbox').eDialog({
				animationTime: 500,
				overlayBackground: '#444 url(images/overlay_bg.png) repeat-x',
				overlayOpacity: 0.33,
				closeSelector: '#boxClose',
				openSelector: '#openbox'
	});
	
	$("#brandslist").semantictabs({
  panel:'.panel',            //-- Selector of individual panel body
  head:'h3',             //-- Selector of element containing panel header, i.e. h3
  active:':first'                         //-- Which panel to activate by default
});
});
