﻿
var padelclickNews = function () {

    // carga las noticias
    this.loadData = function (url) {
        $.ajaxSetup({ cache: false });
        $.getJSON('newsfeed.aspx', { url: url }, function (newsPosts) {
            initNews(newsPosts);
        });
    }

    // inicializa la página con las noticias
    function initNews(newsPosts) {
        createNewsBody(newsPosts);

        $("#pikame").PikaChoose();

        $("#pikame").jcarousel({ scroll: 4,
            initCallback: function (carousel) {
                $(carousel.list).find('img').click(function () {
                    //console.log($(this).parents('.jcarousel-item').attr('jcarouselindex'));
                    carousel.scroll(parseInt($(this).parents('.jcarousel-item').attr('jcarouselindex')));
                });
            }
        });

        $("div#controller").jFlow({
            slides: "#slides",
            width: "960px",
            height: "390px",
            timer: 20000,
            duration: 800
        });

        $('.menu').superfish();
    }

    function getUrlValue(variable) {
        var query = window.location.search.substring(1);
        var vars = query.split("&");
        for (var i = 0, l = vars.length; i < l; i++) {
            var pair = vars[i].split("=");
            if (pair[0] == variable) {
                return pair[1];
            }
        }
    }

    // Aqui se crean dinámicamente los elementos html que necesitas.
    function createNewsBody(newsPosts) {
        var slides = document.getElementById("slides");
        var controller = document.getElementById("controller");

        var totalLength = newsPosts.length;

        var id = getUrlValue("id");

        var slidesLength = totalLength > 3 ? 3 : totalLength;

        // aqui crea los slides
        for (var i = 0, l = slidesLength; i < l; i++) {
            var post = newsPosts[i];
            if (id == null || id == post.id) {
                slides.appendChild(createPost(post));
                controller.appendChild(createPostController(i + 1));
            }
        }

        // aqui crea las cajitas
        if (totalLength > 3) {
            var homePage = document.getElementById("homepage");

            for (var i = 3, l = 15; i < l; i++) {
                homePage.appendChild(createPageBox(newsPosts[i]));
            }
        }
    }

    function createPageBox(post) {
        var container = document.createElement("div");
        container.setAttribute("class", "homepagebox");

        var h4 = document.createElement("h4");
        container.appendChild(h4);

        var link = document.createElement("a");
        link.setAttribute("href", "/noticias2.aspx?idPost=" + post.id);
        link.innerHTML = post.title;
        h4.appendChild(link);

        var imageLink = document.createElement("a");
        imageLink.setAttribute("href", "/noticias2.aspx?idPost=" + post.id);
        container.appendChild(imageLink);
		
        var image = document.createElement("img");
        image.setAttribute("title", "noticia");
        image.setAttribute("alt", post.title);
        image.setAttribute("src", post.imgUrl);
        image.setAttribute("class", "attachment-home-btm-thumbnail wp-post-image");
        image.setAttribute("width", "210px");
        image.setAttribute("height", "150px");
        imageLink.appendChild(image);

        return container;
    }

    // crea el cuerpo de una noticia.
    function createPost(post) {
        var postContainer = document.createElement("div");

        var imageLink = document.createElement("a");
        imageLink.setAttribute("href", "/noticias2.aspx?idPost=" + post.id);

        imageLink.rel = "bookmark";
        postContainer.appendChild(imageLink);

        if (post.imgUrl) {
            var image = document.createElement("img");
            image.setAttribute("title", "noticia");
            image.setAttribute("alt", "club");
            image.setAttribute("src", post.imgUrl);
            image.setAttribute("width", "540px");
            image.setAttribute("height", "360px");
            imageLink.appendChild(image);
        }

        var postBody = document.createElement("div");
        postBody.className = "bannercontent";
        postContainer.appendChild(postBody);

        if (post.title) {
            var title = document.createElement("h1");
            postBody.appendChild(title);
            var link = document.createElement("a");
            title.appendChild(link);
            link.href = "#";
            link.setAttribute("rel", "bookmark");
            link.innerHTML = post.title;
        }

        if (post.date) {
            var postDate = document.createElement("h4");
            postDate.innerHTML = "Noticia " + post.date;
            postBody.appendChild(postDate);
        }

        var postText = document.createElement("div");
        postBody.appendChild(postText);
        postText.innerHTML = post.body;

        return postContainer;
    }

    // crea el "controller" del pie
    function createPostController(postIndex) {
        var link = document.createElement("a");
        link.href = "#blank";

        var span = document.createElement("span");
        span.className = "jFlowControl";
        span.setAttribute("alt", "Noticia " + postIndex);
        link.appendChild(span);

        return link
    }
}
