﻿
jQuery(document).ready(function() {
    var navdelay;
    $("#community-cat").ready(function() {
        navdelay = setInterval(function() {
            $("#community-cat .section-content").height(250);
            $("#community-cat .cat-sub").slideDown('fast');
        }, 500);
    }, function() {
        clearInterval(navdelay);
        $("#community-cat .section-content").height(150);
        $("#community-cat .cat-sub").slideUp('fast');
    });

    simpletabsSelect(0);

    // AJAX calls

    $.ajaxSetup({
        error: function(xhr) { }
    });

    loadAnnouncement();
    loadBlogthreads();
    loadRecentThread();
    loadRecentVideo();
    loadTagCloud();
    loadUserOnline();
});

function loadAnnouncement() {
    var tempHTML = "";
    $.get("/Projects/Community/Ajaxrespond.aspx?src=FSC-announcement&lang=EN", function(xml) {
        var tempHTML = '';
        var tempSubject, tempTeaser, tempID;
        $(xml).find("messages").each(function() {
            tempSubject = $(this).find("subject:first").text();
            tempTeaser = $(this).find("teaser:first").text();
            tempID = $(this).find("board_id:first").text();
            tempHTML += '<div class="amt"><a href="http://www.futureshopforums.ca/futureshop/board/message?board.id=blog&thread.id=' + tempID + '" class="subject">' + tempSubject + '</a><div class="teaser">' + tempTeaser + '</div></div>';
        });
        $("#whatsnew #w-content").append(tempHTML);
        $("#whatsnew #w-content .amt:odd").addClass("row-even");
    });
}

function loadBlogthreads() {
    $.get("/Projects/Community/Ajaxrespond.aspx?src=FSC-blogs&lang=EN", function(xml) {
    //$.get("/Projects/Community/include/test.xml", function(xml) {
        var tempHTML = '';
        var tempSubject, tempTeaser, tempID, tempUsername, tempauthor;

        var counter = 0;
        $(xml).find("messages").each(function() {
            tempSubject = $(this).find("subject:first").text();
            tempTeaser = $(this).find("teaser:first").text();

            // adding this to fix the problem with images being interrupted. (Alex Usanin)
            var tempTeaserImages = new Array();
            var tempTeaserImgCount = 0;

            tempTeaser = tempTeaser.replace(/&lt;/g, '<');
            tempTeaser = tempTeaser.replace(/&gt;/g, '<');
            tempTeaser = tempTeaser.replace(/&quot;/g, '"');
            tempTeaser = tempTeaser.replace(/<p>&nbsp;<\/p>/g, '');
            tempTeaser = tempTeaser.replace(/\<a[^\>]*\>[^\<]*\<\/a\>/g, '');

            var pattern = new RegExp("\<img[^\>]*\/?>+");
            var tempTeaserImages = '';

            tempTeaserImages = pattern.exec(tempTeaser)

            tempTeaser = tempTeaser.replace(/<img.+>/g, '');

            if(tempTeaserImages != null)
            {
                tempTeaser = tempTeaserImages + tempTeaser;
            }

            tempID = $(this).find("board_id:first").text();
            tempUsername = $(this).find("author login:first").text();
            tempauthor = $(this).find("author").attr("href").substring(10);


            tempDate = $(this).find("post_time:first").text();
            objDate = new Date(Date.UTC(parseInt(tempDate.substr(0, 4)), parseInt(tempDate.substr(5, 2)) - 1, parseInt(tempDate.substr(8, 2)), parseInt(tempDate.substr(11, 2)), parseInt(tempDate.substr(14, 2)), parseInt(tempDate.substr(17, 2))));
            currDate = new Date();

            diffMin = Math.ceil((currDate.getTime() - objDate.getTime()) / 1000 / 60);
            if (diffMin < 2) {
                outDate = '<span class="recent-under-hour">' + diffMin + " minute ago</span>";
            } else if (diffMin < 61) {
                outDate = '<span class="recent-under-hour">' + diffMin + " minutes ago</span>";
            } else if (currDate.toLocaleDateString() == objDate.toLocaleDateString()) {
                outDate = objDate.toLocaleTimeString() + " today";
            } else {
                outDate = objDate.toLocaleString();
            }

            tempHTML += '<div class="amt"><a href="http://www.futureshopforums.ca/futureshop/board/message?board.id=techblog&thread.id=' + tempID + '" class="subject">' + tempSubject + '</a><div class="teaser">' + tempTeaser + '<br /><br /><a href="http://www.futureshopforums.ca/futureshop/board/message?board.id=techblog&thread.id=' + tempID + '" class="asubject"><strong class="readmore">Read more</strong></a></div><div class="postinfo"><div class="clear"></div>Posted by: <a href="http://www.futureshopforums.ca/futureshop/profile?user.id=' + tempauthor + '">' + tempUsername + '</a> | @ ' + outDate + '</div></div>';

            counter = counter + 1;
            if (counter > 5) return false;
        });
        $("#blogs #b-content").append(tempHTML);
        $("#blogs #b-content .amt:odd").addClass("row-even");
    });
};

