var currentTweets = 0;
var hoveringTweets = false;

$(function(){
    // Open external links in new windows:
    $("a[rel=external]").click(function(){
        window.open($(this).attr("href"));
        return false;
    });

    /*
    $("#keywords, #search-arrow").bind("focus, click", function () {
        $(".switch").css("display", "block");
    });

    $(".switch .close").click(function () {
        $(".switch").css("display", "none");
    });

    $(".switch input[name=switch]").change(function () {
        if ($(this).val() == "site") {
            $("#keywords").attr("name", "keywords");
            $("#menu form").attr("action", "/search/");
        } else {
            $("#keywords").attr("name", "query");
            $("#menu form").attr("action", "/knowledge/");
        }
    });
    */

    initTweets();

    $(".twitter .tweets article p a").attr("target", "_blank");
});

function open_customer_portal() {
    //event.preventDefault();

    /*var cpwindow = window.open(this.href,"customer-portal","menubar=0,resizable=0,width=800,height=600");
    var left = ($(window).width() / 2) - 400;
    var top = ($(window).height() / 2) - 300;
    cpwindow.moveTo(left, top);*/

    if ($("#popup").length == 0) {
        $("<div id='black'></div>").appendTo($("body"));
        $("<div id='popup'><iframe id='customer_portal_frame' frameborder='0' allowtransparency='true'></iframe></div>")
            .appendTo($("body"));//<div id='close_btn'></div>
    } else {
        $("#popup").html("<iframe id='customer_portal_frame' frameborder='0' allowtransparency='true'></iframe>");
    }

    $("#black, #popup").show();
    $("#popup iframe").attr("src", "https://www.aquantia.com/products_CustomerPortal.php").css({
        width: "100%",
        height: "100%"
    });
    $("#popup").css({
        width: "800px",
        height: "600px",
        top: "50%",
        left: "50%",
        marginLeft: "-400px",
        marginTop: "-300px"
    });

    $("#close_btn").click(function(){
        close_iframe();
    });
    $("#black").unbind("click");
}

function close_iframe() {
    $("#black, #popup").hide();
    $("#popup iframe").attr("src", '');
}

function close_iframe_cust() {
    $("#black, #popup").hide();
    $("#popup iframe").attr("src", '');

    window.location = "http://www.aquantia.com";
}

function initTweets() {
    var elements = $(".twitter .tweets .slider article:lt(2)").clone();
    $(".twitter .tweets .slider").append(elements);
    showTweets(currentTweets);
    $(".twitter .tweets").hover(function () {
        hoveringTweets = true;
    }, function () {
        hoveringTweets = false;
    });
}

function showTweets(nr) {
    if (hoveringTweets == false) {
        currentTweets = nr;
        if (nr == 4) {
            $(".twitter .tweets .slider").stop().animate({
                marginTop: (nr * -230) + "px"
            }, 1000, function () {
                nr = 0;
                currentTweets = nr;
                $(".twitter .tweets .slider").css("margin-top", "0px");
            });
        } else {
            $(".twitter .tweets .slider").stop().animate({
                marginTop: (nr * -230) + "px"
            }, 1000);
        }
    }

    setTimeout('nextTweets()', 7000);
}

function nextTweets()
{
    showTweets(currentTweets + 1);
}
