var host = 'http://www.1cv.lv';
var default_language_id = 'lat';

var path = window.location.pathname.split('/');

var language_id = path[1];

if (!language_id) {
  
  language_id = default_language_id;
  
}

var email = 'in*o!1#v.lv';

function prefillClear(field) {

  if (field.defaultValue==field.value) {field.value = '';}
  else if (field.value == '') {field.value = field.defaultValue;}
		
}

$(document).ready(function() {

  $('.news_input').val($('.news_input').attr('rel'));

  $('a[href$="' + host + '/attachment/"]').removeAttr('href');

  if ($('#slider').length) {
  
    preloadSliderImages();
  
    showSlider();
    
    if ($('#actual a').size() > 1) {
    
      setInterval("showSlider()", 5000);
      
    }
    
  }

  email = email.replace('*', 'f').replace('!', '@').replace('#', 'c');

  $('.mail a').html(email);  
  $('.mail a').attr('href', 'mailto:' + email);

  showSideBoardContent();

  $('#sidebarmenu a').click(function() {
  
    $('#sidebarmenu a').attr('class', 'secondcat');
    
    $('.white').remove();    
      
    $(this).attr('class', 'firstcat');
    
    tabString = $(this).html();
    
    $(this).html(tabString + '<div class="white"></div>');
    
    showSideBoardContent();
    
  });
  
  $('.button2').click(function() {
  
    $.post(host + '/mail.php', { mail: $('.news_input').val() }, function(data) {
    
      if (data == 'OK') {
      
        setTimeout("restoreSubscribe()", 1000);
        
      }
      
    } );
    
  });
  
});

function restoreSubscribe() {

  $('.news_input').val($('.news_input').attr('rel'));
  
}

function showSideBoardContent() {

  $('.sidebar').hide();
  
  $('.sidebar[rel="' + $('.firstcat').text() + '"]').show();
  
}

function showSlider() {

  $content = $('#actual a.active');
  
  if ($content.length == 0) {
  
    $active = $('#actual a:first');
    
    $active.attr('class', 'active');
    
    $content = $('#actual a.active');
    
  }
  
  $('#slider').children().fadeTo('slow', 0.0, function() {
  
    $('#slider').parent().attr('href', $content.attr('href'));
  
    $('#slider img').attr('src', $('img', $content).attr('src').replace('_s', '_b'));
    
    if ($('.pricetag', $content).length != 0) {
    
      $('#slider .pricetag').text($('.pricetag', $content).text());
      
    } else {
    
      $('#slider .pricetag').text($('.pricetagaction', $content).text().replace('%! ', ''));
      
    }
    
    if ($content.attr('title').length > 50) {
    
      $('#slider h2').text($content.attr('title').substring(0, 48) + ' ...');
      
    } else {
    
      $('#slider h2').text($content.attr('title'));
      
    }    
    
    if ($('p', $content).text().length > 60) {
    
      $('#slider p').text($('p', $content).text().substring(0, 58) + ' ...');
      
    } else {
    
      $('#slider p').text($('p', $content).text());
      
    }

  });  
  
  $('#slider').children().delay(100).fadeTo('slow', 1.0);
  
  if ($content.next().length) {
  
    $next = $content.next();
    
  } else {
  
    $next = $('#actual a:first');
    
  }
  
  $('#actual a').attr('class', '');
  
  $next.attr('class', 'active');
  
}

function preloadSliderImages() {

  $('#actual img').each(function() {
  
    $('#hidden').append('<img src="' + $(this).attr('src').replace('_s', '_b') + '" alt="" />');
    
  });
  
}
