/*
*   MaM E-SHOP - JS Document 
*
*   Author: MaM Multimedia, s.r.o
* 
*/


/* NEW JS FUNCTION */
if(typeof String.prototype.trim !== 'function') {
  String.prototype.trim = function() {
    return this.replace(/^\s+|\s+$/g, ''); 
  }
}

Number.prototype.formatMoney = function(c, d, t) {
  var n = this, c = isNaN(c = Math.abs(c)) ? 2 : c, d = d == undefined ? "," : d, t = t == undefined ? "." : t, s = n < 0 ? "-" : "", i = parseInt(n = Math.abs(+n || 0).toFixed(c)) + "", j = (j = i.length) > 3 ? j % 3 : 0;
  return s + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : "");
};

function isFloat(num){
  return num%Math.floor(num);
}

/*  jQuery Functions  */
jQuery.fn.ForceNumericOnly = function() {

    return this.each(function()
    {
        $(this).keydown(function(e)
        {
            var key = e.charCode || e.keyCode || 0;
            // allow backspace, tab, delete, pageup, pagedown, home, end, arrows, numbers and keypad numbers ONLY
            return (
                key == 8 ||
                key == 9 ||
                key == 46 ||
                (key >= 33 && key <= 40) ||
                (key >= 48 && key <= 57) ||
                (key >= 96 && key <= 105));
        })
    })
};

jQuery.fn.ForceNumericOnlyFloat = function() {

    return this.each(function()
    {
        $(this).keydown(function(e)
        {
            var key = e.charCode || e.keyCode || 0;
            // allow backspace, tab, delete, pageup, pagedown, home, end, arrows, numbers and keypad numbers ONLY
            return (
                key == 188 ||   // comma ,
                key == 190 ||   // normal .
                key == 110 ||   // keypad .
                key == 8 ||
                key == 9 ||
                key == 46 ||
                (key >= 33 && key <= 40) ||
                (key >= 48 && key <= 57) ||
                (key >= 96 && key <= 105));
        })
    })
};



/*
*   JS
*/
function cenaZmenaDual () {

  var item   = '#' + $(this).attr('rel'),
      cena_j = $(item).attr('rel'),
      pocet  = $(this).val(),
      suma, sumaDual, cena, cenaDual;

  order_calculate = true;
  
  pocet = pocet.replace(/,/gi, ".");

  if (pocet<0) $(this).val(0).change();


  if (!isNaN(pocet)) {

    pocet  = pocet*1;
    suma   = 0;
    cena   = (pocet.toFixed(1)*cena_j).toFixed(2);
    cena   = cena*1;
    cenaDual = (cena*CURRENCY_RATE).toFixed(2);
    cenaDual = cenaDual*1;

    $(item).html( cena.formatMoney(2, ',', '.') + ' ' + CURRENCY);
    $(item+'_dual').html( '(' + cenaDual.formatMoney(0, ',', '.') + ' ' + CURRENCY_DUAL + ')');

    $(".kus").each(function(index) {
      item_cena_j = $('#' + $(this).attr('rel')).attr('rel');
      item_pocet  = $(this).val().replace(/,/gi, ".");
      item_pocet  = item_pocet*1;
      suma = suma + (item_cena_j*item_pocet);
    });

    suma_bezdph = (suma/DPH_VALUE).toFixed(2);
    suma_bezdph = suma_bezdph*1;

    sumaDual = (suma*CURRENCY_RATE).toFixed(2);
    sumaDual = sumaDual*1;
    sumaDual_bezdph = (sumaDual/DPH_VALUE).toFixed(2);
    sumaDual_bezdph = sumaDual_bezdph*1;

    $('#suma').html('<strong>' + suma.formatMoney(2, ',', '.') + ' ' + CURRENCY + '</strong> (' + suma_bezdph.formatMoney(2, ',', '.') + ' ' + CURRENCY + ' bez DPH)');
    $('#suma_dual').html('<strong>' + sumaDual.formatMoney(0, ',', '.') + ' ' + CURRENCY_DUAL + '</strong> (' + sumaDual_bezdph.formatMoney(0, ',', '.') + ' ' + CURRENCY_DUAL + ' bez DPH)');
    $('#suma_save_ship').html( FREE_SHIP );

    if ( isFloat($(this).val()) )
      $(this).val(pocet.toFixed(1));

    $(this).val( $(this).val().replace(/,/gi, ".") );
  }
}

