$("html").addClass("js");

if ($.browser.msie) {
  $("html").addClass("msie").addClass("msie-"+ parseInt($.browser.version, 10));
}

$("#content a").each(function(){
  if (this.protocol.indexOf("http") != 0) return;
  if (this.hostname.replace(/:[0-9]+$/, "") != document.location.hostname
  || this.href.match(/\.pdf$/i)
  ) {
    $(this).click(function(){
      var popup = window.open(this.href);
      return false;
    });
  }
});

$(".dropdown").each(function(){
  var obj = this;
  cournerize($(".content", obj));
  $(".content", obj).hide();
  $(".selection", obj).click(function(){
    $(obj).toggleClass("open");
    $(".content", obj).slideToggle(250, "swing", function(){});
    return false;
  });
});

$(".extcollapse h2").each(function(i){
  var obj = this;
  obj.content = $("<div id=extcont"+ i +"/>").hide();
  $(obj).after(obj.content);
  $("a", obj).attr("id", "link"+ i);
  $("a", obj).click(function(){
    $(this).toggleClass("open");
    if (!$(this).hasClass("loaded")) {
      $(this).addClass("loaded");
      $.get(this.href, {}, function(data, textStatus){
        if (textStatus == "success") {
          obj.content.append($("#content > div", data).html());
          $("h1", obj.content).remove();
          obj.content.toggle("normal");
        }
      });
    }
    else obj.content.toggle("normal");
    return false;
  });
});

$("#setorgroup").each(function(){
  var obj = this;
  obj.content = $("<div id="+ obj.id +"-content/>");
  $(obj).after(obj.content);
  obj.content.append($("ul", obj));
  $("ul:gt(0)", obj.content).hide();
  $("li:eq(0)", obj).addClass("open");
  $("li", obj).each(function(i){this.id = "tab"+ i});
  $("li", obj).click(function(){
    var i = this.id.replace("tab", "");
    $("li", obj).removeClass("open");
    $("li:eq("+ i +")", obj).addClass("open");
    $("ul", obj.content).hide();
    $("ul:eq("+ i +")", obj.content).show();
    $(obj).height($("li:eq("+ i +")", obj).innerHeight()); // IE7!
  });
});

$("#style .sets").each(function(){
  var obj = this;
  obj.sleep = 5000;
  obj.showNext = function(){
    $("li:first-child", obj).toggleClass("ie7");
    $("li:first-child + li", obj).toggleClass("ie7");
    $("li:first-child", obj).animate({opacity: "0"}, 500, "linear", function(){
      $("li:first-child", obj).appendTo(obj).css({opacity: "1"});
      setTimeout(obj.showNext, obj.sleep);
    });
  };
  setTimeout(obj.showNext, obj.sleep);
});

$(".collapseheaders h2 + *").each(function(){ $(this).hide(); });
$(".collapseheaders h2").click(function(){
  $(this).toggleClass("open");
  $("+ *", this).toggle("normal");
});

$("#page").append("<div id=overlay><div class=content/></div>");
$("#overlay").hide();
cournerize($("#overlay"));
$("#overlay").click(function(){
  if ($.browser.msie) $("#overlay").hide();
  else $("#overlay").overlay().close();
});
$("a[rel=overlay]").each(function(){
  var obj = this;
  $(obj).click(function(){
    $.get(
      this.href
    , {}
    , function(data, textStatus){
        if (textStatus == "success") {
          $("#overlay .content").html($("#setorproduct .image", data).html());
          $("#overlay").overlay({load: true, fixed: false, left: 120, top: 50, speed: $.browser.msie ? 0 : 200 });
          $("#overlay").overlay().load(); // Needed!
        }
      }
    , "html"
    );
    return false;
  });
});

$("#ticker li a").each(function(){
  if ($(this).height() < 118) $(this).css({paddingTop: ((118 - $(this).height()) / 2) +"px"});
});

$("#ticker").each(function(){
  var obj = this;
  if ($("li", obj).length > 3) {
    $(obj).wrapInner("<div class=container/>");
    obj.container = $(".container", obj);
    obj.previous = $("<span class=previous/>");
    obj.next = $("<span class=next/>");
    $(obj).prepend(obj.next).prepend(obj.previous);
    obj.timer = null;
    obj.sleep = 5000;
    obj.animRun = false;
    obj.start = function(){obj.timer = setTimeout(obj.showNext, obj.sleep)};
    obj.showNext = function(single){
      clearTimeout(obj.timer);
      if (obj.animRun) {setTimeout(obj.showNext, 50);return;}
      obj.animRun = true;
      $("li:first-child", obj).animate({marginLeft: "-313px"}, 750, "swing", function(){
        $("li:first-child", obj).appendTo("#ticker ul").css({marginLeft: "0px"});
        if (single !== true) obj.start();
        obj.animRun = false;
      });
    };
    obj.showPrevious = function(single){
      clearTimeout(obj.timer);
      if (obj.animRun) {setTimeout(obj.showPrevious, 50);return;}
      obj.animRun = true;
      $("li:last-child", obj).prependTo("#ticker ul").css({marginLeft: "-313px"});
      $("li:first-child", obj).animate({marginLeft: "0px"}, 750, "swing", function(){
        if (single !== true) obj.start();
        obj.animRun = false;
      });
    };
    $(obj).mousemove(function(){clearTimeout(obj.timer)});
    $(obj).mouseout(obj.start);
    obj.previous.click(function(){obj.showPrevious(true)});
    obj.next.click(function(){obj.showNext(true)});
    obj.start();
  }
});

if (swfobject.hasFlashPlayerVersion("9.0.115")) {
  swfobject.embedSWF(contextPath+"swf/logo.swf", "logo", "99", "69", "9.0.115.0",
                     contextPath+"swf/expressInstall.swf", {}, {quality: "high", wmode: "transparent"},
                     {styleClass: "noOutline"});

  if ($("#music") != null) {
    swfobject.embedSWF(contextPath+"swf/music.swf", "music", "60", "60", "9.0.115.0",
                       contextPath+"swf/expressInstall.swf", {}, {quality: "high", wmode: "transparent"},
                       {styleClass: "noOutline"});
  }
}

function cournerize(jq){
  jq.addClass("crn");
  jq.wrapInner("<div class=i/>").wrapInner("<div class=l/>").wrapInner("<div class=r/>");
  jq.prepend("<div class=tr><div class=tl><div class=t/></div></div>");
  jq.append("<div class=br><div class=bl><div class=b/></div></div>");
}

