﻿

var defaultDuration = 5000; // indica in millisecondi il tempo di persistenza di un contenuto
var transactionDuration = 2000;
var currAgroPopup = 0;
var numAgroPopups;
var agroPopupContents;
var effect = "fade";

function showAgroPopup() {

    document.getElementById('agroPopup').style.display = 'block';
    document.getElementById('agroPopup').style.left = '50%';
    document.getElementById('agroPopup').style.marginLeft = '-300px';

    jQuery('#agroPopup').animate(
            {top: '30px'}
			, 1000
			, hideAgroPopupDelay
		);
}



function GetPopupItemDuration() {
    var duration = defaultDuration;  
    var currDuration = jQuery(agroPopupContents[currAgroPopup]).children("input[type='hidden'][id='popup_duration_" + currAgroPopup + "']").val();
    if (!isNaN(currDuration))
        duration = currDuration * 1000;
    return duration;
}
function showAgroPopupCall() {
    if (numAgroPopups > 1) {
        // 1 nascondo il contenuto corrente
        jQuery(agroPopupContents[currAgroPopup]).hide();
        // 2 incremento il contatore dei contenuti
        GetNextPopupContent();
        // 3 mostro il prossimo contenuto
        jQuery(agroPopupContents[currAgroPopup]).show();
        // 4 dissolvenza in entrata
        jQuery('.popupContentsContainer').fadeTo(transactionDuration, 1, hideAgroPopupDelay);
        
        /*
        jQuery('.popupContentsContainer').animate(
            { opacity: 1 }
			, transactionDuration
			, 'easeInOutQuint'
			, hideAgroPopupDelay
		);
		*/

        /*
        jQuery('#agroPopup').effect(
            effect
            ,{ }
			, transactionDuration
			, hideAgroPopupDelay
		);
		*/
    }
}

function hideAgroPopupDelay() {
    setTimeout(hideAgroPopup, GetPopupItemDuration());
}

function hideAgroPopup() {
    if (numAgroPopups > 1) {
        // dissolvenza in uscita
        jQuery('.popupContentsContainer').fadeTo(transactionDuration, 0, showAgroPopupCall);
        
        /*
        jQuery('.popupContentsContainer').animate(
            { opacity: 0 }
			, transactionDuration
			, 'easeInOutQuint'
			, showAgroPopupCall
		);
		*/

        /*
        jQuery('#agroPopup').effect(
            effect
            ,{  }
			, transactionDuration			
			, showAgroPopupCall
		);
		*/
    }
}

function GetNextPopupContent() {
    currAgroPopup = ((++currAgroPopup) % numAgroPopups);
}

function startPopupCustom() {
    jQuery(document).ready(function() {

        if (jQuery("#popup_effect").length > 0)
            effect = jQuery("#popup_effect").val();

        var popup_transaction_duration = jQuery("#popup_transaction_duration");
        if (popup_transaction_duration.length > 0 && !isNaN(popup_transaction_duration.val()))
            transactionDuration = parseInt(popup_transaction_duration.val());


        numAgroPopups = jQuery('div.popupContent').length;
        agroPopupContents = jQuery('div.popupContent');
        // Nascondo tutti i contenuti tranne il primo
        jQuery('div.popupContent:gt(0)').each(function() {
            $(this).hide();
        });

        if (numAgroPopups > 0) {
            showAgroPopup(currAgroPopup);
        }
    });
}

