var wears = {
    params: {},
    flash: null
};


var load_flash = function(){
    if(!wears.flash){
        wears.flash = $("#design_movie").get(0);
        /*
        if(navigator.appName.indexOf("Microsoft") != -1){
            // wears.flash = window.design_movie;
            wears.flash = $("#design_movie").get(0);
        } else {
            // wears.flash = window.document.design_movie;
            wears.flash = $("#design_movie").get(0);
        }
        */
    }
}

var price_refresh = function(){
    $("#total-price").load("/wears/design/price/");
};

var flash_refresh = function(){
    load_flash();

    if(wears.flash !== null && typeof wears.flash != "undefined" && wears.flash.design_refresh){
        wears.flash.design_refresh();
    }
    /* swfobject.embedSWF("/media/flash/design.swf", "design_movie", "520", "215", "9.0.0", "/media/flash/design.swf"); */
};

var _refresh = function(){
    price_refresh();
    flash_refresh();
};

var _parameters = function(value){
    if(value.indexOf("?") != -1){
        return value.split("?")[1];
    }
    return value;
};

$(function(){
    $("#front-edition > ul.tabs, #back-edition > ul.tabs, #sex > ul.tabs").livequery(function(){
        if(typeof $(this).tabs != "undefined"){
            $(this).tabs();
        }
    });

    $(".wear-link").livequery(function(){
        $(this).click(function(e){
            e.preventDefault();
            var href = _parameters($(this).attr("href"));
            wears.params.wear = href;
            $("#available-sizes-container").load("/wears/design/available-sizes/?" + href);
            $.get("/wears/design/wear/?" + href, function(data){
                _refresh();
            });
        });
    });

    $(".front-stamp-link").livequery(function(){
        $(this).click(function(e){
            e.preventDefault();
            var href = _parameters($(this).attr("href"));
            wears.params.front_stamp = href;
            if($(this).hasClass("text-stamp")){
                $.get("/wears/design/front-stamp/?" + href, function(data){
                    _refresh();
                });
            } else {
                $.get("/wears/design/front-stamp/?" + href, function(data){
                    _refresh();
                });
            }
        });
    });

    $(".back-stamp-link").livequery(function(){
        $(this).click(function(e){
            e.preventDefault();
            var href = _parameters($(this).attr("href"));
            wears.params.back_stamp = href;
            $.get("/wears/design/back-stamp/?" + href, function(data){
                _refresh();
            });
        });
    });

    $("form.iw-stamps").livequery(function(){
        var url = "/wears/design/front-user-stamp/";

        if($(this).attr("id") == "back-iw-stamps"){
            url = "/wears/design/back-user-stamp/";
        }

        $(this).bind('reset', function() {
            $(this).ajaxSubmit({ url: url + "?reset=1", success: _refresh });
            return false;
        });
    });

    $(".user-stamps form").livequery(function(){
        var parent = $(this).parent();
        var url = "/wears/design/front-user-stamp/";

        if(parent.parent().attr("id") == "back-user-stamps"){
            url = "/wears/design/back-user-stamp/";
        }

        $(this).bind('submit', function() {
            //$(this).ajaxSubmit({ url: url, target: $(parent.get(0)), success: _refresh });
            if(navigator.appName.indexOf("Microsoft") != -1){
                $(this).ajaxSubmit({ url: url, success: _refresh });
            } else {
                $(this).ajaxSubmit({ url: url, target: $(parent.get(0)), success: _refresh });
            }
            return false;
        });

        $(this).bind('reset', function() {
            $(this).ajaxSubmit({ url: url + "?reset=1", success: _refresh });
            return false;
        });
    });

    $(".text-stamp form").livequery(function(){
        var parent = $(this).parent();
        var url = "/wears/design/front-user-text/";

        if(parent.parent().attr("id") == "back-text-stamp"){
            url = "/wears/design/back-user-text/";
        }

        $(this).bind('submit', function() {
            $(this).ajaxSubmit({ url: url, target: $(parent.get(0)), success: _refresh });
            return false;
        });

        $(this).bind('reset', function() {
            $(this).ajaxSubmit({ url: url + "?reset=1", success: _refresh });
            return false;
        });
    });

    $(".position-form-submit").hide();

    $(".position-form").livequery(function(){
        $(this).bind('submit', function() {
            $(this).ajaxSubmit();
            return false;
        });

        $("select", $(this)).change(function(){
            var value = $("option:selected", $(this)).attr("value");
            var select = $(this);
            var url = "/wears/design/front-position/";

            if($(this).attr("name") == "front_stamp_position") {
                wears.params.front_position = "front_stamp_position=" + value;
            } else {
                wears.params.back_position = "back_stamp_position=" + value;
                url = "/wears/design/front-position/";
            }

            $(this).parent("form").ajaxSubmit({
                url: url,
                success: function(data, status){
                    var parent;
                    for(parent = select.parent(); parent; parent = parent.parent()){
                        if(parent.hasClass("edition")){
                            break;
                        }
                    }

                    $("select option[value=" + value +"]", parent).each(function(){
                        if(!$(this).attr("selected")){
                            $(this).attr("selected", true);
                        }
                    });
                    _refresh();
                }
            });
        });
    });

    $(".category-form-submit").hide();

    $(".category-front-form").livequery(function(){
        $("select", $(this)).change(function(){
            var value = $("option:selected", $(this)).attr("value");
            $("#front-stamps-images").load("/wears/design/stampcategory/" + value + "/stamps/?type=front");
        });
    });

    $(".category-back-form").livequery(function(){
        $("select", $(this)).change(function(){
            var value = $("option:selected", $(this)).attr("value");
            $("#back-stamps-images").load("/wears/design/stampcategory/" + value + "/stamps/?type=back");
        });
    });

    /* *** */
    /* Buy forms */

    $("#buy form.buy-first").bind('submit', function() {
        var params = Array();
        for(var p in wears.params) {
            if(p){
                params.push(wears.params[p].replace(/^\?/, ""));
            }
        }
        document.location = "/wears/buy/?" + params.join("&");
        return false;
    });

    $("#id_2-same_as_user").livequery(function(){
        $(this).click(function(){
            var step = "2", prev_step = "1";
            var inputs = [ "first_name",
                           "last_name",
                           "email_address",
                           "city",
                           "address",
                           "postal_code",
                           "phone_number" ];
            var selects = [ "country" ];
            var i, field, prev, option;

            if($(this).attr("checked")){
                for(i in inputs){
                    if(i){
                        field = $("#id_" + step + "-" + inputs[i]);
                        prev = $("#id_" + prev_step + "-" + inputs[i]);

                        if(field.length == 1 && prev.length == 1){
                            field.attr("value", prev.attr("value"));
                        }
                    }
                }

                for(i in selects){
                    if(i){
                        field = $("#id_" + step + "-" + selects[i]);
                        prev = $("#id_" + prev_step + "-" + selects[i]);

                        if(field.length == 1 && prev.length == 1){
                            option = $("option[value='" + prev.attr("value") + "']", field);
                            if(option.length > 0){
                                option.attr("selected", true);
                            }
                        }
                    }
                }
            } else {
                for(i in inputs){
                    if(i){
                        field = $("#id_" + step + "-" + inputs[i]);
                        if(field.length == 1){
                            field.attr("value", "");
                        }
                    }
                }

                for(i in selects){
                    if(i){
                        field = $("#id_" + step + "-" + selects[i]);

                        if(field.length == 1){
                            option = $("option:first", field);
                            if(option.length > 0) {
                                option.attr("selected", true);
                            }
                        }
                    }
                }
            }
        });
    });

    $("#id_2-user_will_pickup").livequery(function(){
        $(this).click(function(){
            var step = "2", i;
            var inputs = [ "city",
                           "address",
                           "postal_code" ];
            var selects = [ "country" ];

            for(i in inputs){
                if(i){
                    if($(this).attr("checked")){
                        $("#id_" + step + "-" + inputs[i]).attr("disabled", true).parent("div.field-block").fadeOut("normal");
                    } else {
                        $("#id_" + step + "-" + inputs[i]).attr("disabled", false).parent("div.field-block").fadeIn("normal");
                    }
                }
            }

            for(i in selects){
                if(i){
                    if($(this).attr("checked")){
                        $("#id_" + step + "-" + selects[i]).attr("disabled", true).parent("div.field-block").fadeOut("normal");
                    } else {
                        $("#id_" + step + "-" + selects[i]).attr("disabled", false).parent("div.field-block").fadeIn("normal");
                    }
                }
            }
        });
    });

    $(".payment-method").livequery(function(){
        $(this).hide();
    });

    $("#id_3-payment_method").livequery(function(){
        $(this).change(function(){
            var value = $("option:selected", $(this)).attr("value");
            $(".payment-method.enabled").hide();
            $("#" + value).toggleClass("enabled").fadeIn("normal");
        });
    });

    /*
    $(".wear-link-first").livequery(function(){
        $(this).click();
    });
    */
});
