this.resetAll = function() {
  
}

this.fadeIn = function(obj) {
  obj.animate( {top: '+=5'}, 750, 'easeOutBounce', function() {
    fadeOut($(this));
  })
}

this.fadeOut = function(obj, delay) {
  obj.animate({top: '-=5'}, 750, 'easeInQuad', function() {
    fadeIn($(this));
  })
}

this.sfadeOut = function(index) {
  fadeOut($(".pin:eq("+index+")"));
}

var origs = new Array();

this.ticker_rotate = function() {
    $('#publishers div.pub-item:visible').fadeOut("slow", function() {
      next = $(this).next(".pub-item");
      if (next.length) {
          next.fadeIn("slow", function() {
            var idx = $("#publishers div.pub-item").index($(this));
            resetPubs();
            $(".pub-icons li:eq("+idx+") img").attr('src', '/++resource++nms.site/images/icon-active.gif');
          });
      } else {
          $('#publishers div.pub-item:first').fadeIn("slow", function() {
            var idx = $("#publishers div.pub-item").index($(this));
            resetPubs();
            $(".pub-icons li:eq("+idx+") img").attr('src', '/++resource++nms.site/images/icon-active.gif');
          });
      }
    });
}

this.news_rotate = function() {
    $('#news-ticker div.news-item:visible').fadeOut("slow", function() {
      next = $(this).next();
      if (next.length) {
          next.fadeIn("slow");
      } else {
          $('#news-ticker div.news-item:first').fadeIn("slow");
      }
    });
}

var tickerInterval;
var newsInterval;

this.resetPubs = function() {
  $(".pub-icons img").attr('src', '/++resource++nms.site/images/icon-inactive.gif');
}

this.showPub = function(idx) {
  resetPubs();
  $(".pub-icons li:eq("+idx+") img").attr('src', '/++resource++nms.site/images/icon-active.gif');
  window.clearInterval(tickerInterval);
  $('#publishers div.pub-item:visible').fadeOut("slow", function() {
    $('#publishers div.pub-item:eq('+idx+')').fadeIn("slow");
    tickerInterval = setInterval("ticker_rotate()", 10000);  
  });
}

$(function() {
  
  $("#billboard .items a").click(function(ev) {
    ev.preventDefault();
    $("#billboard-item img").attr('src', $(this).attr('href'));
  })

  tickerInterval = setInterval("ticker_rotate()", 10000);  
  $('#publishers div.pub-item:first').show();
  
  newsInterval = setInterval("news_rotate()", 10000);  
  $('#news-ticker div.news-item:first').show();

  $("#nav li").hover(function() {
      $(this).siblings().removeClass("sfhover").find("ul:first").hide();
      $(this).addClass("sfhover").find("ul:first").slideDown("fast");
    }, function () {
      $(this).removeClass("sfhover").find("ul:first").hide();
  });
  
  $(".pub-icons li").click(function() {
    var idx = $(".pub-icons li").index($(this));
    showPub(idx);
  })
  
  $(".portlet img").each(function() {
    $(this).closest(".portlet").addClass("has-image");
  })
  
  $("ul#casestudies li").hover(function() {
    $("img", this).animate({opacity:0}, "fast");
    $(".info", this).fadeIn("fast");
  }, function() {
    $("img", this).animate({opacity:1},"fast");
    $(".info", this).fadeOut("fast");
  })

  $(".portlet").hover(function() {
    $(".small", this).animate({opacity:0}, 0, function() {
      $(this).next().slideDown("fast");
    });
  }, function() {
    $(".full", this).slideUp("fast", function() {
      $(this).prev().animate({opacity:1}, 0);
    });  
  }).click(function() {
    window.location.href = $("a:first", this).attr('href');
    return false;
  });
  $("input[type=checkbox],input[type=radio]").css('border','0');

  /*$("#pins .pin").each(function() {
    origs[$("#pins .pin").index(this)] = $(this).css('top');
    startTimer($(this))
    $(this).hover(function() {
      $(this).stop();;
      $("img.bubble", this).fadeIn();
    }, function() {
      $("img.bubble", this).fadeOut();
      $(this).animate({top: origs[$("#pins .pin").index(this)]}, function() {
        fadeOut($(this));
      });
    })
  })*/
  
  setInterval("carousel_rotate()", 10000);  
  $('#carousel li:not(:first)').hide();

  setup_popup();
  
  var base_url = $("#logo a").attr('href');
  //$("a:not([href^='"+base_url+"'])").attr('target', '_blank');
})

var popupInterval;

this.showPopup = function() {
  $.get('event-popup', function(data) {
    $("#event-overlay").fadeIn();
    $("#event-popup").html(data);
    var button = $('<input type="button" value="No thanks" class="context" />').click(function(ev) {
      ev.preventDefault();
      $("#event-overlay").fadeOut();
      $("#event-popup").fadeOut(function() {
        $(this).html('');
      });
    })
    $("#event-popup .formControls").prepend(button)
      .find("input[type=submit]").attr('class', 'context2');
    $("#event-popup form").append($("#event-popup input[name=event]"));
    $("#event-popup").fadeIn();    
  })
}

this.setup_popup = function() {
  if( $("#event-popup").length > 0 ) {
    $("#popup").append($("#event-popup"));
    setTimeout(showPopup, 2000);
    $("#event-overlay").css('height', $(document).height()+'px');
  }
  $("#event-popup form").live("submit", function(ev) {
    ev.preventDefault();
    var button = $('<input type="button" value="No thanks" class="context" />').click(function(ev) {
      ev.preventDefault();
      $("#event-overlay").fadeOut();
      $("#event-popup").fadeOut(function() {
        $(this).html('');
      });
    })
    $.post($(this).attr('action'), $(this).serialize(), function(data) {
      //alert(data);
      $("#event-popup form").append($("#event-popup input[name=event]"));
      if ($(data).find("input[type=submit]").length > 0) {
        $("#event-popup").html(data).find(".formControls").prepend(button)
         .find("input[type=submit]").attr('class', 'context2');        
      } else {
        $("#event-popup #success").show();
        var button2 = $('<input type="button" value="Close" class="context closeme" />').click(function(ev) {
          ev.preventDefault();
          $("#event-overlay").fadeOut();
          $("#event-popup").fadeOut(function() {
            $(this).html('');
          });
        })   
        $("#event-popup form").remove();
        $("#event-popup #success").append(button2);     
      }
    })
  })
}

this.carousel_rotate = function() {
    $('#carousel li:visible').fadeOut("slow", function() {
      
    });
    next = $('#carousel li:visible').next();
    if (next.length) {
        next.fadeIn("fast", function() {
          
        });
    } else {
        $('#carousel li:first').fadeIn("fast", function() {
          
        });
    }
}

this.startTimer = function(obj) {
  delay = Math.random() * 2000;
  setTimeout("sfadeOut('"+$("#pins .pin").index(obj)+"')", delay);
}
