    var StreamType = "All";
    var StreamCount = 6;
    var Update = true;
    //Total number of items for updating
    var TotalStreamItemCount;
    //Number of items for selected streamType
    var MaxItemCount;

    $j(document).ready(function() {
        SetStreamInterval(60000);
        GetStreamItemCount(StreamType);

        $j("#AllButton").click(function() {
            StreamType = "All";
            StreamCount = 6;
            GetStreamItemCount(StreamType);
        });
        $j("#VideoButton").click(function() {
            StreamType = "Video";
            StreamCount = 6;
            GetStreamItemCount(StreamType);
        });
        $j("#TweetsButton").click(function() {
            StreamType = "Tweet";
            StreamCount = 6;
            GetStreamItemCount(StreamType);
        });
        $j("#PhotoButton").click(function() {
            StreamType = "Photo";
            StreamCount = 6;
            GetStreamItemCount(StreamType);
        });
        $j("#MoreButton").click(function() {
            StreamCount += 6;
            GetStreamItemCount(StreamType);
        });
        $j("#NewItemsButton").click(function() {
            document.getElementById("NewItemsButton").className = "newitemsbutton hidden";
            Update = true;
            StreamType = "All";
            StreamCount = 6;
            GetStreamItemCount(StreamType);
        });
    });
    
    function GetStreamItemCount(type) {
        $j.ajax({
            type: "POST",
            url: "/CMSPages/ActieSiteQueryRepeater.aspx/GetStreamItemCount",
            data: "{\"category\":\"" + type + "\"}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function(msg) {
                MaxItemCount = msg.d;
                UpdateStream();
            }
        });
    }

    function SetUpdateStreamButton() {
        if (Update) {
            $j.ajax({
                type: "POST",
                url: "/CMSPages/ActieSiteQueryRepeater.aspx/GetStreamItemCount",
                data: "{\"category\":\"All\"}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function(msg) {
                    var NewTotalStreamItemCount = msg.d;
                    if (NewTotalStreamItemCount != TotalStreamItemCount) {
                        TotalStreamItemCount = NewTotalStreamItemCount;
                        //Show updatebutton
                        document.getElementById("NewItemsButton").className = "newitemsbutton";
                        Update = false;
                    }
                }
            });
        }
    }

    function SetStreamInterval(miliseconds) {
        $j.ajax({
            type: "POST",
            url: "/CMSPages/ActieSiteQueryRepeater.aspx/GetStreamItemCount",
            data: "{\"category\":\"All\"}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function(msg) {
                TotalStreamItemCount = msg.d;
                setInterval(SetUpdateStreamButton, miliseconds);
            }
        });
    }

    function UpdateStream() {        
        //Disable morebutton if max is reached
        if (StreamCount >= 36) {
            StreamCount = 36;
            document.getElementById("MoreButton").className = "updatestreambutton hidden";
            document.getElementById("MoreButtonDisabled").className = "updatestreambuttondisabled";
        }
        else {
            document.getElementById("MoreButton").className = "updatestreambutton";
            document.getElementById("MoreButtonDisabled").className = "updatestreambuttondisabled hidden";
        }

        //Collect new items
        var content = $j.ajax({ url: "/CMSPages/ActieSiteQueryRepeater.aspx?type=" + StreamType + "&count=" + StreamCount, async: false }).responseText;

        //determine the amount of items returned 
        //returnstring example <count>;<items-json>
        var itemCount = content.substring(0, content.indexOf(";"));
        content = content.substring(content.indexOf(";") + 1);

        //Update the content            
        $j("#UpdateStream").html(content);
        _typeface_js.replaceText(document.getElementById("StreamHeader"));

        //if lower as 6 items, make it 6
        if (itemCount < 6) {
            itemCount = 6;
            document.getElementById("MoreButton").className = "updatestreambutton hidden";
            document.getElementById("MoreButtonDisabled").className = "updatestreambuttondisabled"
        }
        else if (itemCount < StreamCount) {
            document.getElementById("MoreButton").className = "updatestreambutton hidden";
            document.getElementById("MoreButtonDisabled").className = "updatestreambuttondisabled"
        }
        else if (MaxItemCount <= StreamCount) {
            document.getElementById("MoreButton").className = "updatestreambutton hidden";
            document.getElementById("MoreButtonDisabled").className = "updatestreambuttondisabled"
        }

        //calculate the height
        var heightVar = (100 * Math.ceil(itemCount / 3)).toString() + "px";

        //animate to new height 
        $j("#UpdateStream").animate({ height: heightVar }, 1000);
    }

