//
// Site
//
;(function(window) {

    /**
    * Site Object
    **/
    window.Site = Site = new Object();

    /*#region Site -> Home*/
    Site.Home = function() {
        /******* Variables */
        var postForm;

        /******** Page actions */

        var mycarousel_itemVisibleInCallback = function(carousel, item, i, state, evt) {
            // The index() method calculates the index from a
            // given index who is out of the actual item range.       
            var idx = carousel.index(i, totalFeaturedArticles);
            carousel.add(i, carousel_getItemHTML(featuredArticles[idx - 1]));
        };

        var mycarousel_itemVisibleOutCallback = function(carousel, item, i, state, evt) {
            carousel.remove(i);
        };

        var carousel_getItemHTML = function(item) {
            var thumbnailUrl = item.thumbnail == "" ? defaultFeaturedArticleImage : item.thumbnail;
            var imageTag = "<a href='" + item.fullUrl + "' class='thumb'><img src='" + thumbnailUrl + "' alt=''/></a>";
            return imageTag + "<div class='container'><p class='publish-details'>Published in <a class='category' href='" + item.categoryUrl + "'>" + item.category + "</a>, " + item.published + ", by <a class='category' href='/UserProfile/" + item.owner + "'>" + item.owner + "</a> </p><a class='title' href='" + item.fullUrl + "'>" + item.title + "</a> <p class='description'> " + item.description + "</p><a class='more' href= " + item.fullUrl + ">Read more &rsaquo;</a><p class='community'>Comments <span> " + item.comments + "</span>     Recommends <span>" + item.recommends + "</span></p></div>";

        };

        var onNextButtonClick = function(pageNumber) {
            window.location = "/Page/" + pageNumber;
        };

        /********* Initialize everything */
        (this.Init = function() {
        	if (window.location.search != "")
        	{
        		var pos = window.location.search.indexOf("=");
        		var type = window.location.search.substring(pos+1);
        		$("#submenu-bar a."+type).addClass("selected");
        	}
            
            if (totalArticles <= articlesPerPage) {
              $(".pagination").hide();
            }

            $.cookie("categoryTitle", "");

            $(".entry img").each(function() {
                if ($(this).attr("src") == "") {
                    $(this).attr("src", defaultArticleImage);
                }
            });

            $('.featured-articles img').each(function(){
            	if ($(this).attr('src') == '')
            		$(this).attr('src',defaultFeaturedArticleImage);
            });
            
            var featuredArticlesCount = $('.featured-articles li').length;
            $('.featured-articles').jcarousel({
                itemVisibleOutCallback: function(a,b,c,d,e) { if (c > featuredArticlesCount) a.remove(c); },
                scroll: 1,
                wrap: "circular",
                auto: 10,
                size: featuredArticlesCount
            });

            $(".pagination").pagination(totalArticles, { items_per_page: articlesPerPage,
                num_display_entries: 5,
                current_page: pageNumber - 1,
                num_edge_entries: 1,
                link_to: pageBaseUrl + "__id__",
                prev_text: prevText,
                next_text: nextText,
                ellipse_text: "...",
                prev_show_always: true,
                next_show_always: true,
                callback: function(page_index, jq) {  }
            });

            $("#" + sortBy).addClass("selected");

            postForm = new PipenoUI.SubmitForm("/", {});

            $("#menu-bar div.search input").bind("click", searchInputClicked);
            $('#menu-bar div.search input').bind("keypress", searchInputKeypress);
            $("#menu-bar div.search a").bind("click", menuSearchClicked);
            $(".panel-button").children().bind("click", panelButtonClickHandler);
            $("div.tab.container a.see-all").bind("click", { form: postForm }, seeAllButtonClicked);
        })();
    };
    /*#endregion*/
})(window);
/* End Of File */

