(function() {
  $(document).ready(function() {
    var backnew = $('.rotator .active').attr('rel')
    if (backnew)
    {
      $("#backdrop").css("background-image","url("+backnew+")");
    }
    if (($(".gun-description").length > 0) && ($(".product_aside").length > 0))
    {
      if ($(".product_aside").height() > $(".gun-description").height())
      {
        $(".gun-description").height($(".product_aside").height()+40)
      }
    }
    var products_scroller = $('.pscontainer').jScrollPane({
      showArrows: false,
      horizontalDragMinWidth: 98,
      horizontalDragMaxWidth: 98,
      autoReinitialise: true
    });
    if (products_scroller) {
      var products_scroller_api = products_scroller.data('jsp');
    }
    $('span.que').click(function() {
      if ($(this).hasClass('opened') == false)
      {
        $("span.que").removeClass('opened')
        $(this).addClass('opened')
        $('#spec-block').empty()
        $('#spec-block').append('<h3>'+$(this).attr('title')+'</h3><p>'+$(this).attr('desc')+'</p>')
      }
      return false
    })
    $('.pricerow .prev').click(
      function() {
        products_scroller_api.scrollByX(-30);
        return false
      }
    );
    $('.pricerow .next').click(
      function() {
        products_scroller_api.scrollByX(30);
        return false
      }
    );
    $('.pscontainer img').click(
      function () {
        if ($(this).hasClass('active') == false)
        {
          $('.pscontainer img').removeClass('active')
          $(this).addClass('active')

		  var mainpeek = $('#mainpic');
		  var li = $(this).parents('li');
		  var a = li.find('a');
		  mainpeek.attr('href',a.attr('rel'));
		  replacer = jQuery('<img src="'+$(this).attr('rel')+'" alt=""/>');
		  mainpeek.empty().append(replacer);
          
//          $('#productpeek').empty().append('<img src="'+$(this).attr('rel')+'" alt=""/>')
        }
      }
    );
    $('.x span').click(function() {
      var dest, parent_box, rotator;
      dest = $(this).parent().attr('class');
      parent_box = $(this).parents('.x');
      rotator = parent_box.parent('.rotator');
      if (!parent_box.hasClass(dest)) {
        parent_box.fadeOut('fast').removeClass('active');
        parent_box.siblings('.' + dest).fadeIn('fast').addClass('active');
        $("#backdrop").css("background-image","url("+$('.rotator div[class~="'+dest+'"]').attr("rel")+")");
      }
    });
    $('.texts h3').each(
      function() {
        if ($(this).hasClass("title") == false)
        {
          c = jQuery('<div class="h3-out"><div class="h3-left"></div><div class="h3"></div><div class="h3-right"></div><span>'+$(this).text()+'</span></div>');
          $(this).empty().append(c)
          $(this).find('.h3').css('width',parseInt($(this).find('span').eq(0).width()-28)+"px")
        }
      }
    );
    $('.dealer-list a.title').click(
      function() {
        lat = $(this).attr('lat')
        lng = $(this).attr('lng')
        li = $(this).parent()
        $('.map-popup').remove()
        mapbox = jQuery('<div class="map-popup"></div>')
        mapbox_container = jQuery('<div class="wrapper"></div>')
        mapbox_closer = jQuery('<div class="closebox"></div>')
        mapbox_desc = jQuery('<div class="desc"></div>')
        mapbox_map = jQuery('<div class="map" id="xmap"></div>')
        mapbox_desc.append(li.html())
        mapbox.append(mapbox_closer)
        mapbox.append(mapbox_desc)
        mapbox.append(mapbox_map)
        $('#content').append(mapbox)
        var myLatlng = new google.maps.LatLng(lat,lng);
        var myOptions = {
          zoom: 13,
          center: myLatlng,
          mapTypeId: google.maps.MapTypeId.ROADMAP
        }
        var map = new google.maps.Map(document.getElementById('xmap'), myOptions);
    
        var marker = new google.maps.Marker({
          position: myLatlng, 
          map: map
        });
      }
    )
    $('.dealer-enter div.password span').click(
      function() {
        $(this).hide()
      }
    )
    $('.dealer-enter div.password input').focus(
      function() {
        $(this).next().hide()
      }
    )
    $('.dealer-enter div.password input').blur(
      function() {
        if (($(this).val() == 'Пароль') || ($(this).val() == ''))
        {
          $(this).next().show()
        }
      }
    )
    $('.dealer-enter div.login input').focus(
      function() {
        if (($(this).val() == 'Логин') || ($(this).val() == ''))
        {
          $(this).val("")
        }
      }
    )
    $('.dealer-enter div.login input').blur(
      function() {
        if (($(this).val() == 'Логин') || ($(this).val() == ''))
        {
          $(this).val("Логин")
        }
      }
    )
    $('.map-popup .closebox').live('click', function() {
      $(this).parent().remove()
    })
    $('#questions .tumbler').click(function() {
      var detail, table;
      table = $(this).parents('table');
      detail = table.next();
      table.hide();
      detail.show();
      return false;
    });
    $('#questions .close').click(function() {
      var detail, table;
      detail = $(this).parent();
      table = detail.prev();
      detail.hide();
      table.show();
      return false;
    });
    $('.mediafile-listing .real-box').width($('.wrapper-width').width() + 'px');
    $('#scrollarea ul').css('width', $('#scrollarea li').size() * 126 + "px");
    $('#scrollarea').jScrollPane({
      showArrows: false,
      horizontalDragMinWidth: 98,
      horizontalDragMaxWidth: 98,
      autoReinitialise: true
    });
    $('.scrollarea ul').css('width', $('.scrollarea li').size() * 171 + "px");
    var images_scroller = $('.scrollarea').jScrollPane({
      showArrows: false,
      horizontalDragMinWidth: 98,
      horizontalDragMaxWidth: 98,
      autoReinitialise: true
    });
    if (images_scroller) {
      var images_scroller_api = images_scroller.data('jsp');
    }
    $('.scrollarea a').click(
      function() {
        return changePeek($(this),images_scroller_api,0)
      }
    );
    $('#mainback').click(
      function() {
        current = $('.scrollarea li.active');
        next = current.prev();
        if (next) {
          return changePeek(next.find('a'),images_scroller_api,-161)
        }
      }
    );
    $('#mainforward').click(
      function() {
        current = $('.scrollarea li.active');
        next = current.next();
        if (next) {
          return changePeek(next.find('a'),images_scroller_api,161)
        }
      }
    );
    $('.mediafile-listing .back').click(
      function() {
        images_scroller_api.scrollByX(-161);
        return false
      }
    );
    $('.mediafile-listing .forward').click(
      function() {
        images_scroller_api.scrollByX(161);
        return false
      }
    );
    $('ol.results').each(
      function() {
        var starts = parseInt($(this).attr('starts'),10);
        $(this).children('li').each(
          function() {
            $(this).append('<span>'+starts+'.</span>')
            starts = starts + 1
          }
        )
      }
    )
    $('.video a.overlay, .video a.over').click(function() {
      var lnk = $(this).attr('rel');
      if (lnk)
      {
        $.get(lnk, function(data) {
          closebox = jQuery('<span class="closebox" onclick="$(\'#shadowbox\').hide();$(this).parent().parent().remove(); "></span>')
          y = jQuery('<div class="container"></div>').append(closebox).append(data)
          x = jQuery('<div class="mediabox"></div>').append(y)
          $('#shadowbox').show().after(x)
        });
        return false;
      }
    });
    /* Закрытие fancy-box при клике по самой картинке */
    $('#fancybox-img').live('click',function() {
    	$('#fancybox-close').trigger('click');
    });
    
  });
  $(window).load(function() {
    $('.texts img.floaterbox').each(
      function() {
        c = jQuery('<div class="image-holder right"><a href="'+$(this).attr('rel')+'" title="'+$(this).attr('title')+'"><img alt="'+$(this).attr('title')+'" height="'+$(this).attr('height')+'" src="'+$(this).attr('src')+'" width="'+$(this).attr('width')+'" /></a><a class="title" title="'+$(this).attr('title')+'" href="'+$(this).attr('rel')+'">'+$(this).attr('title')+'</a></div>');
        $(this).after(c);
        c.find("a").fancybox({
		      'overlayOpacity'	:	0.6,
		      'overlayColor'    : "#000",
		      'titlePosition'   : 'over'
	      });
        $(this).detach();
      }
    );
    $('.texts img.fncimg').each(
      function() {
        c = jQuery('<a class="fnclink" href="'+$(this).attr('rel')+'" title="'+$(this).attr('title')+'"><img alt="'+$(this).attr('title')+'" height="'+$(this).attr('height')+'" src="'+$(this).attr('src')+'" width="'+$(this).attr('width')+'" /></a>');
        $(this).after(c);
        c.find("a").fancybox({
              'overlayOpacity'    :    0.6,
              'overlayColor'    : "#000",
              'titlePosition'   : 'over'
          });
        $(this).detach();
      }
    );
    $('image-holder a, #mainpic, .fnclink').fancybox({
		  'overlayOpacity'	:	0.6,
		  'overlayColor'    : "#000",
		  'titlePosition'   : 'over'
	  });
	  
	  //resize pscontailer's ul
	  var pswidth = 0
	  $('.pscontainer li').each(
	    function() {
	      pswidth = pswidth + $(this).width()
	    }
	  )
	  $('.pscontainer ul').width(pswidth)
    return $('.mediafile table span').each(function() {
      return $(this).css('height', $(this).parent().height() + 1 + 'px');
    });
  });
}).call(this);

$(window).resize(
  function() {
    if ($('#gun-description'))
    {
      $('#gun-description').width(($('.wrapper-width').width()-30) + 'px');
    }
  }
)

function changePeek(li,scroller,delta) {
  var list = li.parents('ul');
  var mainpeek = $('#mainpic');
  list.find('li.active').removeClass('active');
  li.parent().addClass('active');
  scroller.scrollByX(delta);
  mainpeek.attr('href',li.attr('rel')).attr('title',li.attr('title'));
  replacer = jQuery('<img src="'+li.attr('href')+'" title="'+li.attr('title')+'" alt=""/>');
  mainpeek.empty().append(replacer);
  $('.mediafile table span').each(function() {
    $(this).css('height', $(this).parent().height() + 1 + 'px');
  });
  return false;
}
