﻿
var activeId = -1;
var count;
var twitterCount = 0;
var account;
var itervalID;

var itemIDs = ['Canada-1', 'Morocco', 'Indonesia-1', 'Turkey', 'Canada-2', 'Mexico', 'Columbia-1', 'Canada-3', 'Indonesia-2', 'Tunisia', 'Columbia-2', 'China-1'];
//var accounts = ['Morocco', 'Tunisia', 'Turkey', 'Mexico', 'Canada', 'Columbia', 'Indonesia'];


$(document).ready(function () {

    count = itemIDs.length;


    var config = {
        over: showMap, // function = onMouseOver callback (REQUIRED)    
        timeout: 500, // number = milliseconds delay before onMouseOut    
        out: activateAnimation // function = onMouseOut callback (REQUIRED)    
    };

    $(".MapDiv").hoverIntent(config);


    $(".retweet a").css("display", "none");
    $(".cli-home-item").hide();

    updateMap();
    activateAnimation();
    //var itervalID = setInterval("updateMap()", 5000);


    function showMap() {
        var mapIndex = 0;
        var map = $(this).attr("map");
        map = map.split("#");
        if (map.length > 1) {
            mapIndex = random(map.length);
        }
        $("#" + itemIDs[activeId]).hide(1500);
        $("#" + map[mapIndex]).show(1500);
        clearInterval(itervalID);
        for (var i = 0; i < itemIDs.length; i++) {
            if (itemIDs[i] == map[mapIndex]) {
                activeId = i - 1;
            }
        }
        updateMap();
        var Timeout = setTimeout(activateAnimation, 5000);
    }

    function random(interval) {
        var index = Math.floor(Math.random() * interval)
        //alert(index);
        return index;
    }


    function activateAnimation() {
        clearInterval(itervalID)
        itervalID = setInterval(updateMap, 5000);
    }

    $("#cli-language-menu").mouseover(function () {
        $(this).css("cursor", "pointer");
        $(this).css("overflow", "visible");
    });

    $("#cli-language-menu").mouseout(function () {
        $(this).css("cursor", "default");
        $(this).css("overflow", "hidden");
    });



    $(".Cli_Select").mouseover(function () {
        $(this).css("cursor", "pointer");
        $(this).css("overflow", "visible");
    });

    $(".Cli_Select").mouseout(function () {
        $(this).css("cursor", "default");
        $(this).css("overflow", "hidden");
    });




});

$(window).resize(function () {
    $(".twitter-box").removeShadow();
    $(".twitter-box").dropShadow({ "left": 2, "top": 2, "opacity": 0.3 });
});


function updateMap() {

    var item;
    if ((activeId + 1) % count == 0) {
        activeId = 0;
        $("#" + itemIDs[count - 1]).hide(1500);


        item = $("#" + itemIDs[activeId]);
        $(item).show(1500);



    } else {
        activeId++;
        $("#" + itemIDs[activeId - 1]).hide(1500);
        item = $("#" + itemIDs[activeId]);
        $(item).show(1500);
    }


    account = $(item).find(".twitter-account").attr('value');
    if (account == undefined) {
        account = $(item).find(".weibo-account").attr('value');
        var WeiboUserUrl = "http://weibo.com/" + account;
        $(item).find(".user").html("<a href='" + WeiboUserUrl + "'>@" + account);
        showWeibo()
    }
    else {
        var twitterUserUrl = "http://twitter.com/" + account;
        $(item).find(".user").html("<a href='" + twitterUserUrl + "'>@" + account);

        if (twitterCount < count) {

            $.ajax({
                type: 'GET',
                url: "/server/twitter.ashx?account=" + account,
                success: showTweet
            });
            twitterCount++;
        }

    }

    $("#" + itemIDs[activeId]).find(".twitter-box").removeShadow();
    $("#" + itemIDs[activeId]).find(".twitter-box").dropShadow({ "left": 2, "top": 2, "opacity": 0.3 });

}

function showWeibo() {
    var LastPost = $("[id$=_WeiboLastPost]").val();
    $("#" + itemIDs[activeId]).find(".tweet").html(LastPost);
}

function showTweet(tweet) {

    if (tweet.length > 0) {
        var str = tweet.split("[split]");
        var retweetString = str[0];
        var displayString = str[1];

        $("#" + itemIDs[activeId]).find(".tweet").html(displayString);

        var twitterUrl = "http://twitter.com/share?" + retweetString + "&via=" + account;

        $("#" + itemIDs[activeId]).find(".retweet a").attr('href', twitterUrl);
        $("#" + itemIDs[activeId]).find(".retweet a").css("display", "block");
    } else {
        $("#" + itemIDs[activeId]).find(".retweet a").css("display", "none");
    }

    $("#" + itemIDs[activeId]).find(".twitter-box").removeShadow();
    $("#" + itemIDs[activeId]).find(".twitter-box").dropShadow({ "left": 2, "top": 2, "opacity": 0.3 });

}


