﻿$(document).ready(function(){
    $(".search-btn").click(function(){
	    var search = $('.search-input').val();
	    window.location = '/../results.aspx?s='+search;
    });
    renderblogtitle(); 
});

function renderblogtitle(){
    if(location.href.search("/blog/") != -1){
    
        rendercomments();
        
        $(".sub-nav ul").attr("id","nav");$("li.postDate").append(document.getElementById("hdnDate").value);$("li.author").append(document.getElementById("hdnAuthor").value.split(":")[1]);$("li.categories").append(document.getElementById("hdnCategories").value);$("a.numComment").prepend(document.getElementById("hdnNumComm").value.split(":")[1]);$("a.articlesAuthor").attr("href", document.getElementById("hdnAuthor").value.split(":")[0]);$("a.emailAuthor").attr("href", document.getElementById("hdnAuthor").value.split(":")[2]);$("a.numComment").attr("href", document.getElementById("hdnNumComm").value.split(":")[0]);$("a.addComment").attr("href", document.getElementById("hdnAddComm").value.split(":")[0]);
        $("#sidepanel input[type='button']").wrap("<div class='addbtn'></div>");$("#sidepanel select#widgetselector").wrap("<div class='adminSelector'></div>");

        if(location.href.split("/blog/")[1].search("/") != -1){
                
            var m_names = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
            var month = location.href.split("/blog/")[1].split("default.aspx")[0].split("/")[1].replace("0", "");
            var year = location.href.split("/blog/")[1].split("default.aspx")[0].split("/")[0];
            
            if(location.href.search("c=")!= -1){
                var query = location.href.split("?")[1].split("c=")[1];
                $("select#ddl_monthlyArchive").val(m_names[month-1] + " " + year + " (" + query + ")");
            }
            if(location.href.search("c=") == -1 && location.href.search("/category/") == -1 && location.href.search("/author/") == -1)
                $(".title h2").append(" - " + $("p.pubDate").text());
            else{
                if(location.href.search("/author/") != -1)
                    $(".title h2").append(" - posts by " + location.href.split("/author/")[1].split(".aspx")[0]);
                else if(location.href.search("/category/") != -1)
                    $(".title h2").append(" - " + location.href.split("/category/")[1].split(".aspx")[0]);
                else
                    $(".title h2").append(" - " + m_names[month-1] + " " + year);}
        }else
            $(".title h2").append(" - " + document.getElementById("ddl_monthlyArchive").options[0].text.split("(")[0]);
    }
}

function rendercomments(){
        var i=0;
        $("div.post").each(function(i){
            if($("div#post"+i + " .footer-blog p a:first-child").text().search("Unapproved Comments")!= -1)
                $("div#post"+i + " .footer-blog p a:first-child").attr("href", $("div#post"+i + " .footer-blog p a:last-child").attr("href")+"&ab=c");
        });
        if(location.href.search("ab=c")!= -1){
            $("#comment-form").hide();
            $("p#comment").text($("div.post .footer-blog p a:first-child").text());
            $("div.comment").each(function(j){
                if($(this).html().search("Approve") == -1)
                    $(this).hide();
            });
        }
        else{
             $("div.comment").each(function(k){
                if($(this).html().search("Approve") != -1)
                    $(this).hide();
            });
        }
}

//submit search when enter is pressed for search textbox
function submitenter(myfield,e){
    var keycode;
    if (window.event) keycode = window.event.keyCode;
    else if (e) keycode = e.which;
    else return true;
	
    if (keycode == 13){
	       $(".search-btn").click();
	       return false;}
    else
	    return true;
}

sfHover = function() {
	var sfEls = document.getElementById("nav").getElementsByTagName("LI");
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover=function() {
			this.className+=" sfhover";
		}
		sfEls[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
		}
	}
}
if (window.attachEvent) window.attachEvent("onload", sfHover);