$(document).bind("ready", function() {

if($("#opt_type").length > 0){
    if ($("#opt_type").val() == '2'){
        $("#sec_selector").change(function(){
            opt_id = $("#sec_selector").val();
            $(".primary_selector").each(function(){
                $(this).attr("name", '');
                $(this).hide();
                });
            $("#primary_selector_"+opt_id).show();
            $("#primary_selector_"+opt_id).attr("name", 'primary_selector');
            });
        }
 $('#single_product_form').validate({
            rules: {
                qty:{
                    required:true,
                    number:true,
                    max: function () {
                        return parseInt($("select[name='primary_selector'] option:selected").attr('qty'));
                        }
                }
            },
            messages: {},
            errorPlacement: function(error, element) {}
        });

    }

    
    $("#print_page").click(function() {

    var display='toolbar=0,location=0,directories=1,menubar=1,scrollbars=1,resizable=1,width=650,height=650,left=100,top=25';
    var docPrint = window.open('/basket/print_page/'+ $('#order_id').val() +'/','printerFriendly',display);

        return false;
    });

    $("#cat_list_submit").click(function(){
        var cat_id = $("#cat_list_selector").val();
        var cat = $("#cat_list_selector option:selected").attr("fragmentURL");
        window.location = "/products/Seogeresultat/" + cat + "/" + cat_id + "/";
    });


    if( $("#clientDataFormId").length>0 ){
        $('#showAlternativeForm').click(function(){
           if($('#alternative_data').css("display") == 'none'){
                $('#alternative_data').css("display","block");
                $('#alt_bet-header').css("display","block");
           } else {
                $('#alternative_data').css("display","none");
                $('#alt_bet-header').css("display","none");
                $('#id_alt_name').val('');
                $('#alt_name_error').html('');
                $('#id_alt_address').val('');
                $('#alt_address_error').html('');
                $('#id_alt_post').val('');
                $('#alt_post_error').html('');
                $('#id_alt_city').val('');
                $('#alt_city_error').html('');
                $('#id_alt_phone').val('');
                $('#alt_phone_error').html('');
                $('#id_alt_email').val('');
                $('#alt_email_error').html('');
           }
        });
        $('#clientDataFormId').validate({
            rules: {
                name:{required:true},
                address:{required:true},
//                post:{required:true,number:true},
                post:{required:true},
                city:{required:true},
//                phone:{required:true,number:true},
                phone:{required:true},
                email:{required:true,email:true},
                //comment:{required:true},
                alt_name:{required:function(element){if($('#alternative_data').css("display") == 'none'){return false;} else {return true;}}},
                alt_address:{required:function(element){if($('#alternative_data').css("display") == 'none'){return false;} else {return true;}}},
                alt_city:{required:function(element){if($('#alternative_data').css("display") == 'none'){return false;} else {return true;}}},
                alt_post:{required:function(element){if($('#alternative_data').css("display") == 'none'){return false;} else {return true;}},number:true},
                alt_phone:{required:function(element){if($('#alternative_data').css("display") == 'none'){return false;} else {return true;}},number:true},
                alt_email:{required:function(element){if($('#alternative_data').css("display") == 'none'){return false;} else {return true;}},email:true}
            },
            messages: {
                name:{required: "Indtast for- og efternavn"},
                address:{required: "Indtast Adresse"},
                post:{required: "Indtast Post nr", number:"Enter a valid post code"},
                city:{required: "Enter your City"},
                phone:{required: "Indtast telefonnummer", number:"Enter a valid phone number"},
                email:{required: "Indtast e-mail adresse", email:"Enter a valid email address"},
                comment:{required:"Indtast kommentar"},
                alt_name:{required: "Indtast for- og efternavn"},
                alt_address:{required: "Indtast Adresse"},
                alt_city:{required: "Enter your City"},
                alt_post:{required: "Indtast Post nr",number:"Enter a valid post code"},
                alt_phone:{required: "Indtast telefonnummer",number:"Enter a valid phone number"},
                alt_email:{required: "Indtast e-mail adresse",email:"Enter a valid email address"}
            },
            errorPlacement: function(error, element) {
                var er = element.attr("name")+"_error";
                //if( element.attr("name") == "city") {
                //    $( '#post_error' ).append(error);
                //}
                //else {
                    $( '#'+er ).append(error);
                //}
            },
            success: function(label) {
                label.addClass("checked");
            }
        });
    }
    /****************************
     * delivering method's
     */
    $( "#leverings_id" ).change( function(){
        $( "#leverings_form_id" ).submit();
    });
    /****************************
     * "Send product link" interface initializing
     */
    $("#sltf_form").dialog({
        autoOpen: false,
        resizable: false,
        width: 360,
        buttons: {
            "Send": function () {
                sltf_productid = $("#sltf_form [name=sltf_productid]").attr("value"),
                sltf_productlink = $("#sltf_form [name=sltf_productlink]").attr("value");
                sltf_toemail = $("#sltf_form [name=sltf_toemail]").attr("value"),
                sltf_name = $("#sltf_form [name=sltf_name]").attr("value"),
                sltf_fromemail = $("#sltf_form [name=sltf_fromemail]").attr("value"),
                sltf_comments = $("#sltf_form [name=sltf_comments]").attr("value")
                var send_data = {
                    sltf_productid:sltf_productid,
                    sltf_productlink:sltf_productlink,
                    sltf_toemail:sltf_toemail,
                    sltf_name:sltf_name,
                    sltf_fromemail:sltf_fromemail,
                    sltf_comments:sltf_comments
                };
                $.post($("#sltf_form [name=sltf_action_link]").attr("value"), send_data, function(response) {
                    if (response == "Sent") {
                        $("#sltf_form").dialog("close");
                    } else {
                        alert(response);
                    }
                });
            },
            "Annuler": function () {
                $("#sltf_form").dialog("close");
            }
        }
    });
    $("#product-center .send-till-end").click(function(e) {
        $("#sltf_form [name=sltf_productid]").val($(this).attr("id"));
        $("#sltf_form [name=sltf_productlink]").val($(this).find("input").attr("value"));
        $("#sltf_form [name=sltf_toemail]").val("");
        $("#sltf_form [name=sltf_name]").val("");
        $("#sltf_form [name=sltf_fromemail]").val("");
        $("#sltf_form [name=sltf_comments]").val("");
        $("#sltf_form").dialog("open");
    });

    /* replaced by mine from daekThreeCheck() */
    //sdmitry preloading images with jquery
    jQuery.preloadImages = function()
    {
        for(var i = 0; i<arguments.length; i++)
        {
            jQuery("<img>").attr("src", arguments[i]);
        }
    }
    $.preloadImages(
        "/static/Images/images/ok_act.jpg",
        "/static/Images/images/slet_act.jpg",
        "/static/Images/images/op-var_act.jpg",
        "/static/Images/images/gat_act.jpg",
        "/static/Images/images/legikurv_act.jpg",
        "/static/Images/images/prisLaediKurv_act.jpg",
        "/static/Images/images/prisSendTilEnVen_act.jpg",
        "/static/Images/images/send-til-ven-act.jpg",
        "/static/Images/images/prod-detaljer-act.jpg"
    );

     $(".act1").imghover({suffix: '_act'});
     $(".act2").imghover({suffix: '_act'});
     $(".act3").imghover({suffix: '_act'});
     $(".act4").imghover({suffix: '_act'});
     $(".act_slet1").imghover({suffix: '_act'});
     $(".act_slet2").imghover({suffix: '_act'});
     $(".act_slet3").imghover({suffix: '_act'});
     $(".act_op-var").imghover({suffix: '_act'});
     $(".act_gat").imghover({suffix: '_act'});


try
{
    //Run some code here

$().piroBox({
    my_speed: 300, //animation speed
    bg_alpha: 0.5, //background opacity
    radius: 4, //caption rounded corner
    scrollImage : false, // true == image follows the page _|_ false == image remains in the same open position
    // in some cases of very large images or long description could be useful.
    slideShow : 'true', // true == slideshow on, false == slideshow off
    slideSpeed : 3, //slideshow
    pirobox_next : 'piro_next', // Nav buttons -> piro_next == inside piroBox , piro_next_out == outside piroBox
    pirobox_prev : 'piro_prev', // Nav buttons -> piro_prev == inside piroBox , piro_prev_out == outside piroBox
    close_all : '.piro_close' // add class .piro_overlay(with comma)if you want overlay click close piroBox
    });
    }
    catch(err)
    {
        //Handle errors here
        }


});

