$(document).ready(toggleFooter);
$(window).load(toggleFooter);
$(window).resize(toggleFooter);

var BANNER_DURATION = 18000;
//var BANNER_DURATION = 9000;

function toggleFooter() {
  var utilityContainer = $('#utilitybarcontainer');
  var windowHeight = $(window).height();
  var contentHeight = $('#pagecontainer').outerHeight(true) + utilityContainer.outerHeight(true);

  utilityContainer.toggleClass('relative', windowHeight < contentHeight);
  utilityContainer.toggleClass('fixed', windowHeight >= contentHeight);
}

function checkhash() {
  var h = new String(document.location.hash);
  h = h.replace(/#/, '');
  if(h != "" && $('#' + h).length > 0) {
    $('#' + h).trigger('click');
  }
}

function init_whatlists() {
  $('.whatlist a').each(function() {
    var $this = $(this);
    $this.addClass("thickbox")
        .attr("href", $this.attr("href") + "?height=280&width=600")
        .attr("title", "");
  });
  $('.whatlist a').click(function() {
    document.location.hash = this.id;
  });
  tb_init('.whatlist a.thickbox');
}

function init_clientpop() {
  $('#banner-text .client_pop a').each(function() {
    var $this = $(this);
    $this.addClass("thickbox")
        .attr("href", $this.attr("href") + "?height=540&width=730")
        .attr("title", "");
  });
}

function init_clientnames() {
  $('.clientlist a').each(function() {
    var $this = $(this);
    $this.addClass("thickbox")
        .attr("href", $this.attr("href") + "?height=540&width=730")
        .attr("title", "");
  });
  $('.banner-slice a').each(function() {
    var $this = $(this);
    $this.addClass("thickbox")
        .attr("href", $this.attr("href") + "?height=540&width=730")
        .attr("title", "");
  });
  $('.clientlist a').click(function() {
    document.location.hash = this.id;
  });
  $('.banner-slice a').click(function() {
    document.location.hash = this.id;
  });
  tb_init('.banner-slice a.thickbox, .clientlist a.thickbox');
}

function init_banner() {
  tb_init('#banner-text a.thickbox');
}

function ajaxImageBankCall(locationInImages, images) {
  $.ajax({
    type: "POST",
    url: "/image_bank_requests.php",
    data: "id=" + images[locationInImages],
    success: function(msg) {
      if(msg.indexOf('*|*') > 0) {
        //alert("index is > 0");
        temp = msg.split('*|*');
        if(temp.length == 4) {
          $("#banner-text").animate({ opacity: 0 }, 1000);
          $("#banner-image-content").animate({ opacity: 0 }, 1000, null, function () {
            $("#banner-swf").html("");
            if(temp[3] == "jpg") {
              var img = new Image();
              $("#banner-image-content").html(img); //put image in fadedOut div
              $("#banner-text").html(temp[1]); //put new text in fadedOut div
              init_clientpop();
              init_banner();

              // wrap our new image in jQuery, then:
              $(img)
                // once the image has loaded, execute this code
                  .load(function () {
                // set the image hidden by default
                //alert("going to fade in");
                // fade our image in to create a nice effect
                $("#banner-image-content").animate({ opacity: 1 }, 1000);
                $("#banner-text").animate({ opacity: 1 }, {
                  duration: 1000,
                  complete: function() {
                    if(jQuery.browser.msie) {
                      this.style.removeAttribute('filter');
                    }
                  }
                });
              })

                // if there was an error loading the image, react accordingly
                  .error(function () {
                // notify the user that the image could not be loaded
              })

                // *finally*, set the src attribute of the new image to our image
                  .attr('src', temp[0]);  //when loaded, will show
            } else if(temp[3] == "swf") {
              $("#banner-image-content").html("<div id=\"banner-swf\"></div>");
              $("#banner-text").html(temp[1]); //put new text in fadedOut div
              init_clientpop();
              init_banner();
              var params = { allowScriptAccess: "always" };
              var atts = {
                name: "banner-swf-obj",
                id: "banner-swf-obj" };
              swfobject.embedSWF(temp[0], "banner-swf", "889", "220", "9.0.0", null, null, params, atts);
              $("#banner-image-content").append(swfobject);


              $("#banner-image-content").animate({ opacity: 1 }, 1000);
              $("#banner-text").animate({ opacity: 1 }, {
                duration: 1000,
                complete: function() {
                  if(jQuery.browser.msie) {
                    this.style.removeAttribute('filter');
                  }
                }
              });

              /*swfplayer = document.getElementById("banner-swf-obj");
               if(swfplayer){
               swfplayer.addEventListener("onStateChange", function(newState) {
               alert(newState);
               if(newState == 5){ //video cued
               $("#banner-image-content").animate({ opacity: 1 }, 1000 );
               $("#banner-text").animate({ opacity: 1 }, 1000 );
               }
               });
               }else{
               alert("swfplayer is null");
               }*/
              //									tb_init('#banner-text a.thickbox');
            }
          });
        }

      }
    }
  });
}

