﻿$(function () {
  var browserName = $.grep(["webkit", "opera", "msie", "mozilla"], function (b) {
    return $.browser[b] === true;
  })[0];

  $("html")
    .addClass(browserName)
    .addClass(browserName + $.browser.version.match(/([^\.]+)(\.|$)/)[1]);

  $("#tabs .box").live("click", function (e) {
    e.preventDefault();
    if ($(e.target).closest(".inactive")) {
      $("#tabs").find(".simpleBox").addClass("hidden");
      $($(e.target).closest(".box").find("a").attr("href")).removeClass("hidden");

      $("#tabs").find(".active").removeClass("active").addClass("inactive").addClass("light-text");
      $(e.target).closest(".inactive").removeClass("inactive").removeClass("light-text").addClass("active");

      Cufon.replace('#tabs h2');
      Cufon.now();
    }
  });

  $(".event-list .item").live("click", function (e) {
    e.preventDefault();
    window.location.href = $(this).find("h3 a").attr("href");
  });

  Utils.installSmartImageBox($('.smartImageBox'));
});

var Utils = {

  // Causes a text field (such as a search box) to be
  // cleared of the default text when given focus.
  installAutoClear: function (el) {
    var origVal = el.val();

    el.focus(function () {
      if (el.val() == origVal) {
        el.val('');
      }
    });

    el.blur(function () {
      if (el.val() == '') {
        el.val(origVal);
      }
    });
  },

  installHovered: function () {
    $('.unhovered').live('mouseenter', function () {
      $(this).removeClass("unhovered").addClass("hovered");
    });
    $('.hovered').live('mouseleave', function () {
      $(this).removeClass("hovered").addClass("unhovered");
    });
  },

  installSmartImageBox: function (box) {
    $('a', box).hide();
    $(box).hover(
            function () {
              $('a', this).addClass("hover");
              $('a', this).show();
            },
            function () {
              $('a', this).removeClass("hover");
              $('a', this).hide();
            }
        );

    $('.remove').click(function () {
      var obj = this;
      $.ajax({ url: $(this).attr("rel") + "/RemoveMainImage", context: document.body, success: function () {
        $('img', $(obj)).parent().hide();
        $('input', $(obj).parent()).removeClass("hide");
      }
      });
    });

    $('.delete').click(function () {
      var obj = this;
      $.ajax({ url: "/profiel/deleteimage", context: document.body, success: function () {
        $('img', $(obj).parent()).hide();
        $('input', $(obj).parent().parent()).removeClass("hide");
      }
      });
    });

    $('.deleteCaseImage').click(function () {
      var obj = this;
      $.ajax({ url: "DeleteCaseImage?stepName=" + $(this).attr("rel"), context: document.body, success: function () {
        $('img', $(obj).parent()).hide();
        $('input', $(obj).parent().parent()).removeClass("hide");
      }
      });
    });

  },

  installAndersNamelijk: function () {
    $('select').each(function (i, selectEl) {
      var id = selectEl.id;
      var otherOptionEl = $(selectEl).children('option[value=other]')[0];
      var otherTextEl = $('#' + id + '_');

      var showOtherEl = function () {
        otherTextEl.css('display', 'inline');
        $(selectEl).attr('name', id + '_');
        otherTextEl.attr('name', id);
      }

      if (otherOptionEl) {
        if (otherTextEl.val()) {
          $(selectEl).val('other');
          showOtherEl();
        }

        $(selectEl).change(function () {
          var selectedValue = selectEl.value;
          if (selectedValue == 'other') {
            showOtherEl();
            otherTextEl.focus();
          } else {
            otherTextEl.css('display', 'none');
            $(selectEl).attr('name', id);
            otherTextEl.attr('name', id + '_');
          }
        });
      }
    });
  },

  installCollapsor: function (containerSelector, headingSelector, toggleNow) {
    $(containerSelector).each(function () {
      var thisLevel = $(this);

      // On-load collapse of tips, for graceful degradability in a non-JS situation.
      if (toggleNow) {
        thisLevel.toggleClass('expanded collapsed');
      }

      $(headingSelector, thisLevel).click(function () {
        thisLevel.toggleClass('expanded collapsed');
      });
    });
  },

  installPartnerLogoCarousel: function () {
    var pos = 0;
    var nlogos = $('.partnerLogo').length;
    var maxPos = nlogos - 6;

    if (maxPos <= 0) {
      $('.partnerLogos .arrow-left img').css('display', 'none');
      $('.partnerLogos .arrow-right img').css('display', 'none');
    } else {
      var setEnabled = function (el, enabled) {
        el.css('cursor', enabled ? 'pointer' : 'auto');
        el.animate({ 'opacity': enabled ? '1' : '0.1' });
      };
      var set = function (i) {
        pos = i;
        $('.partnerLogosInner').animate({ 'left': (i * -150) }, 600);
        setEnabled($('.partnerLogos .arrow-left'), pos > 0);
        setEnabled($('.partnerLogos .arrow-right'), pos < maxPos);
      };
      var adjust = function (d) {
        pos += d;
        pos = Math.max(0, pos);
        pos = Math.min(pos, maxPos);
        set(pos);
      };
      $('.partnerLogos .arrow-left').click(function () { adjust(-1); });
      $('.partnerLogos .arrow-right').click(function () { adjust(1); });
      set(0);
    }
  }

};
