﻿/*! Copyright © 2010 General Mills. All Rights Reserved. No permission is granted to use, copy or extend this code. */
QRV.namespace('QRV.FeaturedContent');
QRV.FeaturedContent =
{
    url: '/handlers/FeaturedContentHandler.ashx',

    init: function ()
    {
        $(document).ready(function ()
        {
            QRV.FeaturedContent.btnVerMas = $('.verMas');
            QRV.FeaturedContent.fc = $('.featured-content-wrapper');
            QRV.FeaturedContent.selectorWrapper = $('.featured-content-selectors');
            // $.get(QRV.FeaturedContent.url, { action: 'getData' }, QRV.FeaturedContent.getContent_callback, 'json');
            $.ajax({
                url: QRV.FeaturedContent.url,
                data: { action: 'getData' },
                cache: true,
                success: QRV.FeaturedContent.getContent_callback,
                type: "GET",
                datatype: "json"
            });
        });
    },

    getContent_callback: function (results)
    {
        if (results != null && results.length > 0)
        {
            for (var i = 0; i < results.length; i++)
            {
                var data = results[i];
                var div = $(document.createElement('div')).addClass('featured-content-thumb');
                var span = $(document.createElement('span')).text(data.title);
                var img = $(document.createElement('img')).attr('alt', data.title);
                img.attr('src', QRV.FeaturedContent.url + '?action=getThumb&id=' + data.id)
                img[0].data = data;
                div.append(span);
                div.append(img);

                QRV.FeaturedContent.selectorWrapper.append(div);
            }

            QRV.FeaturedContent.selectors = QRV.FeaturedContent.selectorWrapper.children('div');
            QRV.FeaturedContent.images = QRV.FeaturedContent.selectors.find('img');
            QRV.FeaturedContent.images.click(QRV.FeaturedContent.setSelection);
            QRV.FeaturedContent.images.first().click();
        }
    },

    setSelection: function (e)
    {
        var img = $(this);
        var data = this.data;
        var id = data.id;
        var url = data.url;
        var top = data.top;
        var left = data.left;
        var cm = data.colorMatrix.split(',');

        if (url != null)
        {
            //change href for button click location
            QRV.FeaturedContent.btnVerMas.attr('href', url);
            QRV.FeaturedContent.btnVerMas.parent().css({ 'top': top, 'left': left });
        }

        //change CSS and images...
        QRV.FeaturedContent.fc.css('background-image', 'url(' + QRV.FeaturedContent.url + '?action=getMain&id=' + id + ')');
        QRV.FeaturedContent.selectors.removeClass('thumb_selected');
        img.parent().addClass('thumb_selected');

        QRV.FeaturedContent.selectors.each(function (idx, elm)
        {
            var li = $(this);
            var color = cm[idx];
            li.children().css('color', color);
            if (li.hasClass('thumb_selected'))
            {
                li.children('img').css('border-color', color);
            }
            else
            {
                li.children('img').css('border-color', 'transparent');
            }
        });
    }
}
