﻿
var blogDisplayed = 0;

$(document).ready(function () {
    $("#blogClose").click(function () {
        HideBlog();
    });
    $(window).scroll(function () {
        if (blogDisplayed == 1) {
            centerPopup();
        }
    });
});

function HideBlog() {
    if (blogDisplayed == 1) {
        $("#blogBackground").fadeOut();
        $("#blogBackground").css("position", "absolute");
        $("#blogPopup").css("position", "absolute");
        $("#blogPopup").fadeOut(function () {
            $("#blogContent").children().remove();
            $("#blogPopup").children("h1").html("");
        });
    }
}

function DisplayBlog(data) {
    HideBlog();
    var Data = data.split("|");
    var page = "/GetBlog.asmx/GetBlogContent";
    var params = { 'Author': Data[1], 'PageNumber': Data[2], 'BlogId': Data[0] };
    $.ajax({
        url: page,
        type: "POST",
        data: params,
        success: ShowBlog,
        error: ErrorBlog
    });
    return false;
}

function ShowBlog(Result) {
    var blog = "";
    if (Result.d != undefined) {
        blog = Result.d.split("|");
    }
    else {
        if (Result.text != undefined) {
            blog = Result.text.split("|");
        }
        else {
            blog = Result.documentElement.textContent.split("|");
        }
    }
    // blog[0] contains the title and blog[1] contains the entry            
    $("#blogBackground").prependTo(document.body);
    $("#blogPopup").appendTo(document.body);
    $("#blogPopup").children("h1").prepend(blog[0]);
    $("#blogContent").prepend($("<p>").append(blog[1]));
    blogDisplayed = 1;
    $("#blogBackground").css({ "opacity": "0.7" });
    centerPopup();
    $("#blogBackground").fadeIn("slow");
    $("#blogPopup").fadeIn("slow");

    return false;
}
function ErrorBlog(xhr, textStatus, errorThrown) {
    HideBlog();
    return true;
}
//centering popup  
function centerPopup() {
    //request data for centering
    var windowWidth = document.documentElement.clientWidth;
    var windowHeight = document.documentElement.clientHeight;
    var popupHeight = $("#blogPopup").height();
    var popupWidth = $("#blogPopup").width();
    //centering  
    $("#blogPopup").css({
        "position": "absolute",
        "top": (windowHeight / 2 - popupHeight / 2) + $(document).scrollTop(),
        "left": windowWidth / 2 - popupWidth / 2
    });
    //only need force for IE6  

    $("#blogBackground").css({
        "height": windowHeight + $(document).scrollTop()
    });

}

var tabPosition = 0;
function slideLeft(link) {
    if (tabPosition > 0) {
        // we can slide Left one
        SlideDownBlog();
        $(".tabHolder li.active").removeClass("active").addClass("inactive");
        $(".tabHolder li:eq(" + (tabPosition - 1) + ")").removeClass("inactive").addClass("active");        
        $(".tabHolder li:eq(" + (tabPosition - 1) + ")").show();
        SlideUpBlog();
        tabPosition -= 1;
    }
}

function slideRight(link) {
    if (tabPosition < tabMax - 1) {
        SlideDownBlog();
        $(".tabHolder li.active").removeClass("active").addClass("inactive");
        $(".tabHolder li:eq(" + (tabPosition + 1) + ")").removeClass("inactive").addClass("active");
        $(".tabHolder li:eq(" + tabPosition + ")").hide();
        SlideUpBlog();
        tabPosition += 1;

    }
}

function SlideDownBlog() {
    $(".tabContainer .content .blogs").html("");
}

function SlideUpBlog() {
    var currentBlog = $(".tabContainer .active");
    var content = $(".tabContainer .content");
    content.show();
    GetBlogSummary(currentBlog.find(".blog").val());
    content.find(".name").html(currentBlog.find(".name").html());
    content.find(".title").html(currentBlog.find(".title").html());
    var url = "/" + langCode + "/blog.html?blog=" + currentBlog.find(".blog").val();
    url = url.replace("en/", "");
    $(".tabContainer .content #MoreBlogLink").attr("href", url);
}

function GetBlogSummary(blogId) {
    var page = "/GetBlog.asmx/GetBlogSummary";
    var params = { 'TeamId': blogId, 'LangId': langId };
    $.ajax({
        url: page,
        type: "POST",
        data: params,
        success: DisplayBlogSummary,
        error: DisplayBlogError
    });
    return false;
}

function DisplayBlogError() {
    $(".tabContainer .content .blogs").html("<h4>We apologise for this inconvenience, we have experienced an error while trying to retrieve your selected blog, please select another team member or visit the more blogs link below.</h4>");
}

function DisplayBlogSummary(Result) {
    var blog = "";
    if (Result.d != undefined) {
        blog = Result.d;
    }
    else {
        if (Result.text != undefined) {
            blog = Result.text;
        }
        else {
            blog = Result.documentElement.textContent;
        }
    }
    // we have properly formatted html, we can just output it
    $(".tabContainer .content #blogs").html(blog);
}

function ShowAuthor(link) {

    var author = $(link).parents("li");
    if (author.hasClass("active")) {
        return;
    }
    SlideDownBlog();
    $(link).parents("ul").children("li.active").removeClass("active").addClass("inactive");
    author.addClass("active").removeClass("inactive");
    SlideUpBlog();
}
