$(function(){
  
  if ($.browser.msie) $("body").addClass("msie");
  
  $('#centercol h1.title, #h1').addClass("fonted");
  Cufon.replace('#centercol h1.title, #h1');

  $("a[rel='gallery']").fancybox({
                'transitionIn'	: 'fade',
		'transitionOut'	: 'fade',
                'speedIn'       : 600,
                'speedIn'       : 500,
		'easingIn'      : 'easeOutBack',
		'easingOut'     : 'easeInBack',
                'overlayColor'  : '#fff',
		'titlePosition' : 'over',
		'titleFormat'   : function(title, currentArray, currentIndex, currentOpts) {
		    if (currentArray.length > 1) return '<span id="fancybox-title-over">' +  (currentIndex + 1) + ' / ' + currentArray.length + ' ' + title + '</span>';
        //else currentOpts.showNavArrows = false;
		}
	});
  
  $("#header .order-bar .submit input").each(function(){
    $(this).after("<a href='#'>"+$(this).val()+"</a>");
    $(this).hide().next().click(function(){
      $(this).closest("form").submit();
      return false;
    }).hover(function(){
    $(this).addClass("hover").fadeIn(200);
  }, function(){
    $(this).removeClass("hover");
  });
  });
  orderButtonAnimate();
  bronAnimate();
  
  $('.kwicks').kwicks({
		max: 704,
		spacing:  2,
    duration: 800,
    sticky: ($('.kwicks li.expanded').prevAll().andSelf().length)?true:false,
    defaultKwick: $('.kwicks li.expanded').prevAll().length
	});
  
  $(".order-bar .image img").each(function(i){
    $(this).attr("rel", i);
  });
  
  $(".order-bar select").change(function(){
    $(this).addClass("changed");
    ind = $(this).find("option[value='"+$(this).val()+"']").prevAll().length;
    $(".order-bar .image img").css({zIndex: 0}).eq(ind).css({zIndex: 1});
  });
  
  $(".order-bar .image img:first").addClass("next-animated").css({zIndex: 1});
  animateOrderBar();
  setInterval(function(){ animateOrderBar(); }, 4000);
  
  
  //$("h1.abs").insertAfter($("#footer .copy a")).before(", ").css({position: "static", display: "inline"});
});

function animateOrderBar() {
    if (!$(".order-bar select").hasClass("changed"))
    {
      imgs = $(".order-bar .image img");
      next_anim = imgs.filter(".next-animated");
      imgs.parent().append(next_anim.clone().removeClass("next-animated").addClass("temp").css({opacity: 0, zIndex: 2}));
      imgs.parent().find(".temp").animate({opacity: 1}, 800, function(){
        next_anim.siblings().css({zIndex: 0}).end().css({zIndex: 1});
        $(this).remove();
        $(".order-bar select option").eq(imgs.filter(".next-animated").prevAll().length).attr("selected", "selected");
        if (next_anim.next().length) next_anim.next().addClass("next-animated");
        else imgs.eq(0).addClass("next-animated");
        next_anim.removeClass("next-animated");
      });
    }
  }

function querySt(ji) {
  hu = window.location.search.substring(1);
  gy = hu.split("&");
  for (i=0;i<gy.length;i++) {
    ft = gy[i].split("=");
    if (ft[0] == ji) {
      return ft[1];
    }
  }
}

function orderButtonAnimate()
{
    obj = $("#header .order-bar .submit a");
    if (!obj.filter(".hover").length)
    {
      if (obj.filter(":visible").length) obj.fadeOut(500);
      else obj.fadeIn(500);
    }
    setTimeout("orderButtonAnimate()", 600);
}

function bronAnimate()
{
    obj = $("#bron");
    if (!obj.filter(".hover").length)
    {
      if (obj.filter(":visible").length) obj.fadeOut(400);
      else obj.fadeIn(400);
    }
    setTimeout("bronAnimate()", 500);
}
