/* - ++resource++d9t.websitetheme.scripts/pfg-beautifier.js - */
jQuery(function() {
    if (jQuery(".formid-zimbra-network-edition-pro, .formid-sugarcrm-ce, .formid-cyn-in, .formid-telefonanlage, .formid-managed-vps-hosting, .formid-zope-plone-hosting, .formid-wsgi-hosting, .formid-php-hosting, .template-fg_thankspage_view_p3").length > 0) {
        /* change html */
        /* first mark the form that we have js (just for our css) */
        jQuery(".pfg-form").addClass("pfg-form-with-js")
        jQuery(".PFG-RichLabel").each(function() {
            title = jQuery(this).find("h2").first().text();
            if (!title) {
                title = "More";
            }
            /* add a open link before the labels */
            jQuery(this).before('<a class="pfglabelShow" href="#" title="Open">'+title+'</a>');
            /* add a close thing to the labels */
            jQuery(this).prepend('<a class="hide" href="#" title="Close">hide</a><div class="visualClear"></div>');
        });
        jQuery(".pfg-form .field > span > label").each(function(){
            /* let's create a nice table. */
            html = jQuery(this).html();
            html = "<table class='nicePriceTable'><tr><td class='first'>" + html + "</td></tr></table>";
            html = html.replace('#','</td><td>');
            jQuery(this).html(html);
        });
        /* and the same for the thank-you page */
        jQuery(".template-fg_thankspage_view_p3 #content > blockquote > dl > dd").each(function(){
            /* let's create a nice table. */
            html = jQuery(this).html();
            title = jQuery(this).prev().text();
            html = "<table class='nicePriceTable'><tr><th>" + title + "</th><td class='first'>" + html + "</td></tr></table>";
            html = html.replace('#','</td><td>');
            jQuery(".template-fg_thankspage_view_p3 #content > blockquote > dl").parent().append(html);
        });
        jQuery(".template-fg_thankspage_view_p3 #content > blockquote > dl").hide();

        /* Feature: Show "archetypes-fieldname-storage-teilen-mit-vps" only if option
         * storage=storage_1 is selected. */
        active = jQuery("#storage input[name=storage]:checked").attr("value");
        if (active != '0') {
            jQuery("#archetypes-fieldname-storage-teilen-mit-vps").hide();
        }
        jQuery("#storage > input").change(function(){
            active = jQuery("#storage input[name=storage]:checked").attr("value");
            if (active == '0') {
                jQuery("#archetypes-fieldname-storage-teilen-mit-vps").slideDown();
            } else {
                jQuery("#archetypes-fieldname-storage-teilen-mit-vps").slideUp();
            }
        });

        /* Feature: Show "archetypes-fieldname-domainname" only if option
         * managed-ssl-zertifikat-1=managed is selected. */
        active = jQuery("#managed-ssl-zertifikat-1 input[name=managed-ssl-zertifikat-1]:checked").attr("value");
        if (active != 'managed') {
            jQuery("#archetypes-fieldname-domainname").hide();
        }
        jQuery("#managed-ssl-zertifikat-1 > input").change(function(){
            active = jQuery("#managed-ssl-zertifikat-1 input[name=managed-ssl-zertifikat-1]:checked").attr("value");
            if (active == 'managed') {
                jQuery("#archetypes-fieldname-domainname").slideDown();
            } else {
                jQuery("#archetypes-fieldname-domainname").slideUp();
            }
        });

        /* Feature: Show "pfg-fieldsetname-bankverbindung" only if one of the options
         * zahlungsweise=(mb, hb, jb) is selected. */
        active = jQuery("#zahlungsweise input[name=zahlungsweise]:checked").attr("value");
        active_values = ['mb','hb','jb'];
        if (jQuery.inArray(active, active_values) == -1) {
            jQuery("#pfg-fieldsetname-bankverbindung").hide();
        }
        jQuery("#zahlungsweise > input").change(function(){
            active_values = ['mb','hb','jb'];
            active = jQuery("#zahlungsweise input[name=zahlungsweise]:checked").attr("value");
            if (jQuery.inArray(active, active_values) != -1) {
                jQuery("#pfg-fieldsetname-bankverbindung").slideDown();
            } else {
                jQuery("#pfg-fieldsetname-bankverbindung").slideUp();
            }
        });

        /* add events */
        jQuery(".pfglabelShow").live("click", function() {
            jQuery(this).next().slideDown();
            jQuery(this).slideUp()
            return false;
        });
        jQuery(".hide").live("click", function() {
            jQuery(this).parent().slideUp();
            jQuery(this).parent().prev().slideDown();
            return false;
        });

        /* ajax price calculation */
        updatePrice = function(){
            data = jQuery(".field input[type=radio], .field input[name$=_count]").serializeArray();
            jQuery.post("price", data, function(ret) {
                jQuery('.pfg-sum-price .price').html(ret);
                jQuery('.pfg-cart-price .price').html(ret);
            });
        };

        /* cart calculation */
        updateCart = function() {
            if (jQuery("#cart").length == 0) {
                jQuery("#pfg-fieldwrapper").append('<div id="cart"></div>');
            }
            jQuery("#cart").html('<h2>Ihre Auswahl</h2>');
            jQuery(".pfg-form input:radio:checked").each(function(){
                label = jQuery(this).next();
                html = label.html();
                jQuery("#cart").append(html);
            });
            jQuery("#cart").append('<div class="pfg-cart-price">pro Monat: <span class="currency">€</span><span class="price"></span></div>');
            jQuery("#cart").append('<div class="pfg-cart-price">+Einmalige Einrichtung: <span class="currency">€</span><span class="price"></span></div>');
        }
        jQuery(".field input[type=radio]").live("click", updateCart);
        jQuery(".field input[type=radio]").live("click", updatePrice);
        jQuery(".field input[name=count]").live("change", updatePrice);

        updateCart();
        updatePrice();
    }
});



