window.addEvent('domready',add_events);



function add_events()
{
    var to_hide = $$('.hide_when_js');
    to_hide.each( function(elmt){
        elmt.style.display="none";
    } );
    
    var to_display = $$('.hide_when_no_js');
    to_display.each( function(elmt){
        elmt.style.display="block";
    } );

    if($('article_id')) {
    	$('article_id').disabled;
    }
    
    if($('cart_add'))
    {
    	$('cart_add').addEvent( 'submit' , ajax_submit_cart.bindWithEvent($('cart_add')) );
    } 
    if( $('privatesale') ) {
    	$$('.cart_add').each( function(elmt){
    		$(elmt).addEvent( 'submit' , ajax_submit_cart.bindWithEvent($(elmt)) );
        });
    }
    
    //$('_article_id').addEvent( 'change' , update_article.bindWithEvent( $('_article_id') , $('_model_id').value ) );
    
    var switch_image = $$('.switch_image');
    switch_image.each( function(elmt){
        elmt.addEvent('click',changeImage.bindWithEvent(elmt))
    } );
}

function ajax_submit_cart(e)
{
	e.stop();
	if(this.stock_id.value!='')
    {
        var Expression = new RegExp("^((http://)?[^/]*)(.*)$");
        var old_action = this.action;
        var matches = Expression.exec(this.action);
        this.action = matches[1]+"/ajax"+matches[3];
        
        this.set('send', {
            onSuccess: update_minicart,
    		method: this.method,
    		url: this.action
        });
        this.send();                
        this.action = old_action;
        
        var html_msg = "Vous venez d'ajouter cet article à votre panier, souhaitez-vous :<br /><a href='/cart/' class='grownlButton monPanier'>Voir mon panier et commander</a>";
        
	     window.notimooManager.show({
	     	title: '',
	     	message: html_msg,
	     	width: 200, 
	        visibleTime: 2500,
	        sticky: true
	     });

        
      /*  myGrowl = Growl.Bezel({
            title: '',
            text: html_msg,
            duration: null
        });*/
    }
    else
    {
        window.notimooManager.show({
	     	title: '',
	     	message: I18N.Messages.ChooseSize,
	     	width: 200, 
	     	image: '/images/growl/ajax_loader_warning.gif',
	        visibleTime: 2500
	     });
    }
    
    return correctSubmitHandler(e);
}

function update_minicart()
{
	$request = new Request({
		url: '/ajax/cart/minicart/',
		method: 'get',
        onSuccess: function(html) {
			$('shopping_cart').set('html',html);
		},
        evalScripts: true
	}).send();
}

function filter_quantity(quantity)
{
    $('_quantity').empty();
    var html = "";
    var limit = 5;
    (quantity<5) ? limit = quantity : limit = 5;
    for(i=1;i<=limit;i++)
        html += "<option value='"+i+"'>"+i+"</option>";
    $('_quantity').innerHTML = html;
    
    if(quantity ==  0)
    {
        $('status_stock').innerHTML = "épuisé";
        return;
    }
    $('status_stock').innerHTML = quantity+" pièce(s)";
}

function update_article(e,model_id)
{
    container = $('detail_model');
    var article_id = $('_article_id').value;

	var $request = new Request({
		url: "/ajax/model/detail/"+model_id+"/?article_id="+article_id,
		method: 'get',
        update: container , 
        evalScripts: true,
		data: {'article_id': article_id},
		onComplete: add_events
	});
	$request.send();
    return false;   
}

function changeImage(e)
{
 
    var Expression = new RegExp("^thumb_img_([0-9]+)-?([0-9]+)$");
    var matches = Expression.exec(this.id);

    var visible_photo = $$('.visible_photo');
    visible_photo.each( function(elmt){
		var Expression2 = new RegExp("^big_img_([0-9]+)");
		var matches_model = Expression2.exec(elmt.id);
		if(matches_model[1]==matches[1]) {
			elmt.className = 'hidden_photo';
		}
    } );

    $('big_img_'+matches[1]+'-'+matches[2]).className = 'visible_photo';


    return correctSubmitHandler(e);
}

function removeTheme(event, theme_id, model_id) {
	new Request.HTML({update: 'admin_tag_div_id', method: 'post', url:'/theme/remove_model', data:'model_id='+model_id+'&theme_id='+theme_id}).send();
	return false;
}
     
function addTheme(event, theme_id, model_id) {
	new Request.HTML({update: 'admin_tag_div_id', method: 'post', url:'/theme/add_model', data:'model_id='+model_id+'&theme_id='+theme_id}).send();
	return false;
}

function removePrivatesale(event, privatesale_id, model_id) {
	new Request.HTML({update: 'admin_tag_div_id', method: 'post', url:'/privatesale/remove_model', data:'model_id='+model_id+'&privatesale_id='+privatesale_id}).send();
	return false;
}

function addPrivatesale(event, privatesale_id, model_id) {
	new Request.HTML({update: 'admin_tag_div_id', method: 'post', url:'/privatesale/add_model', data:'model_id='+model_id+'&privatesale_id='+privatesale_id}).send();
	return false;
}
