var SITE_URL = 'http://'+ document.domain;


$( document ).ready( function(){
  sli_update();
});


function ord_date_d_sel( day_id, day_title ){
  $( '#sli_head_date_d' ).html( day_title );
  $( '#sli_body_date_d' ).fadeOut( 500 );
  date_d = day_id;
  week_day = new Date( date_y, date_m-1, date_d );
  date_w = week_day.getDay();
  time_row_arr = times_arr[ date_w ];
  time_arg_arr = [];
  for( z = 0; z < time_row_arr.length; z++ ){
    time_arg_arr[ z ] = [time_row_arr[ z ],time_row_arr[ z ]];
  }
  date_t = '';
  sli_build( '', 'time', '', 'ord_time_sel', time_row_arr, false, time_arg_arr, {head:{style:'width:70px;'},body:{style:'width:70px;'}} );
}

function ord_date_m_sel( month_id, month_title ){
  $( '#sli_head_date_m' ).html( month_title );
  $( '#sli_body_date_m' ).fadeOut( 500 );
  date_m = month_id;
  week_day = new Date( date_y, date_m-1, date_d );
  date_w = week_day.getDay();
  time_row_arr = times_arr[ date_w ];
  time_arg_arr = [];
  for( z = 0; z < time_row_arr.length; z++ ){
    time_arg_arr[ z ] = [time_row_arr[ z ],time_row_arr[ z ]];
  }
  date_t = '';
  sli_build( '', 'time', '', 'ord_time_sel', time_row_arr, false, time_arg_arr, {head:{style:'width:70px;'},body:{style:'width:70px;'}} );
}

function ord_date_y_sel( year_id, year_title ){
  $( '#sli_head_date_y' ).html( year_title );
  $( '#sli_body_date_y' ).fadeOut( 500 );
  date_y = year_id;
  week_day = new Date( date_y, date_m-1, date_d );
  date_w = week_day.getDay();
  time_row_arr = times_arr[ date_w ];
  time_arg_arr = [];
  for( z = 0; z < time_row_arr.length; z++ ){
    time_arg_arr[ z ] = [time_row_arr[ z ],time_row_arr[ z ]];
  }
  date_t = '';
  sli_build( '', 'time', '', 'ord_time_sel', time_row_arr, false, time_arg_arr, {head:{style:'width:70px;'},body:{style:'width:70px;'}} );
}

function ord_time_sel( time_id, time_title ){
  $( '#sli_head_time' ).html( time_title );
  $( '#sli_body_time' ).fadeOut( 500 );
  date_t = time_id;
}

function ord_submit(){
  is_empty = false;
  if( $( '#user_input_title' ).val().length <= 1 ){	$( '#user_input_title' ).css( 'border', '1px solid red' ); 	is_empty = true; }
  if( $( '#user_input_name' ).val().length <= 1 ){ 	$( '#user_input_name' ).css( 'border', '1px solid red' ); 	is_empty = true; }
  if( $( '#user_input_content' ).val().length <= 1 ){ 	$( '#user_input_content' ).css( 'border', '1px solid red' ); 	is_empty = true; }
  if( date_t == '' ){ $( '#sli_head_time' ).css( 'border', '1px solid red' ); is_empty = true; }
  if( is_empty == false ){
    data_arr = [];
    data_arr[ 'title' ]	= escape(  $( '#user_input_title' ).val()  );
    data_arr[ 'name' ]	= escape(  $( '#user_input_name' ).val()  );
    data_arr[ 'content' ] = escape(  $( '#user_input_content' ).val().substr( 0, 300 )  );
    data_arr[ 'date' ]	= date_y +'-'+ date_m +'-'+ date_d;
    data_arr[ 'time' ]	= date_t;
    data_str = 'i=5&actmode=ord_submit';
    for( key in data_arr ) data_str += '&'+ key +'='+ data_arr[ key ];
    $( '#submit_button_box' ).hide();
    $( '#submit_status_box' ).css( {'background-image':'url(\''+ SITE_URL +'/img/loader_max.gif\')','background-repeat':'no-repeat','background-position':'center left'} ).show().empty();
    $.ajax( {
      type: 'POST',
      url:  SITE_URL +'/index.php',
      data: data_str,
      success: function( data ){
        result = data.substr( 0, 1 );
        if( result == 1 ){
          $( '#submit_status_box' ).css( {'background':'transparent','color':'green'} ).html( 'Заявка успешно отправлена' ).fadeOut( 4000, function(){ 
            $( '#submit_button_box' ).show();
            $( '#submit_status_box' ).hide();
            $( '#user_input_title' ).val('');
            $( '#user_input_name' ).val('');
            $( '#user_input_content' ).val('');
          } );
        }else if( result == 2 ){
          inner_html = data.substr( 1 );
          $( '#submit_status_box' ).css( {'background':'transparent','color':'orange'} ).html( 'Ошибка: '+ inner_html ).fadeOut( 8000, function(){ 
            $( '#submit_button_box' ).show();
            $( '#submit_status_box' ).hide();
          } );
        }else if( result == 0 ){
          $( '#submit_status_box' ).css( {'background':'transparent','color':'red'} ).html( 'Извините, при отправке произошла ошибка. Повторите попытку позднее' ).fadeOut( 4000, function(){ 
            $( '#submit_button_box' ).show();
            $( '#submit_status_box' ).hide();
          } );
        }else{
          $( '#submit_status_box' ).css( {'background':'transparent','color':'red'} ).html( 'Извините, при отправке произошла ошибка. Повторите попытку позднее' ).fadeOut( 4000, function(){ 
            $( '#submit_button_box' ).show();
            $( '#submit_status_box' ).hide();
          } );
        }
      },
      error: function( XMLHttpRequest, textStatus ){
        $( '#submit_status_box' ).css( {'background':'transparent','color':'red'} ).html( 'Извините, при отправке произошла ошибка. Повторите попытку позднее' ).fadeOut( 4000, function(){ 
          $( '#submit_button_box' ).show();
          $( '#submit_status_box' ).hide();
        } );
      }
    } );
  }else{
    alert( 'Пожалуйста заполните все поля' );
  }
}


