if (typeof console=="undefined") {
  var console = {
    log: function(x){alert(x);}
  };
}

jQuery(document).ready(function(){
  var $b2b = jQuery('.ui-item-b2b-image-contener');

  function toggle() {
    var $this = jQuery(this);
    if($this.hasClass('ui-active')) {
      $this.removeClass('ui-active').animate(
        {height: 215},
        600,
        'swing'
      ).next().addClass('ui-more-open').removeClass('ui-more-close');
    } else {
      $this.addClass('ui-active').animate(
        {height: 450},
        600, 
        'swing'
      ).next().addClass('ui-more-close').removeClass('ui-more-open');
    }
  }

  $b2b.each(function(k, v){
    var $this = jQuery(this);
    if(!$this.children().size()) {
      $this.hide().next().hide();
    } else {
      $this.click(toggle);
    }
  });

  jQuery('.ui-item-b2b .ui-link-more').click(function(){
    jQuery(this).prev().click();
    return false;
  });
});


var setVCard;
var nextVCard;
var prevVCard;
var timeout_id = 0;
jQuery("#ProductPromoted").ready(function(){
  var $promotedProducts = jQuery("#ProductPromoted");
  var $promotedProductsContener = jQuery("#ProductPromoted .ui-product-contener-inner");
  var $promotedProductsElements = jQuery('#ProductPromoted .ui-item-slider');
  var $next = jQuery('#ProductPromoted .ui-button-next');
  var $prev = jQuery('#ProductPromoted .ui-button-prev');

  var $numbers = [];
  var $singleWidth = $promotedProductsElements.first().width();
  var $width = $promotedProductsElements.size() * $singleWidth;

  $promotedProductsElements.each(function(k, v){
    var $this = jQuery(this);
    $numbers.push('<div class="ui-menu-el ui-menu-el-list-el ui-button">'+(k+1)+'</div>');
  });
  var $bts = $promotedProducts
    .append('<div class="ui-menu-el-list"> </div>')
    .children()
    .last()
    .append($prev)
    .append($numbers.join(''))
    .append($next)
    .children(':not(.ui-button-prev, .ui-button-next)');
  $promotedProductsContener.width($width);

  function moveTo(num) {
    var $this = jQuery($bts[num]);

    if(jQuery(this).hasClass('ui-active')) {
      return false;
    }
    $promotedProductsContener.stop(true, true);
    $promotedProductsContener.animate(
      {left: -1 * $singleWidth * num},
      600,
      'swing' //'easeOutBounce'
    );
    $bts.removeClass('ui-active');
    $this.addClass('ui-active');
    return true;
  }

  $bts.click(function(){
    var i = parseInt(jQuery(this).text()) - 1;
    moveTo(i);
  });

  $prev.click(function(){
    $this = $bts.filter('.ui-active').prev(); 
    if($this.size() && !$this.hasClass('ui-button-prev')) {
      moveTo(parseInt($this.text()) - 1);
    }
  });

  $next.click(function(){
    $this = $bts.filter('.ui-active').next();
    if($this.size() && !$this.hasClass('ui-button-next')) {
      moveTo(parseInt($this.text()) - 1);
    }
  });

  $bts.first().click();
});


(function($){
  var checkboxClass = 'ui-form-checkbox-mock'
  var checkedClass  = 'ui-form-checkbox-checked';

  function click_on_mock() {
    var $checkbox = $($.data(this, 'checkbox'));
    var $el = $(this);
    if($el.hasClass(checkedClass)) {
      $el.removeClass(checkedClass);
      $checkbox.removeAttr('checked');
    } else {
      $el.addClass(checkedClass);
      $checkbox.attr('checked', 'checked');
    }
  }

  function on_change() {
    var $el = $.data(this, 'element');
    var $checkbox = $(this);

    if($checkbox.is(':checked')) {
      $el.addClass(checkedClass);
    } else {
      $el.removeClass(checkedClass);
    }
  }
  
  function make_checkbox() {
    return this.each(function() {
      var $checkbox = $(this);
      var $el = $('<span class="'+checkboxClass+'"> </span>');
      $el = $checkbox.hide().before($el).prev();

      $.data($el[0], 'checkbox', this);
      $.data(this, 'element', $el);

      $el.click(click_on_mock);
      $checkbox.change(on_change);
      $checkbox.change();
    }); 
  }


  $.fn.extend({
    makeCheckbox: make_checkbox
  });
})(jQuery);