function cenaZmena () {

  var item   = '#' + $(this).attr('rel'),
      cena_j = $(item).attr('rel'),
      pocet  = $(this).val(),
      suma, cena;

  order_calculate = true;

  pocet = pocet.replace(/,/gi, ".");

  if (pocet<0) $(this).val(0).change();

  if (!isNaN(pocet)) {

    pocet  = pocet*1;
    suma   = 0;
    cena   = (pocet.toFixed(1)*cena_j).toFixed(2);
    cena   = cena*1;

    $(item).html( cena.formatMoney(2, ',', '.') + ' ' + CURRENCY);

    $(".kus").each(function(index) {
      item_cena_j = $('#' + $(this).attr('rel')).attr('rel');
      item_pocet  = $(this).val().replace(/,/gi, ".");
      item_pocet  = item_pocet*1;
      suma = suma + (item_cena_j*item_pocet);
    });

    suma_bezdph = (suma/DPH_VALUE).toFixed(2);
    suma_bezdph = suma_bezdph*1;

    $('#suma').html('<strong>' + suma.formatMoney(2, ',', '.') + ' ' + CURRENCY + '</strong> (' + suma_bezdph.formatMoney(2, ',', '.') + ' ' + CURRENCY + ' bez DPH)');
    $('#suma_save_ship').html( FREE_SHIP );

    if ( isFloat($(this).val()) )
      $(this).val(pocet.toFixed(1));

    $(this).val( $(this).val().replace(/,/gi, ".") );
  }
}

function cenaZmenaArr(el,e) {

  var key = e.charCode || e.keyCode || 0,
      val = el.val()*1;

  if (!isNaN(val)) {
    if ( key == 38 )
      el.val( isFloat(val) ? (val + 1).toFixed(1) : val + 1 );

    if ( (key == 40) && (val > 1) )
      el.val( isFloat(val) ? (val - 1).toFixed(1) : val - 1 );
  }

  el.change();
}


/*  E X T E R N A L   L I N K  */
function external() {
  $("a[rel='external']").attr('target','_blank');
}


/*  S H O P R I N G  */
function shopring() {

  $("#shopring").children('.shopring-box').each(function() {

    var el   = $(this),
        data = '#' + el.children('.shopring-title').attr('title','').attr('name');

    $(data).appendTo(el);
    $(el)
    .mouseover(function () {
        $(el).children('.shopring-data').show();
      })
    .mouseout(function () {
        $(el).children('.shopring-data').hide();
      });

  });

  // Shopring external  links
  $("#shopring a").attr('target','_blank');
}


/*  L I N K S  */
function links() {

  $(".links a").each(function() {

    var el = $(this),
        src = el.children('img').attr('src'),
        top = el.children('img').attr('height') + 2,
        data = '#' + el.attr('name');

    if (data.length>1) {
      el.attr('title','');
      $(data)
      .css({'margin-top':'-' + top + 'px'})
      .appendTo(el);
    }

    el
    .mouseover(function () {
        src = src.replace('_off.','_on.');
        el.children('img').attr('src',src);
        if (data.length>1)
          $(data).show();
      })
    .mouseout(function () {
        src = src.replace('_on.','_off.');
        el.children('img').attr('src',src);
        if (data.length>1)
          $(data).hide();
      });

    // Links external  links
    el.attr('target','_blank');
    
  });
}


