var alignPostPreviewTimer = null;

function init() {
    /*
     * ---------------------------------------------------------------------------------------------------
     * Start of onload event by Dean Edwards/Matthias Miller/John Resig
     * A way to determine when the DOM has fully loaded without waiting for all those pesky images to load
     * http://dean.edwards.name/weblog/2005/09/busted/
     * http://dean.edwards.name/weblog/2006/06/again/
     * ---------------------------------------------------------------------------------------------------
     */
    // quit if this function has already been called
    if (arguments.callee.done) return;

    // flag this function so we don't do the same thing twice
    arguments.callee.done = true;

    // kill the timer
    if (_timer) clearInterval(_timer);
    /*
     * ---------------------------------------------------------------------------------------------------
     * End of onload event by Dean Edwards/Matthias Miller/John Resig
     * A way to determine when the DOM has fully loaded without waiting for all those pesky images to load
     * http://dean.edwards.name/weblog/2005/09/busted/
     * http://dean.edwards.name/weblog/2006/06/again/
     * ---------------------------------------------------------------------------------------------------
     */

    // Align Post Preview Popup To Center
    alignPostPreviewTimer = setInterval("alignPostPreview()", 50);
    
    // Set Scroll Event Timer
    setInterval("onScroll()", 500);
}

function onScroll() {
    // Init Local Variable
    var e = document.getElementById("postPreview");
    var w = 600;
    var h = 600;
    
    // Check NULL
    if (e == null) {
        return;
    }
    
    // Get Screen Width & Height
    var myWidth = 0, myHeight = 0;
    if (typeof(window.innerWidth) == 'number') {
        // Non-IE
        myWidth = window.innerWidth;
        myHeight = window.innerHeight;
    } else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
        // IE 6+ in 'standards compliant mode'
        myWidth = document.documentElement.clientWidth;
        myHeight = document.documentElement.clientHeight;
    } else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
        // IE 4 compatible
        myWidth = document.body.clientWidth;
        myHeight = document.body.clientHeight;
    } else {
        myWidth = screen.width;
        myHeight = screen.height - 200;
    }
    
    // Get Screen Scroll X & Y
    var scrOfX = 0, scrOfY = 0;
    if (typeof(window.pageYOffset) == 'number') {
        // Netscape compliant
        scrOfY = window.pageYOffset;
        scrOfX = window.pageXOffset;
    } else if (document.body && (document.body.scrollLeft || document.body.scrollTop)) {
        // DOM compliant
        scrOfY = document.body.scrollTop;
        scrOfX = document.body.scrollLeft;
    } else if (document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop)) {
        // IE6 standards compliant mode
        scrOfY = document.documentElement.scrollTop;
        scrOfX = document.documentElement.scrollLeft;
    }
    
    // Set Top Position
    var t = 0
    if (myHeight < h) {
        t = scrOfY;
    } else {
        t = ((myHeight - h) >> 1) + scrOfY;
    }
    if (t < 0) {
        e.style.top = "0px";
    } else {
        e.style.top = t + "px";
    }
}

function alignPostPreview() {
    // Init Local Variable
    var e = document.getElementById("postPreview");
    var w = 600;
    var h = 600;
    var layoutWidth = 800;
    
    // Check NULL
    if (e == null) {
        return;
    }
    
    // Get Screen Width & Height
    var myWidth = 0, myHeight = 0;
    if (typeof(window.innerWidth) == 'number') {
        // Non-IE
        myWidth = window.innerWidth;
        myHeight = window.innerHeight;
    } else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
        // IE 6+ in 'standards compliant mode'
        myWidth = document.documentElement.clientWidth;
        myHeight = document.documentElement.clientHeight;
    } else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
        // IE 4 compatible
        myWidth = document.body.clientWidth;
        myHeight = document.body.clientHeight;
    } else {
        myWidth = screen.width;
        myHeight = screen.height - 200;
    }
    
    // Layout Set To Relative
    myWidth = layoutWidth;
    
    // Set Left Position
    var l = (myWidth - w) >> 1;
    if (l < 0) {
        e.style.left = "0px";
    } else {
        e.style.left = l + "px";
    }
    
    // Set Top Position
    var t = (myHeight - h) >> 1;
    if (t < 0) {
        e.style.top = "0px";
    } else {
        e.style.top = t + "px";
    }
    
    // Clear Timer
    if (alignPostPreviewTimer != null) {
        clearInterval(alignPostPreviewTimer);
        alignPostPreviewTimer = null;
    }
}

