/*********************************************************************************************************************/
/*********************************************************************************************************************/
/*********************************************************************************************************************/
/*********************************************************************************************************************/
/*** ALL WEB SITE CORE JAVASCRIPTS, INCLUDED ON ALL PAGE *************************************************************/
/*********************************************************************************************************************/
/*********************************************************************************************************************/
/*********************************************************************************************************************/
/*********************************************************************************************************************/
/**
* NAME: kthweb.js
*
*/
/**
* Javascript functions for the KTH website.
*/
/**
* Get the Google Analytics tracking number based on the location.
*
* @return returns the tracking number based on the requested site.
*/
function getGoogleAnalyticsTrackingNumber() {
var site = "" + document.location;
trackingNumber = null;
if (site.indexOf("www.kth.se") > -1 ) {
trackingNumber = 'UA-6683156-1';
} else if (site.indexOf("campi.kth.se") > -1) {
trackingNumber = 'UA-7171856-1';
} else if (site.indexOf("intra.kth.se") > -1) {
trackingNumber = 'UA-7171805-1';
}
return trackingNumber;
}
/**
* Toggles the given element.
* @param id the id of the element to toggle.
*/
function toogle(id) {
var element = getElementById(id);
if (isDisplaying(element)) {
element.style.display = 'none';
} else {
element.style.display = 'block';
}
}
/**
* Get element by id wrapping browser incompatibility.
* Overloads the document.getElementById
*/
function getElementById(id) {
if (!document.getElementById){
if (document.all) {
document.getElementById = function() {
if (typeof document.all[id] != "undefined") {
return document.all[id];
} else {
return null;
}
};
} else if (document.layers) {
document.getElementById = function(){
if (typeof document[id] != "undefined") {
return document[id];
} else {
return null;
}
};
}
}
return document.getElementById(id);
}
/**
* Check if the given element i displayed.
*
* @param element the given element.
* @returns {Boolean}
*/
function isDisplaying(element){
if (element == null) {
return false;
}
if (element.style.display == null) {
return true;
}
if (element.style.display == "") {
return true;
}
if (element.style.display.toLowerCase() == 'block') {
return true;
}
return false;
}
/**
* Switches to internal javascript-search
*/
function toInternalJsSearch(action) {
var form = jQuery("#toogleSearchMain > form");
form.prepend("<input type='hidden' name='cof' value='FORID:11'>");
form.attr("action", action);
}
function toggleExpandableBlock(id) {
var dropdownBody = $("#" + id + " .expandableBlockBody");
if (dropdownBody.css("display") != "block") {
dropdownBody.slideDown();
$("#" + id + " .expandableBlockTrigger").addClass("expandedBlock");
} else {
dropdownBody.slideUp();
$("#" + id + " .expandableBlockTrigger").removeClass("expandedBlock");
}
}
function placeholderFix() {
if (!("placeholder" in document.createElement("input"))) {
$('[placeholder]').focus(function() {
var input = $(this);
if (input.val() == input.attr('placeholder')) {
input.val('');
input.removeClass('placeholder');
}
}).blur(function() {
var input = $(this);
if (input.val() == '' || input.val() == input.attr('placeholder')) {
input.addClass('placeholder');
input.val(input.attr('placeholder'));
}
}).blur().parents('form').submit(function() {
$(this).find('[placeholder]').each(function() {
var input = $(this);
if (input.val() == input.attr('placeholder')) {
input.val('');
}
});
});
}
}
/**
*
* Submits the education choice on the studentwebsite.
* Later to be used for selecting content in the navigation.
*
* NOTE! This method uses an form by the name "rs" (Rember Selection)
* that has to be present in the page to work.
*/
function submitEducationChoice( form ){
form.rs.value = ( document.rsForm.rs.checked ) ? 'true' : 'false';
return true;
}
function enableEducationType( select ) {
jQuery(".educationTypeSelect").css("display", "none");
var value = jQuery(select).val();
jQuery("#educationType_" + value + ".educationTypeSelect").css("display", "block");
}
/*********************************************************************************************************************/
/*********************************************************************************************************************/
/*********************************************************************************************************************/
/**
* NAME: tree.cookie.js
* USED IN: linkTree
*/
/**
* Cookie plugin
*
* Copyright (c) 2006 Klaus Hartl (stilbuero.de)
* Dual licensed under the MIT and GPL licenses:
* http://www.opensource.org/licenses/mit-license.php
* http://www.gnu.org/licenses/gpl.html
*
*/
/**
* Create a cookie with the given name and value and other optional parameters.
*
* @example $.cookie('the_cookie', 'the_value');
* @desc Set the value of a cookie.
* @example $.cookie('the_cookie', 'the_value', {expires: 7, path: '/', domain: 'jquery.com', secure: true});
* @desc Create a cookie with all available options.
* @example $.cookie('the_cookie', 'the_value');
* @desc Create a session cookie.
* @example $.cookie('the_cookie', null);
* @desc Delete a cookie by passing null as value.
*
* @param String name The name of the cookie.
* @param String value The value of the cookie.
* @param Object options An object literal containing key/value pairs to provide optional cookie attributes.
* @option Number|Date expires Either an integer specifying the expiration date from now on in days or a Date object.
*                             If a negative value is specified (e.g. a date in the past), the cookie will be deleted.
*                             If set to null or omitted, the cookie will be a session cookie and will not be retained
*                             when the the browser exits.
* @option String path The value of the path atribute of the cookie (default: path of page that created the cookie).
* @option String domain The value of the domain attribute of the cookie (default: domain of page that created the cookie).
* @option Boolean secure If true, the secure attribute of the cookie will be set and the cookie transmission will
*                        require a secure protocol (like HTTPS).
* @type undefined
*
* @name $.cookie
* @cat Plugins/Cookie
* @author Klaus Hartl/klaus.hartl@stilbuero.de
*/
/**
* Get the value of a cookie with the given name.
*
* @example $.cookie('the_cookie');
* @desc Get the value of a cookie.
*
* @param String name The name of the cookie.
* @return The value of the cookie.
* @type String
*
* @name $.cookie
* @cat Plugins/Cookie
* @author Klaus Hartl/klaus.hartl@stilbuero.de
*/
jQuery.cookie = function(name, value, options) {
if (typeof value != 'undefined') { // name and value given, set cookie
options = options || {};
if (value === null) {
value = '';
options.expires = -1;
}
var expires = '';
if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
var date;
if (typeof options.expires == 'number') {
date = new Date();
date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
} else {
date = options.expires;
}
expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
}
var path = options.path ? '; path=' + options.path : '';
var domain = options.domain ? '; domain=' + options.domain : '';
var secure = options.secure ? '; secure' : '';
document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
} else { // only name given, get cookie
var cookieValue = null;
if (document.cookie && document.cookie != '') {
var cookies = document.cookie.split(';');
for (var i = 0; i < cookies.length; i++) {
var cookie = jQuery.trim(cookies[i]);
// Does this cookie string begin with the name we want?
if (cookie.substring(0, name.length + 1) == (name + '=')) {
cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
break;
}
}
}
return cookieValue;
}
};
$(document).ready(function(){
$(".gray").treeview({
control: ".treecontrol",
persist: "cookie",
cookieId: "treeview-gray"
});
$(".treeview-gray li").css({backgroundImage:"url(/img/treeview-gray-line.gif)"})
$(".treeview-gray .hitarea, .treeview-gray li.lastCollapsable, .treeview-gray li.lastExpandable").css({backgroundImage:"url(/img/treeview-gray.gif)"})
});
/*********************************************************************************************************************/
/*********************************************************************************************************************/
/*********************************************************************************************************************/
/**
* NAME: tree.treeview.js
* USED IN: linkTree
*/
/*
* Treeview 1.4 - jQuery plugin to hide and show branches of a tree
*
* http://bassistance.de/jquery-plugins/jquery-plugin-treeview/
* http://docs.jquery.com/Plugins/Treeview
*
* Copyright (c) 2007 Jörn Zaefferer
*
* Dual licensed under the MIT and GPL licenses:
*   http://www.opensource.org/licenses/mit-license.php
*   http://www.gnu.org/licenses/gpl.html
*
* Revision: $Id: tree.treeview.js,v 1.1 2008-05-22 08:31:27 hoyce Exp $
*
*/
(function($) {
$.extend($.fn, {
swapClass: function(c1, c2) {
var c1Elements = this.filter('.' + c1);
this.filter('.' + c2).removeClass(c2).addClass(c1);
c1Elements.removeClass(c1).addClass(c2);
return this;
},
replaceClass: function(c1, c2) {
return this.filter('.' + c1).removeClass(c1).addClass(c2).end();
},
hoverClass: function(className) {
className = className || "hover";
return this.hover(function() {
$(this).addClass(className);
}, function() {
$(this).removeClass(className);
});
},
heightToggle: function(animated, callback) {
animated ?
this.animate({ height: "toggle" }, animated, callback) :
this.each(function(){
jQuery(this)[ jQuery(this).is(":hidden") ? "show" : "hide" ]();
if(callback)
callback.apply(this, arguments);
});
},
heightHide: function(animated, callback) {
if (animated) {
this.animate({ height: "hide" }, animated, callback);
} else {
this.hide();
if (callback)
this.each(callback);
}
},
prepareBranches: function(settings) {
if (!settings.prerendered) {
// mark last tree items
this.filter(":last-child:not(ul)").addClass(CLASSES.last);
// collapse whole tree, or only those marked as closed, anyway except those marked as open
this.filter((settings.collapsed ? "" : "." + CLASSES.closed) + ":not(." + CLASSES.open + ")").find(">ul").hide();
}
// return all items with sublists
return this.filter(":has(>ul)");
},
applyClasses: function(settings, toggler) {
this.filter(":has(>ul):not(:has(>a))").find(">span").click(function(event) {
toggler.apply($(this).next());
}).add( $("a", this) ).hoverClass();
if (!settings.prerendered) {
// handle closed ones first
this.filter(":has(>ul:hidden)")
.addClass(CLASSES.expandable)
.replaceClass(CLASSES.last, CLASSES.lastExpandable);
// handle open ones
this.not(":has(>ul:hidden)")
.addClass(CLASSES.collapsable)
.replaceClass(CLASSES.last, CLASSES.lastCollapsable);
// create hitarea
this.prepend("<div class=\"" + CLASSES.hitarea + "\"/>").find("div." + CLASSES.hitarea).each(function() {
var classes = "";
$.each($(this).parent().attr("class").split(" "), function() {
classes += this + "-hitarea ";
});
$(this).addClass( classes );
});
}
// apply event to hitarea
this.find("div." + CLASSES.hitarea).click( toggler );
},
treeview: function(settings) {
settings = $.extend({
cookieId: "treeview"
}, settings);
if (settings.add) {
return this.trigger("add", [settings.add]);
}
if ( settings.toggle ) {
var callback = settings.toggle;
settings.toggle = function() {
return callback.apply($(this).parent()[0], arguments);
};
}
// factory for treecontroller
function treeController(tree, control) {
// factory for click handlers
function handler(filter) {
return function() {
// reuse toggle event handler, applying the elements to toggle
// start searching for all hitareas
toggler.apply( $("div." + CLASSES.hitarea, tree).filter(function() {
// for plain toggle, no filter is provided, otherwise we need to check the parent element
return filter ? $(this).parent("." + filter).length : true;
}) );
return false;
};
}
// click on first element to collapse tree
$("a:eq(0)", control).click( handler(CLASSES.collapsable) );
// click on second to expand tree
$("a:eq(1)", control).click( handler(CLASSES.expandable) );
// click on third to toggle tree
$("a:eq(2)", control).click( handler() );
}
// handle toggle event
function toggler() {
$(this)
.parent()
// swap classes for hitarea
.find(">.hitarea")
.swapClass( CLASSES.collapsableHitarea, CLASSES.expandableHitarea )
.swapClass( CLASSES.lastCollapsableHitarea, CLASSES.lastExpandableHitarea )
.end()
// swap classes for parent li
.swapClass( CLASSES.collapsable, CLASSES.expandable )
.swapClass( CLASSES.lastCollapsable, CLASSES.lastExpandable )
// find child lists
.find( ">ul" )
// toggle them
.heightToggle( settings.animated, settings.toggle );
if ( settings.unique ) {
$(this).parent()
.siblings()
// swap classes for hitarea
.find(">.hitarea")
.replaceClass( CLASSES.collapsableHitarea, CLASSES.expandableHitarea )
.replaceClass( CLASSES.lastCollapsableHitarea, CLASSES.lastExpandableHitarea )
.end()
.replaceClass( CLASSES.collapsable, CLASSES.expandable )
.replaceClass( CLASSES.lastCollapsable, CLASSES.lastExpandable )
.find( ">ul" )
.heightHide( settings.animated, settings.toggle );
}
}
function serialize() {
function binary(arg) {
return arg ? 1 : 0;
}
var data = [];
branches.each(function(i, e) {
data[i] = $(e).is(":has(>ul:visible)") ? 1 : 0;
});
$.cookie(settings.cookieId, data.join("") );
}
function deserialize() {
var stored = $.cookie(settings.cookieId);
if ( stored ) {
var data = stored.split("");
branches.each(function(i, e) {
$(e).find(">ul")[ parseInt(data[i]) ? "show" : "hide" ]();
});
}
}
// add treeview class to activate styles
this.addClass("treeview");
// prepare branches and find all tree items with child lists
var branches = this.find("li").prepareBranches(settings);
switch(settings.persist) {
case "cookie":
var toggleCallback = settings.toggle;
settings.toggle = function() {
serialize();
if (toggleCallback) {
toggleCallback.apply(this, arguments);
}
};
deserialize();
break;
case "location":
var current = this.find("a").filter(function() { return this.href.toLowerCase() == location.href.toLowerCase(); });
if ( current.length ) {
current.addClass("selected").parents("ul, li").add( current.next() ).show();
}
break;
}
branches.applyClasses(settings, toggler);
// if control option is set, create the treecontroller and show it
if ( settings.control ) {
treeController(this, settings.control);
$(settings.control).show();
}
return this.bind("add", function(event, branches) {
$(branches).prev()
.removeClass(CLASSES.last)
.removeClass(CLASSES.lastCollapsable)
.removeClass(CLASSES.lastExpandable)
.find(">.hitarea")
.removeClass(CLASSES.lastCollapsableHitarea)
.removeClass(CLASSES.lastExpandableHitarea);
$(branches).find("li").andSelf().prepareBranches(settings).applyClasses(settings, toggler);
});
}
});
// classes used by the plugin
// need to be styled via external stylesheet, see first example
var CLASSES = $.fn.treeview.classes = {
open: "open",
closed: "closed",
expandable: "expandable",
expandableHitarea: "expandable-hitarea",
lastExpandableHitarea: "lastExpandable-hitarea",
collapsable: "collapsable",
collapsableHitarea: "collapsable-hitarea",
lastCollapsableHitarea: "lastCollapsable-hitarea",
lastCollapsable: "lastCollapsable",
lastExpandable: "lastExpandable",
last: "last",
hitarea: "hitarea"
};
// provide backwards compability
$.fn.Treeview = $.fn.treeview;
})(jQuery);
/*********************************************************************************************************************/
/*********************************************************************************************************************/
/*********************************************************************************************************************/
/**
* NAME: tree.js
* USED IN: linkTree
*/
/**
* Loads the tree view on document ready.
*/
$(document).ready(function(){
$(".gray").treeview({
control: ".treecontrol",
persist: "cookie",
cookieId: "treeview-gray"
});
$(".treeview-gray li").css({backgroundImage:"url(/img/treeview-gray-line.gif)"})
$(".treeview-gray .hitarea, .treeview-gray li.lastCollapsable, .treeview-gray li.lastExpandable").css({backgroundImage:"url(/img/treeview-gray.gif)"})
});
/*********************************************************************************************************************/
/*********************************************************************************************************************/
/*********************************************************************************************************************/
/**
* NAME: tagCompletion/common.js
* USED IN: categorySearchResult
*/
(function($) {
function redirectToTagSearch(url, tagsStringTFID){
var idStr = "#"+tagsStringTFID;
var tf = jQuery(idStr)[0];
var tagsString = tf.value;
var tagNamesArray=[];
var tokeNizer = new $.tokenizer([','],
function(text,isSeparator,er){
if (!isSeparator){
// Category text trimmed!
var tagText = jQuery.trim(text);
if (jQuery.inArray(tagText,tagNamesArray)==-1){
tagNamesArray.push(tagText);
}
}
}); // End of .tokenizer stuff
tokeNizer.parse(tagsString);
var theUrl = url;
if (tagNamesArray.length > 0){
var bFirst = true;
if (theUrl.indexOf("?")==-1){
theUrl+="?";
bFirst = true;
}else{
bFirst = false;
}
for (var n=0;n<tagNamesArray.length;n++){
if (!bFirst){
theUrl+="&";
}
bFirst = false;
theUrl+="tag=";
theUrl+=tagNamesArray[n];
}
document.location.href = theUrl;
}else{
document.location.href = url;
}
}
})();

