/* * * */
$j = jQuery.noConflict();
/* * * */

function add_favorite(a) {
  
  my_title = document.title;
  url = document.location;
  try {
    eval("window.external.AddFa-vorite(url, my_title)".replace(/-/g,''));
  }
  catch (e) {
    try {
      window.sidebar.addPanel(my_title, url, "");
    }
    catch (e) {
      // Opera
      if ( typeof(opera)=="object" ) {
        a.rel="sidebar";
        a.title=my_title;
        a.url=url;
        return true;
      }
      else {
        alert('Нажмите Ctrl-D чтобы добавить страницу в закладки');
      }
    }
  }
  return false;
}
/**
 *
 * @access public
 * @return void
 * 
 **/
function setBatchSelectControls( select_class, select_all_id, submit_id ){
    $j("#"+select_all_id).change(function() {
        select_all = $j(this).attr("checked");
        $j("."+select_class).each(function() {
            if(select_all)
            {
                $j(this).attr("checked", true);
            }
            else
            {
                $j(this).removeAttr("checked");
            }
        });
    });

    $j("#"+submit_id).click(function() {
        count = 0;
        $j("."+select_class+":checked").each(function(){ count++ });
        if(!count)
        {
            alert("Выберите хотя-бы одно объявление");
			return false;
        }
		return true;
    });

    $j("."+select_class).click(function(){
        count_checked = count_unchecked = 0;
        $j("."+select_class).each(function(){
            if( $j(this).attr("checked") == true )
            {
                count_checked++;
            }
            else
            {
                count_unchecked++;
            }
        });
        if(!count_checked)
        {
            $j("#"+select_all_id).removeAttr("checked");
        }
        else if( !count_unchecked )
        {
            $j("#"+select_all_id).attr("checked", "checked");
        }
    });

}
/************/
function bindShowElementByClick( element_to_show, element_to_click)
{
   $j("#"+element_to_click).click(function() {
	  $j("#"+element_to_show).show();
   });
}
function bindHideElementByClick( element_to_hide, element_to_click )
{
   $j("#"+element_to_click).click(function() {
	  $j("#"+element_to_hide).hide();
   });
}

/*******************************/

function setUpdateSelectChild(source_id, destination_id, method, add_empty){
	$j("#"+source_id).change(function(){
		var id = $j('#'+source_id).val();
		if( ! id ){
			$j('#'+destination_id).empty();
			return;
		}
		$j.ajax( {
			url: '/ajaxActions/'+method+'/id/'+id+'/add_empty/'+add_empty,
			dataType: "json",
			success: function(json){
				$j('#'+destination_id).empty();
				$j.each(json, function( id, value ){
					if( !value ) // especially for IE
					{
					  value = add_empty;
					}
					selected_attr = ''; // especially for opera 9 (win)
					if( !id )
					{
					  selected_attr = 'selected="selected"';
					}
					$j('#'+destination_id).append('<option value="'+id+'" '+selected_attr+'>'+value+'</option>');
				})
			}
		});
	});
}

