// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

function datesMatch(date1,date2) {
  if (date1.getYear()  == date2.getYear()  &&
      date1.getMonth() == date2.getMonth() &&
      date1.getDate()  == date2.getDate()    )
    return true;
  else
    return false;
}

function setupUnofficialSchedule() {
	var index = 1
	$('#fullcarousel').jcarousel({
	    scroll: 1,
	    start: index,
	    visible: 1
	  });
	$('.schedule_list').jScrollPane({ showArrows:true, scrollbarWidth: "14" });
}

function updateCarouselHeight(carousel, li, index, state) {
  var newHeight = $('.jcarousel-item').eq(index-1)[0].offsetHeight
  $('#mycarousel').animate({height: newHeight}, 250)
}

function setupSchedule() {
  //display today's schedule
  var sep_11 = new Date(); sep_11.setFullYear(2009,8,11);
  var sep_12 = new Date(); sep_12.setFullYear(2009,8,12);
  var sep_13 = new Date(); sep_13.setFullYear(2009,8,13);
  var sep_14 = new Date(); sep_14.setFullYear(2009,8,14);
  var sep_15 = new Date(); sep_15.setFullYear(2009,8,15);
  var sep_16 = new Date(); sep_16.setFullYear(2009,8,16);
  var sep_17 = new Date(); sep_17.setFullYear(2009,8,17);
  var today = new Date();
  var index = 1;
  if (datesMatch(today,sep_11)) index = 2;
  if (datesMatch(today,sep_12)) index = 3;
  if (datesMatch(today,sep_13)) index = 4;
  if (datesMatch(today,sep_14)) index = 5;
  if (datesMatch(today,sep_15)) index = 6;
  if (datesMatch(today,sep_16)) index = 7;
  if (datesMatch(today,sep_17)) index = 8;
  $('#mycarousel').jcarousel({
    scroll: 1,
    start: index,
    visible: 1,
    itemFirstInCallback: updateCarouselHeight
  });

  //formatting
  $('.schedule_event:first-child').addClass('no_top_border');
  $('.schedule_event:nth-child(even)').addClass('stripe');
};

var checkOverlayPosition = null;
function fixOverlayPosition() {
	  var imageHeight = $("#contentWrap").height();
	  var imageWidth = $("#contentWrap").width();
      var marginsH = $(window).width() - imageWidth;
      var marginsV = $(window).height() - imageHeight;
      var leftMargin = marginsH / 2;
      var topMargin = marginsV / 2;
	  if (imageWidth > 800) {
		leftMargin = 400;
		$("#contentWrap").css('width','800px');
		$('#contentWrap').css('overflow','hidden');
	  }
      $('#overlay').css('left',leftMargin);
	  $('#overlay').css('visibility','visible');
	  if (imageWidth > 150) {
		clearInterval(checkOverlayPosition)
	  }
};

function init() {
	//Switch the "Open" and "Close" state per click
	$("h2.trigger").toggle(function(){
			$(this).addClass("active");
			$("#listToggleLink").innerHTML = "View List of Models";
		}, function () {
			$(this).removeClass("active");
			$("#listToggleLink").innerHTML = "Hide List of Models";
	});

	//Slide up and down on click
	$("h2.trigger").click(function(){
		$(this).next(".toggle_container").slideToggle("slow");
	});

  //setupSchedule();
  $('.thumbnailImageLink').overlay({
    onBeforeLoad: function() {
		$('#overlay').css('visibility','hidden');
		$('#contentWrap').css('width','auto');
		var wrap = this.getContent().find("#contentWrap");  // grab wrapper element inside content
			wrap.html("");
			wrap.load(this.getTrigger().attr("href"), function(){ checkOverlayPosition = setInterval('fixOverlayPosition()', 50); });  // load the page specified in the trigger
    }
  });
}

$(document).ready(init);