function openPostPreview(previewUrl) {
    var myForm = document.mainForm;
    var xmlHttp = getXmlHttpObject();
    
    if (((previewUrl.indexOf("blog.asp") >= 0) && (checkPost(myForm))) ||
        ((previewUrl.indexOf("post.asp") >= 0) && (checkComment(myForm, true))) ||
        ((previewUrl.indexOf("review.asp") >= 0) && (checkReview(myForm, true)))) {
        if (xmlHttp == null) {
            myForm.action = previewUrl;
            myForm.submit();
        } else {
            // Detect Browser & Version
            var browser = navigator.appName;
            var b_version = navigator.appVersion;
            if (browser == "Microsoft Internet Explorer") {
                var ieVersion = b_version.split("MSIE");
                b_version = ieVersion[1];
            }
            var version = parseFloat(b_version);

            // Set Data to send
            var data = null;
            if (previewUrl.indexOf("blog.asp") >= 0) {
                data = "previewUrl=" + escapeVal(previewUrl) + "&title=" + escapeVal(myForm.title.value) + "&text=" + escapeVal(myForm.text.value);
            } else {
                data = "previewUrl=" + escapeVal(previewUrl) + "&text=" + escapeVal(myForm.text.value);
            }
            
            // Set Loading
            var e = document.getElementById("postPreview");
            e.innerHTML = "<table width=\"100%\" height=\"100%\" border=\"0\">" +
                "<tr valign=\"top\">" +
                "<td>" +
                "<br />" +
                "<p align=\"center\"><img align=\"middle\" vspace=\"200\" src=\"res/ajax_loading.gif\"></p>" +
                "</td>" +
                "</tr>" +
                "<tr valign=\"bottom\">" +
                "<td>" +
                "<p align=\"center\"><font class=\"main3\"><input type=\"button\" value=\"Close Preview\" onclick=\"javascript:closePostPreview();\"></font></p>" +
                "<br />" +
                "</td>" +
                "</tr>" +
                "</table>";
            if ((browser == "Microsoft Internet Explorer") && (version <= 6.5)) {
                e.innerHTML = e.innerHTML + "<iframe style=\"display: block; position: absolute; top: 0; left: 0; z-index: -1; filter: mask(); width: 100%; height: 100%;\"></iframe>";
            }
            e.style.display = "block";
            
            // Ajax call
            xmlHttp.onreadystatechange = function() {
                if (xmlHttp.readyState == 4) {
                    // Set Response Text
                    setHtml(e, xmlHttp.responseText);
                    if ((browser == "Microsoft Internet Explorer") && (version <= 6.5)) {
                        e.innerHTML = e.innerHTML + "<iframe style=\"display: block; position: absolute; top: 0; left: 0; z-index: -1; filter: mask(); width: 100%; height: 100%;\"></iframe>";
                    }
                }
            }
            xmlHttp.open("POST", "post_preview_ajax.asp", true);
            xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
            xmlHttp.send(data);
        }
    }
}

function closePostPreview() {
    document.getElementById("postPreview").style.display = "none";
}

function submitPostPreview() {
    var myForm = document.mainForm;
    myForm.submit();
}

/*
 * ---------------------------------------------------------------------------------------------------
 * Start of onload event by Dean Edwards/Matthias Miller/John Resig
 * A way to determine when the DOM has fully loaded without waiting for all those pesky images to load
 * http://dean.edwards.name/weblog/2005/09/busted/
 * http://dean.edwards.name/weblog/2006/06/again/
 * ---------------------------------------------------------------------------------------------------
 */
/* for Mozilla/Opera9 */
if (document.addEventListener) {
    document.addEventListener("DOMContentLoaded", init, false);
}

/* for Internet Explorer */
/*@cc_on @*/
/*@if (@_win32)
    document.write("<scr" + "ipt id=__ie_onload defer src=javascript:void(0)><\/scr" + "ipt>");
    var script = document.getElementById("__ie_onload");
    script.onreadystatechange = function() {
        if (this.readyState == "complete") {
            init(); // call the onload handler
        }
    };
/*@end @*/

/* for Safari */
if (/WebKit/i.test(navigator.userAgent)) { // sniff
    var _timer = setInterval(function() {
        if (/loaded|complete/.test(document.readyState)) {
            init(); // call the onload handler
        }
    }, 10);
}

/* for other browsers */
window.onload = init;
/*
 * ---------------------------------------------------------------------------------------------------
 * End of onload event by Dean Edwards/Matthias Miller/John Resig
 * A way to determine when the DOM has fully loaded without waiting for all those pesky images to load
 * http://dean.edwards.name/weblog/2005/09/busted/
 * http://dean.edwards.name/weblog/2006/06/again/
 * ---------------------------------------------------------------------------------------------------
 */

window.onresize = alignPostPreview;

$(document).ready(function() {
	$('.warning').remove();
	$.get('token.asp', function(txt) {
		$('.secure').append('<input type="hidden" name="token" value="' + txt + '" />');
	});
});