Array.prototype.is_empty=function() {
    for (var i in this) {
        if (this[i] > 0) return false;
    }
    return true;
}

Array.prototype.has=function(val) {
    for (var i in this) {
        if (this[i] == val) return true;
    }
    return false;
}

var cur_category = -1;
var acc1_slider_on = false; 
var acc2_slider_on = false; 
var acc3_slider_on = false; 

$(document).ready(function() {
    if ($("#showDialogProperty").length > 0) {
        $("#editPageProperty").dialog({
            width:450,
            height:150,
            modal: true,
            //resizable: false,
            title: "Page\'s property",
            autoOpen: false,
            buttons: {
                "Save": function() {
                        form = $(this).children("form");
                        form.validate({
                            rules: {
                                title: {
                                    required: true
                                }
                            },
                            errorPlacement: function(error, element) {
                                var er = element.attr("name");
                                error.appendTo(element.parent().parent().find("td.myerrors"));
                            }
                        });
                        if (form.validate().form()) {
                            var queryData = form.formSerialize();
                            $.post(form.attr("action"), queryData, function(data) {
                                if (data == "error") {
                                    alert ("Error while saving the data! Please, call to Support.");
                                } else if(data == "page_error") {
                                    alert ("Page does not exist.");
                                } else if(data == "url_error"){
                                    alert("Current url already exist.");
                                } else {
                                    alert("Changes will take effect in 1-2 minutes.");
                                    $("#editPageProperty").dialog("close");
                                }
                            });
                        }
                    },
                "Close": function() {
                    $(this).dialog("close");
                }
            }
        });
        $("#showDialogProperty").click(function(){
            $("#editPageProperty").dialog("open");
        });

    }


    $('#left-kontekt div').cycle({
        delay: 1000,
        speed: 2000,
        pause: 1,
        fx: 'fade',
        before: function() {
            var desc = $(this).find('img').attr('alt');
            var content = '<a href="' + $(this).attr('href') + '" class="yellow-block-in">' + desc + '</a>';
            $("#yellow-block").html(content)
        }
    });

    if ($('#left-kontekt div a').length == 1) {
        var desc = $('#left-kontekt div img').attr('alt');
        var content = '<a href="' + $('#left-kontekt div a').attr('href') + '" class="yellow-block-in">' + desc + '</a>';
        $("#yellow-block").html(content)
    }

    $('#project-index').css('display', 'block');

    // accessory

    $('#block_white').click(function(){  
        $('.popap-list-prod-container').hide('fold');
        $('#block_white').hide();
    });

    $('.list-model-single').hover(
        function(){ 
            var acc = $(this).find('img');
            acc.attr('src', '/static/img/' + acc.attr('alt') + '_act.jpg');
        }, 
        function(){ 
            var acc = $(this).find('img');
            acc.attr('src', '/static/img/' + acc.attr('alt') + '.jpg');
        }    
    );

    $('#accesory-handrail').css('cursor', 'pointer').click(function(obj){
        cur_category = 0;
        $('.popap-list-prod-container').hide();
        $('#handrail-accessory-list').css('top', $('#model-block').position().top)
        $('#handrail-accessory-list').css('left', $('#model-block').position().left - $('#handrail-accessory-list').width())
        $('#block_white').show();
        $('#handrail-accessory-list').show();
        if (!acc1_slider_on) {
            $('.acc-slider').hoverscroll();
            acc1_slider_on = true;
        }
    });

    $('#accesory-railing').css('cursor', 'pointer').click(function(obj){
        cur_category = 1;
        $('#block_white').show();
        $('.popap-list-prod-container').hide();
        $('#railing-accessory-list').css('top', $('#model-block').position().top);
        $('#railing-accessory-list').css('left', $('#model-block').position().left - $('#railing-accessory-list').width());
        $('#railing-accessory-list').show();
        if (!acc2_slider_on) {
            $('.acc-slider').hoverscroll();
            acc2_slider_on = true;
        }
    });

    $('#accesory-floor').css('cursor', 'pointer').click(function(obj){
        cur_category = 2;
        $('#block_white').show();
        $('.popap-list-prod-container').hide();
        $('#floor-accessory-list').css('top', $('#model-block').position().top);
        $('#floor-accessory-list').css('left', $('#model-block').position().left - $('#floor-accessory-list').width());
        $('#floor-accessory-list').show();
        if (!acc3_slider_on) {
            $('.acc-slider').hoverscroll();
            acc3_slider_on = true;
        }
    });

    $('.close-accessory').click(function(){
        $('#block_white').hide();        
        $('.popap-list-prod-container').hide('fold');
    });

    $('#accept-accessory').click(function(obj) {
        var id = parseInt($(this).attr('value'));
        if ($(this).attr('checked')) {
            selected_accessory[cur_category] = id;
        } else {
            selected_accessory[cur_category] = 0;
        }
    });

    // toggle effect for accessory
 
    $('.scroll-left').hover(
        function(){ $(this).attr('src', '/static/img/popap-left-act.png') }, 
        function(){ $(this).attr('src', '/static/img/popap-left.png') }
    );

    $('.scroll-right').hover(
        function(){ $(this).attr('src', '/static/img/popap-right-act.png') }, 
        function(){ $(this).attr('src', '/static/img/popap-right.png') }
    );    

    // menu hover effect
    
    // preload menu hover effect 
    var mh1 = $('<img />').attr('src', '/static/img/menu-forsiden_act.jpg');
    var mh2 = $('<img />').attr('src', '/static/img/menu-showroom_act.jpg');
    var mh6 = $('<img />').attr('src', '/static/img/menu-produktion_act.jpg');
    var mh3 = $('<img />').attr('src', '/static/img/menu-vores_act.jpg');
    var mh4 = $('<img />').attr('src', '/static/img/menu-igang_act.jpg');
    var mh5 = $('<img />').attr('src', '/static/img/menu-kontakt_act.jpg');

    $('#menu-forsiden').hover(
        function(){ $(this).attr('src', mh1.attr('src')) }, 
        function(){ $(this).attr('src', '/static/img/menu-forsiden.jpg') }
    );

    $('#menu-vores').hover(
        function(){ $(this).attr('src', mh3.attr('src')) }, 
        function(){ $(this).attr('src', '/static/img/menu-vores.jpg') }
    );

    $('#menu-showroom').hover(
        function(){ $(this).attr('src', mh2.attr('src')) }, 
        function(){ $(this).attr('src', '/static/img/menu-showroom.jpg') }
    );

    $('#menu-production').hover(
        function(){ $(this).attr('src', mh6.attr('src')) },
        function(){ $(this).attr('src', '/static/img/menu-produktion.jpg') }
    );

    $('#menu-igang').hover(
        function(){ $(this).attr('src', mh4.attr('src')) }, 
        function(){ $(this).attr('src', '/static/img/menu-igang.jpg') }
    );

    $('#menu-kontakt').hover(
        function(){ $(this).attr('src', mh5.attr('src')) }, 
        function(){ $(this).attr('src', '/static/img/menu-kontakt.jpg') }
    );

    // *** Contact us {

    $('.popup-contact-close').click(function() {
        $('.popap-contact').hide();
    });

    $('.show-economy').click(function() {
        $('#popup-economy').show();
    });

    $('.show-promoters').click(function() {
        $('#popup-promoters').show();
    });

    $('.show-admin').click(function() {
        $('#popup-admin').show();
    });

    $('.show-seller').click(function() {
        $('#popup-seller').show();
    });

    $('.show-ingenior').click(function() {
        $('#popup-ingenior').show();
    });
    //#show-economy:hover, #show-promoters:hover, #show-vendors:hover    

    // *** } Contact us 

    // reference
    
    $('#btn-reference').hover(
        function(){ 
            var btn = $(this).find('img')
            btn.attr('src', '/static/img/btn-reference-hover.jpg') 
        }, 
        function(){ 
            var btn = $(this).find('img')
            btn.attr('src', '/static/img/btn-reference.jpg') 
        }    
    );


});

