﻿$(function() {
    var x = Math.floor(Math.random() * 10 + 1)
    setHeader(x);
    getNews();
    $("#getNews").click(function() { getNews(); });
    $("#getComics").click(function() { getComics(); });
});
function setHeader(n) {
    
    
    switch (n) {
        case 1:
            $("#header").text("Walking on water and developing software from a specification are easy if both are frozen.");
            break;
        case 2:
            $("#header").text("Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live");
            break;
        case 3:
            $("#header").text("Java is to JavaScript what Car is to Carpet");
            break;
        case 4:
            $("#header").text("In order to understand recursion, one must first understand recursion.");
            break;
        case 5:
            $("#header").text("Linux is only free if your time has no value.");
            break;
        case 6:
            $("#header").text("Debuggers don't remove bugs. They only show them in slow motion.");
            break;
        case 7:
            $("#header").text("Measuring programming progress by lines of code is like measuring aircraft building progress by weight.");
            break;
        case 8:
            $("#header").text("Perfection is achieved, not when there is nothing more to add, but when there is nothing left to take away.");
            break;
        case 9:
            $("#header").text("Never trust a programmer in a suit.");
            break;
        case 10:
            $("#header").text("To iterate is human, to recurse divine.");
            break;          
        default: 
            $("#header").text("Debugging is anticipated with distaste, performed with reluctance, and bragged about forever.");
            break;     
     }
}

function getNews() {
    $("#news").removeClass("hidden");
    $("#comics").addClass("hidden");
    if($("#dn").text().length === 0)
        getDN();
    if ($("#deli").text().length === 0)
        getDELI();
    if ($("#pitch").text().length === 0)
        getPITCH();    
}

function getComics() {
    $("#news").addClass("hidden");
    $("#comics").removeClass("hidden");
    if ($("#toothpaste").text().length === 0)
        getTooth();
    if ($("#dilbert").text().length === 0)
        getDilbert();
    if ($("#xkcd").text().length === 0)        
        getXKCD();
}

var Param;
function getTooth() {    
    Param = new Array(); //array with parameters
    Param.push("url");
    Param.push("http://toothpastefordinner.com/rss/rss.php");
    Param.push("takeItems");
    Param.push(1);
    PageMethod('ReadRss', Param, "#toothpaste");
}

function getDilbert() {
    Param = new Array();
    Param.push("url");
    Param.push("http://feedproxy.google.com/DilbertDailyStrip");
    Param.push("takeItems");
    Param.push(1);    
    PageMethod('ReadRss', Param, "#dilbert");
}
function getXKCD() {    
    Param = new Array();
    Param.push("url");
    Param.push("http://xkcd.com/rss.xml");
    Param.push("takeItems");
    Param.push(1);    
    PageMethod('ReadRss', Param, "#xkcd");
}

function getPITCH() {
    Param = new Array();
    Param.push("url");
    Param.push("http://feeds.idg.se/idg/vzzs");
    Param.push("takeItems");
    Param.push(7);
    $("#pitch").append("<h2>IDG.SE</h2>");
    PageMethod('ReadRss', Param, "#pitch");
}
function getDELI() {
    Param = new Array();
    Param.push("url");
    Param.push("http://feeds.delicious.com/v2/rss/popular/webdev?count=15");
    Param.push("takeItems");
    Param.push(15);
    $("#deli").append("<h2>DELICIOUS</h2>");
    PageMethod('ReadRss', Param, "#deli");
}
function getDN() {
    Param = new Array();
    Param.push("url");
    Param.push("http://www.dn.se/m/rss/senaste-nytt");
    Param.push("takeItems");
    Param.push(10);
    $("#dn").append("<h2>DAGENS NYHETER</h2>");
    PageMethod('ReadRss', Param, "#dn");
}

function PageMethod(fn, paramArray, div) {
    var pagePath = "http://start.mindexplosion.se/default.aspx";    
    var paramList = '';
    if (paramArray.length > 0) {
        for (var i = 0; i < paramArray.length; i += 2) {
            if (paramList.length > 0) paramList += ',';
            paramList += '"' + paramArray[i] + '":"' + paramArray[i + 1] + '"';
        }
    }
    paramList = '{' + paramList + '}';
    $.ajax({
        type: "POST",
        url: pagePath + "/" + fn,
        contentType: "application/json; charset=utf-8",
        data: paramList,
        dataType: "json",
        success: function(response) {
            var rssItems = (typeof response.d) == 'string' ? eval('(' + response.d + ')') : response.d;
            for (var i = 0; i < rssItems.length; i++) {
                $(div).append("<a href='" + rssItems[i].link + "' target='_blank' alt='" + rssItems[i].title + "'><h3>" + rssItems[i].title + "</h3></a>");
                if (rssItems[i].content != null) {
                    $(div).append("<span class='published'>[" + rssItems[i].published + "]</span>");
                    $(div).append("<p>" + rssItems[i].content + "</p>");
                }
               
            }
        },
        error: function(msg) { $(div).append(msg); }
    });
}   


