﻿var homecontent = {
    rcode: null,
    readCookie: function(name) {
        var nameEQ = name + "=";
        var ca = document.cookie.split(';');
        for (var i = 0; i < ca.length; i++) {
            var c = ca[i];
            while (c.charAt(0) == ' ') c = c.substring(1, c.length);
            if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
        }
        return null;
    },
    renderProfile: function(strJSON) {
        var objJson = eval("(" + strJSON + ")");
        var content = objJson.rows[0];
        document.getElementById('profile1').innerHTML = content.hypertext;
    },
    renderNewsEvents: function(strJSON) {
        var objJson = eval("(" + strJSON + ")");
        var content = objJson.rows[0];
        document.getElementById('news1').innerHTML = content.hypertext;
    },
    renderNYCSchools: function(strJSON) {
        var objJson = eval("(" + strJSON + ")");
        var content = objJson.rows[0];
        //document.getElementById('news2').innerHTML = content.hypertext;
    },
    fetchProfileContent: function(pageId) {
        var myjsonservice = new JSONService();
        if (this.rcode == "CM") {
            myjsonservice.loadScript("content_fetchprofile_0001", "services/content.aspx?action=fetchcontent&id=" + pageId + "&callback=homecontent.renderProfile&expires=0");
        }
        else {
            myjsonservice.loadScript("content_fetchprofile_0001", "services/content.aspx?action=fetchcontent&id=" + pageId + "&callback=homecontent.renderProfile&expires=600");
        }
    },
    fetchNewsEvents: function(pageId) {
        var myjsonservice = new JSONService();
        if (this.rcode == "CM") {
            myjsonservice.loadScript("content_fetchnewsevents_0001", "services/content.aspx?action=fetchcontent&id=" + pageId + "&callback=homecontent.renderNewsEvents&expires=0");
        }
        else {
            myjsonservice.loadScript("content_fetchnewsevents_0001", "services/content.aspx?action=fetchcontent&id=" + pageId + "&callback=homecontent.renderNewsEvents&expires=600");
        }
    },
    fetchNYCSchools:function(pageId) {
        var myjsonservice = new JSONService();
        if (this.rcode == "CM") {
            myjsonservice.loadScript("content_fetchnycschools_0001", "services/content.aspx?action=fetchcontent&id=" + pageId + "&callback=homecontent.renderNYCSchools&expires=0");
        }
        else {
            myjsonservice.loadScript("content_fetchnycschools_0001", "services/content.aspx?action=fetchcontent&id=" + pageId + "&callback=homecontent.renderNYCSchools&expires=600");
        }
    },
    load: function() {
        var objJson = eval('(' + homecontent.readCookie("pencilauth") + ')');
        if (objJson != null) {
            this.rcode = objJson.login.rcode;
        }
        else {
            this.rcode = "";
        }
        this.fetchProfileContent(114);
        this.fetchNewsEvents(117);
	this.fetchNYCSchools(242);
    }
};
homecontent.load();