function gourl(url) {
    window.location.href = url;
}

function view_big_img(img_url){
    $('#product-bigimg').attr('src', img_url);
}

function close_accessory_detail() {
    $('#block_white').hide();
    $('#popap-list').hide();
}

function show_accessory_detail(obj) {
    var img = $(obj);
    $('#accessory-rubric').text(img.attr('rubric'));
    $('#accessory-subrubric').text(img.attr('subrubric'));
    $('#accept-accessory').attr('value', img.attr('id'));
    
    $('#accept-accessory').attr('checked', false);
    cur_val = parseInt($('#accept-accessory').attr('value'));

    if (selected_accessory.has(cur_val) == true) {
        $('#accept-accessory').attr('checked', true);
    }

    $('#accessory-img').empty();
    $('#accessory-img').append("<img src='"+img.attr('src')+"' alt='"+img.attr('alt')+"' width='320px' height='216px' />");

    $('#popap-list').css('top', $('#model-block').position().top)
    $('#popap-list').css('left', $('#model-block').position().left - $('#popap-list').width())
    $('#popap-list').show();
}

function save_accessory() {
    $.ajax({
        type: "POST",
        url: "/product/save_accessories/",
        data: 'accessories='+selected_accessory.join(',')
    });
    $('.popap-list-prod-container').hide();
    close_accessory_detail();
}