var FormFocusCleaner = {};
(function(ffc){
	/**
	 * onFocus event.
	 * Checks if field has default value if true then clear it for fast field write.
	 *
	 * @param e event data
	 */
	function on_focus(e) {
		//console.log(e.data);
		var el = e.data.el;
		if(el.attr('value') == e.data.inp.text) {
			el.attr('value', '');
		}
	}

	/**
	 * Checks if field is empty if yes put there default value.
	 *
	 * @param e event data
	 */
	function on_blur(e) {
		var el = e.data.el;
		if(el.attr('value') == '') {
			el.attr('value', e.data.inp.text);
		}
	}

	/**
	 * Register events focus and blur on given fields. Field can be text input or
	 * textarea (others not tested).
	 *
	 * inputs is array variable with objects that defines field for script for
	 *
	 * example:
	 * FormFocusCleaner.register([
	 *  {id:'elementdID_1', text:'defaultText_1'},
	 *  {id:'elementdID_2', text:'defaultText_2'}
	 * ]);
	 *
	 * @param array inputs
	 */
	function register_fields(inputs) {
		for(i in inputs) {
			var inp = inputs[i];
			var el = jQuery(document.getElementById(inp.id));
			if(el.size() > 0) {
				if(el.attr('value') == '') {
					el.attr('value', inp.text);
				}
				el.bind('focus', {el: el, inp: inp}, on_focus);
				el.bind('blur', {el: el, inp: inp}, on_blur);
			}
		}
	}

	ffc.register = register_fields;
})(FormFocusCleaner);
jQuery(document).ready(function(){
  FormFocusCleaner.register([{id:'Form_Element_email', text:'Twój e-mail'}]);
});

var FlodRotator = {};
(function($){
  function Flod(object) {}
  
  Flod.prototype.start = function() { 
    this.perPixelTime = 40;
//    this.goToNext();
  };
  
  Flod.prototype.goToNext = function() {
    if(this.perPixelTime) {
      var fl = this;
      var $ul = $($.data(this, 'flod_element'));
    } else {
      var $ul = $(this);
      var fl = $.data(this, 'flod_rotator');
    }
    
    fl.first = $ul.children().first();
    fl.left = fl.first.width();
    if(fl.left < 0) fl.left = fl.left * -1;
    $ul.animate(
    {
      left: (-1*fl.left)
    },
    fl.left * fl.perPixelTime,
    'linear',
    fl.switchEls
  )
  };
  
  
  Flod.prototype.onMouseOver = function () {
    var $ul = $(this);
    $ul.stop(true, false);
  };
  
  Flod.prototype.onMouseOut = function () {
    var fl = $.data(this, 'flod_rotator');
    var $ul = $(this);
    var tmp = $ul.position().left;
    $ul.animate(
      {left: -1 * fl.left},
      ($ul.left + tmp)*fl.perPixelTime,
      'linear',
      fl.switchEls
    );
  };
  
  Flod.prototype.switchEls = function() {
    var fl = $.data(this, 'flod_rotator');
    var $ul = $(this);
    $ul.append(fl.first);
    $ul.attr('style', '');
    $ul.css('left', 0);
    fl.goToNext();
  };
  
  $.fn.flodRotator = function() {
    $(this).each(function(){
      $(this).parent().addClass('ui-flod-rotator');
      var fl = new Flod(this);
      $.data(this, 'flod_rotator', fl);
      $.data(fl, 'flod_element', this);
      this.goToNext = fl.goToNext;
      fl.start();
      this.goToNext();
    });
  }
})(jQuery);



