var af_interval;
var scrollCount = new Array(2);
var scrollInterval = new Array(2);
var currentScroll = new Array(new Array(0,0,0,0),new Array(0,0,0,0));
var oldScroll = new Array(0,0);
var weatherHidden = true;

var af_top = 5;
var odd = new Array(2);
var tags = new Array("arrivals","departs");
var actionTags = new Array(2);
section_open = 0;

var airfares = 1;
var airfare_height = 21;

$(document).ready(function() {

  airfares = $("#airfare_wd_list tr").size();
  airfare_height = airfares * 21;

  $("table#airfare_wd_list tr td:nth-child(2)").addClass("alignRight");
  $("table#airfare_wd_list tr td:nth-child(3)").addClass("fareType")

  // Create scrolling tables
  // there are two of them.
  for (i = 0; i < 2; i++) {
    actionTags[i] = "div." + tags[i] + ".Scroll";

    // for accessibility, flight info starts life as a table, and is converted to divs on pageload.
    //row 0 is the top header
    var tableRowSelector = ".rTable" + i + " table." + tags[i] + ".homepage tr";
    var replaceTable = "div.rTable" + i;

    var rStr = $(tableRowSelector + ":eq(0)").html();

    var headerStr = '<div class="' + tags[i] + ' ScrollTop">' + rStr + '</div>';

    // row 1 is the key header
    var outTh = "";
    $(tableRowSelector + ":eq(1) th").each(function(j) {
      outTh += " \n<div class=\"c" + j + "\">" + $(this).text() + "</div>";
    });
    outTh = "  \n" + '<div class="' + tags[i] + ' ScrollHead container">' + outTh + "\n" + '</div>';


    var bodyRows = $(tableRowSelector).size();
    var outBody = "";
    for (k = 2; k < bodyRows - 1; k++) {
      var temp = "";
      $(tableRowSelector + ":eq(" + k + ") td").each(function(j) {
        temp += "  \n<div class=\"c" + j + "\">" + $(this).text() + "</div>";
      });
      outBody += "\n" + '<div class="' + tags[i] + ' Scroll">' + temp + "\n" + '</div>';
    }

    outBody = "\n" + '<div id="scrollup' + i + '">' + outBody + "\n" + '</div>';
    var outFooter = "";

    $(replaceTable).html(headerStr + outTh + outBody + outFooter + "\n");

    odd[i] = SetUpScrollCells(i);

    $(tags[i]).each(function(j) {
      $(this).css('top', (j + currentScroll[i]) * 21 + 'px');
    });
    ScrollRotate(i);

    scrollInterval[i] = setInterval("ScrollRotate(" + i + ")", 5000); //time in milliseconds

    af_interval = setInterval(af_rotate, 550);

    $('#prompt').hover(function() {
      clearInterval(af_interval);
    }, function() {
      //af_interval = setInterval(af_rotate,700);
      //af_rotate();
    });



  }

  $(".showBigWeather").click(function(event) {
    toggleWeather();
    return false;
  });
  $(".weatherSwap_wrapper").click(function(event) {
    toggleWeather();
    return false;
  });

});
 
function ScrollRotate(n) {
	for (k=0; k<4; k++)
		{
  		 currentScroll[n][k] = (oldScroll[n] + 1 + k) % scrollCount[n];
		}
  
 
   $(actionTags[n]+":eq(" + oldScroll[n] + ")").animate({top: -21},"normal", function() {
     $(this).css('top','80px');
   });
   
   for( k=0;k<4; k++)
   		{
   		$(actionTags[n]+":eq(" + currentScroll[n][k] + ")").show().animate({top: 21*k},"normal");
		}
   
   fixStripes(n,oldScroll[n]);
    oldScroll[n] = currentScroll[n][0];
 }
 
 function fixStripes(n, which){
 	if(odd[n])
 	{
   if($(actionTags[n]+":eq(" + which + ")").hasClass("alt"))
   		{
   		$(actionTags[n]+":eq(" + which + ")").removeClass("alt");
   		}
   	else
   		{
   		$(actionTags[n]+":eq(" + which + ")").addClass("alt");
   		}
   	}
 }
 	  
function SetUpScrollCells(n){
	//zebra stripes
	$(actionTags[n]+":even").addClass("alt");

	//create classes for the fake table cells so that we can line things up nice
 	$(actionTags[n]+" div").each(function (i) {
		$(this).addClass('c'+i%6);				   
	});
	
	scrollCount[n] = $(actionTags[n]).size();
	return scrollCount[n]%2!=0;
}

function toggleWeather(){
	if(weatherHidden)
			{
			$("div#nav").css({"z-index":"0"});
			$(".weatherSwap").css({"left":"160px","z-index":"2000"});
			$(".weatherSwap_wrapper").css({"position":"absolute","width":"100%","background":"url(images/grey_465359_50.png)","height":"100%","left":"0","top":"0","z-index":"1000"});
			}
		else
			{
			$("div#nav").css("z-index", "1");
			$(".weatherSwap").css({"left":"-2000px","z-index":"0"});
			$(".weatherSwap_wrapper").css({"position":"relative","width":"0","background":"none","height":"0","left":"-200px","top":"-170px","z-index":"0"});
			}
		weatherHidden = !weatherHidden;
		return false;
}

function af_rotate() {
	var newTop = af_top+1;
	var delta = -newTop;
	
	$("table#airfare_wd_list").css('top',delta+"px");
	
	
	//$("table#airfare_wd_list").animate({top: delta},"slow", function(){
	//$(this).css('top',af_top + 'px');
	//});
	af_top = newTop;
	
	// the following is bogus, should be replaced with logic that calculates the correct height of the table.
	if (af_top > airfare_height) 
		{
		af_top = -179;
		}
}

