function Zeep(){}
Zeep.CANVAS_WIDTH = 920;

Zeep.hideTimer = null;
Zeep.clientRows = [];
Zeep.animateInterval = null;
Zeep.animate = function(){
    Zeep.stopAnimation();
    Zeep.animateInterval = setInterval('Zeep.animateClientRows();', 3000);
}
Zeep.stopAnimation = function(){
    clearInterval(Zeep.animateInterval);
}
Zeep.animateClientRows = function(){
    if (Zeep.clientRows[0] != undefined) {
        Zeep.clientRows[0].animate({
            'top': '100px'
        });
        Zeep.clientRows[1].show();
        Zeep.clientRows[1].animate({
            'top': '0'
        });
        Zeep.clientRows[2].hide();
        Zeep.clientRows[2].animate({
            'top': '-100px'
        });
        Zeep.clientRows.push(Zeep.clientRows.shift());
    }
}

jQuery(document).ready(function($) {
	
    // apply css, store tip text, and remove title attributes from tooltippable items
    $('#govspro td h2[title]')
    .css({
        'borderBottom': '1px dotted #009FD7',
        'display': 'inline'
    })
    .qtip({
        style: {
            border: {
                width: 1
            },
            color: '#009FD7',
            fontSize: '12px',
            name: 'light'
        },
        position: {
            corner: {
                target: 'bottomMiddle',
                tooltip: 'topLeft'
            }
        }
    });

    // ajax submit for global unsub form
    $("#subscribe_form").submit(function() {
        $(this).ajaxSubmit({
            resetForm: true,
            dataType: "json",
            url: "/newsletter-submit/index.php",
            success: homeNewsletterSuccess
        });
        return false;
    });

    function homeNewsletterSuccess(responseText, statusText) {
        if (responseText.processed) {
            // replace the form with the success data
            $("#home_newsletter").css("display", "none");
            $("#home_newsletter_success").css("display", "block");
        }
    }

    $("#sidebar_subscribe_form").submit(function() {
        $(this).ajaxSubmit({
            resetForm: true,
            dataType: "json",
            url: "/newsletter-submit/index.php",
            success: sidebarNewsletterSuccess
        });
        return false;
    });

    function sidebarNewsletterSuccess(responseText, statusText) {
        if (responseText.processed) {
            // replace the form with the success data
            $("#sidebar_box").css("display", "none");
            $("#sidebar_box_success").css("display", "block");
        }
    }

    // ajax submit for global unsub form
    $("#unsubscribe").submit(function() {
        $(this).ajaxSubmit({
            resetForm: true,
            dataType: "json",
            url: "/unsubscribe-submit/index.php",
            success: unsubscribeSuccess
        });
        return false;
    });

    function unsubscribeSuccess(responseText, statusText) {
        if (responseText.processed) {
            // replace the form with the success data
            $("#unsubscribe").css("display", "none");
            $("#success-message").css("display", "block");
        }
    }


    // populate the email field of the complaint-abuse form
    if (typeof complaintData != "undefined") {
        // set the email field value
        $("#email").attr("value", complaintData.email);
        // create hidden inputs with the rest of the complaint data
        $("#complaint-abuse").append(easydom.input({
            "type":"hidden",
            "name":"clientId",
            "value":complaintData.clientId
        }));
        $("#complaint-abuse").append(easydom.input({
            "type":"hidden",
            "name":"listId",
            "value":complaintData.listId
        }));
        $("#complaint-abuse").append(easydom.input({
            "type":"hidden",
            "name":"campaignId",
            "value":complaintData.campaignId
        }));
        $("#complaint-abuse").append(easydom.input({
            "type":"hidden",
            "name":"subscriberId",
            "value":complaintData.subscriberId
        }));
        $("#complaint-abuse").append(easydom.input({
            "type":"hidden",
            "name":"scheduleId",
            "value":complaintData.scheduleId
        }));
    }

    // ajax submit for abuse form
    $("#complaint-abuse").submit(function() {
        $(this).ajaxSubmit({
            resetForm: true,
            dataType: "json",
            url: "/abuse-submit/index.php",
            success: complaintSuccess
        });
        return false;
    });

    function complaintSuccess(responseText, statusText) {
        if (responseText.processed) {
            // replace the form with the success data
            // $("#complaint-abuse").css("display", "none");
            $("#success-message").css("display", "block");
        }
    }
    
    // home page client scroll pop code
    $('.PopupListItem').each(function(){
        var targetPopupId = this.id + "_popup";
        var targetPopup = $("#" + targetPopupId);

        $(this).mouseover(function(){
            clearTimeout(Zeep.hideTimer);
            Zeep.stopAnimation();
            $(".PopupContainer").hide();
            var eventCenter = $(this).position().left + $(this).width() / 2 + parseInt($("#client_bar_list").css('padding-left'));

            // Position popup:
            var popupLeft = Math.min(Math.max(eventCenter - targetPopup.width() / 2, 0), Zeep.CANVAS_WIDTH - targetPopup.width());
            targetPopup.css({
                'left': popupLeft
            });
            // Position arrow
            targetPopup.find('.PopupArrow').css({
                'left': (eventCenter - 42) - popupLeft
            });

            targetPopup.show();
        });

        $(this).mouseout(function(){
            clearTimeout(Zeep.hideTimer);
            Zeep.hideTimer = setTimeout("jQuery(\".PopupContainer\").hide();", 1500);
            Zeep.animate();
        });

        targetPopup.mouseover(function(){
            Zeep.stopAnimation();
            clearTimeout(Zeep.hideTimer);
        });

        targetPopup.mouseout(function(){
            clearTimeout(Zeep.hideTimer);
            Zeep.animate();
            Zeep.hideTimer = setTimeout("jQuery(\".PopupContainer\").hide();", 200);
        });
    });

    // ClientRow Animation code
    $(".ClientRow").each(function(){
        $(this).hide();
        Zeep.clientRows.push($(this));
    });
    // back up the animation one so the first doesn't transition through
    Zeep.clientRows.unshift(Zeep.clientRows.pop());


    $("#client_bar").mouseover(function(){
        Zeep.stopAnimation();
    });
    $("#client_bar").mouseout(function(){
        Zeep.animate();
    });
    Zeep.animate();
    Zeep.animateClientRows();
});
