/********************************************************************************
 * Content
 ********************************************************************************/

var content = null;
var mainRoot = "http://familjencarlsson.com/main/";


function Content() {
    // categories
    this.categories = new Array();

    // add content
    this.addContent = function(content, categoryId) {
	var found = false;
	for (i in this.categories) {
	    var category = this.categories[i];
	    if (category.id == categoryId) {
		category.add(content);
		found = true;
	    }
	}

	if (!found) alert("category not found" + categoryId);
    };

    // add category
    this.addCategory = function(category) { this.categories.push(category); };

    // write html as cells
    this.writeHtml = function(limit) {
	HTML("<table id='menutable' valign='up'>");
	for (i in this.categories) {
	    this.categories[i].writeHtml(limit, "menu"); 
	}
	HTML("</table>");
    };

    // write specific category
    this.writeCatHtml = function(catId, limit) {
	HTML("<table valign='up'>");
	for (i in this.categories) {
	    var cat = this.categories[i];
	    if (cat.id == catId) cat.writeHtml(limit, "fmenu");
	} 
	HTML("</table>");
    }
}


function Category(id, label, image) {
    this.contents = new Array();
    this.id = id;
    this.label = label
    this.image = image;

    // add content
    this.add = function(content) { this.contents.push(content); };

    // write html
    this.writeHtml = function(limit, classGr) {
	//HTML("<tr> <td class='mheader'>" + this.label + "</td></tr>");
	HTML_HMENU(classGr + "header", this.id, this.label);
	for (i in this.contents) {
	    if (i < limit || limit == -1) this.contents[i].writeHtml(classGr);
	    else if (i == limit) HTML_MENU(classGr, "more" + this.id, "Visa alla >>");
	}
	var moreCell = document.getElementById("more" + id);
	if (moreCell) {
	    moreCell.onclick = this.onClick;
	    moreCell.onmouseover = this.onOver;
	    moreCell.onmouseout = this.onOff;
	}
    };


    this.onClick = function() {
	contentArea.src = "../main/asp/content.asp?cat=" + id;
    };


    this.onOver = function() {
	imball = document.getElementById("imgbmore" + id);
	if (imball) imball.src = mainRoot + "graphics/amball.gif";
	this.className = "hmenu";
    };

    this.onOff = function() {
	imball = document.getElementById("imgbmore" + id);
	if (imball) imball.src = mainRoot + "graphics/mball.gif";
	this.className = "menu";
       };
}


function ContentItem(id, label, image, src, scroll) {
    this.id = id;
    this.label = label;
    this.image = image;
    this.src = src;
    this.scroll = scroll;

    this.writeHtml = function(classGr) { 
	//HTML("<tr><td class='menu' id='" + this.id + "'>" + this.label + "</td></tr>");
	HTML_MENU(classGr, this.id, this.label);
	var cell = document.getElementById(this.id);
	if (cell) {
	    cell.onclick = this.onClick;
	    if (classGr != "fmenu") { // ugly special hack...
		cell.onmouseover = this.onOver;
		cell.onmouseout = this.onOff;
	    }
	}
    };

    this.onClick = function() { 
	if (contentArea) {
	    contentArea.src = src; 
	} else {
	    window.location = src;
	}
    };

    this.onOver = function() {
	imball = document.getElementById("imgb" + id);
	if (imball) imball.src = mainRoot + "graphics/amball.gif";
	this.className = "hmenu";
    };

    this.onOff = function() {
	imball = document.getElementById("imgb" + id);
	if (imball) imball.src = mainRoot + "graphics/mball.gif";
	this.className = "menu";
    };
}


function addStdCategory(id, label, image) {
    if (!content) content = new Content();
    content.addCategory(new Category(id, label, image));
}


function addContent(id, label, image, src, scroll, category) {
    if (!content) content = new Content();
    var item = new ContentItem(id, label, image, src, scroll);
    content.addContent(item, category);
}


function addExtContent(id, label, image, src, scroll, category) {
    if (!content) content = new Content();
    var item = new ContentItem(id, label, image, src, scroll);
    //item.onClick =  function() { window.open(src); };
    item.onClick =  function() { window.location = src; };
    
    content.addContent(item, category);
}


function writeContentToDocument() {
    content.writeHtml(4);
}


function writeCatContentToDocument(cat) {
    if (!contentArea) contentArea = window.document.getElementById("area");
    content.writeCatHtml(cat, -1);
}

/********************************************************************************
 * Help
 ********************************************************************************/

function HTML(text) {
    document.write(text);
}


function HTML_MENU(cl, id, label) {
    HTML("<tr><td class='" + cl + "'><img id='" + "imgb" + id + "' border='0' src='http://familjencarlsson.com/main/graphics/mball.gif'> </td>");
    HTML("<td class='" + cl + "' id='" + id + "'>" + label + "</td></tr>");
}


function HTML_HMENU(cl, id, label) {
    //HTML("<tr><td></td><td class='mheader' id='" + id + "'>" + label + "</td></tr>");
    HTML("<tr><td colspan='2' class='" + cl + "' id='" + id + "'>" + label + "</td></tr>");
}


/********************************************************************************
 * Add Actual Content
 ********************************************************************************/


function setupContent() {
    addStdCategory("start", "Start", null);
    addContent("home", "Start", null, "http://www.familjencarlsson.com/main/start.html", true, "start");

    addStdCategory("sites", "Hemsidor", null);
    addExtContent("jojoblogg", "Johannas blogg", null, "http://gosjojos.blogg.se/", true, "sites");
    addExtContent("melwins", "Melwins sida", null, "http://www.familjencarlsson.com/melwin/", true, "sites");
    addExtContent("ellas", "Ellas sida", null, "http://www.familjencarlsson.com/ella/", true, "sites");
    addExtContent("db", "Vårat bröllop", null, "http://www.familjencarlsson.com/brollop/", true, "sites");
    addExtContent("jj", "Jessika&Joels", null, "http://www.familjencarlsson.com/jessika-joel/", true, "sites");

    genericAddPictureContent();

    addStdCategory("links", "Länkar", null);
    addExtContent("dn", "Dagens nyheter", null, "http://www.dn.se", true, "links");
    addExtContent("hemnet", "Hemnet", null, "http://www.hemnet.se", true, "links");
    addExtContent("familjeliv", "Familjeliv", null, "http://www.familjeliv.se", true, "links");
    addExtContent("gunnarso", "Gunnarsö stugby", null, "http://www.gunnarso.com", true, "links");
    addExtContent("östgötatrafiken", "Östgötatrafiken", null, "http://www.ostgotatrafiken.se", true, "links");
    addExtContent("eniro", "Eniro", null, "http://www.eniro.se", true, "links");
    addExtContent("scb", "SCB (Statistiska Central Byrån)", null, "http://www.scb.se", true, "links");
    addExtContent("sidan", "Sidan (get.to/lkpg)", null, "http://get.to/lkpg", true, "links");


    addStdCategory("contact", "Kontakt", null);
    addExtContent("wbm", "Webmaster", null, "mailto:webmaster@familjencarlsson.com", true, "contact");
}


/********************************************************************************
 * Frame
 ********************************************************************************/

var contentArea;

function contentAreaHtml() {
    HTML("<iframe id='area' scrolling='auto' marginheight='0' marginwidth='0' frameborder='0'></iframe>");
    contentArea = document.getElementById("area");
    contentArea.src = "http://www.familjencarlsson.com/main/start.html";
    contentArea.width = "700";
    contentArea.height = "550";
}