function loadRecentThread() {
    $.get("/Projects/Community/Ajaxrespond.aspx?src=FSC-recent-thread&lang=EN", function(xml) {
		var tempHTML = '';
		var tempName, tempID, tempDate, tempThread, tempBoard, objDate, diffMin, currDate, outDate;
		$(xml).find("threads > thread").each(function() {
			tempName = $(this).find("author login:first").text();
			tempID = $(this).find("author").attr("href").substring(10);
			tempDate = $(this).find("post_time:first").text();
			objDate = new Date(Date.UTC(parseInt(tempDate.substr(0,4)), parseInt(tempDate.substr(5,2))-1, parseInt(tempDate.substr(8,2)),parseInt(tempDate.substr(11,2)),parseInt(tempDate.substr(14,2)),parseInt(tempDate.substr(17,2))));
			currDate = new Date();
			
			diffMin = Math.ceil((currDate.getTime() - objDate.getTime())/1000/60);
			if (diffMin < 2) {
				outDate = '<span class="recent-under-hour">' + diffMin + " minute ago</span>";
			} else if (diffMin < 61) {
				outDate = '<span class="recent-under-hour">' + diffMin + " minutes ago</span>";
			} else if (currDate.toLocaleDateString() == objDate.toLocaleDateString()) {
				outDate = objDate.toLocaleTimeString() + " today";
			} else {
				outDate = objDate.toLocaleString() ;
			}
			
			tempBoard = $(this).find("board:first").attr("href").substring(11);
			tempThread = $(this).find("board_id:first").text();
			tempHTML += '<div class="thread"><a href="http://www.futureshopforums.ca/futureshop/board/message?board.id=' + tempBoard + '&message.id=' + tempThread + '" class="subject">' + $(this).find("subject:first").text() + '</a><span class="author"><span class="sub-title">By <a href="http://www.futureshopforums.ca/futureshop/profile?user.id=' + tempID + '" name="' + tempID + '" class="name">' + tempName + '</a></span>' + ' @ ' + outDate + '</span></div>';
		});
		$("#threads #t-content").append(tempHTML);
		$("#threads #t-content .thread:odd").addClass("row-even");
		$("#threads .name").mouseover(function() {
			tempID = $(this).attr("name");
			tempName = $(this).text();
			if (!$(this).parent().find("img").size()) {
				$(this).parent().append($('<div class="name_popover">').html(getAvatarIcon(tempID, tempName) + tempName));
			}
			$(this).parent().find(".name_popover").show();
		});
		$("#threads .name").mouseout(function() {
			$(".name_popover").hide();
		});
	});
};

function loadRecentVideo() {
};

function loadTagCloud() {
    $.get("/Projects/Community/Ajaxrespond.aspx?lang=EN&src=FSC-tag-cloud", function(xml) {
        var tempHTML = '<p>';
        var tagsArr = new Array();

        $(xml).find("text").each(function() {
            tagsArr.push($(this).text());
        });

        var tempArray = new Array();

        for (i = 0; i < tagsArr.length; i++) {
            if (i <= 10) {
                tempArray[i] = '<span class="big"><a href="http://www.futureshopforums.ca/futureshop/tagging/view_tag?tag.text=' + tagsArr[i] + '">' + tagsArr[i] + '</a></span>';
            }
            else if ((i > 10) && (i <= 22)) {
            tempArray[i] = '<span class="mid"><a href="http://www.futureshopforums.ca/futureshop/tagging/view_tag?tag.text=' + tagsArr[i] + '">' + tagsArr[i] + '</a></span>';
            }
            else if ((i > 22) && (i <= 33)) {
                tempArray[i] = '<span class="norm"><a href="http://www.futureshopforums.ca/futureshop/tagging/view_tag?tag.text=' + tagsArr[i] + '">' + tagsArr[i] + '</a></span>';
            }
            else {
                tempArray[i] = '<span class="small"><a href="http://www.futureshopforums.ca/futureshop/tagging/view_tag?tag.text=' + tagsArr[i] + '">' + tagsArr[i] + '</a></span>';
            }
        }

        tempArray.sort(randOrd);
        //tempstring = tempArray.toString();

        tempHTML += tempArray.join(" ") + "</p>";

        $("#populartags #content").html(tempHTML);
    });
};

function loadUserOnline() {
    $.get("/Projects/Community/Ajaxrespond.aspx?lang=EN&src=FSC-user-online", function(xml) {
        var tempHTML = '';
        tempHTML += "<div class='clear'></div>"
        var tempName, tempID;
        $(xml).find("user").each(function() {
            tempName = $(this).find("login").text();
            tempID = $(this).find("id").text();

            if ((tempHTML.indexOf(tempName) < 0)) {
                tempHTML += '<div class="user"><a href="http://www.futureshopforums.ca/futureshop/profile?user.id=' + tempID + '">' + getAvatarIcon(tempID, tempName) + '</a></div>';
            }
        });
        tempHTML += "<div class='clear'></div>"
        $("#whosonline #u-content").html(tempHTML);
    });
};

function getAvatarIcon(id, name) {
    var tempPath;
    $.ajax({
        url: "/Projects/Community/Ajaxrespond.aspx?lang=EN&src=FSC-avatar-icon&id=" + id,
        success: function(xml) {
            tempPath = $(xml).find("profile[name='url_icon']").text();
        },
        async: false
    });

    if (tempPath != null && tempPath.indexOf("http") > -1) {
        return '<img src="' + tempPath + '" border="0" class="avatar" alt="' + name + '" title="' + name + '" width="36" height="36">';
    } else if (tempPath == "none" || tempPath == null) {
        return '<img src="/Projects/Community/Assets/icon.gif" class="avatar" alt="' + name + '" title="' + name + '" border="0" width="36" height="36">';
    } else {
    return '<img src="http://www.futureshopforums.ca' + tempPath + '" border="0" class="avatar" alt="' + name + '" title="' + name + '" width="36" height="36">';
    }
};

function randOrd() {
    return (Math.round(Math.random()) - 0.5);
}

function urlencode(str) {
    str = escape(str);
    return str.replace(/[*+\/@]|%20/g,
        function(s) {
            switch (s) {
                case "*": s = "%2A"; break;
                case "+": s = "%2B"; break;
                case "/": s = "%2F"; break;
                case "@": s = "%40"; break;
                case "%20": s = "+"; break;
            }
            return s;
        }
    );
}

