/*
 * copyright (c) zitine.com 
 */

window.addEvent('domready', function() {
    if ($('custom-notice')) {
        if (Cookie.read('help_windows') == null) {
            $('custom-notice').show();
        }   
    }

    if ($('do_payment')) {
        $('do_payment').addEvent('click', function() {
            var exit = false;
        
            $$('.selsize').each(function(item) {
                if (item.selectedIndex == 0) {
                    exit = true;
                    item.setStyle('border', '1px solid red');
                } else {
                    item.setStyle('border', 'none');
                }
            });

            if (exit) {
                alert($('exit_size').get('text'));
                return;
            }
            
            if ($('cgu').checked != true) {
                alert($('exit_cgv').get('text'));

            } else {
                location.href = '/caddy.php?step=2';
            }
        }).setStyle('cursor', 'pointer');
    }

    $$('.undef').each(function(el) {
        el.setProperty('value', '');
    });

    $$('.shop_item_p').each(function(el) {
    /*
        xxx: bg or not
        var src = el.getProperty('rel');
        var old = el.getProperty('src');
        el.addEvent('mouseenter', function() {
            el.setProperty('src', src);    
        });
        el.addEvent('mouseleave', function() {
            el.setProperty('src', old);    
        });
    */    
    });

    if ($('shop_resize_h') && $('shop_user')) {
        $('shop_resize_h').setStyle('height', $('shop_user').getHeight().toInt() +'px');
    }
    if ($('shop_resize_h') && $('shop')) {
        $('shop_resize_h').setStyle('height', $('shop').getHeight().toInt() +'px');
    }
});

function load_infobulle(id, text, y, x, w, a) {
    el = new Element('div', { 'class': 'infobulle', 'id': 'inf_'+ id}).setStyles({
        'position': 'absolute',
        'z-index': 9999,
        'top': y,
        'left': x,
        'width': w,
        'background-image': 'url(/img/infobulle_'+ a +'.gif)',
        'background-position': a +' center',
        'background-repeat': 'no-repeat'
    }).set('html', text).inject($('custom_main'), 'top');

    if (a != 'left' && a != 'right') {  
        el.setStyle('padding-'+ a, '12px');
    } else {
        el.setStyle('padding-'+ a, '15px');
    }
}

function stop_help() {
    $$('.infobulle').each(function(el) {
        el.hide();
    });
    $('custom-notice-stop').hide();
    $('custom-notice').show();
}

function start_help(close_txt, click_txt) {
    $('custom-notice').hide();

    $('t1').focus();

    new Element('div', {'id':'custom-notice-stop', 'class': 'notice'}).setStyles({
        'margin-bottom': '10px' }).set('html', close_txt +' <a href="#" onclick="stop_help(); return false"'+
            ' class="classic"><strong>'+ click_txt +'</strong></a>').inject($('custom_main'), 'top');

}

function add(id) {
    var c = $('caddy_'+ id);
    
    new Request.JSON({url: '/_caddy_update.php', 
        onSuccess: function(json) {
            $('caddy_'+ json.id).getElement('.nb').set('text', json.nb);
            $('caddy_'+ json.id).getElement('.total').set('text', json.price);
            $('port').set('text', json.port_ttc);
            $('total_ttc').set('text', json.total_ttc);
            $('total_wport').set('text', json.items_ttc);
        }
    }).send('type=add_nb&id='+ id);
}

function del(id) {
    var c = $('caddy_'+ id);
    
    new Request.JSON({url: '/_caddy_update.php', 
        onSuccess: function(json) {
            $('caddy_'+ json.id).getElement('.nb').set('text', json.nb);
            $('caddy_'+ json.id).getElement('.total').set('text', json.price);
            $('port').set('text', json.port_ttc);
            $('total_ttc').set('text', json.total_ttc);
            $('total_wport').set('text', json.items_ttc);
        }
    }).send('type=del_nb&id='+ id);
}

function setsize(id) {
    var c = $('caddy_'+ id);
    
    new Request.JSON({url: '/_caddy_update.php'}).send('type=set_size&id='+ id +'&size='+ 
            c.getElement('.selsize').options[c.getElement('.selsize').selectedIndex].value.toInt());
}

var delivery = $A([]);

function delivery_fields(obj) {
    obj.addClass('hide');
    $('delivery_fields').removeClass('hide');
    $('delivery_fields_open').setProperty('value', 1);
}

function select_delivery() {
    var id = $('addr_delivery').options[$('addr_delivery').selectedIndex].value.toInt();
    delivery.each(function(item) {
        if (item.id != id) {
            return;
        }

        $('order_firstname').setProperty('value', item.firstname);
        $('order_lastname').setProperty('value', item.lastname);
        $('order_zipcode').setProperty('value', item.zipcode);
        $('order_city').setProperty('value', item.city);
        $('order_email').setProperty('value', item.email);
        $('order_phone').setProperty('value', item.phone);

        $('order_address').set('text', item.address);

        $A($('order_gender').options).each(function(opt, index) {
            if (opt.getProperty('value') == item.gender) {
                $('order_gender').selectedIndex = index;
            }
        });
        $A($('order_country').options).each(function(opt, index) {
            if (opt.getProperty('value') == item.country) {
                $('order_country').selectedIndex = index;
            }
        });
    });
}

function my_shop_upload_notice(notice) {
    $('upload_notice').set('text', notice).highlight('#0066cc'); 
}

function my_shop_upload_file_queue_error(file, code, msg) {
    my_shop_upload_notice('Erreur: le fichier est incorrect');    
}

function my_shop_upload_file_dialog_complete(selected, queued) {
    if (queued > 0) {
        this.startUpload();
    }
}

function my_shop_upload_progress(file, bytes) {
    percent = Math.ceil((bytes / file.size) * 100);

    if (percent == 100) {
        my_shop_upload_notice('Enregistrement, veuillez patienter ...'); 

    } else {
        my_shop_upload_notice('Téléchargement en cours... ('+ percent +'%)'); 
    }
}

function my_shop_upload_error(file, code, msg) {
    switch (code) {
    }
    my_shop_upload_notice('Erreur ('+ code +'): '+ msg);
}

var uploads = [];

function my_shop_upload_success(file, response) {
    json = JSON.decode(response);
    $('upload_notice').empty();

    uploads[uploads.length] = json;
}

function my_shop_upload_complete(file) {
    if (this.getStats().files_queued > 0) {
        this.startUpload();
        return;
    }

    var form = $('upload_form');

    form.removeClass('hide');
    
    for (var i = 0; i < uploads.length; i++) {
        new Element('option').set('html', uploads[i].name).inject($('files'));
        new Element('input', {'type':'hidden', 'name':'uploads[]', 'value':uploads[i].img}).inject(form);
    }

    uploads = [];
}


