//onload
$(function(){
    HR.navigation();
    HR.overview();
    HR.content();
    HR.faq();
    HR.loginForm();
    
    $("ul.list li:even").addClass("bg");
    $("#print").bind("click", function() { window.print() });
    
    //lightbox selector
    if ($("a[@rel*=lightbox]").length > 0) {
        $("a[@rel*=lightbox]").lightBox();
    }
    
    //startpage teaser museover
    /*$("#start-teaser .teaser").hover(function() {
        $(this).children().addClass("hoover");
    }, function() {
        $(this).children().removeClass("hoover");
    });*/
    
});

var HR = {
    /**
    * navigation
    * adds navigation image mouseovers
    * called by window.onload init()
    **/
    navigation: function() {
        $("#nav img").hover(function() {
            $(this).attr({origSrc: $(this).attr("src"), src: $(this).attr("src").replace(/_off/, "_on")});
        }, function() {
            $(this).attr("src", $(this).attr("origSrc"));
        });
    },
    
    /**
    * overview
    * sets the height of the relative positioned container(#overview)
    **/
    overview: function() {
        $("#overview").each(function() {
            var text = $("#overview-text").height() + 100;
            var montage = $("#overview-montage").height();
            $("#overview").height(text > montage ? text : montage);
        });
    },

    /**
    * content
    * sets the height of the relative positioned container(#content)
    **/
    content: function() {
        // check the right column first,
        // otherwise #content-content has abnormal height
        $("#cc-right").each(function() {
            $("#cc-main").width(484);
        });
        $("#content").each(function() {
            var text = $("#content-content").height() + parseInt($("#content-content").css("top"));
            var montage = $("#content-montage").height();
            $("#content").height((text > montage ? text : montage) + 0);
        });
        // if #ch-leftblock is present
        // set padding and extend #content height
        $("#ch-leftblock").each(function() {
            var newHeight = $("#content").height() + $("#ch-leftblock").height() - 100;
            $("#content").height(newHeight);
            $("#content-content").css("padding-top", 100);
        });
    },
    
    /**
    * faq
    * add click handlers on questions(div) to show answers(p)
    **/
    faq: function() {
    	  if (showFaqEntries() == true) {
    	  	//we have to check the height too
          HR.content();
        }
        $("ol#faq li div").bind("click", function() {
            $("ol#faq li p").each(function() {
                $(this).hide();
            });

            $(this).next().show();
            //we have to check the height too
            HR.content();
        });
        
    },
    
    /**
    * loginForm
    * add click handler on login form
    **/
    loginForm: function() {
        $("#login-handler").each(function() {
            $(this).toggle(function() {
                $(this).toggleClass("active");
                $("#login").show();
            }, function() {
                $(this).toggleClass("active");
                $("#login").hide();
            });
        });
    },
    
    /**
    * tabs
    **/
    tabs: function() {
        //check if location hash is set
        if (document.location.hash) {
            //load iframe
            $("#tab-iframe").attr("src", $(document.location.hash).attr("href"));
            //change tab-header background image
            $("#tab-header").css("background-image", "url(" + $(document.location.hash).attr("tabSrc") + ")");
            //show current .tabcontent
            $("#" + $(document.location.hash).attr("id").replace(/tab_/, "tabcontent_")).show();
        } else {
            //load default iframe(first tab)
            $("#tab-iframe").attr("src", $("#tab_01").attr("href"));
             //aichha also set header!!
             $("#tab-header").css("background-image", "url(" + $("#tab_01").attr("tabSrc") + ")");
             //aichha and tabcontent!
             $("#" + $("#tab_01").attr("id").replace(/tab_/, "tabcontent_")).show();
        }
        //add click handlers
        $("#tab-header a").each(function() {
            // hide 'a' linktext
            $(this).html("");
            // add onclick handler
            $(this).click(function() {
                //hide all .tabcontent
                $(".tabcontent").hide();
                //show current .tabcontent
                $("#" + $(this).attr("id").replace(/tab_/, "tabcontent_")).show();
                //change tab-header background image
                $("#tab-header").css("background-image", "url(" + $(this).attr("tabSrc") + ")");
                //load url in iframe
                $("#tab-iframe").attr("src", $(this).attr("href"));
                //set location hash to current tab
                document.location.hash = $(this).attr("id");
                //prevent link to be followed
                return false;
            });
        });
    }

}

/**
 * log function
 **/
log = function(msg) {
    this.msg = !msg ? "undefined" : msg;
    if (typeof msg == "object") {
        var tmp = "", i, v;
        for (var i in this.msg) {
            if (this.msg.hasOwnProperty(i)) {
                v = this.msg[i];
                switch (typeof v) {
                    case "undefined":
                        break;
                    default:
                        tmp += i+": <strong>"+v+"</strong><br />";
                }
            } else {
                tmp += "<strong>"+i+"</strong><-<br />";
            }
        }
        this.msg = tmp;
    } else {
        this.msg = "<strong>" + this.msg + "</strong><hr />";
    }
    if (!document.getElementById("netural-log")) {
        var logDiv = document.createElement("div");
        logDiv.id = "netural-log";
        logDiv.style.background = "#fff",
        logDiv.style.color = "#000",
        logDiv.style.padding = "15px",
        logDiv.style.fontSize = '1.2em',
        logDiv.style.maxHeight = "500px",
        logDiv.style.overflow =  "auto"
        document.body.appendChild(logDiv);
    }
    var d = new Date();
    document.getElementById("netural-log").innerHTML = d + " " +this.msg + document.getElementById("netural-log").innerHTML;
} 


/**
 * reads GET parameter 'q' and opens all FAQ entries which contain the query (OR)
 **/

var openedFaqsAlready = false;

function showFaqEntries() {
  var a = new Array();
  var reqUrl = document.URL;
	if (reqUrl.indexOf("?q=") != -1) {
		var q = unescape(reqUrl.substring(reqUrl.indexOf("?q=")+3));
	  a = q.split(" ");
	}

  if (q != "" && openedFaqsAlready == false) {
    
	  $('ol#faq li').map(function(){
	      // find LI element...
	      var b = $(this).find('li');
	      // return LI element
	      return b.length ? b[0] : this;
	  })
    // for each LI found...
    .each(function(){
  	
      // get the text...
      var me = $(this);
      txt = me.text();
    
    	for (var i=0; i < a.length; i++) {
        if (txt.toLowerCase().indexOf(a[i].toLowerCase()) != -1) {
          me.children("p").show();
        }
      }
    
    }); 
    openedFaqsAlready = true;
    return true;
  }
  return false;
}