/*  L O G I N  */
function login() {

  $("#loginMessage").hide();

  $('#loginBtn').click(function(){

    $.fancybox({
      'type'         : 'inline',
      'href'         : '#loginBox',
      'scrolling'    : 'no',
      'width'        : 498,
      'height'       : 'auto',
      'padding'      : 0,
      'margin'       : 0,
      'overlayColor' : '#000',
      'overlayOpacity': 0.5,
      'titleShow'       : false,
      'showNavArrows'   : false,
      'showCloseButton' : true,

      'onClosed'   : function() {
        $("#loginMessage").hide();
        $('#loginSubmit')
          .attr('disabled','')
          .next('.loader').remove();
      }
    });

    $('#fancybox-content, #fancybox-content div:first-child').css({'overflow':'visible'});
    $('#fancybox-outer').css({'background':'none'});
    $('#fancybox-close').css({'width':'37px','height':'37px','top':'21px','right':'21px','background':'url(images/ico_close.png) no-repeat center center'});

    return false;
  });

  $('#loginSubmit').click(function(){

    $("#loginMessage").slideUp(750,function() {
    $('#loginMessage').hide();

    $('#loginSubmit')
      .after('<img src="images/ico_loader.gif" class="loader" />')
      .attr('disabled','disabled');

    $.post('/', {
        login: $('#login-name').val(),
        heslo: $('#login-pass').val(),
        ajax: ''
      },
      function(data){

        $("#loginMessage")
          .html(data)
          .slideDown('slow');

        $('#loginSubmit')
          .attr('disabled','')
          .next('.loader').fadeOut('slow',function(){$(this).remove()});

        if(data.match('success') != null) {

          $("#loginMessage")
            .removeClass('fail')
            .addClass('succ');

          $('#loginForm')
            .slideUp('slow')
            .submit();
        }
      });
    });

    return false;
  });

}


/* T O O L T I P */
var posunY = 10;
var posunX = 20;

function tooltip(){
  $('a[rel=tooltip]').mouseover(function(e) {
    var tip = $(this).attr('title'),
        css = $(this).attr('class');

    $(this).attr('title','');
    $(this).append('<div id="tooltip" class="' + css + '"><div class="tipHeader"></div><div class="tipBody">' + tip + '</div><div class="tipFooter"></div></div>');

    $('#tooltip').css('top', e.pageY + posunY );
    $('#tooltip').css('left', e.pageX + posunX );
    $('#tooltip').css('opacity', 0 );

    //$('#tooltip').fadeIn('500');
    //$('#tooltip').fadeTo('10',0.8);
    $('#tooltip').animate({opacity: 0.8}, {queue:false, duration: 'normal'});

  }).mousemove(function(e) {
    var st  = $(this).hasClass('static');

    if(st) {
      //$('#tooltip').css('top', posunY );
      //$('#tooltip').css('left', posunX );
    }
    else {
      $('#tooltip').css('top', e.pageY + posunY );
      $('#tooltip').css('left', e.pageX + posunX );
    }

  }).mouseout(function() {
    $(this).attr('title',$('.tipBody').html());
    $(this).children('div#tooltip').remove();

  });
}


/* T A B S */
function cssTabs (el) {

  var tabs = $(el);
  tabs.removeClass("ui-widget ui-widget-content ui-corner-all");
  tabs.children("ul.ui-tabs-nav").removeClass("hide ui-helper-reset ui-helper-clearfix ui-widget-header ui-corner-all");
  tabs.children("ul.ui-tabs-nav").children("li").removeClass("ui-state-default ui-corner-top");
  tabs.children("ul.ui-tabs-nav").children("li").children("a").css({'cursor':'pointer'});
  tabs.children("div.ui-tabs-panel").removeClass("ui-widget-content ui-corner-bottom");
}


/*
*   I N I T
*/
$(document).ready(function(){
  tooltip();
  shopring();
  links();
  external();
});



/*
*   O L D
*/
function plus(a) {
  var m = document.getElementById('mnozstvo');
  try {
    m.value = parseInt(m.value,10) + a;
    if (m.value<1)
      m.value = 1;
  } catch (e) {
    m.value = 1;
  }
}