function msg_submit(){
  is_empty = false;
  if( $( '#user_input_title' ).val().length <= 1 ){	$( '#user_input_title' ).css( 'border', '1px solid red' ); 	is_empty = true; }
  if( $( '#user_input_content' ).val().length <= 1 ){ 	$( '#user_input_content' ).css( 'border', '1px solid red' ); 	is_empty = true; }
  if( is_empty == false ){
    data_arr = [];
    data_arr[ 'title' ]	= escape(  $( '#user_input_title' ).val()  );
    data_arr[ 'content' ] = escape(  $( '#user_input_content' ).val().substr( 0, 500 )  );
    data_str = 'i=6&actmode=msg_submit';
    for( key in data_arr ) data_str += '&'+ key +'='+ data_arr[ key ];
    $( '#submit_button_box' ).hide();
    $( '#submit_status_box' ).css( {'background-image':'url(\''+ SITE_URL +'/img/loader_max.gif\')','background-repeat':'no-repeat','background-position':'center left'} ).show();
    $.ajax( {
      type: 'POST',
      url:  SITE_URL +'/index.php',
      data: data_str,
      success: function( data ){
        result = data.substr( 0, 1 );
        if( result == 1 ){
          $( '#submit_status_box' ).css( {'background':'transparent','color':'green'} ).html( 'Сообщение успешно отправлено' ).fadeOut( 4000, function(){ 
            $( '#submit_button_box' ).show();
            $( '#submit_status_box' ).hide();
            $( '#user_input_title' ).val('');
            $( '#user_input_content' ).val('');
          } );
        }else if( result == 2 ){
          inner_html = data.substr( 1 );
          $( '#submit_status_box' ).css( {'background':'transparent','color':'orange'} ).html( 'Ошибка: '+ inner_html ).fadeOut( 4000, function(){ 
            $( '#submit_button_box' ).show();
            $( '#submit_status_box' ).hide();
          } );
        }else if( result == 0 ){
          $( '#submit_status_box' ).css( {'background':'transparent','color':'red'} ).html( 'Извините, при отправке произошла ошибка. Повторите попытку позднее' ).fadeOut( 4000, function(){ 
            $( '#submit_button_box' ).show();
            $( '#submit_status_box' ).hide();
          } );
        }else{
          $( '#submit_status_box' ).css( {'background':'transparent','color':'red'} ).html( 'Извините, при отправке произошла ошибка. Повторите попытку позднее' ).fadeOut( 4000, function(){ 
            $( '#submit_button_box' ).show();
            $( '#submit_status_box' ).hide();
          } );
        }
      },
      error: function( XMLHttpRequest, textStatus ){
        $( '#submit_status_box' ).css( {'background':'transparent','color':'red'} ).html( 'Извините, при отправке произошла ошибка. Повторите попытку позднее' ).fadeOut( 4000, function(){ 
          $( '#submit_button_box' ).show();
          $( '#submit_status_box' ).hide();
        } );
      }
    } );
  }else{
    alert( 'Пожалуйста заполните все поля' );
  }
}


