var news = null;
var feedForm = null;
var randomnumber=Math.floor(Math.random()*100001)

var tmpBalloon = null;

var loc = window.location.href;

var fullpathname = window.location.pathname;
var pathParts = fullpathname.split("/");
// var root = "/"+pathParts[1]+"/";
var root="/";

document.observe('dom:loaded', function () {
    news     = new News();
    feedForm = new FeedForm();
    $('commentForm').observe('submit', respondToSubmit);
});


var News = Class.create();
News.prototype = {
    initialize: function() {
        this.update(null);
    },
    showTip: function(itemId, title) {
        // toggle
        if (tmpBalloon != null) {
            try {
                tmpBalloon.toggle();
            } catch (err) {
                tmpBalloon = null
            }
        }
        tmpBalloon = new HelpBalloon({
            imgPrefix: root,
            title: title,
            content: 'loading...',
            icon: document.getElementById(itemId),
            dataURL: root+'contentProvider.do?action=loadPreview&contentId='+itemId,
            cacheRemoteContent: false
        });
        tmpBalloon.show();

    },
    showDisclaimer: function() {
        var res   = "<h1>Disclaimer</h1><br />";
        res = res + "<b>Links to Third Party Sites</b><br />";
        res = res + "ALL LINKED SITES ARE NOT UNDER THE CONTROL OF ORKLZ.COM AND ORKLZ.COM IS NOT RESPONSIBLE FOR THE CONTENTS OF ANY LINKED SITE OR ANY LINK CONTAINED IN A LINKED SITE, OR ANY CHANGES OR UPDATES TO SUCH SITES. ORKLZ.COM IS NOT RESPONSIBLE FOR WEBCASTING OR ANY OTHER FORM OF TRANSMISSION RECEIVED FROM ANY LINKED SITE. ORKLZ.COM IS PROVIDING THESE LINKS TO YOU ONLY AS A CONVENIENCE, AND THE INCLUSION OF ANY LINK DOES NOT IMPLY ENDORSEMENT BY ORKLZ.COM OF THE SITE.<br /><br />";
        res = res + "<b>Trademarks</b><br />";
        res = res + "Apple is a trademark of Apple Computer, Inc., registered in the U.S. and other countries.<br />";
        res = res + "Java is a trademark of Sun Microsystems, Inc., registered in the U.S. and other countries.<br />";
        res = res + "Linux is a trademark of Linus Torvalds in the US and some other countries.<br />";
        res = res + "Other trademarks belong to their respective owners.<br />";
        res = res + "<br />";
        $('right').update(res);
    },
    update: function(feedId) {
        $('right').update("<img src='/img/large-loading.gif' />");
        new Ajax.Request(root+'contentProvider.do', {
            method: 'get',
            parameters: {action: 'news', feedId: feedId, rand: randomnumber, userTime: new Date().getTime(), date: askingDate},
            onSuccess: function(transport){
                var contentCnt = $('right');
                var res = '';
                if (transport.responseText != null && transport.responseText.strip() != "") {
                    var json = transport.responseText.evalJSON();
                    for (var i = 0; i < json.length; i++) {
                        var headline = "<span class='headline'>" + json[i].hourKeyString + "</span><br />" + "<hr />";
                        res = res + headline;
                        var cntArr = json[i].result;
                        for (var j = 0; j < cntArr.length; j++) {
                            var icn = root + cntArr[j].feed.icon;
                            var fTi = cntArr[j].feed.title;
                            var url = cntArr[j].articleUrl;
                            var til = cntArr[j].title;
                            var id = cntArr[j].id;
                            var hasContent = cntArr[j].hasContent;
                            var icon = "<img align='absmiddle' alt='" + fTi + "' width='16' height='16' src='"+ icn + "' />&nbsp;";
                            res = res + icon;
                            if (til.indexOf("'") > -1 && til.indexOf("\"") > -1) {
                                til = til.replace("'", " ");
                                til = til.replace("\"", " ");
                            }
                            if (hasContent && !Prototype.Browser.IE6) {
                                // if (til.indexOf("'") == -1 && til.indexOf('"') == -1) {
                                    var prevTil = til.replace(/\"/g, "");
                                    prevTil = prevTil.replace("\'", "");
                                    var preview = "<img style='cursor: help;' id='" + id + "' onclick='news.showTip(" + id + ",\"" +prevTil+ "\")' align='absmiddle' src='"+root+"img/preview.png' border='0' />&nbsp;";
                                    // var preview = "<a id='" + id + "' onclick='return false;' href='#'><img id='" + id + "' align='absmiddle' src='img/preview.png' border='0' /></a>&nbsp;";
                                    res = res + preview;
                                // }
                            }

                            // var line = "<a onclick='clickCounter("+id+",\""+url+"\")' href='" + url + "'>" + til + "</a><br />";
                            var line = "<a href='"+url+"'>" + til + "</a><br />";
                            res = res + line;
                        }
                        res = res + "<br />";

                    }
                } else {
                    res = "No results...";
                }
                contentCnt.update(res);
            },
            onFailure: function(){
                $('right').update("Something went wrong!?");
            }
        });
    }
};

var FeedForm = Class.create();
FeedForm.prototype = {
    initialize: function() {
        this.update();
    },
    update: function() {
        new Ajax.Updater('nav',root+'contentProvider.do', {
            method: 'get',
            parameters: {action: 'nav', rand: randomnumber, userTime: new Date().getTime()}
        });
    }
};

function clickCounter(id, url) {

    new Ajax.Request(root+'hitcounter.do', {
        parameters: {
            id: id
        },
        onSuccess: function() {
            location.href = url;
        },
        onFailure: function() {
            location.href = url;
        }
    });
    // location.href = url;
}

function respondToSubmit(event) {
    $('commentForm').request({
        onFailure: function() {
            alert("failure");
        },
        onSuccess: function(t) {
            $('commentbox').update("<span class='headline'>" + t.responseText + "</span>");
        }
    });
    Event.stop(event);
}


