$(document).ready(function() {
  if ($(window).width() > 1200) {
    $('body').addClass('wide');
  } else {
    $('body').removeClass('wide');
  }
  
  var carousel = $('.carousel-container');
  if (carousel.length) {
    if ($.browser.msie && $.browser.version.substr(0,1) == 6) {
      return false;
    };
    var visible = 3;
    if ($('body').hasClass('wide')) {
      visible = 4;
    };
    $('ul', carousel).jcarousel({visible: visible})
  };
  
  var gallery = $('.products-item .images');
  if (gallery.length) {
    $('a[rel=gallery]', gallery).fancybox();
  };
   
  var advSearch = $('.aside .adv-search');
  if (advSearch.length) {
    $('.toggle .pseudo-link', advSearch).click(function(e) {
      $(this).parents('fieldset').addClass('hidden').siblings('fieldset').removeClass('hidden');
      e.preventDefault();
    });
  };
  
  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);
          };
        });
      };
    });
  };
 
  $('.adv-search .options-full li .pseudo-link.toggle').click(function(e) {
    $(this).parents('li:first').toggleClass('expanded');
    e.preventDefault();
  });
  
});

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


function disableAddToCart(elementId, mode, text)
{
	var	element = document.getElementById(elementId);
	if (!element)
		return;
	
	if (mode == "detail")
		$(element).html("<span>" + text + "</span>").toggleClass("disabled")
			.removeAttr("href").unbind('click').css("cursor", "default");
	else if (mode == "list")
		$(element).html("<span>" + text + "</span>").toggleClass("disabled")
			.removeAttr("href").unbind('click').css("cursor", "default");
}

function addToCart(element, imageToFlyId, mode, text)
{
	if (!element || !element.href)
		return;

	var button = $(element);
	if (mode == "detail")
		button.toggleClass("disabled").unbind('click').css("cursor", "default");
	else if (mode == "list")
		button.toggleClass("disabled").unbind('click').css("cursor", "default");

	$.get(
		element.href + "&ajax_buy=1",
		$.proxy(
			function(data) {

				if (this.mode == "detail")
					this.button.removeAttr("href").html("<span>" + text + "</span>");
				else if (this.mode == "list")
					this.button.removeAttr("href").html("<span>" + text + "</span>");

				$("#cart_line").html(data);
				return;
			}, { "button": button, "mode": mode, "imageToFlyId" : imageToFlyId }
		)
	);

	return false;
}

function disableAddToCompare(elementId, text)
{
	var	element = document.getElementById(elementId);
	if (!element)
		return;

	$(element)
		.removeClass("pseudo-link")
		.text(text)
		.unbind('click').removeAttr("href")
		.css("cursor", "default");

	return false;
}

function addToCompare(element, text)
{
	if (!element || !element.href) 
		return;

	var href = element.href;
	var button = $(element);

	button.removeClass("pseudo-link").unbind('click').removeAttr("href").css("cursor", "default");

	$.get(
		href + '&ajax_compare=1&ajax=1&backurl=' + decodeURIComponent(window.location.pathname),
		$.proxy(
			function(data) {

				$("#compare").html(data);
				
				this.text(text);
				
				if (compare.css("display") == "none") {
					//compare.css({ "display": "block" });
				}
			}, button
		)
	);
	
	return false;
}
