function $_(o) {
	return (typeof o=='string') ? o=document.getElementById(o) : o;
}
function truebody(){
	return (!window.opera && document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body;
}
function subsearch(){
    var v= jQuery.trim(document.quick_find.keywords.value);
    if(v && v!='--Rechercher un article--' && v.length>1){
       _href='http://www.mistersmoke.com/'+v+'-c-5.html';
       document.location.href = _href;
       return false;
    }
    return true;
}

function inputclean(_this, defval){
    if(_this.value == defval){ _this.value = '';}
    else if(_this.value == ''){ _this.value = defval;}
    return;
}
function _search(defval){
  var v= jQuery.trim(document.quick_find.keywords.value);
  if(v && v!=defval && v.length>1){
    return true;
  }
  return false;
}
function isValidEmailAddress(emailAddress) {
var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
return pattern.test(emailAddress);
}

var obj = null;
function checkHover() {
	if (obj) {
		obj.find('.menu').css({display:'none'});
		$_('content').style.zIndex = '0';	
	} 
}
$(document).ready(function() {
	$('#Nav > li').hover(function() {
		if (obj) {
			obj.find('.menu').css({display:'none'});
			$_('content').style.zIndex = '0';
			obj = null;
		}
		$_('content').style.zIndex = '-1';
		$(this).find('.menu').css({display:'block'});
	}, function() {
		obj = $(this);
		setTimeout(
			"checkHover()",
			0);
	});
	
	if($("#slider").length){
	  $("#slider").easySlider({
		auto: false,
		continuous: true,
		prevId:	'prevBtn',
		nextId:	'nextBtn',	
		controlsShow: false  
	  });
	}
	
	$('#fnews').submit(function() {
		_mail = $.trim( $('#mail').val());
		if(!_mail || !isValidEmailAddress(_mail)) return false;
		_url = $(this).attr('action')+'&mail='+_mail;
		$.ajax({
  		  url: _url,
  		  success: function(data) {  
			$('#inscription_txt').html('Votre inscription est <b>validée !</b>');
			$('.inscription_txt').val('Votre inscription est validée !');
			check_news = '<img src="http://tre.emv3.com/D2UTF8?emv_tag=96A810A46202096A&emv_ref=EdX7CqkdEmWv8SA9MOPQNk7eLj1yHa2y_j7fe6s2UcaoKSU&EMAIL_FIELD='+ _mail +'&SOURCE_FIELD=onsite" style="width:1px;height:1px;"/>';
			$('#check_news').html( check_news);
		  }
  		});
		return false;
	});
	
	if(window.location.hash){
		_hash = window.location.hash;
		//_hash = _hash.substring(1, 16);
		if(_hash == 'utm_source=fm28' || _hash.indexOf('fm28')>=0){
			var date = new Date();
			date.setTime(date.getTime()+(30*86400000));
			var expires = "; expires="+date.toGMTString();
			document.cookie = "__utm_source_fm28="+(date.getTime()/1000)+expires+"; path=/";
		}
	}
	
});


function ContentResultList(Content, Debut, Fin){
   var x = Content.indexOf(Debut) + Debut.length;
   var y = Content.lastIndexOf(Fin);
   return Content.slice(x, y);
}

function quant(_add){
  _val = parseInt(document.getElementById('iquant').value);
  if(!(_add<0 && _val<2)){
   _val+=_add;
   document.getElementById('iquant').value = _val;
  }
  return false;
}

var poscart = false;
var returncart = '';
function addtocart_success(data){
  
  var _ncart = ContentResultList(data, '<!--//ncart://-->', '<!--//end ncart;//-->');
  var _total = ContentResultList(data, '<!--//total://-->', '<!--//end total;//-->');
  
  if(!_total){
     document.location.href = returncart;
	 return false;
  }
  _val = 1;
  _interval = 1800;
  if(document.getElementById('iquant')){
    _val = parseInt(document.getElementById('iquant').value);
    _interval = 1400;
  }
  div_addcart = '<div id="addcart">+'+_val+'</div>';
  $("#main").after(div_addcart);
  
  _plus = 0;
  _moins = 0;
  
  _w = $(poscart).width();
  if(_w > 24) _plus = ($(poscart).width()/2);
  
  
  var productX        = $(poscart).offset().left + _plus  + 12;
  var productY        = $(poscart).offset().top - 12;
  
  if( $(window).scrollTop() > $(window).height() ) _moins = ($(window).scrollTop() - $(window).height());
  
  var cartX         = $("#totalcart").offset().left - 20;
  var cartY         = $("#totalcart").offset().top + 12 + _moins;
  var gotoX         = cartX - productX;
  var gotoY         = cartY - productY;
  
  $('#addcart')
	.show()
	.css({'position' : 'absolute','z-index':9999,'left':productX,'top':productY})
	.animate({ marginLeft: gotoX, marginTop: gotoY}, _interval, function() {
	  $(this).remove();
	  $("#totalcart").html(_ncart);
	  doBlink("#blinkstr",1,3);
  });	  
}  
  
function addtocart(data){
  
  if(!data['url'] && data['id']){
    var _id = parseInt( data['id']);
    var _model = parseInt( data['model']);
    var _quant = parseInt( data['quant']);
    var _oscsid = '';
    if(data['oscsid']) _oscsid = "&osCsid="+_form.osCsid.value;
    if(_id && _quant>0){
        data['url'] = 'shopping_cart.php?action=buy_now&products_id='+_id+'&qty='+_quant+'&opt='+_model+'&ajaxboxes=1'+_oscsid+'&rand='+Math.random();
    }
    returncart = 'product_info.php?products_id='+_id+'&opt='+_model;
  
  }else{
    returncart = $(poscart).attr('rel');
  }

  if(data['url']){
    _url = data['url'];	
    
	$.ajax({
        type: "GET",
        url: _url,
        cache: false,
        error:function(msg){
          document.location.href = _url.replace('&ajaxboxes=1','&ajaxboxes=0');;
        },
        success: addtocart_success
    });    
  }  
}

$(function(){	
  
  $('a[href=#main]').click( function() {
			$('html, body').animate({scrollTop:0}, 'slow');
			return false;
  });
  
  $('a[href=#mobile_media]').click( function() {
			//alert('media');
			$('html, body').animate({scrollTop:400}, 'slow');
			return false;
  });
  
  if($('#outofstock_div').length){
    $('#outofstock_msg').html($('#outofstock_div').html());
  }  
  
  $('#addToBasket').click(function() {
	if(document.cart_quantity){
	   $('#addToCart').click();
    }
  });  
	
  $('#addToCart').click( function() {
    if(document.cart_quantity){
      _form = document.cart_quantity;
      if(_form.action.value=='nostock') return false;
	  data = {};
	  data['id'] = parseInt( _form.products_id.value);
	  data['model'] = ($_('seloption'))? $_('seloption').value : 0;
	  data['quant'] = parseInt( _form.qty.value);
	  
	  poscart = this;
	  
	  data['oscsid'] = '';
   	  if(_form.osCsid) data['oscsid'] = _form.osCsid.value;
	  
	  addtocart(data);
    }
	return false;
  });
  
  if(!($('#blinkstr').css('display')== 'none')){
    doBlink("#blinkstr",1,1);
  }
  
});


function buy_now(n){
  _id = 'buynow_'+n;
  _this = $('#'+_id);
  poscart = '#'+_id;
  var _url = _this.attr('href');
  _url+= '&ajaxboxes=1&rand='+Math.random();
  data = {};
  data['url'] = _url;
  addtocart(data);
  return false;
}


var _initprice = 0;
function swap_gtarif(){
    _val = parseInt(document.getElementById('iquant').value);
    _check = true;
    if(!_val) _val=0;
    else{
	  _check = false;
	  _tabval = $('.str_gt').attr('rel');                    
	  _tabval = _tabval.split('|');
	  for(i=0;i<_tabval.length;i++){
	    _verif = parseInt(_tabval[i]);
	    if(!_check && _val>=_verif){
      	  _check = true;
	      checkpolicies(_verif, 1);
	      break;
		}
	  }
	}
	if(!_check) checkpolicies(_val,1);
}	

function new_gtarif(){
  _val = parseInt(document.getElementById('iquant').value);
  if(!_initprice) _initprice = $('#price').html();
  _uprice = _initprice;
  
  if(document.getElementById('seloption')){
	_id = parseInt(document.getElementById('seloption').value);
    if(_id && document.cart_quantity['_optprice'+_id]){
	  _uprice = document.cart_quantity['_optprice'+_id].value+' €';
	}
	if(_id && document.cart_quantity['_optspecial'+_id]){
	  if($('.old-price').length){
	    _display = (document.cart_quantity['_optspecial'+_id].value == '1')? 'block' : 'none';
	    $('.old-price').css('display', _display);
	  }
	}
  }	
  _check = true;				
  if(!_val) _val=0;
  else{
    _check = false;
	_tabval = $('.str_gt').attr('rel');                    
	if(_tabval){
	_tabval = _tabval.split('|');
	for(i=0;i<_tabval.length;i++){
	  _verif = parseInt(_tabval[i]);
	  if(!_check && _val>=_verif){
        _check = true;
        _vval = _tabval[i].split(':');
        $('#price').html(_vval[1]+' €');
	    break;
	  }
	}
	}
  }	
  if(!_check) $('#price').html(_uprice);
}	

function checkpolicies(_n, _modif){
  _this = $_('gt_'+_n);
  _this_div = $_('divgt_'+_n);
  if(_this) _class = _this.className;
  $('.checkedbox').attr('className','checkbox');
  if(_this){
    if(_class=='checkbox' || _modif){
      _this.className='checkedbox'
      if(!_modif) document.cart_quantity.qty.value = _n;
    }else{
      document.cart_quantity.qty.value = 1;
      _this.className='checkbox'
    }
  }
  if(_this_div){
    if(_class=='checkbox' || _modif){
      _this_div.className='checkedbox'
    }else{
      _this_div.className='checkbox'
    }
  }
  new_gtarif();
}

function sel_option_image(_id){
  
  if(document.cart_quantity['_pathimg'+_id]){
    _src = 'images/'+document.cart_quantity['_pathimg'+_id].value;
    $('.jqzoom img').attr('src', _src);
	$('.jqzoom img').attr('jqimg', _src);	    			   
	$('.ithumb').attr('href', _src);
  }else{
    _i = (document.cart_quantity['_optimg'+_id])? parseInt(document.cart_quantity['_optimg'+_id].value) : -1;
    _i = _i + 1;
    if($('#ithumb'+_i)){
      newsrc = $('#ithumb'+_i).attr('jqimg');
	  $('.jqzoom img').attr('src', newsrc);
	  $('.jqzoom img').attr('jqimg', newsrc);
    }
  }
  _disabled = '';
  $('option.outofstock').each(function(){
  	_disabled+= '|'+   $(this).attr('value');
  });
  if(_disabled) _disabled+= '|';
  if(_disabled.indexOf('|'+_id+'|')>=0){
    $_('outofstock_msg').style.display = 'block'; 
    document.cart_quantity['action'].value = 'nostock';
  }else{  
    $_('outofstock_msg').style.display = 'none';
    document.cart_quantity['action'].value = 'buy_now';
  }
  new_gtarif();
}

function verif(frm){
 var email= frm.adresse_mail.value;
 var re_email = /^[0-9a-z]([-_\.]?[0-9a-z])*@[0-9a-z]([-_\.]?[0-9a-z])*\.[a-z]{2,4}$/;
 var echec=false;
 var msg = "Votre adresse email ne semble pas valide";
 if (!(re_email.test(email))) {msg; echec=true;}
 if (echec) {alert(msg);return false;}
 else {return true;}
}

var cs=0;function note(e,i,c){var l=5;if(!i){i=cs}if(c){l=i;if(e.className=='star'&&i==cs){i=0;l=5}cs=i;document.product_reviews_write.rating.value=cs}for(j=1;j<=l;j++){_classname=(j>i||(i<1))?'empty_star':'star';e.parentNode.getElementsByTagName("li")[j].className=_classname}}

function checkComment(_f){
  _rating = parseInt(_f.rating.value);
  _review = _f.review.value;
  _minreview = 15;
  if(_review == 'Ajoutez votre commentaire.'){
    _f.review.value = '';
    _review = '';
  }	
  _strerror = '';
  if(_review.length<_minreview){
     _strerror+= '- Votre commentaire doit contenir au moins '+_minreview+' caractères.\n';
  }
  if(_rating<1){
     _strerror+= '- Cliquez sur les étoiles pour noter ce produit.';
  }
  if(_strerror){
    _f.review.focus();
    _f.review.style.color='#181818';
	_f.review.style.height='108px';
	_f.review.style.fontSize='14px';
	alert(_strerror);
    return false;  
  } 
  return true;
}

var doBlink = function(obj,start,finish) { jQuery(obj).fadeOut(800).fadeIn(1200); if(start!=finish) { start=start+1; doBlink(obj,start,finish); } } 


