//-------------------------------------------------------------------------------------------
// MediaPortal.kiev.ua Custom JavaScript File (c) 2008 Butcher http://www.mediaportal.kiev.ua
//-------------------------------------------------------------------------------------------

  // ������� DRAW_SITE_OLD (�) Butcher, MediaPortal.kiev.ua
  // ���������� "����� �����" �����
  function draw_site_old( )
  {
    d0 = new Date( "February 26, 2008" );
    d1 = new Date();
    dt = ( d1.getTime( ) - d0.getTime( ) ) / ( 1000 * 60 * 60 * 24 ) + 365;
    document.write( "���� � ���� <b>" + Math.round( dt ) + "</b>-� ����" );
  }
 
  // ������� TOGGLEBLOCK (�) Butcher, MediaPortal.kiev.ua
  // ����������� ��������� �����
  function toggleblock( mp_block_id, add_block_to_hidding )
  {
    togglecategory( mp_block_id, add_block_to_hidding );
    return true;
  }

  // ������� TOGGLECATEGORY (�) Butcher, MediaPortal.kiev.ua
  // ����������� ��������� �������� ��������, �������� ���������� � cookie
  function togglecategory( fid, add )
  {
    saved = new Array();
    clean = new Array();
    if( tmp = get_mp_cookie( "hidding_blocks" ) )
    {
      saved = tmp.split( ',' );
    }
    for( i = 0 ; i < saved.length; i++ )
    {
      if( saved[i] != fid && saved[i] != '' )
      {
        clean[clean.length] = saved[i];
      }
    }
    if( add == 2 )
    {
      hide_mp_div( mp_get_by_id( "fc_" + fid ) );
      hide_mp_div( mp_get_by_id( "fo_" + fid ) );
    }
    else if( add )
    {
      clean[ clean.length ] = fid;
      show_mp_div( mp_get_by_id( "fc_" + fid ) );
      hide_mp_div( mp_get_by_id( "fo_" + fid ) );
    }
    else
    {
      show_mp_div( mp_get_by_id( "fo_" + fid ) );
      hide_mp_div( mp_get_by_id( "fc_" + fid ) );
    }
    set_mp_cookie( "hidding_blocks", clean.join( ',' ), 1 );

    return true;
  }

  // ������� CLEAR_MP_COOKIE (�) Butcher, MediaPortal.kiev.ua
  // ������� ��������� cookie ������������
  function clear_mp_cookie( cookie_id )
  {
    set_mp_cookie( cookie_id, '', 1 );

    return true;
  }

  // ������� ADD_TO_MP_COOKIE (�) Butcher, MediaPortal.kiev.ua
  // ��������� ������������� � cookie ������������
  function add_to_mp_cookie( cookie_id, id )
  {
    if( id == null )
    {
      return false;
    }
    var cookie = get_mp_cookie( cookie_id );
    if( cookie && cookie.search( id ) != -1 )
    {
      return true;
    }
    if( !cookie )
    {
      set_mp_cookie( cookie_id, id, 1 );
    }
    else
    {
      set_mp_cookie( cookie_id, cookie + ',' + id, 1 );
    }
    return true;
  }

  // ������� DELETE_FROM_MP_COOKIE (�) Butcher, MediaPortal.kiev.ua
  // ������� ������������� �� cookie ������������
  function delete_from_mp_cookie( cookie_id, id )
  {
    saved = new Array();
    clean = new Array();

    if( tmp = get_mp_cookie( cookie_id ) )
    {
      saved = tmp.split( ',' );
    }

    for( i = 0 ; i < saved.length; i++ )
    {
      if( saved[i] != id && saved[i] != '' )
      {
        clean[clean.length] = saved[i];
      }
    }
    set_mp_cookie( cookie_id, clean.join( ',' ), 1 );

    return true;
  }

  // ������� ADD_TO_ARRAY (�) Butcher, MediaPortal.kiev.ua
  // ��������� ������� � ������, ���� �� ����������� � ���� ������� � ���������� ���� ������
  function add_to_array( input_array, value )
  {
    if( !mp_in_array( value, input_array ) )
    {
      input_array.push( value );
    }

    return input_array;
  }

  // ������� REMOVE_FROM_ARRAY (�) Butcher, MediaPortal.kiev.ua
  // ������� ������� �������, ���� ��� �������� ��������� � ��������, ��������� ������ ��� ����� ��������
  function remove_from_array( input_array, value )
  {
    var i = 0;
    var output_array = new Array();
    if( input_array instanceof Array )
    {
      for( i = 0; i < input_array.length; i++ )
      {
        if( input_array[i] == value )
        {
          delete input_array[i];
        }
      }
      for( i = 0; i < input_array.length; i++ )
      {
        if( input_array[i] )
        {
          output_array = add_to_array( output_array, input_array[i] );
        }
      }
    }

    return output_array;
  }

  // ������� GET_MP_COOKIE (�) Butcher, MediaPortal.kiev.ua
  // ���������� ���������� cookie ������������
  function get_mp_cookie( name )
  {
    cname = name + '=';
    cpos = document.cookie.indexOf( cname );
    if( cpos != -1 )
    {
      cstart = cpos + cname.length;
      cend = document.cookie.indexOf( ';', cstart );
      if( cend == -1 )
      {
        cend = document.cookie.length;
      }
      return unescape( document.cookie.substring( cstart, cend ) );
    }
    return null;
  }

  // ������� SET_MP_COOKIE (�) Butcher, MediaPortal.kiev.ua
  // ������� cookie � �������� ����������
  function set_mp_cookie( name, value, sticky )
  {
    expire = '';
    if( sticky )
    {
      expire = "; expires=Wed, 1 Jan 2020 00:00:00 GMT";
    }
    document.cookie = name + '=' + value + "; path=" + '/' + expire + ';';

    return true;
  }

  // ������� SHOW_MP_DIV (�) Butcher, MediaPortal.kiev.ua
  // ������ div ������� �������
  function show_mp_div( element )
  {
    if( !element )
    {
      var element = mp_get_by_id( element );
      if( !element )
      {
        return false;
      }
    }
    element.style.display = '';
    return true;
  }

  // ������� SHOW_MP_ELEMENT (�) Butcher, MediaPortal.kiev.ua
  // ������ ������� ������� �������
  function show_mp_element( id )
  {
    var element = mp_get_by_id( id );
    if( !element )
    {
      return false;
    }
    show_mp_div( element );
    return true;
  }

  // ������� HIDE_MP_DIV (�) Butcher, MediaPortal.kiev.ua
  // ������ div ������� �������
  function hide_mp_div( element )
  {
    if( !element )
    {
      var element = mp_get_by_id( element );
      if( !element )
      {
        return false;
      }
    }
    element.style.display = "none";
    return true;
  }

  // ������� HIDE_MP_ELEMENT (�) Butcher, MediaPortal.kiev.ua
  // ������ ������� ������� �������
  function hide_mp_element( id )
  {
    var element = mp_get_by_id( id );
    if( !element )
    {
      return false;
    }
    hide_mp_div( element );
    return true;
  }

  // ������� MP_GET_BY_ID (�) Butcher, MediaPortal.kiev.ua
  // ���������� ������� �������� � �������� ���������������
  function mp_get_by_id( id )
  {
    var element = null;
    if( document.getElementById )
    {
      element = document.getElementById( id );
    }
    else if( document.all )
    {
      element = document.all[id];
    }
    else if( document.layers )
    {
      element = document.layers[id];
    }

    return element;
  }

  // ������� HIDE_HIDDED_BLOCKS_DIVS (�) Butcher, MediaPortal.kiev.ua
  // �������� ������� ����� ��������
  function hide_hidded_blocks_divs( )
  {
    hidded_blocks = new Array();
    if( tmp = get_mp_cookie( "hidding_blocks" ) )
    {
      hidded_blocks = tmp.split( ',' );
    }
    for( i = 0 ; i < hidded_blocks.length; i++ )
    {
      current_block_opened = mp_get_by_id( "fo_" + hidded_blocks[i] );
      current_block_closed = mp_get_by_id( "fc_" + hidded_blocks[i] );
      if( current_block_opened && current_block_closed )
      {
        if( current_block_opened.style.display != "none" )
        {
          show_mp_div( current_block_closed );
          hide_mp_div( current_block_opened );
        }
        else
        {
          show_mp_div( current_block_opened );
          hide_mp_div( current_block_closed );
        }
      }
    }

    return true;
  }

  // ������� MM_PRELOADIMAGES (�) Butcher, MediaPortal.kiev.ua
  // ���������� �������� ����������� � ��� ��������
  function MM_preloadImages( )
  {
    if( document.images )
    {
      if( !document.MM_p )
      {
        document.MM_p = new Array();
      }
      var i, j = document.MM_p.length, a = MM_preloadImages.arguments;
      for( i = 0; i < a.length; i++ )
      {
        if( a[i].indexOf( '#' ) != 0 )
        {
          document.MM_p[j] = new Image;
          document.MM_p[j++].src = a[i];
        }
      }
    }

    return true;
  }

  // ������� DRAWCLOCK (�) Butcher, MediaPortal.kiev.ua
  // ���������� ������ ���� �� ��������
  function drawclock( )
  {
    var d = new Date();
    var s = d.getSeconds( );
    var m = d.getMinutes( );
    var h = d.getHours( );
    if( s < 10 )
    {
      s = '0' + s;
    }
    if( m < 10 )
    {
      m = '0' + m;
    }
    if( h < 10 )
    {
      h = '0' + h;
    }
    document.getElementById( "clock" ).innerHTML = h + ':' + m + ':' + s;
    setTimeout( "drawclock()", 1000 );

    return true;
  }

  // ������� DRAW_CURRENT_DATE (�) Butcher, MediaPortal.kiev.ua
  // ���������� ������� ���� �� ��������
  function draw_current_date( )
  {
    var d = new Date( );
    var day = d.getDay( );
    var date = d.getDate( );
    var month = d.getMonth( );
    var year = d.getFullYear( );
    switch( day )
    {
      case( 1 ): day = "�����������"; break;
      case( 2 ): day = "�������"; break;
      case( 3 ): day = "�����"; break;
      case( 4 ): day = "�������"; break;
      case( 5 ): day = "�������"; break;
      case( 6 ): day = "�������"; break;
      case( 0 ): day = "�����������"; break;
      default: break;
    }
    switch( month )
    {
      case( 0 ): month = "������"; break;
      case( 1 ): month = "�������"; break;
      case( 2 ): month = "�����"; break;
      case( 3 ): month = "������"; break;
      case( 4 ): month = "���"; break;
      case( 5 ): month = "����"; break;
      case( 6 ): month = "����"; break;
      case( 7 ): month = "�������"; break;
      case( 8 ): month = "��������"; break;
      case( 9 ): month = "�������"; break;
      case( 10 ): month = "������"; break;
      case( 11 ): month = "�������"; break;
      default: break;
    } 
    document.write( day + ", " + date + ' ' + month + ' ' + year );

    return true;
  }

  // ������� SEARCHFIELD_CLICK (�) Butcher, MediaPortal.kiev.ua
  // ���������� ������� � ����� �������� ������
  function searchfield_click( element )
  {
    if( searchfield_clicked == false )
    {
      element.value = '';
      element.style.color = "#000000";
      element.style.textAlign = "left";
      searchfield_clicked = true;
    }
    element.focus();

    return true;
  }

  // ������� SUBMIT_SEARCH (�) Butcher, MediaPortal.kiev.ua
  // ����������/������ ������������� � ����� �������� ������
  function submit_search( )
  {
    var search_field = mp_get_by_id( "search_field" );
    if( search_field.value == "������� ����� ��� ������..." || search_field.value.length == 0 )
    {
      alert( "�� �� ����� �� ������ ������� � ��������� ������" );
    }
    else if( search_field.value.length < 3 )
    {         
      alert( "�� ����� � ������ ������ ������ ���� ��������" );
    }
    else
    {
      document.search_form.submit();
    }

    return false;
  }

  // ������� TOGGLE_DISABLED_BLOCKS (�) Butcher, MediaPortal.kiev.ua
  // ������������� ��������� ����������� ������ ��������
  function toggle_disabled_blocks( id, add )
  {
    saved = new Array();
    clean = new Array();
    if( tmp = get_mp_cookie( "disabled_blocks" ) )
    {
      saved = tmp.split( ',' );
    }
    for( i = 0 ; i < saved.length; i++ )
    {
      if( saved[i] != id && saved[i] != "" )
      {
        clean[clean.length] = saved[i];
      }
    }
    if( add )
    {
      clean[ clean.length ] = id;
    }
    set_mp_cookie( "disabled_blocks", clean.join( ',' ), 1 );

    return true;
  }

  // ������� CHECK_BLOCK_DISABLED (�) Butcher, MediaPortal.kiev.ua
  // ��������, �������� �� �������� ���� ����������� �� ��������
  function check_block_disabled( id )
  {
    var disabled_blocks = new Array();

    if( tmp = get_mp_cookie( "disabled_blocks" ) )
    {
      disabled_blocks = tmp.split( ',' );
    }
    if( mp_in_array( id, disabled_blocks ) )
    {
      return true;
    }

    return false;
  }

  // ������� MP_IN_ARRAY (�) Butcher, MediaPortal.kiev.ua
  // ��������, �������� �� �������� ������� ��������� ��������� �������
  function mp_in_array( needle, haystack )
  {
    var key;
    for( key in haystack )
    {
      if( haystack[key] == needle )
      {
        return true;
      }
    }
 
    return false;
  }

  // ������� TOGGLEVIEW (�) Butcher, MediaPortal.kiev.ua
  // ������������ ��������� ��������� �������� ��������
  function toggleview( id )
  {
    var element = mp_get_by_id( id );
    if( element == null )
    {
      return false;
    }
    if( element.style.display == "none" )
    {
      element.style.display = '';
    }
    else
    {
      element.style.display = "none";
    }

    return true;
  }

  // ������� ISSET (�) Kevin van Zonneveld (http://kevin.vanzonneveld.net)
  // �������� ������������� ����������
  function isset( )
  {
    var a = arguments;
    var l = a.length;
    var i = 0;
    if( l == 0 )
    { 
      throw new Error( "Empty isset" ); 
    }
    while( i != l )
    {
      if( typeof( a[i] ) == "undefined" || a[i] === null )
      { 
        return false; 
      }
      else
      { 
        i++; 
      }
    }

    return true;
  }

  // ������� SET_BLOCK_PRIORITY (�) Butcher, MediaPortal.kiev.ua
  // ��������� ���������� ���������� ��� ������������� �����
  function set_block_priority( id, priority )
  {
    var saved = new Array();
    var clean = new Array();
    var separator = '|';
    var new_value = '';
    var cookie = get_mp_cookie( "blocks_priority" );
    if( parseInt( id ) < 1 )
    {
      id = 1;
    }
    new_value = id + separator + priority;
    if( cookie )
    {
      saved = cookie.split( ',' );
      for( i = 0 ; i < saved.length; i++ )
      {
        if( saved[i].search( id ) == -1 && saved[i] != "" )
        {
          add_to_array( clean, saved[i] );
        }
      }
      add_to_array( clean, new_value );
      set_mp_cookie( "blocks_priority", clean.join( ',' ), 1 );
    }
    else
    {
      set_mp_cookie( "blocks_priority", new_value, 1 );
    }

    return true;
  }

  function set_rel_category( element, id )
  {
    var cookie_id = "disabled_rel_categories";
    if( element.checked == true )
    {
      delete_from_mp_cookie( cookie_id, id );
    }
    else
    {
      add_to_mp_cookie( cookie_id, id );
    }
    return true;  
  }

  // ������� MAKE_BOOKMARK (�) Butcher, MediaPortal.kiev.ua
  // ���������� ����� � �������� / ���������
  function make_bookmark( )
  {
    if( document.all )
    {
      window.external.AddFavorite( "http://www.mediaportal.kiev.ua", "��� ������� ���� - MediaPortal" );
    }
  }

  // ������� SETSTARTPAGE (�) Butcher, MediaPortal.kiev.ua
  // ���������� ����� � �������� / ���������
  function setStartPage( obj )
  {
    if( document.all )
    {
      obj.style.behavior = "url(#default#homepage)";
      obj.setHomePage( "http://www.mediaportal.kiev.ua" );
    }
    else if( window.sidebar )
    {
      window.location.href = "index.php?do=static&page=set_homepage_firefox";
    }
    else if( window.opera )
    {
      window.location.href = "index.php?do=static&page=set_homepage_opera";
    }
  }

  function mp_switch_top_releasers( what_to_switch, set_cookie )
  {
    var name = "top_releasers_";
    var active_bgcolor = "#608ad8";
    var active_color = "#ffffff";
    var bgcolor = "#ffffff";
    var color = "#000000";
    var active_cursor = "default";
    var cursor = "pointer";
    var link_all = mp_get_by_id( name + 'link_all' );
    var link_month = mp_get_by_id( name + 'link_month' );
    var link_today = mp_get_by_id( name + 'link_today' );
    var div_all = mp_get_by_id( name + 'div_all' );
    var div_month = mp_get_by_id( name + 'div_month' );
    var div_today = mp_get_by_id( name + 'div_today' );

    if( what_to_switch == "today" )
    {
      if( link_all )
      {
        link_all.style.backgroundColor = bgcolor;
        link_all.style.color = color;
        link_all.style.cursor = cursor;
      }
      if( link_month )
      {
        link_month.style.backgroundColor = bgcolor;
        link_month.style.color = color;
        link_month.style.cursor = cursor;
      }
      if( link_today )
      {
        link_today.style.backgroundColor = active_bgcolor;
        link_today.style.color = active_color;
        link_today.style.cursor = active_cursor;
      }
      hide_mp_div( div_all );
      hide_mp_div( div_month );
      show_mp_div( div_today );
    }
    else if( what_to_switch == "month" )
    {
      if( link_all )
      {
        link_all.style.backgroundColor = bgcolor;
        link_all.style.color = color;
        link_all.style.cursor = cursor;
      }
      if( link_month )
      {
        link_month.style.backgroundColor = active_bgcolor;
        link_month.style.color = active_color;
        link_month.style.cursor = active_cursor;
      }
      if( link_today )
      {
        link_today.style.backgroundColor = bgcolor;
        link_today.style.color = color;
        link_today.style.cursor = cursor;
      }
      hide_mp_div( div_all );
      show_mp_div( div_month );
      hide_mp_div( div_today );
    }
    else if( what_to_switch == "all" )
    {
      if( link_all )
      {
        link_all.style.backgroundColor = active_bgcolor;
        link_all.style.color = active_color;
        link_all.style.cursor = active_cursor;
      }
      if( link_month )
      {
        link_month.style.backgroundColor = bgcolor;
        link_month.style.color = color;
        link_month.style.cursor = cursor;
      }
      if( link_today )
      {
        link_today.style.backgroundColor = bgcolor;
        link_today.style.color = color;
        link_today.style.cursor = cursor;
      }
      show_mp_div( div_all );
      hide_mp_div( div_month );
      hide_mp_div( div_today );
    }
    else
    {
      return false;
    }
    if( set_cookie )
    {
      set_mp_cookie( "top_releasers", what_to_switch, 1 );
    }

    return true;  
  }

  var searchfield_clicked = false;