﻿function buildForIE(){
  selectorsLastChild = '.flashbox li:last-child'
  + ', .main_navigation li:last-child'
  + ', .news_list li:last-child'
  + ', .staff_categories_list dd:last-child'
  + ', .search_results li:last-child';
  jQuery(selectorsLastChild).addClass('last-child');
  selectorsFirstChild = '.contacts dl:first-child';
  jQuery(selectorsFirstChild).addClass('first-child');
  jQuery('.gallery').addClass('gallery_ie');
}



/*--*/
function setPlaceholder( field, focused ){
  var defaultValue = jQuery(field).data().defaultValue;
  if( focused ){
    if( field.value == defaultValue ){
      field.value = '';
      jQuery(field).removeClass('placeholder');
    }
  }else{
    if( field.value == defaultValue || field.value == '' ){
      jQuery(field).addClass('placeholder');
      if( field.value == '' ){
        field.value = defaultValue;
      }
    }
  }
}

function placeholder( field, defaultValue ){
  jQuery(field).data('defaultValue',defaultValue).bind('focus',function(){
    setPlaceholder(this,true);
  }).bind('blur',function(){
    setPlaceholder(this,false);
  }).blur();
}
/*-/-*/



/*--*/
function expandList(){
  var fullInfo = jQuery(this).siblings('.full_info');
  var duration = fullInfo.children('li').length * 100 || 'slow';
  fullInfo.animate({'height':'toggle'},duration,null,function(){
    jQuery(this).parent().toggleClass('collapsed');
    /*--force reflow (hack ie7/6)--*/
    var reflow = document.body;
    jQuery(reflow).addClass('js_force_reflow');
    reflow.offsetHeight;
    jQuery(reflow).removeClass('js_force_reflow');
    /*--/force reflow--*/
  });
}

function expandableList(){
  var i, l, buttons = jQuery('li > .expand_button',this);
  for( i=0, l=buttons.length; i < l; i++ ){
    if( jQuery('.full_info',buttons.eq(i).parent()).length && jQuery('.full_info',buttons.eq(i).parent())[0].hasChildNodes() ){
      buttons.eq(i).after('<span class="state_marker"></span>').bind('click',expandList).parent().addClass('collapsed').children('.full_info').hide();
    }else{
      buttons.eq(i).removeClass('expand_button');
    }
  }
  /*--force reflow (hack ie7/6)--*/
  var reflow = document.body;
  jQuery(reflow).addClass('js_force_reflow');
  reflow.offsetHeight;
  jQuery(reflow).removeClass('js_force_reflow');
  /*--/force reflow--*/
}
/*-/-*/



/*--*/
function orderingList(){
  var i, l, itemContent;
      items = jQuery(this).addClass('jsmarked').children('li');
  for( i=0, l=items.length; i < l; i++ ){
    items.eq(i).html('<span class="marker">'+(i+1)+'.</span><div class="item_content">'+items.eq(i).html()+'</div>');
  }
  /*--force reflow (hack ie8)--*/
  jQuery(this).hide();
  this.offsetHeight;
  jQuery(this).show();
  /*--/force reflow--*/
}
/*-/-*/



/*--*/
function focusCount(){
}
function blurCount(){
  if( this.value == '' ){
    this.value = 0;
    jQuery(this).data().prevValue = this.value;
  }
}

function validateCount(){
  var count = this.value,
      prevValue = jQuery(this).data().prevValue || 1;
  if( count == '' ){
    return;
  }
  if( isNaN(count) ){
    this.value = prevValue;
  }else if( count < 0 ){
    this.value = prevValue;
  }else{
    this.value = count;
  }
  jQuery(this).data().prevValue = this.value;
}

function calcItem(item){
  var itemId, count, price;
  itemId = jQuery('input[name="item_id"]',item)[0];
  if( !(itemId && itemId.value) ){
    return;
  }
  itemId = "item_id_" + itemId.value;
  price = jQuery('input[name="'+itemId+'_price"]',item)[0];
  if( !(price && price.value) ){
    return;
  }
  price = parseInt(price.value) || 0;
  count = jQuery('input[name="'+itemId+'_count"]',item)[0];
  if( !(count && count.value) ){
    return;
  }
  count = parseInt(count.value) || 0;
  return price * count;
}

