$(function(){ 
  /* All Page */
  
  $('.page .c .content .b-h').hover(function(){
    var maxWidth = $(this).width();
    var elWidth = $('h2',this).width();
    if ( elWidth > maxWidth ){
      $('h2', this).animate({ left: '-' + ((elWidth - maxWidth) + 30) + 'px' }, 3500);
    }
  }, function(){
    $('h2', this).stop(true).animate({ left: '25px' });
  });
  
  // Search Field
  
  $('.page .search form input.field').val('ÏÎÈÑÊ ÏÎ ÌÀÃÀÇÈÍÓ...').bind("focus", function(){				
    if ( this.value == 'ÏÎÈÑÊ ÏÎ ÌÀÃÀÇÈÍÓ...' ) { this.value = ''; }
  }).bind("blur", function(){
    if ( this.value == '' ) { this.value = 'ÏÎÈÑÊ ÏÎ ÌÀÃÀÇÈÍÓ...'; }
  });
  
  // Categories Menu
  
  if ( openCategory != '' ){
    if ($('div.b-c-categories a[rel='+openCategory+']').hasClass('mcat-parent') == true){
      $('div.b-c-categories a[rel='+openCategory+']').parents('li').addClass('active');
      $('div.b-c-categories a[rel='+openCategory+']').next('span').removeClass('close').addClass('open');
    } else {
      $('div.b-c-categories a[rel='+openCategory+']').addClass('active');
      $('div.b-c-categories a[rel='+openCategory+']').parents('.mcat-parent-li').addClass('active').find('span.status').removeClass('close').addClass('open');
    }
  }
  
  $('.b-c-categories ul li').hover(function(){
    $(this).addClass('hover');
  }, function(){
    $(this).removeClass('hover');
  });
  
  $('.b-c-categories ul li .status').hover(function(){
    if ( $(this).hasClass('close') == true ) {
      $(this).addClass('close-hover');
    }
  }, function(){
    $(this).removeClass('close-hover');
  });
  
  $('.b-c-categories ul li span.status').click(function(){
    if ( $(this).hasClass('close') == true || $(this).hasClass('open') == true ){
      if ( $(this).hasClass('close') == true ) {
        $(this).removeClass('close').addClass('open');
        $(this).parents('li').addClass('active');
        $(this).next("ul").slideDown();
      } else {
        $(this).removeClass('open').addClass('close');
        $(this).parents('li').removeClass('active');
        $(this).next("ul").slideUp();
      }
    }
  });
  
  // Scrap Text
  
  $('.scrap').truncate({max_length: 199, more: '...', less: '...' });
  $('.desc').truncate({ max_length: 60 });
  $('.scrap-info').truncate({ max_length: 60 });
  $('.scrap-cat-info').truncate({ max_length: 800 });
  
  // Tabs
  
  /*var tabsOpen = 0;
  
  if ( $.url.attr('anchor') == 'tab-cart' || $.url.param('page') == 'checkout.index' ){
    tabsOpen = 1;
  } else if( $.url.attr('anchor') == 'tab-order' ) {
    tabsOpen = 2;
  }
  
  var apiTabs = $('ul.tab-h').tabs('div.tab-c > div.tab-c-item',{initialIndex:tabsOpen});
  */
  $('ul.tab-h li').hover(function(){
    if ( $(this).hasClass('current') == false ){
      $(this).addClass($(this).attr('id')+'-hover');
    }
  }, function(){
    $(this).removeClass($(this).attr('id')+'-hover');
  });
  
  $('ul.tab-h li').each(function(){
    $('span', this).width( ($(this).width()+3)+'px' );
  });
  
  if ( $.url.param('searchword') != undefined ) {
    var tabContentSearch = $('.search_context').html();
    if ( tabContentSearch == null ) {
      tabContentSearch = '';
    }
    $('.search_context').remove();
    $('.t-b-hit').html('<div class="search_context">'+tabContentSearch+'</div>');
  }
  
  // Buttons
  
  $('.field-button').hover(function(){
    $(this).addClass('field-button-hover');
  }, function(){
    $(this).removeClass('field-button-hover');
  });
  
  // Basket Table
  
  $('.t-b-basket tr, .t-b-order tr, .order-shipping tr').hover(function(){
    $(this).addClass('hover');
  }, function(){
    $(this).removeClass('hover');
  });
  
  // Genries
  
  if ( openGenre != '' ){
    $('div.box-genries a[rel='+openGenre+']').addClass('active');
  }
  
  $('a.select-genre').click(function(){
    if ( $(this).hasClass('select-genre-active') == false ) {
      $(this).addClass('select-genre-active');
      $('div.box-genries').slideDown();
    } else {
      $(this).removeClass('select-genre-active');
      $('div.box-genries').slideUp();
    }
    
    return false;
  });
  
  // Filters
  
  $('a.link-filter').click(function(){
    if ( $(this).hasClass('active') == false ) {
      $(this).addClass('active');
      $('#'+$(this).attr('rel')).slideDown();
    } else {
      $(this).removeClass('active');
      $('#'+$(this).attr('rel')).slideUp();
    }
    
    return false;
  });
  
  $('.box-filter a').click(function(){
    var urlOpen = window.location.toString();
    
    if ( $.url.param($(this).parent('div.box-filter').attr('rel')) == undefined ){
      document.location.href = urlOpen+'&'+$(this).attr('href');
    } else {
      urlOpen = urlOpen.replace('&'+$(this).parent('div.box-filter').attr('rel')+'='+$.url.param($(this).parent('div.box-filter').attr('rel')), '');
      document.location.href = urlOpen+'&'+$(this).attr('href');
    }
    
    return false;
  });
  
  // Comment Votes
  
  $('div.comment-votes a').click(function(){
    $('div.comment-votes input').removeAttr('checked');
    $('div.comment-votes a').removeClass('active');
    $('div.comment-votes input#'+$(this).attr('rel')).attr('checked', true);
    $(this).addClass('active');
    
    return false;
  });
  
  // Images Box
  
  $("a[rel^='xbox']").prettyPhoto();
  
  // Authorize
  
  $('.order .field-button').formValidator({ 
    errorClass: 'field-error', 
    scope:'.order'
  });
  
  $('.registration .field-button').formValidator({ 
    errorClass: 'field-error', 
    scope:'.registration'
  });
  
  $('.partner .field-button').formValidator({ 
    errorClass: 'field-error', 
    scope:'.partner'
  });
  
  // Selects
  
  $('.field-select-country').change( function(){
    $('.field-select-state option').removeAttr('style');
    $('.field-select-state option.country-state-'+$(this).val()).show();
  });
  
  $('.field-select-state option:selected').each(function(){
    $('.field-select-state option.'+$(this).attr('class')).show();
  });
  
  /* Home Page */
  $('#notification').xNotifica();
  $('.scrollable').scrollable({size: 1, clickable: false});
  $('.scrollable-hit').scrollable({size: 5, clickable: false});
  
  // BookMarks
  
  $('.add-bookmark').hover(function(){
    $(this).addClass('bookmark-hover');
  }, function(){
    $(this).removeClass('bookmark-hover');
  });
  
  $('.add-bookmark').click(function(){
    var ajaxUrl;
    
    if ( $(this).hasClass('bookmark-active') == true ){
      ajaxUrl = $(this).attr('rel');
      $(this).removeClass('bookmark-active');
    } else {
      ajaxUrl = $(this).attr('href');
      $(this).addClass('bookmark-active');
    }
    
    $.ajax({ url: ajaxUrl, cache: false, success: function(html){ } });
    
    return false;
  });
});

