$(document).ready(function() {

    $("#footer").css("background-image", "none");
      
    //if IE6
    if((navigator.appVersion.indexOf("MSIE 6") > 0) ? true : false){
      $("#transparentborders").height($("#maincontentcontainer").height() + 42);
      
      $("#pglogo_img, #transparentborders, #transparentbottom").supersleight();
    }
    
   //change out backgroun-images / colors
   $("#outer.red, #outer.green, #outer.purple, #outer.orange").css("background-color", "#091822");
   
   //fix links that make link to "flash pages"
   $("#maincontentcontainer a").each(function(){
   
     	var href = $(this).attr("href").toLowerCase();
     	if(href.lastIndexOf("/") != href.length - 1){
     		href += "/";
     	}
     	
     	if(href == "/driving-business-with-consumer-advocacy/" || 
     	href == "/creating-effective-advocacy/" || 
     	href == "/delivering-measurable-results/" || 
     	href == "/revealing-case-studies/"){
     		$(this).attr("href", "/#" + href);
     	}
   });
   
});


$(window).ready(function(){

  $("body").css("background-image", "url(/images/bodybg.png)");
  
  if((navigator.appVersion.indexOf("MSIE 6") > 0) ? true : false){
    $("#footer").css("bottom", "0");
  }

  if(!(navigator.appVersion.indexOf("MSIE 6") > 0) ? true : false){
    $("#contentbg").height(700);
    //fade contents of page in
    if($("#headerimagecontainer").length > 0){
      $("#headerimagecontainer").fadeIn(400,function(){
          $("#headlinecontainer").fadeIn(100, function(){
          
        		    $("#subcontentcontainer").fadeIn(50, function(){
        		 		   $("#rightcontactbuttoncontainer").fadeIn(50, function(){
        		 		   
        		 		       $("#transparentborders").height($("#maincontentcontainer").height() + 42);
        		 		        $("#footer").css("top", $("body").height() - 25);
            		 		    FixSubContentAndFooter();	 		   
        		 		   });
        		 		});
        		 	  $("#subnavcontainer").fadeIn(50);
      		});
      });
    }
    else{
      if($("#subcontentcontainer").length > 0){
        $("#subcontentcontainer").fadeIn(100, function(){
          FixSubContentAndFooter();
        });
      }
      else{
        FixSubContentAndFooter();
      }
    }
  }
  else{
    FixSubContentAndFooter();
  }
});


function FixSubContentAndFooter(){
  $("#contentbg").css("height", "auto");
  
  if($("#subcontentcontainer .right").height() > $("#subcontentcontainer .left").height()){
    $("#subcontentcontainer .right").css("margin-bottom", "380px");
  }    
  
  if(window.location.pathname != "/" || (navigator.appVersion.indexOf("MSIE 6") > 0) ? true : false){
    $("#footer").css("top", $("body").height() - 25);
  }
    
}

var $ajaxloadingcontainer = $("#ajaxloadingcontainer");
function ChangeContent(url) {

  if(url.indexOf("/") == 0){
    url = url.substring(1);
  }

  $.get("/ajaxpagecontent.aspx?url=/" + url + "", function(data){

    //load content in temp div
    $ajaxloadingcontainer.html(data);
    
    //load html
    $("#headlinecontainer").html($ajaxloadingcontainer.find("#headlinecontainer").html());
    $("#subcontentcontainer").html($ajaxloadingcontainer.find("#subcontentcontainer").html());
    $("#subnavcontainer").html($ajaxloadingcontainer.find("#subnavcontainer").html());
    document.title = $ajaxloadingcontainer.find("#pagetitle").html();
    
    HookUpFadeInContentLinks();
    
    FixSubContentAndFooter();
    
    if((navigator.appVersion.indexOf("MSIE 6") > 0) ? true : false){
      $("#transparentborders").height($("#maincontentcontainer").height() + 42);
    }
    
    /*
    //fix footer and transparentborders
    if((navigator.appVersion.indexOf("MSIE 6") > 0) ? true : false){
      FixSubContentAndFooter();
      $("#transparentborders").height($("#maincontentcontainer").height() + 42);
    }
    */
    
  }); 
}

var $subcontentleft = $("#subcontentcontainer .left");
var PreviousOn = $("#subnavcontainer a.on");

function ChangeSubContent(obj, url){
    url = url.replace("http://" + window.location.host, "")
    
    $("#subnavcontainer a.on").removeClass("on");
    $("#subnavcontainer a[href='#" + url + "']").addClass("on");
    $("#subnavcontainer a[href='#" + url + "/" + "']").addClass("on");
    
    $.get("/HtmlWS/?url=" + url + "&xpath=/root/LeftContent", function(data){
      var Height = $subcontentleft.height();
      $("#subcontentcontainer .left").fadeOut(200, function(){
        $(this).html(data).fadeIn(200, function(){
            FixSubContentAndFooter();
            $("#footer").css("top", $("body").height() - 25);
            /*
            if((navigator.appVersion.indexOf("MSIE 6") > 0) ? true : false){
              $("#footer").css("top", $("body").height() - 25);
              $("#transparentborders").height($("#maincontentcontainer").height() + 42);
            }
            */
            
            HookUpFadeInContentLinks();
            
            if((navigator.appVersion.indexOf("MSIE 6") > 0) ? true : false){
              $("#transparentborders").height($("#maincontentcontainer").height() + 42);
            }
        });
      });
   });
}


//hooks up the click event for the links within content that are to load page / sub page content via ajax
function HookUpFadeInContentLinks(){
 $("#subcontentcontainer a.FadeInContent").each(function(){
 
    //replace the # to fix the weird double ## problem
    var href = $(this).attr("href").replace("http://" + window.location.host, "").replace("#", "");
    
    //length of 4 means it is a a page load, not sub content load
    if(false){
    //if(href.split("/").length == 4){
      $(this).attr("href", "#" + href).unbind().click(function(event){
        event.preventDefault();
        ChangeContent(href);
      });
    }
    else{
      $(this).attr("href", "#" + href).unbind().click(function(event){
        event.preventDefault();
        ChangeSubContent(this, href);
      });
    }
  });
}