
$(function() {
	if(!$.boxModel)
		alert("Not box model");


    // hide the subtoolbar if it does not contain anything
    if($("#subtoolbar *").length > 0) {
        $("#subtoolbar").css("display", "block");
    }


	if($.browser.msie && $.browser.version < 7) {
		initToolbarForMSIE6();
	} else {
		initToolbar();
	}

    // decrease margins when we show a movie (in an iframe)
    if($("iframe").length > 0) {
        $("div#content").css("margin-left", "-7px").css("margin-right", "0");

    } 

    addTitleToListSidebar();
    addTableStyles();
    addPopupHooks();	
    addCategoryArticleList();
    addSearchLayout();
});

/** Set up the toolbar.  */
function initToolbar() {
    // show the active button in the toolbar (current page button)
    $("div#toolbar ul.menu li.active a").css({
                "border-left-color":  "#eee",
                "border-right-color": "#000"
    });
    $("div#toolbar ul.menu li.active").css({
                "border-left-color":  "#000",
                "border-right-color": "#eee"
    });

	// Show borders on toolbar button on hover. 
	// But only on the buttons that are not active 
	// (the page we are looking at).
	$("div#toolbar ul.menu a").hover(function() {
        if(!$(this.parentNode).hasClass("active")) {
    		$(this).css({ 
	    		"border-left-color":  "#adadac", 
		    	"border-right-color": "#000" 
    		});
	    	$(this.parentNode).css({ 
		    	"border-left-color":  "#000", 
			    "border-right-color": "#adadac" 
    		}); 
        }
	}, 
	function() {
        if(!$(this.parentNode).hasClass("active")) {
    		$(this).css({ 
	    		"border-left-color" : "transparent", 
		    	"border-right-color": "transparent" 
    		});
	    	$(this.parentNode).css({ 
		    	"border-left-color" : "transparent", 
			    "border-right-color": "transparent" 
    		});
        }
	});
}

/**  */
function initToolbarForMSIE6() {
	// remove the borders on the toolbar buttons (transparency does not work)
	$("div#toolbar ul.menu li a").css("border-width", "0").css("height", "22px");
	$("div#toolbar ul.menu li").css("border-width", "0");
	
    // show the active button in the toolbar (current page button)
    $("div#toolbar ul.menu li.active a").css({
                "border-left-color":  "#eee",
                "border-left-width": "1px",
                "border-right-color": "#eee",
                "border-right-width": "1px"
    });

	// show borders on toolbar button on hover
	$("div#toolbar ul.menu a").hover(function() {
        if(!$(this.parentNode).hasClass("active")) {
		    $(this).css({ 
			    "border-left": "1px solid #adadac", 
    			"border-right": "1px solid #adadac" 
	    	});
        }
	}, 
	function() {
        if(!$(this.parentNode).hasClass("active")) {
		    $(this).css({ 
    			"border-width": "0" 
	    	});
        }
	});	
}

/**
 * The mod_placehere that points to a category is handled as a list
 * The class name specified should be "_list My_Title" where My_Title
 * is the title that will be added.
 */
function addTitleToListSidebar() {
    $(".mod_placehere_list").each(function() {
        var cls = $(this).attr("class").split(" ")[1].replace("_", " ");
        var title = cls;
        $(this).prepend("<h1>" + title + "</h1><div style='width: 170px; height: 20px;'><hr/></div>");
    });
}



/** Pimp the tables. Note: for this to work the table must have the class 'styled'. */
function addTableStyles() {
    $("table[class='styled'] tr:odd").addClass("odd");
    $("table[class='styled'] tr:even").addClass("even");
}


function addPopupHookOnLink(link, id) {
    // rewrite the link so that it shows a popup instead
    link.href="#";
    link.onclick = null;
    link.target = null;

    link.href = "http://kebnekaise.2conciliate.com/joomla/index.php?option=com_content&id=" + id + "&format=raw";
    $(link).facebox();
}

// All links to an article in the category "Popup" (id 28) are shown in a popup.
function addPopupHooks() {
    // look at each link 
    $("a").each(function() {
        // look at each link and extract the parameters from it
        var href = this.href;

        // category with id 28 is called popup
        // all links to this should be shown in a popup
        if(href.indexOf("catid=28") != -1) {
            var params = href.split("&");
            for(var i = 0; i < params.length; i++) {
                var p = params[i];
                var colon = "%3A";
                // the id has a text (title) after it
                if(p.indexOf("id=") == 0 && p.indexOf(colon) != -1) {
                    var title = p.substr(p.indexOf(colon) + colon.length);
                    var id = p.substr(3, p.indexOf(colon)-3);
                    addPopupHookOnLink(this, id);
                }
            }
        }
    }); 
}


/** 
 * On a page for a category where all the articles belonging to
 * that category is shown, style that list.
 */
function addCategoryArticleList() {
    $("div.blog_more li:even").each(function() {
        $(this).addClass("even");
    });

    $("div.blog_more li:odd").each(function() {
        $(this).addClass("odd");
    });

    $("div.blog_more div strong").css("display", "none");
}



/**
 * Add styles to the search page.
 * Could not modify the template, so we look at each page
 * and if it contains a search form we add css classes so
 * we can style it in css.
 */
function addSearchLayout() {
    var isSearchPage = $("form#searchForm").size() == 1;
    if(isSearchPage == false)
        return;

    $("#content").css("width", "100%");
    $("table.contentpaneopen td").css("padding-bottom", "10px");
}


