﻿function addListener(element, type, expression, bubbling) {
    bubbling = bubbling || false;
    if (window.addEventListener) { // Standard
        element.addEventListener(type, expression, bubbling);
        return true;
    } else if (window.attachEvent) { // IE
        element.attachEvent('on' + type, expression);
        return true;
    } else return false;
}

var ImageLoader = function (url) {
    this.url = url;
    this.image = null;
    this.loadEvent = null;
};

ImageLoader.prototype = {
    load: function () {
        this.image = document.createElement('img');
        var url = this.url;
        var image = this.image;
        var loadEvent = this.loadEvent;
        addListener(this.image, 'load', function (e) {
            if (loadEvent != null) {
                loadEvent(url, image);
            }
        }, false);
        this.image.src = this.url;
    },
    getImage: function () {
        return this.image;
    }
};

var i2 = new Image(1, 1);
var r = document.referrer.replace(/html/g, 'htm');
i2.src = domain + 'stat/?d=1&ddd=' + ddd + '&r=' + r;
i2.onload = function () { return; }

function ShowCommentBox() {

    $('divCommentAdd').innerHTML = 'Add Comment <span style="color:#0c55a0; margin-left:5px; cursor:pointer">Loading ...</span>';
    var script = document.createElement('script');
    
    script.setAttribute('src', domain + 'comment/box/?ddd=' + ddd);
    script.onreadystatechange = function () {
        if (script.readyState == 'loaded' || script.readyState == 'complete')
            commentCallbackRet();
    }

    script.onload = function () {
        script.readyState = 'loaded';
        script.onreadystatechange();
    }

    document.getElementsByTagName('head')[0].appendChild(script);
}

function commentCallbackRet() {
    var divCommentBoxS = $S('divCommentBox');
    divCommentBoxS.display = divCommentBoxS.display == 'none' || divCommentBoxS.display == '' ? 'block' : 'none';
    $('divCommentAdd').innerHTML = 'Add Comment';

    if ($('txtK') != null)
        $('txtK').value = ddd;
    if ($('txtC') != null)
        $('txtC').value = $('txtCap').value;
    //refreshCaptcha();
}

function handleResponse(res) {}

function getQuery() {
    var query = '';
    query += 'txtName=' + $('txtName').value.replace(/&/g, '%26');
    query += '&txtComment=' + $('txtComment').value.replace(/&/g, '%26');
    query += '&txtCaptcha=' + $('txtCaptcha').value;
    query += '&txtK=' + $('txtK').value;
    query += '&txtC=' + $('txtC').value;
    return query;
}

function setComment() {
    if ($('chkTerms').checked == false)
        alert('You should agree with our terms of use before submitting a comment.');
    else if($('txtComment').value.length > 500)
        alert('Comments must be 500 characters or less.');
    else {
        var script1 = document.createElement('script');
        script1.setAttribute('src', domain + 'comment/?' + getQuery());
        script1.onreadystatechange = function () {
            if (script1.readyState == 'loaded' || script1.readyState == 'complete')
                handleResponse();
        }
        script1.onload = function () {
            script1.readyState = 'loaded';
            script1.onreadystatechange();
        }
        document.getElementsByTagName('head')[0].appendChild(script1);
    }
}

function refreshCaptcha() {
    var dd = new Date();
    var ddd = dd.getDate() + '' + dd.getHours() + '' + dd.getMinutes() + '' + dd.getSeconds();

    var loader = new ImageLoader(domain + 'captcha/?ddd=' + ddd);
    loader.loadEvent = function (url, image) {
        var imgCaptcha = document.createElement('img');
        imgCaptcha.setAttribute('src', domain + 'captcha/?ddd=' + ddd);

        var divCaptcha = $('divCaptcha');
        var divCaptchaImgs = divCaptcha.getElementsByTagName('img');
        if (divCaptchaImgs.length > 0)
            divCaptcha.removeChild(divCaptchaImgs[0]);
        divCaptcha.appendChild(imgCaptcha);
        if ($('txtK') != null)
            $('txtK').value = ddd;
        if ($('txtC') != null)
            //$('txtC').value = getCookie('p_cap');
            $('txtC').value = $('txtCap').value;
    }
    loader.load();
}

