﻿var clicked = false;

 $(document).ready(function() {
 	$("#list").find("li").each(function() {
		var name = $(this).find("img").attr("class")
		//Add hover event
		$(this).hover(function(){
			if (!$(this).hasClass("selected")) {
				$(this).find("img").attr("src", "images/" + section + "/" + name + "_over.jpg"); 
			};
		}, function() {
			if (!$(this).hasClass("selected")) {
				$(this).find("img").attr("src", "images/" + section + "/" + name + ".jpg");
			};
		});
		//Make Link obvious
		$(this).addClass("cursor");
		//Add click events
		$(this).click(function(){
			ItemClicked($(this))
		});
		
		$(this).find("a").each(function() {
			$(this).click(function(){
				ItemClicked($(this).parent().parent());
				return false;
			});
		});
	});
	if($.cookie('n2_selected' + section) && querySt("p") == ""){
	    var item = $("img." + $.cookie('n2_selected' + section)).parent().parent().parent();
	    ItemClicked(item, false);
	};
 });
 
 function querySt(ji) {
    var result
    hu = window.location.search.substring(1);
    gy = hu.split("&");
    for (i=0;i<gy.length;i++) {
        ft = gy[i].split("=");
            if (ft[0] == ji) {
                result = ft[1];
            };
    };
    if (result==undefined){
        return "";
    }else{
        return result;
    };
 };
 
function ItemClicked(p_item, animate){
     if ( animate === undefined ) {
          animate = true;
       }

 	if (p_item.hasClass("selected")) {
		return false;
	}else{
	    if (clicked == false) {
	        clicked = true;
		    //Get Name
		    var name = p_item.find("img").attr("class")
		    //Loop through and remove selected class from everything.. reset images
		    $("#list").find("li").each(function(i) {
			    var loop_name = $(this).find("img").attr("class")
			    $(this).removeClass("selected");
			    $(this).find("img").attr("src", "images/" + section + "/" + loop_name + ".jpg");
		    });
		    //Add selected class
		    p_item.addClass("selected");
		    //Set Image
		    p_item.find("img").attr("src", "images/" + section + "/" + name + "_on.jpg");
		    //Load Document
	        $("#slide2").load(section + "/" + name + ".html", "", function(text, status){
		        if (status != "success"){
			        $("#slide1").text("Error loading " + section + " data");
			        clicked = false;
		        }else{
		          pageTracker._trackPageview("/" + section + ".aspx?p=" + name);
              if(animate){
	              $("#slide_container").animate({ marginLeft:"-514px" }, "slow", "", function(){
		            $("#slide1").html($("#slide2").html());
		            $("#slide_container").css("margin-left", "0");
		            addExternalClickHandler("#slide1");})
		          }else{
		            $("#slide1").html($("#slide2").html());
		            addExternalClickHandler("#slide1");
		          };
				      $.cookie('n2_selected' + section, name);
				      clicked = false;
		     }});
	    };
	};
};