$(document).ready(function() {
  if ($.browser.msie && $.browser.version.substr(0,1) < 9) {
    $('input[placeholder], textarea[placeholder]').each(function() {
      var title = $(this).attr('placeholder');
      $(this).addClass('hinted');
      if ($(this).is('input')) {
        $(this).attr('value', title);
        $(this).focus(function() {
          if ($(this).attr('value') == title) {
            $(this).removeClass('hinted').attr('value', '');
          };
        });
        $(this).blur(function() {
          if ($(this).attr('value').length == 0) {
            $(this).addClass('hinted').attr('value', title);
          };
        });
      } else {
        $(this).html(title);
        $(this).focus(function() {
          if ($(this).val() == title) {
            $(this).removeClass('hinted').empty();
          };
        });
        $(this).blur(function() {
          if ($(this).val().length == 0) {
            $(this).addClass('hinted').html(title);
          };
        });
      };
    });
  };
  
  var price = $('.item-price');
  if (price.length) {
    $('ul li a', price).click(function(e) {
      if (!$(this).parent().is('act')) {
        var attr = $(this).attr('class');
        $(this).parent().addClass('act').siblings().removeClass('act');
        $('td').each(function() {
          var value = $(this).attr('data-currency-' + attr);
          $(this).html(value);
        });
      };
      e.preventDefault();
    });
  };
  
  $('a[rel]').each(function(index) {
    if ($(this).attr('rel') != 'gallery') {
      $(this).overlay({
        fixed: false,
        top: 'center',
        oneInstance: true
      });
    };
  });
  
  $('.item-rent a, .item-rent-other a').click(function(e) {
    $('.popup.rent').overlay({
      fixed: false,
      top: 'center',
      oneInstance: true,
      load: true
    }).load();
    e.preventDefault();
  });
  
  $('.content .cycle').cycle({
    slideExpr: 'img',
    prev: '.prev',
    next: '.next',
    timeout: 0
  });
  
  var promo = $('.promo');
  var promoNav = $('.nav ul', promo);
  if (promo.length) {
    promo.cycle({
      timeout: 3000,
      pause: 1,
      slideExpr: '.item',
      containerResize: 0,
      width: '100%',
      fit: 1,
      pager: promoNav,
      pagerAnchorBuilder: function(idx, slide) { 
        return '<li><a href="#"><img src="' + $(slide).attr('data-thumb') + '" /></a></li>';
      }
    });
  };
  
  var carousel = $('.carousel');
  if (carousel.length) {
    var visible = 4;
    if ($(window).width() > 1250) {
      visible = 5;
    };
    carousel.jcarousel({visible: visible})
  };
  
  $('a[rel=gallery]').fancybox({
    padding: 30,
    overlayShow: false,
    titlePosition: 'inside'
  });
});

$(window).bind('load resize', function() {
  if ($(window).width() > 1250) {
    $('html').addClass('wide');
  } else {
    $('html').removeClass('wide');
  }
});
