﻿function Showcase_appendContent(divContent, mainDiv) {
	// var mainDiv = $('#showcaseLocation');
	if ($(mainDiv).length <= 0) {
		return;
	}

	$(mainDiv).html(divContent);
	// $(mainDiv).css('overflow-y', 'scroll');
   
	return mainDiv;
}

function Showcase_getContent_page(eventsUrl, element)
{
    $.ajax({
		type: 'GET',
		url: eventsUrl,
		success: function(data, status, xmlHttpRequest) {
		    Showcase_appendContent(data, element); 
		},
		error: function(xmlHttpResponse, status, error) {
			Showcase_appendContent('impossibile recuperare i dati al momento', element); 
		}
    });
}

function Showcase_getContent_jsonp(eventsUrl, element)
{

    $.ajax({
      url: eventsUrl+'&protocol=3&jsonp=?',
      dataType: 'json',
      jsonp:'jsonp',
      success: function(data){
                    var i;
                    var mainDiv = Showcase_appendContent('', element);
                    for (i=0; i<data.length; ++i)
                    {
                        var itemClass = (i == 0) ? 'showcaseItem showcaseFirst' : 'showcaseItem';
                        var textDiv = '';
                        if (data[i].text != undefined)
                        {
                            textDiv = '\<div class=\'showcasePreview\'>' + data[i].text + '</div>';
                        }
                        var row = '\<div onmouseover="$(this).addClass(\'showcaseHilight\')" onmouseout="$(this).removeClass(\'showcaseHilight\')" class="' + itemClass + '"\>\<a class="showcaseLink" href="'+data[i].linkurl+'"\>'+data[i].title+'\</a\>' + textDiv +'\</div\>';
                        $(mainDiv).append(row);
                    }
                },
       error: function()
       {
            debugger;
       }  
    });
}

$(document).ready(function(){
    var showDiv =  $('.showcaseLocation,#showcaseLocation');
    
    showDiv.each(function(index, el) {
        var element = $(el);
        if (element.attr('eventsUrl') != undefined)
        {
            Showcase_getContent_page(element.attr('eventsUrl'), element);
        }
        if (element.attr('xkUrl') != undefined)
        {
            Showcase_getContent_jsonp(element.attr('xkUrl'), element);
        }
        if (element.attr('goto') != undefined)
        {
            element.click(function(){
                window.location = element.attr('goto');
            });
        }
    });
});