function djs_msg_submit(){
  is_empty = false;
  if( $( '#user_input_name' ).val().length <= 1 ){	$( '#user_input_name' ).css( 'border', '1px solid red' ); 	is_empty = true; }
  if( $( '#user_input_question' ).val().length <= 1 ){ 	$( '#user_input_question' ).css( 'border', '1px solid red' ); 	is_empty = true; }
  if( is_empty == false ){
    data_arr = [];
    data_arr[ 'sid' ]	= $( 'input[name=user_input_sid]:hidden' ).val();
    data_arr[ 'name' ]	= escape(  $( '#user_input_name' ).val()  );
    data_arr[ 'question' ] = escape(  $( '#user_input_question' ).val().substr( 0, 500 )  );
    data_str = 'i=4&actmode=msg_submit';
    for( key in data_arr ) data_str += '&'+ key +'='+ data_arr[ key ];
    $( '#submit_button_box' ).hide();
    $( '#submit_status_box' ).css( {'background-image':'url(\''+ SITE_URL +'/img/loader_max.gif\')','background-repeat':'no-repeat','background-position':'center left'} ).show();
    $.ajax( {
      type: 'POST',
      url:  SITE_URL +'/index.php',
      data: data_str,
      success: function( data ){
        result = data.substr( 0, 1 );
        if( result == 1 ){
          $( '#submit_status_box' ).css( {'background':'transparent','color':'green'} ).html( 'Сообщение успешно отправлено' ).fadeOut( 4000, function(){ 
            $( '#submit_button_box' ).show();
            $( '#submit_status_box' ).hide();
            $( '#user_input_title' ).val('');
            $( '#user_input_content' ).val('');
          } );
        }else if( result == 2 ){
          inner_html = data.substr( 1 );
          $( '#submit_status_box' ).css( {'background':'transparent','color':'orange'} ).html( 'Ошибка: '+ inner_html ).fadeOut( 4000, function(){ 
            $( '#submit_button_box' ).show();
            $( '#submit_status_box' ).hide();
          } );
        }else if( result == 0 ){
          $( '#submit_status_box' ).css( {'background':'transparent','color':'red'} ).html( 'Извините, при отправке произошла ошибка. Повторите попытку позднее' ).fadeOut( 4000, function(){ 
            $( '#submit_button_box' ).show();
            $( '#submit_status_box' ).hide();
          } );
        }else{
          $( '#submit_status_box' ).css( {'background':'transparent','color':'red'} ).html( 'Извините, при отправке произошла ошибка. Повторите попытку позднее' ).fadeOut( 4000, function(){ 
            $( '#submit_button_box' ).show();
            $( '#submit_status_box' ).hide();
          } );
        }
      },
      error: function( XMLHttpRequest, textStatus ){
        $( '#submit_status_box' ).css( {'background':'transparent','color':'red'} ).html( 'Извините, при отправке произошла ошибка. Повторите попытку позднее' ).fadeOut( 4000, function(){ 
          $( '#submit_button_box' ).show();
          $( '#submit_status_box' ).hide();
        } );
      }
    } );
  }else{
    alert( 'Пожалуйста заполните все поля' );
  }
}


function image_view( image_file, image_title, image_width, image_height ){
  obj_body = '<table cellpadding="0" cellspacing="0" class="popup_image_view" style="border:1px solid #aaaaaa;"><tr><td align=right><span class=""><a href="javascript:popup_close();">закрыть</a></span></td></tr><tr><td><img src="'+ image_file +'" border=0 alt="'+ image_title +'" '+ (  image_width > 0  ?  'width="'+ image_width +'"'  :  ''  ) +' '+ (  image_height > 0  ?  'height="'+ image_height +'"'  :  ''  ) +' onclick="popup_close();" style="border:1px solid #aaaaaa;" /></td></tr></table>';
  popup_open( obj_body );
}


