/* - - - - - - - - - - - - - - - - - - - - 
T E C H N O G Y M - H O M E
THE WELLNESS SYSTEM
http://www.technogym.com
website by websolute
- - - - - - - - - - - - - - - - - - - - */


/*--------------------------------------------------
showreel - linea
--------------------------------------------------*/
function showreelLinea() {
    var thumbs = $("#linea_thumbs .thumb").size();


    $(".thumb").wrapAll('<div class="wrap"><div class="scroll"></div></div>');

    if (thumbs != 6) {
        $("#linea_thumbs").append('<div class="arr_left_1"></div><div class="arr_right_1"></div>');
    }

    var i = 0;
    var e = thumbs - 6;
    $(".arr_right_1").live("click", function () {
        if (i < e) {
            i++;
            $(".scroll").animate({ "left": "-" + ($("#linea_thumbs .thumb").outerWidth() * i) + "px" }, 400);
        }
        return false;
    });
    $(".arr_left_1").live("click", function () {
        if (i > 0) {
            i--;
            $(".scroll").animate({ "left": "-" + ($("#linea_thumbs .thumb").outerWidth() * i) + "px" }, 400);
        }
        return false;
    });
}

/*--------------------------------------------------
why technogym slider
--------------------------------------------------*/
function showreelWhy() {
    var thumbs = $("#thumbs_why .thumb_why").size();
    var $thumb = $("#thumbs_why .thumb_why a");


    $(".thumb_why").wrapAll('<div class="wrap"><div class="scroll_why"></div></div>');
    $("#thumbs_why").append('<div class="arr_left_why"></div><div class="arr_right_why"></div>');
    var i = 0;
    var e = thumbs - 5;
    $(".arr_right_why").live("click", function () {
        if (i < e) {
            i++;
            $(".scroll_why").animate({ "left": "-" + ($("#thumbs_why .thumb_why").outerWidth() * i) + "px" }, 400);
        }
        return false;
    });
    $(".arr_left_why").live("click", function () {
        if (i > 0) {
            i--;
            $(".scroll_why").animate({ "left": "-" + ($("#thumbs_why .thumb_why").outerWidth() * i) + "px" }, 400);
        }
        return false;
    });

    $thumb.click(function () {
        $("#thumbs_why .thumb_why a.on").removeClass("on");
        $(this).addClass("on");
        ind = $thumb.index($(this));
        $('.why_technogym_home .abstract div').hide();
        $(".why_technogym_home .abstract div:eq(" + ind + ")").fadeIn(500);
        $("#viewport_why #scroller_why").animate({ "left": "-" + (ind * 500) + "px" }, 500, function () {
            $("#viewport_why .item").not($("#viewport_why .item:eq(" + ind + ")")).hide().show();
        });
        return false;
    });

    $thumb.eq(0).click();

    /*$(".arr_right_1").click(function() {
    $("#thumbs .thumb a.on").parent().next().find('a').click();
    return false;
    });
    $(".arr_left_1").click(function() {
    $("#thumbs .thumb a.on").parent().prev().find('a').click();
    return false;
    });*/

}


/*--------------------------------------------------
get last tweet
--------------------------------------------------*/
function getTweet() {

    var user = 'Technogym';

    $.getJSON('http://twitter.com/statuses/user_timeline.json?screen_name=' + user + '&count=1&trim_user=true&include_rts=true&callback=?', function (data) {

        var tweet = data[0].text;

        tweet = tweet.replace(/[A-Za-z]+:\/\/[A-Za-z0-9-_]+\.[A-Za-z0-9-_:%&;\?\/.=]+/g, function (url) {
            return '<a href="' + url + '">' + url + '</a>';
        }).replace(/B@([_a-z0-9]+)/ig, function (reply) {
            return reply.charAt(0) + '<a href="http://twitter.com/' + reply.substring(1) + '">' + reply.substring(1) + '</a>';
        });

        $("#last_tweet p").html(tweet);
    });


}


