var com		=	 {};

if(typeof com.mi						== 'undefined') com.mi = {};
if(typeof com.mi.console				== 'undefined') com.mi.console				= {};

com.mi.console = function(text){
  if(typeof console != 'undefined'){
	if(console.log){
	  console.log(text);
	}
  }
}

if(typeof com.mi.layout					== 'undefined') com.mi.layout				= {};
if(typeof com.mi.layout.disableEntrer	== 'undefined') com.mi.layout.disableEntrer = {};
if(typeof com.mi.layout.blinkMsg		== 'undefined') com.mi.layout.blinkMsg		= {};
if(typeof com.mi.layout.blinkElement	== 'undefined') com.mi.layout.blinkElement	= {};
if(typeof com.mi.layout.quantiteSansDec	== 'undefined') com.mi.layout.quantiteSansDec = {};
if(typeof com.mi.layout.addAllToBasket	== 'undefined') com.mi.layout.addAllToBasket = {};

com.mi.layout.addAllToBasket = function(){
  var $catalog = $j('.blockcatalsg:eq(0)');
  var $button = $j('<a />',{
	 href: '#'
	,id : 'BT_MI_AATB'
	,html : 'Ajouter les articles au panier'
	,click : function(e){
	  var val = {};
	  var $qte = $j('input.cdqte').each(function(){
		var $this = $j(this);

		if($this.val()>0){
		  //QTE_LS_ART_HAR836804
		  var id = $this.attr('id');
		  id = id.split('_')[3];
		  val[id] = $this.val();
		}

	  });

	  if(!$j.isEmptyObject(val)){
/*		val = $j.param(val);
		$j('<input />',{
		   id : 'BT_MI_AATB.x'
		  ,name : 'BT_MI_AATB.x'
		  ,type: 'text'
		  ,value: val
		}).appendTo($j(''));
*/
		$j(window).data('internal',true);
		__doPostBack('BT_MI_AATB');
	  }
	  else{
		alert('Il faut renseigner des quantités pour les articles que vous souhaitez commander');
		com.mi.layout.blinkElement($qte);
	  }

	  return false;
	}
  }).appendTo($catalog).wrap('<div id="BT_MI_AATB_WRAP"/>');


  $button.data('left',$button.css('left'));

  function fixButton(){
	if($j(window).scrollTop()>=top){
	  $button.parent().addClass('fixed');
	  var left = $catalog.offset().left + $catalog.outerWidth(true) + parseFloat($catalog.css('margin-left').replace(/auto/, 0));
	  $button.css('left',left);
	}
	else{
	  $button.parent().removeClass('fixed');
	  $button.css('left',$button.data('left'));
	}
  }

  var msie6 = ($j.browser.msie && parseInt($j.browser.version) < 7);

  if(!msie6){
	var top = $button.offset().top - parseFloat($button.css('margin-top').replace(/auto/, 0)) - 15;
	setInterval(fixButton,200);
    $j(window).scroll(fixButton);
  }
}

com.mi.layout.quantitSansDec = function(){
  $j('.cdqte').each(function(){
	var $this = $j(this);
	var pos = $this.val().indexOf(',');
//	console.log(pos);
	if(pos>0){
//	  console.log(pos);
	  $this.val($this.val().substr(0, pos));
	}
  });
}

com.mi.layout.blinkElement = function (el){
  $j(el).fadeOut(100).fadeIn(100).fadeOut(100).fadeIn(100).fadeOut(100).fadeIn(100);
}

com.mi.layout.disableEntrer = function(selection){
  if(typeof selection == 'undefined') selection = ':input:not(textarea,:submit,:image)';
  // empêcher l'action [enter] sur les champs
	$j(selection).keypress(function(e){
	  if(e.keyCode == 13){
		//$j(this).fadeOut(100).fadeIn(100).fadeOut(100).fadeIn(100).fadeOut(100).fadeIn(100)
		com.mi.layout.blinkElement(this);
		return false;
	  }
	});
}

com.mi.layout.blinkMsg = function(selection){

  if(typeof selection == 'undefined') return ;

  function blink(){
	$elems
	  .fadeOut(200).fadeIn(200)
	  .fadeOut(200).fadeIn(200)
	  .fadeOut(200).fadeIn(200)
	  .fadeOut(400).fadeIn(1000);
  }

  $elems = $j(selection);
  blink();
  setInterval(function(){
	blink();
  },1000*60*5);
}

if(typeof com.mi.uti					  == 'undefined') com.mi.uti					  = {};
if(typeof com.mi.uti.showAdvert			  == 'undefined') com.mi.uti.showAdvert			  = {};
if(typeof com.mi.uti.showModalAdvert	  == 'undefined') com.mi.uti.showModalAdvert	  = {};
if(typeof com.mi.uti.divaLink			  == 'undefined') com.mi.uti.divaLink			  = {};
if(typeof com.mi.uti.sauvegardeAuto		  == 'undefined') com.mi.uti.sauvegardeAuto		  = {};
if(typeof com.mi.uti.saveOnExit			  == 'undefined') com.mi.uti.saveOnExit			  = {};

com.mi.uti.saveOnExit = function(){
  $j(window).bind('beforeunload',function(){
	if($j(window).data('internal')!=true){
	  return "Votre panier contient des articles, ceux-ci seront perdus si vous quittez le site!"
	}
  });

  $j('a[href*=__doPostBack]').live('click',function(){
	$j(window).data('internal',true);
  });

}

com.mi.uti.sauvegardeAuto = function (interval){
  if(typeof interval == 'undefined' ||  interval == 0) return false;

  var to = setTimeout(function(){
	clearTimeout(to);
	__doPostBack('BT_MI_PVPA','');
  }, interval * 60 * 1000);
  return true;
}