function calculateOrder(){
  var orderForm = jQuery(this).parents('form.order_form')[0];
  if( !orderForm ){
    return;
  }
  var totalPrice = 0;
  var items = jQuery('table.order_table tbody tr',orderForm);
  for(var i=0, l=items.length; i < l; i++){
    itemPrice = calcItem(items[i]);
    totalPrice += (itemPrice) ? itemPrice : 0;
  }
  jQuery('table.order_table tfoot',orderForm).hide();           //-force reflow (bug in opera)
  jQuery('.total_price',orderForm).html(totalPrice);
  jQuery('table.order_table tfoot',orderForm)[0].offsetHeight;  //-force reflow (bug in opera)
  jQuery('table.order_table tfoot',orderForm).show();           //-force reflow (bug in opera)
  return false;
}

function orderBill(){
  var orderForm = this;
  var i, l, totalPrice = 0,
      items = jQuery('table.order_table tbody tr',orderForm);
  calculateOrder.apply( jQuery('.recalculate',orderForm).bind('click',calculateOrder) );
  jQuery('.count', items).bind('focus',focusCount);
  jQuery('.count', items).bind('blur',blurCount);
  jQuery('.count', items).bind('focus blur change keyup',validateCount).blur();
}
/*-/-*/



/*--*/
function customSelectChange(){
  var selectedOption = this.options[this.selectedIndex]
  if( jQuery(selectedOption).hasClass('disabled_option') ){
    this.selectedIndex = 0;
    selectedOption = this.options[this.selectedIndex]
  }
  var selectedValue = selectedOption.value;
  var selectedString = selectedOption.text;
  if( selectedValue == -1 ){
    jQuery(this).siblings('.custom_select_value').addClass('placeholder');
  }else{
    jQuery(this).siblings('.placeholder').removeClass('placeholder');
  }
  jQuery(this).siblings('.custom_select_value').children('.custom_select_value_string').html(selectedString);
}

function customSelect(){
  var customSelectContainer = jQuery(this).wrap('<div class="custom_select_container"></div>').parent();
  jQuery(this).before('<div class="custom_select_value"><span class="custom_select_value_string"></span><div class="custom_select_button"></div></div>');
  customSelectChange.apply(this);
  jQuery(this).bind('keypressed keydown keyup change',customSelectChange)
              .bind('focus',function(){
                jQuery(this).parent().addClass('focus')
              })
             .bind('blur',function(){
                jQuery(this).parent().removeClass('focus')
              });
}

function customFileChange(){
  if( !this.value ){
    return;
  }
  var fileName = this.value.substring( this.value.lastIndexOf('\\')+1 );
  jQuery('.custom_file_value',jQuery(this).parents('.custom_file_container')).html(fileName);
}

function customFile(){
  jQuery(this).wrap('<div class="custom_file_container"><div class="form_button"><span class="button_value"></span></div></div>')
              .after('обзор')
              .parents('.form_button').before('<div class="custom_file_value"></div>')
  jQuery(this).bind('change',customFileChange);
  customFileChange.apply(this);
}
/*-/-*/



/*--*/
function fancyboxVideoShow(selectedArray, selectedIndex, selectedOpts){
  var clipUrl = jQuery(selectedArray[selectedIndex]).parents('li').find('input[type="hidden"]')[0].value;
  flowplayer("player", "/flowplayer/flowplayer-3.2.5.swf",clipUrl);
}

function fancyboxVideoClose(){
  jQuery('#player').html('');
}
/*-/-*/

jQuery(document).ready(function(){
  if(jQuery.browser.msie){
    buildForIE();
  }

  jQuery('ol.js_ol').each(orderingList);
  jQuery('input[type="text"].search_string').each(function(){ placeholder(this, 'Поиск по сайту') });
  jQuery('.library_search_string input[type="text"]').each(function(){ placeholder(this, 'Поиск литературы') });
  jQuery('.staff_list, .vacancies_list').each(expandableList);
  jQuery('select.custom_select').each(customSelect);
  jQuery('input[type="file"].custom_file').each(customFile);
  jQuery('form.order_form').each(orderBill);
  jQuery('.video_gallery .preview .play_button').addClass('play_button_ie')

  jQuery(".full_size").fancybox();
  jQuery(".show_video").fancybox({
    onStart:fancyboxVideoShow,
    onClosed:fancyboxVideoClose
  });
});
