/**
 *	Drop-down menu
 *
 *	@author		Alexander Kuznetcov (coolmen@siteleader.ru)
 *	@copyright	SiteLeader, 2005-2010 || http://www.siteleader.ru/ 
 *	@revision	$Id: jquery.slddm.js 318 2010-10-19 01:27:24Z bacil $
 *	@version	20.07.2010
 */

// FIXME IE6 не открывает пункты меню

/**
 *
 */
;(function($){
	/**
	 * Класс выподающего меню
	 * базируется на http://javascript-array.com/scripts/simple_drop_down_menu/
	 * @class $.slddm()
	 */
	$.fn.slddm = function ( settings ) {

		settings = jQuery.extend({
			timeout:		500,
			speedUp:		'fast',
			speedDown:		'fast'
		}, settings );

		var close_timer		= 0;
		var open_menu_item	= $();

		this.open = function () {
			canceltimer();
			if ( $(this).find('>ul')[0] != open_menu_item[0] ) {
				_close();
			}
			else {
				open_menu_item.stop().css('height', '').slideDown( settings.speedDown );
			}
			offset = $(this).offset().left - $(this).parent().offset().left + $(this).find('>ul').eq(0).outerWidth();
 			offset -= 940 - 15;
 			if ( offset > 0 ) {
 				$(this).find('>ul').eq(0).css( 'left', 0 - offset );
 			}
//			console.info( offset );
			open_menu_item = $(this).find('>ul').eq(0).slideDown( settings.speedDown );
			// Если есть дропдаун фиксим баг осла с z-index (html5)
			if ( $(this).find('>ul')[0] && jQuery.browser.msie ) {
				$(this).css( 'z-index', 1 );
			}
		};

		this.timer = function () {
			close_timer = window.setTimeout( _close, settings.timeout );
		};

		function _close () {
			if ( open_menu_item[0] ) {
				open_menu_item.slideUp( settings.speedUp, function () {
					if ( jQuery.browser.msie ) {
						$(this).parent('li').css( 'z-index', '');
					}
				});
			}
		};

		function canceltimer () {
			if ( close_timer ) {
				window.clearTimeout( close_timer );
				close_timer = null;
			}
		};

		$(this).find('>li').mouseenter( this.open );
		$(this).find('>li').mouseleave( this.timer );
		$(document).click( _close );
	}
}(jQuery));


;(function($){
	$.fn.dialogBox = function () {
	
		function _open () {
			$(this).nextAll('.dialog').show();
			return false;
		}

		function _close () {
			$(this).parent('.bottom').parent('.dialog').hide();
			return false;			
		}

		$(this).each( function () {
			$(this).addClass('psev-href');
			$(this).click( _open );
			$(this).nextAll('.dialog').find('.bottom > .close').click( _close );
		});
		
	}
}(jQuery));


/**
 * Класс управления контролом поиска
 * 
 * @class $.searchBox()
 * @param {Object} settings
 * 	text: string
 */
;(function($){
	$.fn.searchBox = function ( settings ) {
		
		settings = jQuery.extend({
			text:		'Поиск по сайту'
		}, settings );

		/**
		 * Потеря фокуса полем поиска
		 */
		function _out () {
			if ($(this).val() == '') {
				$(this).val(settings.text);
			}
		};
		
		/**
		 * Получение фокуса полем поиска
		 */
		function _in () {
			if ($(this).val() == settings.text) {
				$(this).val('');
			}
		}

		$(this).each( function (){
			$(this).focusout( _out ).focusin( _in );
		});
	}
}(jQuery));

/**
 * Класс управления отзывами о товарах
 * 
 * @class $.comments()
 */

;(function($){
	$.fn.comments = function ( settings ) {
		
		$("#add-comment").attr("href", "#addcomment");
		$("#add-comment").attr("style", "text-decoration: none; padding-bottom: 2px; border-bottom: 1px dashed;");

		$("#add-comment").click(function () {
			$("#response").hide();
			if ($("#comment-form").is(":hidden")) {
				$("#comment-form").slideDown();
			} 
			else {
				$("#comment-form").slideUp();
				// $("#response").empty();
			}
			return false;
		});

		$("#comment-form form input[name=ajax]").attr("value", '1');

		var options = { 
			error: function() {
				alert("Error ");
			},
			clearForm: true,
			success: function(responseText, statusText, xhr, $form) {
				$("#indicator").hide();
				$("#comment-form").slideUp();
				$("#response").show();
			},
			beforeSubmit: function(arr, form, options) {
				$("#indicator").show();
			}
		};
		$('#comment-form form').ajaxForm(options); 
	}

}(jQuery));

/**
 * Класс управления формой вопрос-ответ
 *
 * @class $.comments()
 */

;(function($){
	$.fn.infoForm = function ( settings ) {
		$(".ask").attr("href", "#");
		$(".ask").addClass("dash_decor");
		$(".ask").click(function () {
			$("#response").hide();
			if ( $(".qfm").is(":hidden") ) {
				$(".qfm").slideDown();
			}
			else {
				$(".qfm").slideUp();
				$("#output").empty();
			}
			return false;
		});

		$("#question_form input[name=ajax]").attr("value", '1');
		var options = {
			// target: '#response',
			error: function() {
				alert("Error connect");
			},
			clearForm: true,
			success: function(responseText, statusText, xhr, $form) {
				// $("#comment-form :text, #comment-form textarea").val("");
				$("#indicator").hide();
				$(".qfm").slideUp();
				$("#response").show();
			},
			beforeSubmit: function(arr, form, options) {
				$("#indicator").show();
			}
		};
		$('#question_form').ajaxForm(options);
	};
}(jQuery));

/**
 * Старт плагинов
 */
$(document).ready( function() {
	$('#main-menu').slddm();
	if ( typeof $().lightBox == 'function' ) {
		$('.lightbox a').lightBox({
			imageLoading: '/img/lightbox-ico-loading.gif',
			imageBtnClose: '/img/lightbox-btn-close.gif'
		});
	}
	
	$('.search-box input.search').searchBox();
	$('a.dialog-open').dialogBox();
	
});

