query = jQuery.noConflict();
query( document ).ready( init );

function init()
{
  query( "#shade" ).css( "opacity", 0.2 );

  query( "#site-nav ul ul ul" ).each( function()
  {
    var item   = query( this );
    var parent = item.parent().parent();

    item.css( "margin-left", parent.width() );
  } );

  var height = 0,
      index  = 1;

  var section_objects = query( "#sections .section_object" );

  section_objects.each( function()
  {
    var item = query( this );

    if ( item.height() > height )
      height = item.height();

    if ( index % 3 == 0 )
    {
      for ( var i = 0; i < 3; i++ )
	section_objects.eq( index - i - 1 ).css( "height", height );

      height = 0;
    }

    index++;
  } );

  height = 0,
  index  = 1;

  var footer_columns = query( ".column > ul > li" );

  footer_columns.each( function()
  {

    var item = query( this );

    if ( item.height() > height )
      height = item.height();

    if ( index % 3 == 0 )
    {
      for ( var i = 0; i < 3; i++ )
	footer_columns.eq( index - i - 1 ).css( "height", height );

      height = 0;
    }

    index++;
  } );
}

/**
 * news
 *
 */

function renderNewsTpl(item_id) {
  if(item_id) {
    doAjaxRequest('CoMeT_function=run_module&module=News&function=list_news_tpl&action=render_news_tpl&item_id=' + item_id, renderNewsTplResponse);
  }
}

function renderNewsTplResponse(html) {
  jQuery('#popup').show().html(html).center();
  jQuery('#shade').show();
}

function updateNewsYear(year, lang_id, source) {
  ajaxLoader('#articles_inner');
  jQuery('#articles_years').children('a').removeClass('current');
  jQuery(source, 'a').addClass('current');

  doAjaxRequest('CoMeT_function=run_module&module=News&function=list_news&action=update_news_year&year=' + year + '&lang_id=' + lang_id, updateNewsYearResponse);

}

function updateNewsYearResponse(html) {
  jQuery('#articles_inner').html(html);
}



function updateNewsArchiveYear(year, lang_id, source) {
  ajaxLoader('#news_archive');
  jQuery('#articles_archive_years').children('a').removeClass('current');
  jQuery(source, 'a').addClass('current');

  doAjaxRequest('CoMeT_function=run_module&module=News&function=list_news_full_view&action=update_news_archive_year&year=' + year + '&lang_id=' + lang_id, updateNewsArchiveYearResponse);

}

function updateNewsArchiveYearResponse(html) {
  jQuery('#news_archive').html(html);
}


/**
 * search
 *
 */

function doSiteSearch() {
  var searchstring = jQuery("input#searchstring").val();
  doAjaxRequest('CoMeT_function=run_module&module=ModSearch&function=ajax_search&searchstring=' + searchstring, searchResult);
}

function searchResult(html) {
  jQuery('#search_result').html(html).slideDown('fast');
}

function closeSearchResult() {
  jQuery('#search_result').slideUp('fast');
  //jQuery('#search_result').hide();

}

function siteCheckEnter(e) {
  var key;
  if (window.event) {
    key = window.event.keyCode; //IE
  } else {
    key = e.which;
  }
  if (key == 13) {
    doSiteSearch();
    return false;
  }
  return true;
}




/**
 * common
 *
 */

function closeDiv(source) {
  if(source) {
    jQuery(source).remove();
  }
}

function hideDiv(source) {
  if(source) {
    jQuery(source).hide();
    jQuery('#shade').hide();
  }
}

function ajaxLoader(target_div) {
  jQuery(target_div).show();
  div_content = '<div id="ajax_loader"></div>';
  jQuery(target_div).html(div_content);
}

function doAjaxRequest(pars, callback) {
  jQuery.ajax({
    type: "POST",
    url: "?",
    data: pars,
    success: callback
  });
}
