var xmlPath = '/Content/SiteAdmin/';
var imageWindow = null;
var htmlWindow = null;

var newsAll = "";
var ugAll = "";
var bpAll = "";

Ext.useShims = true;
Ext.BLANK_IMAGE_URL = 'lib/ext-3_2_0/resources/images/default/s.gif';

function hide() {
    Ext.MessageBox.hide();
}

function fnShowLargeImage(title, filename) {

    var isImageWindowOpen = false;

    if (imageWindow)
        imageWindow.close();

    imageWindow = new Ext.Window({
        title: title,
        autoHeight: true,
        autoWidth: true,
        resizable: false,
        draggable: true,
        layout: 'fit',
        shim: true,
        plain: false,
        bodyStyle: 'padding:5px;z-index:1;',
        buttonAlign: 'center',
        onEsc: hide,
        closable: true,
        onHide: function () {
            if (isImageWindowOpen)
                Ext.MessageBox.hide();
            else
                isImageWindowOpen = true;
        },
        html: '<img src="' + filename + '" />',
        buttons: [{
            text: 'Close',
            handler: function () {
                Ext.MessageBox.hide();
                imageWindow.hide();
            }
        }]
    });
    imageWindow.show().alignTo("wrapper", "tl", [50, 625]);
}

function fnShowHTMLPopup(title, HTML, width, height, scrollable) {

    var isHTMLWindowOpen = false;

    htmlWindow = new Ext.Window({
        title: decodeHtml(title),
        width: width,
        height: height,
        minWidth: 410,
        minHeight: 320,
        resizable: true,
        draggable: true,
        autoScroll: scrollable,
        layout: 'fit',
        shim: true,
        plain: false,
        bodyStyle: 'padding:5px;z-index:1;font-size:1.3em;',
        buttonAlign: 'center',
        onEsc: hide,
        closable: true,
        onHide: function () {
            if (isHTMLWindowOpen)
                Ext.MessageBox.hide();
            else
                isHTMLWindowOpen = true;
        },
        html: decodeHtml(HTML),
        buttons: [{
            text: 'Close',
            handler: function () {
                Ext.MessageBox.hide();
                htmlWindow.hide();
            }
        }]
    });

    htmlWindow.show();

}
///////////////////////////////////////SHOW XML information///////////////////////////////////////////////

function GetNewsSummary(num) {

    Ext.Ajax.request({
        url: xmlPath + "News.XML",
        success: function (response, opts) {
            var dq = Ext.DomQuery;
            var xml = response.responseXML;
            var nodes = dq.select('article', xml);

            var x = 0;
            var newsSummary = "<ul>";
            var articleHeading;
            var articleBody;

            Ext.each(nodes, function (item, index, allItems) {

                articleHeading = dq.selectValue('heading', item);
                articleBody = dq.selectValue('body', item);

                if (x++ < num) {


                    newsSummary += "<li><a href='#' onclick='javascript:fnShowHTMLPopup(\"" + encodeHtml("MyLearningPlan News Article") + "\", \"" + encodeHtml("<h1>" + articleHeading + "</h1><blockquote>" + articleBody + "</blockquote>") + "\",530,450,true );'>" + articleHeading + "</a></li>";
                    newsAll += "<br><h1>" + articleHeading + "</h1><blockquote>" + articleBody + "</blockquote>";
                }
                else {
                    newsAll += "<br><h1>" + articleHeading + "</h1><blockquote>" + articleBody + "</blockquote>";
                }


            });

            newsSummary += "</ul>";
            var d = Ext.getDom('news_Summary');
            d.innerHTML = newsSummary;
        
        }
    });


}

