/*

artikelDetail.js

zentrale Funktionen rund um die Artikel-Detailansicht

alle globalen Variablen, die direkt in der Seite stehen, beginnen mit "page_"
*/


/*********************  Staffel-Rechner  ****************************/

/*
Staffelpreise , JSON-Def (Beispiel)
var page_staffelpreise = {
   1:45,
   10:40,
   20:35
}
*/

function hasStaffel(){
    return (typeof(page_has_staffel) == 'undefined' || !page_has_staffel)? 0 : 1; 
}

function isPreiswechsel(){
    return (typeof(page_preiswechselInAusf) == 'undefined' || !page_preiswechselInAusf)? 0 : 1;  
}

function staffelrechner(menge){
    
    if(!hasStaffel() && !isPreiswechsel())return;
    menge = parseInt(menge);
    var rechner = new StaffelRechner(menge,page_staffelpreise);
    if (staffelrechner.arguments[1])rechner.isLoading();
    rechner.display();
}

function StaffelRechner(menge,staffeln){
   
   this.menge = menge;
   
   this.is_loading = false;
   
   this.staffeln = staffeln; 
   
   this.display = function (){
      if( isNaN(this.menge) || this.menge <=0){
         this.hide();
         return;  
      }
      
      var einzelpreis = this.getEinzelPreis();
      var summe = this.menge*einzelpreis;
      //alert(einzelpreis +'x'+this.menge+'='+summe);
      var curr = (typeof(page_currency)!='undefined')?page_currency:'EUR';
      var ent  = (typeof(page_bestell_einheit)!='undefined')?page_bestell_einheit:'Stck.';
      jQuery('#staffelrechner_loading').hide();
      jQuery('#staffelrechner_summe').html(format_price(summe)+' '+curr);
      if(menge>1){
         jQuery('#staffelrechner_stueck').html('('+format_price(einzelpreis)+' '+curr+ent+')');
      }else{
         jQuery('#staffelrechner_stueck').html(''); 
      }
   }
   
   this.isLoading = function(){
       this.is_loading = true;
   }
   
   this.getEinzelPreis = function (){
      var m;
      var p=0;
      
      //staffeln sind bereits sortiert!!
      
      for(m in this.staffeln){
         if(m>this.menge)return p;
         p=staffeln[m];
      }
      return p;
   }
   
   this.hide = function (){
        jQuery('#staffelrechner_summe').html('');
        jQuery('#staffelrechner_stueck').html('');
        if(this.is_loading){
            jQuery('#staffelrechner_loading').show();
            this.is_loading = false;
        }else{
            jQuery('#staffelrechner_loading').hide();
        }
        //jQuery('#staffelrechner').hide();
   }
    
}


/*********************  AJAX-Calls  ****************************/
 
function callAjaxArtikelDetail(params, options){
     params = getAjaxParams(params);
     
     var callback = options['callback'];
     var debug = options['debug'];
     var async = (options['wait'])?false:true;
     jQuery.ajax({
         url:(page_ajaxUrl),
         data:params,
         type:'GET',
         timeout:50000,
         async: async,
         dataType:'json',
         error: function(XMLHttpRequest, textStatus, errorThrown){
        	        if(debug)alert('ajaxCall: An error occurred while calling '+page_ajaxUrl+': status '+textStatus + ', response: '+XMLHttpRequest.responseText);
        	    },
         success:function( json)  {
            if(!json){
   	        	if(debug)alert('Ajax-Error alling '+page_ajaxUrl+' (no json returned): '+textStatus);
   	        	return;
   	      }
   	      if(debug){
   	       	    var strDebug = '';var f;
   	       	    for(f in json){
   	       	        strDebug += f + ':' + json[f]+ "\n";   
   	       	    }
   	       	    alert(strDebug);
   	       	}
   	       	if(callback)callback(json);
   	       	
         }
      })
}


/*
getParams

reichert die params mit den standard-page-params an
*/
function getAjaxParams(params){
   
   params['sprache'] = page_sprache;
   params['kundenId'] = page_kundenId;
   params['kdGrpId'] = page_kdGrpId;
   params['preisBasis'] = page_preisBasis;
   params['basisArtikelNr'] = page_basisArtikelNr;
   params['basisArtikelId'] = page_basisArtikelId;
   return params;
}


/*********************  Page-Cache  ****************************/

function resetPageCacheVars(){
   if( eval("page_einzelPreis") && page_einzelPreis != "")  {
         jQuery("#einzelPreis").html(page_einzelPreis);
      }
      if( page_einzelPreisNw != "")   {
         jQuery("#einzelPreisNw").html(page_einzelPreisNw);
      }
      if( page_strAbPreis != "")   {
         jQuery("#strAbPreis").html(page_strAbPreis);
      }
      if( page_staffelTabelle != "")  {
         jQuery("#staffelTabelle").html(page_staffelTabelle);
      }
      if( page_lieferzeitStr != "")  {
         jQuery("#lieferzeitStr").html(page_lieferzeitStr);
      }  
}

function initPageCacheVars(){
   if( page_einzelPreis == "")
         page_einzelPreis = jQuery("#einzelPreis").html();
      if( page_einzelPreisNw == "")
         page_einzelPreisNw = jQuery("#einzelPreisNw").html();
      if( page_strAbPreis == "")
         page_strAbPreis = jQuery("#strAbPreis").html();
      if( page_lieferzeitStr == "")
         page_lieferzeitStr = jQuery("#lieferzeitStr").html();
      if( page_staffelTabelle == "")
         page_staffelTabelle = jQuery("#staffelTabelle").html();
}  

/*********************  andere Funktionen  ****************************/

function showAmpel(ampel){
    jQuery("#ampel_gruen").hide();
    jQuery("#ampel_gelb").hide();
    jQuery("#ampel_rot").hide(); 
    jQuery(ampel).show();
}

function hideBestellEingabe(){
   jQuery("#mengenEingabeContainer").hide();
   jQuery("#bestellButtonContainer").hide();
   jQuery("#bbForm").show();
  
}

function showBestellEingabe(){
   jQuery("#mengenEingabeContainer").show();
   jQuery("#bestellButtonContainer").show();
   jQuery("#bbForm").hide();
}