/*--------------------------------------------------
slide json
--------------------------------------------------*/
function slideJson() {
    // start slideshow 
    var obj = eval(json_data);
    var images = 0;
    for (_obj in obj) images++;
    var totalSlideCount = images;
    var captionStart = 1 + ' / ' + images;
    var eccezione_nazione = false;
    var inizializzazione = true;

    if (technogym_page.country.toLowerCase() == 'nl' || technogym_page.country.toLowerCase() == 'be') { eccezione_nazione = true; }

    $('.wid_right').html(captionStart);
    if (images > 1) {
        $('#background_home_img').append('<img src="' + json_data.documento1.immagine1.path + '"><img src="' + json_data.documento2.immagine1.path + '">');
    } else {
        $('#background_home_img').append('<img src="' + json_data.documento1.immagine1.path + '">');
    }

    /* modifiche per il benelux */
    if (!eccezione_nazione) {
        $('#background_home_img').cycle({
            fx: 'fade',
            speed: 700,
            timeout: 5000,
            next: '.widget_header .arr_right',
            prev: '.widget_header .arr_left',
            before: onBefore,
            after: onAfter
        });
    } else { /*cycle benelux */
        $('#background_home_img').cycle({
            fx: 'none',
            speed: 700,
            timeout: 3000,
            next: '.widget_header .arr_right',
            prev: '.widget_header .arr_left',
            before: onBefore,
            after: onAfter
        });
        /* --------- */
    }

    function onBefore(curr, next, opts) {
        if (!opts.addSlide)
            return;
        var currentImageNum = $('#background_home_img img').size();
        if (currentImageNum == totalSlideCount) {
            opts.addSlide = null;
            return;
        }
        var pathDoc = json_data["documento" + (opts.currSlide + 3)];
        var nuovoPath = pathDoc.immagine1.path;
        opts.addSlide('<img src="' + nuovoPath + '" />');
    };

    function onAfter(curr, next, opts) {
        /* gestione slider benelux */
        if (!inizializzazione && totalSlideCount == 2) {
            if (opts.currSlide == 0) { opts.currSlide = 1; }
            else { opts.currSlide = 0; }
        }

        var caption = (opts.currSlide + 1) + ' / ' + images;
        var currDoc = json_data["documento" + (opts.currSlide + 1)];
        var titolo1 = currDoc.titolo;
        var descrizione = currDoc.descrizione;
        var urlimage = currDoc.url;
        var payoff = currDoc.payoff;

        $('.wid_right').html(caption);
        $('.widget_header .heading').html('<a href="' + urlimage + '">' + titolo1 + '</a>').fadeIn(10);
        $('.widget_header .learn_more').attr("href", urlimage);
        $('.widget_header .wid_left').text(descrizione);

        if (payoff === undefined) {
            $('.widget_header .heading').css({ 'font-size': '27px', 'line-height': '1.2' });
            $('.widget_header .heading div').remove();
        } else {
            $('.widget_header .heading').append('<div>' + payoff + '</div>');
            $('.widget_header .heading').css({ 'font-size': '16px', 'line-height': '1.2' });
        };

        /* modifica per il benelux */
        if (eccezione_nazione) {
            $('.heading').children('a').css('color', '#FFF797').css('font-weight', 'bold').css('font-size', '16px');
            $('.heading').children('div').css('font-weight', 'bold').css('font-size', '13px');

            switch ($('.wid_right').text()) {
                case '1 / 2':
                    $('a.eccezione_benelux').text('naar de shop');
                    break;
                case '2 / 2':
                    $('a.eccezione_benelux').text('');
                    break;
                default:
                    break;
            }
        }

        inizializzazione = false;
        /* -------------- */
    }

};




/*--------------------------------------------------
init
--------------------------------------------------*/
$(document).ready(function () {

    if (location.search.indexOf('social=false') > 0)
        $.cookie('social', 'false', { expires: 365, path: '/' })

    if (location.search.indexOf('social=true') > 0)
        $.cookie('social', '', { expires: 0, path: '/' })

    if ($.cookie('social') == 'false')
        $("#likebox-frame").remove();
    else
        getTweet();

    showreelLinea();
    showreelWhy();
    //getTweet()
   // slideJson();
});
