// jQuery Extensions
// Currently being used by the function "CheckIfYoutubeplaylist";
jQuery.extend({
   getScript: function(url, callback) {
      var 	head = document.getElementsByTagName("head")[0];
      var 	script = document.createElement("script");
      		script.src = url;
      // Handle Script loading
      {
         var done = false;
         // Attach handlers for all browsers
         script.onload = script.onreadystatechange = function(){
            if ( !done && (!this.readyState ||
                  this.readyState == "loaded" || this.readyState == "complete") ) {
               done = true;
               if (callback)
                  callback();

               // Handle memory leak in IE
               script.onload = script.onreadystatechange = null;
            }
         };
      }
      head.appendChild(script);
      // We handle everything using the script element injection
      return undefined;
   }
});

// Main Subpage object
var Subpage = {	
	InitializeAccordion: function () {
		$("#Sidebar h2")
			.append('<img src="/Images/Interface/Interface_Arrow_Down.gif" alt="" />')
			.each(function (index) {
				$(this).click(function(){
					Subpage.ToggleAccordionModule(this);
				})
			});
		$("#TripPlanner h2,#RouteSchedules h2,#RiderAlerts h2").each(function(){
			Subpage.ToggleAccordionModule(this);
			$(this).parent().removeClass("SidebarOpen");
		});
	},
	
	ToggleAccordionModule: function (module) {
		$(module).parent().toggleClass("SidebarOpen");
		$(module).children(":first")
			.attr("src", ($(module).next().toggle().is(":visible"))
				? "/Images/Interface/Interface_Arrow_Down.gif"
				: "/Images/Interface/Interface_Arrow_Left.gif"
			);
	},
	
	SideBarController: function(){
		$("#SubPageMenu .SideBarSub").hide();
		$("#SubPageMenu a").hover(
			function() {
				if($(this).next().hasClass("SideBarSub") & $(this).next().css("display") != "block"){
					$("#SubPageMenu .SideBarSub").hide("fast");
					$(this).next().show("fast");
				}
			},
			function(){
			}
		);
	},
	
	WebCMSshowhide:function(){
		$(".showhide").each(function(){
			$(this).children(".showhidetarget").hide();
			$(this).children(".showhidelink").toggle(
				function(){
					$(this).siblings(".showhidetarget").show("fast");
				},
				function(){
					$(this).siblings(".showhidetarget").hide("fast");
				}
			);
		});
		/*
		$(".showhidetarget").hide();
		$(".showhide").toggle(
			function(){
				$(this).next(".showhidetarget").show("fast");
			},
			function(){
				$(this).next(".showhidetarget").hide("fast");
			}
		);
		*/
	},
	
	CheckIfHttps:function(){
		if(document.location.protocol != "http:"){
			$("#TripPlanner h2").unbind('click');
			$("#TripPlanner h2").click(function(){
				window.location.href= '/tripplanner';
			}); // click bind
		}
	},
	
	CheckIfSubSubPageSelected:function(){
		$("#SubPageMenu .SubSubPageSelected").parent().prev().mouseover();
	},
	clickEvents:function(){
		if($(".trackme").length){
			$(".trackme").each(function(){
				$(this).click(function(){
					var tracklink = $('.trackme').attr('href');
					pageTracker._trackPageview(tracklink);
					//alert(tracklink);
				});						
			}); // each
		} // if 
	} // clickEvents
};

$(Subpage.InitializeAccordion);
$(Subpage.SideBarController);
$(Subpage.WebCMSshowhide);
$(Subpage.CheckIfHttps);
$(Subpage.CheckIfSubSubPageSelected);
//$(Subpage.CheckIfYoutubeplaylist);
$(Subpage.clickEvents);