function getCookie(c_name) {
    if (document.cookie.length > 0) {
        c_start = document.cookie.indexOf(c_name + "=");
        if (c_start != -1) {
            c_start = c_start + c_name.length + 1;
            c_end = document.cookie.indexOf(";", c_start);
            if (c_end == -1) c_end = document.cookie.length;
            return unescape(document.cookie.substring(c_start, c_end));
        }
    }
    return "";
}

function email() {
    var kkk = location + '';
    kkk = kkk.replace('.html', '');
    kkk = kkk.slice(kkk.lastIndexOf("/") + 1);
    window.open(domain + "EMail/?id=" + kkk, "EMail", "status=no,scrollbars=yes,resizable=no,width=518,height=478", "");
}

var h = new Array('divMenu', 'divCrawl', 'divComment', 'divFooterNav', 'divNewsDetail',
                'imgMain', 'divImageDesc', 'spnRightPane', 'divHeaderItems1', 'divHeaderItems2');
function pr() {
    for (i = 0; i < h.length; i++)
        if ($(h[i]) != null)
            $S(h[i]).display = 'none';
    $S('divDetail').width = '920px'
    $S('divNav').width = '920px'

    var _byline = document.createElement('div');
    _byline.innerHTML = $('divNewsByline').innerHTML;
    _byline.className = 'newsByline';
    var _datetime = document.createElement('div');
    _datetime.innerHTML = $('divNewsDatetime').innerHTML;
    _datetime.className = 'newsDatetime';

    $('divTitle').appendChild(_byline);
    $('divTitle').appendChild(_datetime);

    $S('spnPrintBack').display = 'block';
    $S('divDetail').fontSize = '16px';
    $S('divLead').fontSize = '18px';

    this.print();
}
function prBack() {
    $S('divDetail').width = '650px'
    for (i = 0; i < h.length; i++)
        if ($(h[i]) != null)
            $S(h[i]).display = 'block';

    var newsDet = $('divTitle').getElementsByTagName('div');
    while (newsDet[0] != null)
        $('divTitle').removeChild(newsDet[0]);

    $S('divDetail').fontSize = '14px';
    $S('divLead').fontSize = '16px';
    $S('spnPrintBack').display = 'none';
}

if ($('divTitle') != null) {
    var title = $('divTitle').innerHTML;
    var shares = new Array('Facebook', 'Delicious', 'Digg', 'Twitter', 'Reddit', 'Newsvine', 'Mixx', 'StumbleUpon');
    var shareAddr = new Array(
        'http://www.facebook.com/share.php?u=' + location,
        'http://del.icio.us/post?url=' + location + '&title=' + title,
        'http://digg.com/submit?phase=2&url=' + location + "&title=" + title,
        'http://twitter.com/home?status=' + title + ':' + location,
        'http://reddit.com/submit?url=' + location + '&title=' + title,
        'http://www.newsvine.com/_tools/seed&save?u=' + location,
        'http://www.mixx.com/submit/story?page_url=' + location + '&title=' + title,
        'http://www.stumbleupon.com/submit?url=' + location + '&title=' + title);
    for (i = 0; i < shares.length; i++) {
        var anch = document.createElement('a');
        anch.id = 'share' + i;
        anch.href = shareAddr[i];
        var img = document.createElement('img');
        img.src = $('root').value + 'images/shareicons/' + shares[i] + '.png';
        img.title = shares[i];
        img.border = 0;

        anch.appendChild(img);
        $('divShare').appendChild(anch);
    }
}

var inc = document.createElement('span');
inc.style.margin = '5px 0 0 5px';
inc.innerHTML = "<a href='javascript:font_adj(2)' style='margin-right:5px'><img title='Increase' style='border:none' src='../images/icons/font_inc.gif' /></a>" +
    "<a href='javascript:font_adj(0)' style='margin-right:5px'><img title='Increase' style='border:none' src='../images/icons/font_nor.gif' /></a>" +
    "<a href='javascript:font_adj(-2)' style='margin-right:5px'><img title='Increase' style='border:none' src='../images/icons/font_dec.gif' /></a>";
$('divActions').appendChild(inc);

function font_adj(co) {
    var f_size = $S('divDetail').fontSize;
    if (f_size == '') $S('divDetail').fontSize = f_size = '14px';
    if (co == 0)
        $S('divDetail').fontSize = f_size = '14px';
    else {
        if (parseInt(f_size) > 20 || parseInt(f_size) < 11)
            return;
        $S('divDetail').fontSize = parseInt(f_size) + co+ 'px';
    }
}