function popup_open( obj_body ){
  if( $( '#popup_bg' )  &&  $( '#popup_body' ) ){
    $( '#popup_bg' ).empty();
    $( '#popup_body' ).empty();
  }
  div_container = '<div id="popup_bg" style="margin:0px; position:absolute; float:left; left:0px; top:0px; width:100%; height:100%; z-order:99; background-color:#ffffff; FILTER:alpha(opacity=0); -moz-opacity:0; opacity:0;"></div><div id="popup_body" style="margin:0px; position:absolute; float:left; left:0px; top:0px; z-order:98;">'+ obj_body +'</div>';
  $( 'body' ).prepend( div_container );
  var html = document.documentElement;
  var body = document.body;
  scroll_top = (  html  &&  html.scrollTop   ||   body  &&  body.scrollTop   ||   0  );
  $( '#popup_bg' ).css( 'top', scroll_top );
  $( '#popup_body' ).css( 'top', (  scroll_top  +  ( $( '#popup_bg' ).innerHeight() - $( '#popup_body' ).outerHeight() )  /  2  ) ).css( 'left', (  ( $( 'body' ).outerWidth() - $( '#popup_body' ).outerWidth() )  /  2  ) );
  $( '#popup_bg' ).click( function(){ popup_close(); } );
}

function popup_close(){
  $( '#popup_bg' ).hide(); 
  $( '#popup_body' ).hide();
}


function sli_build( sli_type, sli_title, sli_step, func_obj, row_arr, row_sel, arg_arr, sets_arr ){
  var inner_html = '';
  var inner_html_head = '';
  var inner_html_body = '';
  for( z in sets_arr ){
    if( z == 'head' ){
      for( x in sets_arr[ z ] ){
        inner_html_head += ' '+ x +'="'+ sets_arr[ z ][ x ] +'" ';
      }
    }else if( z == 'body' ){
      for( x in sets_arr[ z ] ){
        inner_html_body += ' '+ x +'="'+ sets_arr[ z ][ x ] +'" ';
      }
    }
  }
  inner_html += '<div id="sli_head_'+ sli_title + ( sli_step !== '' ? '_'+ sli_step : '' ) +'" class="sli_'+ ( sli_type.length > 0 ? sli_type +'_' : '' ) +'head'+ ( row_sel === false  &&  ( sli_type == 'hd' || sli_type == 'mn' ) ? '_empty' : '' ) +'" '+ inner_html_head +'>'+ ( row_sel !== false ? '<nobr>'+ row_arr[ row_sel ] +'</nobr>' : '' ) + '</div>';
  inner_html += '<div id="sli_body_'+ sli_title + ( sli_step !== '' ? '_'+ sli_step : '' ) +'" class="sli_'+ ( sli_type.length > 0 ? sli_type +'_' : '' ) +'body" '+ inner_html_body +'>';
  for( z = 0; z < row_arr.length; z++ ){
    if( $.isArray( func_obj ) ){
      func = func_obj[ z ];
    }else{
      func = func_obj;
    }
    row_title = row_arr[ z ];
    if( arg_arr[ z ].length > 0 ){
      for( x = 0; x < arg_arr[ z ].length; x++ ){
        arg_arr[ z ][ x ] = '\''+ arg_arr[ z ][ x ] +'\'';
      }
      inner_html += '<a href="javascript:'+ func +'('+ arg_arr[ z ].join(',') +');" class="sli_'+ ( sli_type.length > 0 ? sli_type +'_' : '' ) +'row">'+ row_title +'</a><br>';
    }else{
      inner_html += '<span class="sli_'+ ( sli_type.length > 0 ? sli_type +'_' : '' ) +'row">'+ row_title +'</span><br>';
    }
  }
  inner_html += '</div>';
  $( '#sli_'+ sli_title + ( sli_step !== '' ? '_'+ sli_step : '' ) ).html( inner_html );
}

function sli_update(){
  $('.sli_body').hide();
  $('.sli_body').mouseleave( function(){ $(this).fadeOut(500); } );
  $('.sli_head').each( function(){
    head_id = this.id;
    body_id = head_id.replace( 'sli_head', 'sli_body' );
    $(this).unbind( 'click' );
    $(this).bind( 'click', {obj_id:body_id}, function( event ){
      obj_id = event.data.obj_id;
      obj_item = document.getElementById( obj_id );
      obj_display = obj_item.style.display;
      $('.sli_body').hide();
      $('.sli_mn_body').hide();
      $('.sli_hd_body').hide();
      $('.sli_gr_body').hide();
      $('.msli_body').hide();
      obj_item.style.display = obj_display;
      if( !obj_item ) return;
      if( obj_item.style.display == 'block'  ||  obj_item.style.display == '' ){
        obj_item.style.display = 'none';
      }else{
        obj_item.style.display = 'block';
      }
    });
  });
}
