/*global jQuery*/
var initHomePage = function ($) {
        'use strict';

        var that = {},
            my = {};

        my.initRiquadro = function (riquadro) {
            var returnValue, margine, link, pad, margineRiquadriId;
            if (!riquadro) {
                return undefined;
            }
			if ($.browser.msie && $.browser.version == "7.0") {
				margineRiquadriId = '20px 0 -30px 0';
			} else {
				margineRiquadriId = '20px 0 30px 0';
			}
            returnValue = $('<div>').attr('id', 'riquadri').css({
                width: '1000px',
                margin: margineRiquadriId,
                'float': 'left',
                color: 'white',
                textAlign: 'center'
            });

            $.each(riquadro, function (index, item) {
                var newDiv;

                if (item.hidden) return;
                if (item.posizione == 1) {
                    margine = '0';
                } else {
                    margine = '16px';
                }
				link = $('<a>').attr('href', item.link).css({color: '#ffffff'});
				if (item.marginTop) {
					pad = item.marginTop;
				} else {
					pad = '25';
				}
                newDiv = $('<div>').css({
                    width: '238',
                    background: 'url(' + item.img + ')',
                    paddingTop: pad + 'px',
					fontSize: '18px',
					fontWeight: 'bold',
                    height: '100' - pad,
                    float: 'left',
                    marginLeft: margine
                });				
                newDiv.append(link.append(item.testo));                
                returnValue.append(newDiv);
            });
            return returnValue;
        };

        my.loadScroller = function () {
            $.getJSON('/docs/homepage/news2.json', function (data) {
                $('#pagina_index').append(my.initRiquadro(data.riquadro));
				my.data = data;
                my.initScroller(data.scroller, data.news);
                my.initContenuto(data.contenuto, data.pubblicita);
				my.startNextSpot();
            });
        };

		my.isTest = function () {
			return ("" + window.location).match(/.*test.*/);
		}

        my.initScroller = function (scrollerImg, news) {
            var returnValue, margine, ul, css, testo, divScrollNews, topValue;
            if (!scrollerImg) {
                return undefined;
            }
            divScrollNews = $('<div>').attr('id', 'scrollerNews').css({
                width: '1000px',
                height: '360',
                'float': 'left',
				marginBottom: '35px'
            });
            returnValue = $('<div>').attr('id', 'scroller').css({
                overflow: 'hidden',
                position: 'relative',
                visibility: 'hidden',
                'float': 'left',
                marginBottom: '35px',
                left: '-5000px'
            });
            ul = $('<ul>').width(1000).appendTo(returnValue);
            $.each(scrollerImg, function (index, item) {
			    if (item.hidden) {
					return;
				}
                testo = $('<div>').css({
                    width: '625px',
                    height: '100px',
					marginTop: '260px',
                    background: 'url(/images/homepage/sfondo_scritta_scroller.png)'
                }).
                append($('<div>').css({
                    color: 'white',
                    padding: '28px 0 0 14px',
                    fontSize: '19px'
                }).append(item.testo));
                ul.append($('<li>').css({
                    width: '625px',
                    background: 'url(' + item.img + ')',
                    height: '360'
                }).append(testo));
            });
            css = {
                display: 'block',
                width: 15,
                height: 32,
                position: 'relative'
            };
			if ($.browser.msie && $.browser.version == "7.0") {
				topValue = '190px';
			} else {
				topValue = '154px';
			}
            $('#pagina_index').append($("<a>").attr('href', '#').attr('id', 'jcPrev').css(css).css({
                left: '-25px',
                top: topValue
            }).append($("<img>").attr('src', '/images/homepage/prev.png').css({width: '15px', height: '32px'})));
            $('#pagina_index').append($("<a>").attr('href', '#').attr('id', 'jcNext').css(css).css({
                left: '635px',
                top: '154px'
            }).append($("<img>")
				.attr('src', '/images/homepage/next.png')
				.css({width: '15px', height: '32px'})));
            divScrollNews.append(returnValue);
            divScrollNews.append(my.initNews(news));
            $('#pagina_index').append(divScrollNews);
            returnValue.jCarouselLite({
                btnNext: "#jcNext",
                btnPrev: "#jcPrev",
                auto: 10000,
                speed: 800
            }).width(625);
        };

        my.initContenuto = function (contenuto, pubblicita) {
            var returnValue, myDiv, h3Css, pCss, bloccoContenuti;
            if (!contenuto) {
                return undefined;
            }
            returnValue = $('<div>').attr('id', 'contenuti').css({
                width: '1000px',
                marginBottom: '35px',
                'float': 'left'
            });

            bloccoContenuti = $('<div>').attr('id', 'bloccoContenuti').css({
                width: '625px',
                'float': 'left'
            });

            h3Css = {
                textTransform: 'uppercase',
                fontSize: '12px',
				marginTop: 0
            };
            pCss = {
                fontSize: '11px',
				textAlign: 'justify',
				lineHeight: '18px'
            };

            $.each(contenuto, function (index, item) {
                if (item.hidden) return;
                if (item.test && !my.isTest()) return;
                myDiv = $('<div>').css({
                    width: '625px',
                    minHeight: '120px',					
					color: '#666666',
					padding: '10px 0',
                    borderBottom: '1px dotted #828282'
                }).append($('<img>').css({
                    border: '1px solid #e6e6e6',
                    marginRight: '10px',
	                'float': 'left'
                }).attr('src', item.img)).append($('<h3>').css(h3Css).append(item.titolo)).append($('<p>').css(pCss).append(item.testo));
                bloccoContenuti.append(myDiv);
            });
            returnValue.append(bloccoContenuti);
            returnValue.append(my.initPubblicita(pubblicita));
            $('#pagina_index').append(returnValue);
        };

        my.initPubblicita = function (pubblicita) {
            var returnValue, item;
            if (!pubblicita) {
                return undefined;
            }
			item = my.data.pubblicita[my.indicePubblicita];
            returnValue = $('<div>').attr('id', 'pubblicita').css({
                width: '340px',
                height: '400px',				
                background: 'url(' + item.img +')',
                marginLeft: '35px',
                'float': 'left',
                textAlign: 'center'
            });			
            return returnValue;
        };
		
		my.startNextSpot = function() {
		if ($("#pubblicita div").size()) {
			$("#pubblicita div").fadeOut(function () {
				$(this).remove();
				my.startNextSpot();
			});
			return;
		}
		var item, div, p;
			item = my.data.pubblicita[my.indicePubblicita];
			if (item.hidden) return;
			$("#pubblicita").css({background: ' url(' + item.img + ')' });
			div = $('<div>').css({
				width: item.larghezza,
				height: item.altezza,
				'float':  'left',
                color: item.coloreText,
                margin: item.margine,
				background: item.colore + ' url(' + item.coloreUrl + ')'
            }).appendTo($("#pubblicita"));
			jQuery.cjTextFx({animation: 8, color: "#666666", animationType: "in", speed: 200, equenceDelay: 0});
			$.each(item.frasi[my.indiceFrase], function (index, f) {
				p = $("<p>").css({
				textAlign: item.allineamento,
				margin: '10px',
				fontSize: item.dimFont,
				lineHeight:  item.lineDim,
                position: 'relative'}).append(f)
				div.append(p);
			});
			div.delay(5000).fadeOut(function () {
				my.indiceFrase ++;				
				if (my.indiceFrase >= item.frasi.size()) {
					my.indicePubblicita ++;
					my.indiceFrase = 0;
					if (my.indicePubblicita >= my.data.pubblicita.size()) {
						my.indicePubblicita = 0;
					}
				}
				my.startNextSpot();
			});
			div.find("p").each(function (index) {				
				$(this).hide().delay(index * 25).fadeIn();
			});
		};

        my.initNews = function (news) {
            var returnValue, ul, li, altezzaLi , larghezzaText;
            if (!news) {
                return undefined;
            }
            returnValue = $('<div>').attr('id', 'news').css({
                width: '330px',
                height: '360px',
                'float': 'right'
            });

            ul = $("<ul>").attr('class', 'newsContainer');

            $.each(news, function (index, item) {
			    if (item.hidden) {
					return;
				}
				if (item.altezza) {
					altezzaLi =  item.altezza;
				} else {
					altezzaLi = '100%';
				}				
				
                li = $("<li>").css({
					listStyle: 'none',
					borderBottom: '1px solid #c1c1c1',
					width: '330px',
					height: altezzaLi,
					'float': 'left',
					background: 'url(' + item.img + ') no-repeat center right'});	

				if (!item.img) {
					larghezzaText =  '100%';
				} else {
					larghezzaText = '220px';
				}
				
				li.append($("<h5>").css({
					width: larghezzaText,
					margin: 0,
					fontSize: '12px'
                }).append(item.title));
				
                li.append($("<p>").css({
                    color: '#666666',
                    fontSize: '12px',
					width: larghezzaText,
					textAlign: 'left',
					textJustify: 'newspaper',
					lineHeight: 'normal',
					padding: '0' ,
					margin: '0'
                }).append(item.text));				
                ul.append(li);
            });

            returnValue.append(ul);

            returnValue.vTicker({
                speed: 500,
                pause: 10000,
                showItems: 3,
                animation: 'fade',
                mousePause: false,
                height: 360,
                direction: 'up'
            });

            return returnValue;
        };

        my.init = function () {
			my.indicePubblicita = Math.floor(Math.random()*3);
			my.indiceFrase = 0;
            my.loadScroller();
        };

        my.init();
        return that;
    };

jQuery(function () {
    'use strict';
    initHomePage(jQuery);
});