function encodeHtml(html) {
    var encodedHtml = "";
    encodedHtml = escape(html);
    encodedHtml = encodedHtml.replace(/\//g, "%2F");
    encodedHtml = encodedHtml.replace(/\?/g, "%3F");
    encodedHtml = encodedHtml.replace(/=/g, "%3D");
    encodedHtml = encodedHtml.replace(/&/g, "%26");
    encodedHtml = encodedHtml.replace(/@/g, "%40");
    return encodedHtml;
}

function decodeHtml(html) {
    var decodedHtml = "";
    decodedHtml = unescape(html);
    decodedHtml = decodedHtml.replace("%2F", /\//g);
    decodedHtml = decodedHtml.replace("%3F", /\?/g);
    decodedHtml = decodedHtml.replace("%3D", /=/g);
    decodedHtml = decodedHtml.replace("%26", /&/g);
    decodedHtml = decodedHtml.replace("%40", /@/g);
    return decodedHtml;
}

function ShowScrenCast(filename) {

    /* <object id="scPlayer" width="640" height="498">
    <param name="movie" value="http://content.screencast.com/users/MyLearningPlan/folders/MLP%20Website/media/11d1cf82-94bf-4896-9d11-4344134c63f0/MLP%20Features%20-%20Planning%20Features_controller.swf"></param>
    <param name="quality" value="high"></param>
    <param name="bgcolor" value="#FFFFFF"></param>
    <param name="flashVars" value="thumb=http://content.screencast.com/users/MyLearningPlan/folders/MLP%20Website/media/11d1cf82-94bf-4896-9d11-4344134c63f0/FirstFrame.png&containerwidth=640&containerheight=498&showstartscreen=true&showendscreen=true&loop=false&autostart=false&color=000000,000000&thumb=FirstFrame.png&thumbscale=45&content=http://content.screencast.com/users/MyLearningPlan/folders/MLP%20Website/media/11d1cf82-94bf-4896-9d11-4344134c63f0/MLP%20Features%20-%20Planning%20Features.mp4"></param>
    <param name="allowFullScreen" value="true"></param>
    <param name="scale" value="showall"></param>
    <param name="allowScriptAccess" value="always"></param>
    <param name="base" value="http://content.screencast.com/users/MyLearningPlan/folders/MLP%20Website/media/11d1cf82-94bf-4896-9d11-4344134c63f0/"></param>
    <embed src="http://content.screencast.com/users/MyLearningPlan/folders/MLP%20Website/media/11d1cf82-94bf-4896-9d11-4344134c63f0/MLP%20Features%20-%20Planning%20Features_controller.swf" quality="high" bgcolor="#FFFFFF" width="640" height="498" type="application/x-shockwave-flash" allowScriptAccess="always" flashVars="thumb=http://content.screencast.com/users/MyLearningPlan/folders/MLP%20Website/media/11d1cf82-94bf-4896-9d11-4344134c63f0/FirstFrame.png&containerwidth=640&containerheight=498&showstartscreen=true&showendscreen=true&loop=false&autostart=false&color=000000,000000&thumb=FirstFrame.png&thumbscale=45&content=http://content.screencast.com/users/MyLearningPlan/folders/MLP%20Website/media/11d1cf82-94bf-4896-9d11-4344134c63f0/MLP%20Features%20-%20Planning%20Features.mp4" allowFullScreen="true" base="http://content.screencast.com/users/MyLearningPlan/folders/MLP%20Website/media/11d1cf82-94bf-4896-9d11-4344134c63f0/" scale="showall">
    </embed>
    </object>    
    */
}


//**********************************************************
//Function Name :GetNewCustomers
//Purpose       :Used to deatil of a single news or all news.
//Params        :id :used to show newsid
//Created On    :11 MAY, 2010
//Return        :None
//***********************************************************
function GetNewCustomers(num) {
    Ext.Ajax.request({
        url: xmlPath + "Customers.XML",
        success: function (response, opts) {
            var dq = Ext.DomQuery;
            var xml = response.responseXML;
            var nodes = dq.select('customer', xml);

            var x = 0;
            Ext.each(nodes, function (item, index, allItems) {
                if (x++ < num) {
                    CustomerSummary += "<li>" + dq.selectValue('name', item) + ", " + dq.selectValue('state', item) + "</li>";
                }
            });

            CustomerSummary += "</ul>";
            var d = Ext.getDom('new_Customers');
            d.innerHTML = CustomerSummary;

        }
    });

    var CustomerSummary = "<ul>";
}

//**********************************************************
//Function Name :GetUGMeetings
//Purpose       :Used to list ug meetings
//Params        :id :used to show ugid
//Created On    :14 SEPT, 2010
//Return        :None
//***********************************************************
function GetUGMeetings(num) {
    Ext.Ajax.request({
        url: xmlPath + "UGMeetings.XML",
        success: function (response, opts) {
            var dq = Ext.DomQuery;
            var xml = response.responseXML;
            var nodes = dq.select('meeting', xml);

            var x = 0;


            Ext.each(nodes, function (item, index, allItems) {

                var date = new Date(dq.selectValue('date', item)).toDateString();
                //if (date != null) { var m = date.getMonth(); var d = date.getDate(); var y = date.getFullYear(); date = m.toString() + "/" + d.toString() + "/" +  y.toString(); };
                if ((date == "Invalid Date") || (date == "NaN")) { date = "Coming Soon!" };

                var time = dq.selectValue('time', item);
                if (time == undefined) { time = "TBD" };

                var topic = dq.selectValue('topic', item);
                if (topic == undefined) { topic = "To Be Determined" };

                var location = dq.selectValue('location', item);
                if (location == undefined) { location = "TBD" };

                var state = dq.selectValue('state', item);
                if (state == undefined) { state = "TBD" };

                var status = dq.selectValue('status', item);
                if (status == undefined) { status = "Close" };

                if (x++ < num && (status == "Open" || status == "TBD")) {
                    ugSummary += "<li><b>" + date + ",</b> " + " " + topic + ", " + location + ", " + state + ", " + time + "</li>";
                    ugAll += "<li><font size='1'><b>" + date + ",</b> " + " " + topic + ", " + location + ", " + state + ", " + time + "</font></li>";
                } else if (status == "Open" || status == "TBD") {
                    ugAll += "<li><font size='1'><b>" + date + ",</b> " + " " + topic + ", " + location + ", " + state + ", " + time + "</font></li>";
                }
            });

            ugSummary += "</ul>";
            var d = Ext.getDom('ug_meetings');
            d.innerHTML = ugSummary;

        }
    });

    var ugSummary = "<ul>";
}

function GetAllPracticeShowcase(num) {

    Ext.Ajax.request({
        url: xmlPath + "BPShowcase.XML",
        success: function (response, opts) {
            var dq = Ext.DomQuery;
            var xml = response.responseXML;
            var nodes = dq.select('showcase', xml);

            var x = 0;
            var bpSummary = "<ul>";
            var bpDistrict;
            var bpMonth;
            var bpHeading;
            var bpBody;

            Ext.each(nodes, function (item, index, allItems) {

                bpDistrict = dq.selectValue('districtname', item);
                bpMonth = dq.selectValue('month', item);
                bpHeading = dq.selectValue('heading', item);
                bpBody = dq.selectValue('body', item);

                if (x++ < num) {
                    //bpSummary += "<li><a href='#' onclick='javascript:fnShowHTMLPopup(\"" + encodeHtml("MyLearningPlan News Article") + "\", \"" + encodeHtml("<h1>" + bpHeading + "</h1><blockquote>" + bpBody + "</blockquote>") + "\",530,450,true );'>" + bpHeading + "</a></li>";
                    bpAll += "<br><h1>" + bpDistrict + "</h1>" + bpMonth + ": " + bpHeading + "<blockquote>" + bpBody + "</blockquote>";
                }
                else {
                    bpAll += "<br><h1>" + bpDistrict + "</h1>" + bpMonth + ": " + bpHeading + "<blockquote>" + bpBody + "</blockquote>";
                }


            });

            bpSummary += "</ul>";
            var d = Ext.getDom('bp_Summary');
            d.innerHTML = bpSummary;

        },
        failure: function (response, opts) {
            Ext.Msg.show({
                title: 'MyLearningPlan',
                icon: Ext.Msg.INFO,
                buttons: Ext.Msg.OK,
                msg: 'Best Practice Showcase unavailable... please check back later'
            })
        }
    });


}
