$.widget( "ui.mazda_widget_news_events", {

	options : {
		isHide : false,
		animationType : "blind",
		animationSpeed : 200,
		titleToShow : "Развернуть",
		titleToHide : "Свернуть",
		hideCookieName : "mazda_widget_news_events",
		cookieOptions : {
			path : '/',
			expires : 60*60*60*60*60*60
		}
	},

	/**
	 * Конструктр
	 */
	_create : function()
	{
		// mod expire
		var date = new Date();
		date.setTime( date.getTime() + ( this.options.cookieOptions.expires ) );
		this.options.cookieOptions.expires = date;

		/** Set event */
		var $this = this;
		$( this.element ).find( "label a" ).click( function()
		{
			$this.options.isHide = !$this.options.isHide;
			if ( $this.options.isHide ) {
				$this.hide();
			}
			else {
				$this.show();
			}
			return false;
		} );

		this._load();
		if ( this.options.isHide ) {
			this.hide( true );
		}
		else
		{
		    this.show(true);
		}
	},

	/**
	 * Скрываем события
	 */
	hide : function( fast )
	{
		if ( !fast ) {
			$( this.element ).find( "dl.hideable" ).hide( this.options.animationType,
					this.options.animationSpeed );
		}
		else {
			$( this.element ).find( "dl.hideable" ).hide( 0 );
		}
		$( this.element ).find( "label a" ).text( this.options.titleToShow );
		$.cookie( this.options.hideCookieName, '1', this.options.cookieOptions );

		this.options.isHide = true;
		this._save();
	},

	/**
	 * Показываем события
	 */
	show : function( fast )
	{
		if ( !fast ) {
			$( this.element ).find( "dl.hideable" ).show( this.options.animationType,
					this.options.animationSpeed );
		}
		else {
			$( this.element ).find( "dl.hideable" ).show( 0 );
		}
		$( this.element ).find( "label a" ).text( this.options.titleToHide );
		this.options.isHide = false;
		this._save();
	},

	/**
	 * Метод сохраняет текущее состояние
	 */
	_save : function()
	{
		$.cookie( this.options.hideCookieName,
				(this.options.isHide) ? '1' : '0', this.options.cookieOptions );
	},

	/**
	 * Метод загружает текущее состояние
	 */
	_load : function()
	{
		this.options.isHide = false;
		if ( $.cookie( this.options.hideCookieName ) == '1' ) {
			this.options.isHide = true;
		}
	}


} );
