$(document).ready(function(){
	//alert( $("div.delayed_loading").length );
	/*$("div.delayed_loading").each( function( pos , el ) {
		$(el).load( "/klarnetCMSlocal/page_parts/" + el.getAttribute( "_part" ) + ".htm" );
		} );*/
	
	$("a.js-link" ).each( function( ) {
		this.setAttribute( "href" , this.getAttribute( "_href" ) );
		} );
	
	$("a.clients-sites-add" ).click( function() {
		$.fn.popup( { width: 400 , height: 100 , position: 'center' , header: 'Добавление сайта' , src: '/klarnetCMSlocal/different/messages/add_site_for_client.htm' } );
		} );	
	
	/* Форма поиска веб-студии */
	if( $( "form#U_select_by_params_form" ).length > 0 ) {
		$form = $( "form#U_select_by_params_form" );
		default_name = "поиск по названию или сайту";
		$("input[name='pan']" , $form )
			.val( default_name )
			.bind( "blur" , function() { if( this.value == "" ) { { this.value = default_name } } } )
			.bind( "focus" , function() { if( this.value == default_name ) { { this.value = "" } } } )
			.suggest( 
				"/klarnetCMSlocal/modules/users/list_by_part_of_name_or_link.php" , {
					onSelect: function() { $form.submit(); }
					}
				);
		}
		
	/* Для панели "Я хочу" */
	var $iw_current = null;
	$("dl.iw_element" ).each( function( ) { 
		var $this = $(this);
		var params = iw_element_get_params( $this );		
		iw_element_set_value( params , $.cookie( params.cookie_key ) ? $.cookie( params.cookie_key ) : params.$input.val() );
		
		$(this).bind( "click" , function( e ) {
			e.stopPropagation();
			if( e.target.tagName.toUpperCase() == "A" ) {
				if( $(e.target).parent( "li" ).hasClass( "disabled" ) ) {
					alert( "Это значение выбрать нельзя" );
					return;
					}
				else {
					iw_element_set_value( params , e.target.id );
					}
				}
			if( params.$variants.css( "display" ) == "none" ) {
				if( $iw_current ) iw_current_hide(); //Скрываем предыдущий открытый, если он еще открыт
				$iw_current = params;
				iw_current_show();
				}
			else iw_current_hide();

			
			} );
		} );
	$(document.body).bind( "click" , function() {
		if( $iw_current ) iw_current_hide();
		} );
	
	function iw_element_get_params( $element ) {
		var params = {};
		params.$visible = $( "dt" , $element );
		params.$variants = $("dd" , $element );
		params.$input = $( "input", $element );
		params.$global_container = $element.parent( "span.iw_elements_for" );
		params.cookie_key = ( params.$global_container.length ? params.$global_container.attr( "id" ) + "_" : "" ) + "iw_" + params.$input.attr( "name" );
		//console.log( params.cookie_key );
		return params;
		}
	function iw_element_set_value( params , value ) {
		var old_value = params.$input.val( );
		$( "li#" + old_value , params.$variants ).removeClass( "active" );
		var $new_value_container = $( "li#" + value , params.$variants );
		params.$visible.text( $new_value_container.text() );
		params.$input.val( value );
		$.cookie( params.cookie_key , value )
		$new_value_container.addClass( "active" );
		
		var element_name = params.$input.attr( "name" );
		if( element_name == "m_f" ) {
			var money_from = parseInt( value );
			$( "dl.i_want_m_t li" ).each( function() {
				if( parseInt( this.id ) ) {
					if( parseInt( this.id ) >= money_from ) {
						$(this).removeClass( "disabled" )
						}
					else {
						$(this).addClass( "disabled" );
						}
					}
				} );
			var $money_to_element = $( "dl.i_want_m_t" );
			var money_to_element_params = iw_element_get_params( $money_to_element )
			if( parseInt( money_to_element_params.$input.val() ) < money_from ) {
				iw_element_set_value( money_to_element_params , money_from );
				}
			}
		else if( element_name == "choose" ) {
			$("span#iw_elements_for_" + old_value ).hide();
			$("span#iw_elements_for_" + value ).show();
			}
		}
	function iw_current_show() {
		$iw_current.$variants.show();
		$iw_current.$visible.addClass( "active" );
		}
	function iw_current_hide() {
		$iw_current.$variants.hide();
		$iw_current.$visible.removeClass( "active" );
		$iw_current = null;
		}
	});