$('.tabbed_tabs a').click(function(e){
  e.preventDefault();
  $('.tabbed_product').hide();
  $('.tabbed_info').show();

});


/**
 * jQuery Cookie plugin
 *
 * Copyright (c) 2010 Klaus Hartl (stilbuero.de)
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 *
 */
jQuery.cookie = function (key, value, options) {

    // key and at least value given, set cookie...
    if (arguments.length > 1 && String(value) !== "[object Object]") {
        options = jQuery.extend({}, options);

        if (value === null || value === undefined) {
            options.expires = -1;
        }

        if (typeof options.expires === 'number') {
            var days = options.expires, t = options.expires = new Date();
            t.setDate(t.getDate() + days);
        }

        value = String(value);

        return (document.cookie = [
            encodeURIComponent(key), '=',
            options.raw ? value : encodeURIComponent(value),
            options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
            options.path ? '; path=' + options.path : '',
            options.domain ? '; domain=' + options.domain : '',
            options.secure ? '; secure' : ''
        ].join(''));
    }

    // key and possibly options given, get cookie...
    options = value || {};
    var result, decode = options.raw ? function (s) { return s; } : decodeURIComponent;
    return (result = new RegExp('(?:^|; )' + encodeURIComponent(key) + '=([^;]*)').exec(document.cookie)) ? decode(result[1]) : null;
};




jQuery.fn.justtext = function() { 
    return $(this)  .clone()
            .children()
            .remove()
            .end()
            .text();
 
};

var cond = {  "from0": {
                      "z24":		4.44 ,
                      "z36":	  3.13 ,
                      "z48":	  2.48 ,
                      "z60":	  2.11 ,
                      "z72":		1.86 ,
                      "z84":		1.68} ,
              "from10000":{         
                      "z24":		4.39  ,
                      "z36":	  3.07 ,
                      "z48":	  2.42 ,
                      "z60":	  2.04 ,
                      "z72":		1.78  ,
                      "z84":		1.60 } ,
              "from25000":{ 
                      "z24":		4.36  ,
                      "z36":	  3.04 ,
                      "z48":	  2.39 ,
                      "z60":	  2.01 ,
                      "z72":		1.75  ,
                      "z84":		1.57 } ,
             "from50000":{
                      "z24":		4.30  ,
                      "z36":	  2.98 ,
                      "z48":	  2.32 ,
                      "z60":	  1.95 ,
                      "z72":		1.69  ,
                      "z84":		1.51 },
             "from75000":{
                      "z24":		4.28  ,
                      "z36":	  2.96 ,
                      "z48":	  2.30 ,
                      "z60":	  1.91 ,
                      "z72":		1.66  ,
                      "z84":		1.48 }
};

var activeMonth=36;
/* Ruler */
var rulerMax = 100;
var rulerDistance = 20;
/* Units */
var startUnit = 24;
var divUnit = 12;
var netprice;


$(document).ready(function(){
  if($('#showleasing').length && $('#netprice').length)
  {              
     
     
     pprice = $('#netprice').justtext();
     pprice = pprice.replace(".","");
     pprice = pprice.replace( /,/,"." );  
     netprice = parseFloat (pprice);  
     
     if($.cookie('activemonth'))
     {
       activeMonth=$.cookie('activemonth');
     }  
    
     if(netprice >= 1000)
     {
      $('#showleasing').html('<strong class="leasinghead">Leasingrechner</strong><div class="calculator"><span class="leasing_txt">Laufzeit</span> <span class="leasing_reg"><span class="leasing_stone"></span></span> <span class="leasing_num">'+activeMonth+'</span> <span class="leasing_txt2">Monate =</span> <span class="leasing_calc"></span><div class="clear"></div></div>');
      xLeft = rulerDistance*((activeMonth-startUnit)/divUnit);
      $('span.leasing_stone').css({'left':xLeft + 'px'});
      getTotalLeasing();
     }
     
      
  }
  
  // Load PlusOne
  var jssource = "https://apis.google.com/js/plusone.js";
  var jsnode = document.createElement('script');
  jsnode.setAttribute('type','text/javascript');
  jsnode.setAttribute('src',jssource);
  document.getElementsByTagName('head')[0].appendChild(jsnode);
  document.getElementById('plus1').innerHTML = "";

});

function getTotalLeasing()
{
    xcond = cond.from0;
    if(netprice >= 10000) xcond = cond.from10000;
    if(netprice >= 25000) xcond = cond.from25000;
    if(netprice >= 50000) xcond = cond.from50000;
    if(netprice >= 75000) xcond = cond.from75000; 
    total = xcond['z'+activeMonth]*netprice/100;
    total = parseFloat(total).toFixed(2).replace(".",",");
    $('span.leasing_calc').html(total + ' &euro;/Monat**');
    $('#leasinglfz').val('Laufzeit ' + activeMonth + ' Monate = ' + total);  
}


$('#showleasing:not(.frozen) span.leasing_stone').live('mousedown touchstart MozTouchDown',function(x){
    x.preventDefault(); 
    
    if(x.originalEvent.touches && x.originalEvent.touches.length) {
        x = x.originalEvent.touches[0];
    } else if(x.originalEvent.changedTouches && x.originalEvent.changedTouches.length) {
        x = x.originalEvent.changedTouches[0];
    }  
    
    var startX = x.pageX;
    var startY = x.pageY;
    var startDiv = parseInt($('span.leasing_stone').css('left'));
    var startFactor = Math.round(startDiv/rulerDistance); 
    
    

    $(document).bind('mousemove touchmove MozTouchMove',function(e){
        
        if(e.originalEvent.touches && e.originalEvent.touches.length) {
            e = e.originalEvent.touches[0];
        } else if(e.originalEvent.changedTouches && e.originalEvent.changedTouches.length) {
            e = e.originalEvent.changedTouches[0];
        } 
        
        divX = (-1)*(startX - e.pageX);
        posX = divX+startDiv;
        
        
        //console.info('Start:' + startX + ' Client X:' + e.pageX + ' Differenz:' + divX + ' Start Div: ' + startDiv); 
        
        if(posX>rulerMax) posX=rulerMax;
        if(posX<0) posX=0;
        
        
        
        
        factor = posX/rulerDistance;
        factor = Math.round(factor);

        if(factor != startFactor)
        {
          startFactor = factor;
          totalUnits = (factor*divUnit)+startUnit;
          $('span.leasing_num').text(totalUnits);
          $('span.leasing_stone').css({'left':factor*rulerDistance + 'px'});
          activeMonth = totalUnits;
          $.cookie('activemonth',activeMonth,{ expires: 7, path: '/', domain: 'cad-and-more.de', secure: false });
          getTotalLeasing();
        }
    });
});

$(document).bind('mouseup touchend MozTouchRelease',function(x){    
    if(x.originalEvent.touches && x.originalEvent.touches.length) {
        x = x.originalEvent.touches[0];
    } else if(x.originalEvent.changedTouches && x.originalEvent.changedTouches.length) {
        x = x.originalEvent.changedTouches[0];
    } 
    
    $(document).unbind('mousemove touchmove MozTouchMove');
});