com.mi.uti.divaLink =  function(){
  // On utilise lvie car dans le cas des retour ajax, les balises n'existent pas encore'
  $j('.diva-link').live('click',function(){
	var $this = $j(this);
	var href = $this.attr('href');
	__doPostBack(href,'');
	return false;
  });
}

com.mi.uti.showAdvert =  function(element,cookieName,href,options){
  if($j(element).length != 0){
	  if($j.cookie(cookieName)!=='true'){
		win = window.open(href,'',options);
		if(win) win.focus();
		$j.cookie(cookieName,"true");
	  }
  }
  else
	  $j.cookie(cookieName,"false");
}
com.mi.uti.showModalAdvert = function(element,cookieName,file,options){

    function closeModal(){
        $j('body').removeClass('show-advert');
        $j('#modal').removeClass('show');
    }

    if($j(element).length != 0 ){
        if($j.cookie(cookieName)!=='true'){
		var title= "Cliquez pour passer l'animation et continuer sur sabatiersas.com"
            $j(document.body).append($j('<div id="modal" title="'+title+'"><div class="modal-background"><h1>Chargement de l\'animation en cours...</h1></div><div class="modal-content"><div id="advert-container"></div><a href="http://www.sabatiersas.com" title="'+title+'" class="close-button">X</a><a class="close-text" href="http://www.sabatiersas.com" title="'+title+'">Passer</a></div></div>'))
            $j('body').addClass('show-advert');
            $j('#modal').addClass('show').click(closeModal);
            $j("#modal .modal-content a[class^='close']").click(function(){
                closeModal();
                return false;
            });

            var width = "100%";
            var height = "100%";

            if(typeof options == 'object' ){
                if(options.width)   width   = options.width;
                if(options.height)  height  = options.height;
            }

            var so = new SWFObject(file,file,width,height,8,"#FFFFFF");

            so.addParam("menu", "true");
            so.addParam("WMODE", "menu");
            so.write("advert-container");

            if(options){
                $j.each(options,function(key,value){
                    $j('#advert-container embed').attr(key, value);
                })
            }

        }

        $j.cookie(cookieName,"true");

    }
    else
        $j.cookie(cookieName,"false");
}

if(typeof com.mi.dialog						== 'undefined') com.mi.dialog					  = {};
if(typeof com.mi.dialog.settings			== 'undefined') com.mi.dialog.settings			  = {};
if(typeof com.mi.dialog.open				== 'undefined') com.mi.dialog.open				  = {};
if(typeof com.mi.dialog.panierMemorise		== 'undefined') com.mi.dialog.panierMemorise	  = {};
if(typeof com.mi.dialog.restaurerPanierAuto == 'undefined') com.mi.dialog.restaurerPanierAuto = {};

function getDialog(){
  var $dialog  = $j('#myDialog');

  if($dialog.length==0){
	$dialog = $j('<div />',{id: 'myDialog'})
	$j('#body').append($dialog);
  }
  return $dialog;
}

com.mi.dialog.settings = {
   height	  :	'auto'
  ,width	  :	500
  ,title	  :	'Titre'
  ,resizable  :	false
  ,buttons	  : {
	  "Fermer" :  function(un,deux,trois){
		getDialog().dialog("close");
	  }
  }
}

com.mi.dialog.open = function(url,options,data){
  var opt	= $j.extend({},com.mi.dialog.settings,options);
  var datas = $j.extend({},data);
 
  //console.log($j.makeArray(datas));

  var $dialog = getDialog();

  $j.ajax({
	url : url
	,success : function(response, status, xhr){
	  //$dialog.empty().html(response);
	  var $response  = $j(response);
	  $j('divalto-data',$response).each(function(){
		var $this = $j(this);
		var name  = $this.attr('name');
		try{
		  var tmp = $j('<span />',{text : eval('datas.'+name)});
		  $this.replaceWith(tmp);
		}catch(e){
//		  console.log(e);
		  $this.remove();
		}
	  });
	  $dialog.empty().html($response);
	  $dialog.dialog(opt);
	}
  });

//  $dialog.empty().load(url,function(){
//  }).dialog(opt);

}

com.mi.dialog.restaurerPanierAuto = function (data){

  var opt = {
	 title	 : 'Sauvegarde automatique'
	,buttons : {
	  "Annuler" : function(){
		__doPostBack('BT_MI_SUPP_'+data.ent_pino, '')
	  }
	  ,"Aller dans mes paniers" : function(){
		__doPostBack('BT_MI_PANP', '')
	  }
	  ,"Restaurer ce panier" : function(){
		__doPostBack('BT_MI_CHPP_'+data.ent_pino, '')
	  }
	}
  }
  
  com.mi.dialog.open('./design/__partial/restaurer-panier-memorise.html',opt,data);
}

com.mi.dialog.panierMemorise = function(){
  var opt = {
	 title	 : 'Panier mémorisé'
	,buttons : {
	  "Aller dans mes paniers" : function(){
		__doPostBack('BT_MI_PANP', '')
	  }
	}
  }
  com.mi.dialog.open('./design/__partial/panier-memorise.html',opt);
}


if(typeof com.mi.onLoad					== 'undefined') com.mi.onLoad = {};

com.mi.onLoad = function(){
  try{com.mi.layout.disableEntrer()}catch(e){};
  try{com.mi.layout.divaLink()}catch(e){};
  try{site.reglerAffichage()}catch(e){};
  try{com.mi.layout.quantitSansDec()}catch(e){};
  try{com.mi.uti.divaLink()}catch(e){};
  
  $j(window).data('internal',false);

  $j('form').submit(function(){
	$j(window).data('internal',true);
	return true;
  });

}


/**
 * En fin
 */


$j(document).bind('mi:dom:loaded',function(){
  com.mi.onLoad();
});