function get_look_autocomplete( v, cont ) {
	var script_name = 'zip.php';
	
	$.get( script_name, {q:v}, 
		function( obj ) {
			var res = [];
			var prod_id = obj[0];
			var prod_id_info = obj[0];
			var prod_name = obj[1];
			
			for ( var i = 0; i < obj[0].length; i++ ) {
				res.push({ id: i, value: prod_name[i], affected_value: prod_id[i],  info: "Èíäåêñ: " + prod_id[i] });
			}
			
			cont( res );
		}, 
		'json' 
	)
}

function init_autocomplete( input, multi ) { 
  $(input).autocomplete({ ajax_get: get_look_autocomplete, multi: multi, cache: false }); 
}

function add_favorite(a){
  title = document.title;
  url = document.location;
  
  try {
    // Internet Explorer
    window.external.AddFavorite(url, title);
  }
  catch (e) {
    try {
      // Mozilla
      window.sidebar.addPanel(title, url, "");
    }
    catch (e) {
      // Opera
      if (typeof(opera)=="object") {
        a.rel = "sidebar";
        a.title = title;
        a.url = url;
        
        return true;
      } else {
        // Unknown
        alert('Íàæìèòå Ctrl-D ÷òîáû äîáàâèòü ñòðàíèöó â çàêëàäêè');
      }
    }
  }
  
  return false;
}

function add_homepage(a){
  if ( $.browser.msie ){
    a.style.behavior = 'url(#default#homepage)'; 
    a.setHomePage('http://www.serialdvd.ru'); 
  }
  
  return false;
}
