var curvyCornersVerbose = false;

$(document).ready(function(){

    /** Javascript is working, so we remove nojs class from body tags */
    $('body').removeClass('nojs');



    /*****
     * Multistep form for giftadd
     ***/

    // First disable the fields - needed to enable validation step by step
    // Only needed for old formwizard script 1.0.1
    //$('.wizardon .formwizard').attr('disabled',true);


    // Then call the formwizard
    $("#giftadd.wizardon").formwizard({
        //form wizard settings
        textSubmit : 'Create Gift Page',
        historyEnabled : true,
        formPluginEnabled: false,
        validationEnabled : true
    },

    {
        //validation settings
        debug: false,
        submitHandler: function(form) {
            // do other stuff for a valid form
            $('.formwizard').attr('disabled',false);
            form.submit();
        },
        invalidHandler: function(form, validator) {
            var errors = validator.numberOfInvalids();
            if (errors) {
                var message = errors == 1
                ? 'You missed 1 field. It has been highlighted'
                : 'You missed ' + errors + ' fields. They have been highlighted';
            //IS
            //$("div.formerror span").html(message);
            // $("div.formerror").show();
            } else {
        // $("div.formerror").hide();
        }
        },
        showErrors: function(errorMap, errorList) {
            var errors = this.numberOfInvalids();
            var message = errors == 1
            ? 'There is 1 error, which is highlighted below.'
            : 'There are ' + errors + ' errors. They have been highlighted';
            $("div.message error").html(message);
            this.defaultShowErrors();
        },
        errorContainer: ".formerror"

    },
    {
    // form plugin settings
    }

    );

    /** Frontpage Slideshow **/
    $('#slideshow').cycle({
        fx: 'fade',
        delay:  2000,
        speed:  500,
        before: onBefore
    });

    function onBefore() {
        $('#slidetitle')
        .html(this.alt);
    }




    $('.clicktip-content').hide();



    /** New datepicker script **/
    $("#closingdate").datepick({
        useThemeRoller: true,
        showAnim: 'slideDown',
        defaultDate: '+1M',
        mandatory: true,
        yearRange: 'c:c+3',
        minDate: '+1',
        closeAtTop: false
       
    });

/** New tooltips
 *  http://flowplayer.org/tools/tooltip.html#configuration
 **/
    // select all desired input fields and attach tooltips to them
    $("#giftadd .formtip, #register .formtip").tooltip({

        // place tooltip on the right edge
        position: "center right",

        // a little tweaking of the position
        offset: [-2, 10],

        // use the built-in fadeIn/fadeOut effect
        effect: "fade",

        // custom opacity setting
        opacity: 0.9,

        // use this single tooltip element
        tip: '.tooltip'

    }).dynamic({});
    $("#contributionform .formtip, #contributionform .clicktip").tooltip({

        // place tooltip on the right edge
        position: "top center",

        // a little tweaking of the position
        offset: [-2, 10],

        // use the built-in fadeIn/fadeOut effect
        effect: "fade",

        // custom opacity setting
        opacity: 0.9,

        // use this single tooltip element
        tip: '.tooltip'

    }).dynamic({});

    /** Flash Notice - enable close button */
    $("#FlashNotice a.close").click( function(){
        $("#FlashNotice").slideUp("slow", function() {
            $("#FlashNotice").remove();
        });
        return false;
    });

    /** Image loading script for gift creation **/

    /** Check the valuse of the URL field **/
    function getUrl()
    {
        if ($("#url").val()) {
            $('#content-ajax span').hide();
            return true;
        } else {
            $('#content-ajax span').show();
            return false;
        }
    }

    /** Get images from given URL **/
    function getImage()
    {
        $("#content-ajax").append("<img src='/images/ajax-loader.gif', 'alt' = 'Please wait...' />");
        if (getUrl()) {
            jQuery.post("/ajax/url_images/", {
                'url' : $("#url").val(),
                'productname' : $("#productname").val()
            }, function(data){
                if (data !='') {
                    $("#content-ajax").html(data);
                } else {
                    $('#content-ajax img').hide(); $('#content-ajax span').show();
                }
                //$("#content-ajax").html(data);
                $("#content-ajax img").click(function(){
                    $("input[name='src_select']").setValue($(this).attr("id"));
                    //IS jquery.field.js needed to read the hidden input.
                    //alert($("input[name='src_select']").getValue());
                    //$("#src_select").val($(this).attr("id"));
                    $("#content-ajax img").removeClass("active");
                    $(this).addClass("active");
                });
            });
        }
    }

    /** Check if URL field has value **/
    if (getUrl()) {
        getImage();
    }

    /** Retrive images from url on URL field click **/
    $("#url").blur(function(){
        getImage();
    });

    $("#price").blur(function(){
        var val = $("#price").val();
        $.getJSON("/ajax/paypal_fee/" + val, function(data) {
            $('[name=charge]').val(data.processing_fee);
            $('[name=total]').val(data.estimated_fee);
        });
    });


    // We removed tabs for now because jquery ui library buggy
    //$("#imagetabs").tabs();
    $("ul.tabs").tabs("div.tabpanes > div.tabpane");

}); // End of doc ready