﻿// JScript File
document.domain = "sqlservervideos.com";
var __site = "www";
window.load = Initialize();

function Initialize() {
    InitializeRollover();
    InitializeComments();
    InitializeUser();
}

function InitializeComments() {
    var comments = document.getElementById("comments");
    if(comments == null) return;

    var video = document.getElementById("video");
    if(video == null) {
        Hide();
        return;
    }    

    var videoId = video.getAttribute("vid_id");
    if(videoId == null) {
        Hide();
        return;
    }
    
    var include = "<script type=\"text/javascript\" src=\"http://" + __site + ".sqlservervideos.com/comments/json/?id=" + videoId + "\"></script>";
    document.write(include);
    
    // set up comments/feedback:
    var commentId = document.getElementById("VideoId");
    commentId.setAttribute("value",videoId);
}

function InitializeUser() {
    var include = "<script type=\"text/javascript\" src=\"http://" + __site + ".sqlservervideos.com/comments/user/\"></script>";
    document.write(include);
}

function BindUser(user) {
    var comments = document.getElementById("comment_section");
    if(comments == null) return;

    if(user == null || user.Name == null) {
        document.getElementById("site").style.display = "none";
        document.getElementById("name").value = "Anonymous";
        return;
    }
    var set = null;
    
    if(user.Name.length > 1) {
        document.getElementById("name").value = user.Name;
        set = true;
    }
    
    if(user.Url.length > 1) {
        document.getElementById("url").value = user.Url;
        set = true;
    }
    
    if(set) {
        document.getElementById("login_link").style.display = "none";
    }
}

function Hide() {
    var comments = document.getElementById("comment_section");
    if(comments == null) return;
    
    comments.style.visibility = "hidden";
    comments.style.display = "none";
}

function LoadComments(results) {
    if(results.length > 0) {
        var comments = document.getElementById("comments");
        var throbber = document.getElementById("throbber");
        
        if(throbber != null)
        comments.removeChild(throbber);
        
        for(var i =0; i < results.length; i++) {
            AppendComment(results[i]);       
        }
    }
}

function AppendComment(comment) {
    var comments = document.getElementById("comments");
    
    var outer = document.createElement("div");
    outer.setAttribute("class","comment");
    outer.setAttribute("className","comment"); // yes, i'm lazy and just addressing the evil MS bug this way (for now). If you think I suck, take it up with the idiots at MS who are truly responsible for non-standards compliant functionality.
    
    var inner = document.createElement("div");
    inner.setAttribute("class","innercomment");
    inner.setAttribute("className","innercomment");
    
    var para = document.createElement("p");
    para.innerHTML = comment.Comment;
    
    var footer = document.createElement("div");
    footer.setAttribute("class","comment_bottom");
    footer.setAttribute("className","comment_bottom");
    
    var footerSpan = document.createElement("span");
    footerSpan.setAttribute("class","postedby");
    footerSpan.setAttribute("className","postedby");
    
    var footerText = "";
    if(comment.Name == "") {
        footerSpan.appendChild(document.createTextNode("Anonymous on " + comment.Date + "."));
    }
    else {
        if(comment.Url == null) {
            footerSpan.appendChild(document.createTextNode(comment.Name + " on " + comment.Date +"."));
        }
        else {
            var ref = document.createElement("a");
            ref.setAttribute("href","http://" + __site + ".sqlservervideos.com/member/" + comment.Url + "/");
            ref.appendChild(document.createTextNode(comment.Name));
            
            footerSpan.appendChild(ref);
            footerSpan.appendChild(document.createTextNode(" on " + comment.Date + "."));
        }
    }
    
    footer.appendChild(footerSpan);
    
    inner.appendChild(para);
    outer.appendChild(inner);
    outer.appendChild(footer);
    
    comments.appendChild(outer);
}


// mouse-over functionality:
function InitializeRollover() {
    for(var i=0; i < document.images.length; i++) {
        var attrib = document.images[i].getAttribute("thumbnail");
        if(attrib != null)
            ConfigureRollover(document.images[i]);
    }
}

function ConfigureRollover(targetImage) {
    var src = targetImage.getAttribute("src");
    var rolloverSrc = src.replace(".gif","_h.gif");

    PreLoad(rolloverSrc);    
}