if (typeof console=="undefined") {
  var console = {
    log: function(x){alert(x);}
  };
}

jQuery(document).ready(function(){
  var $cats = jQuery('.ui-item-catalogue');
  if($cats.size() == 0) {
    return false;
  }

  function click() {
    window.location = jQuery(this).children().filter('a').first().attr('href');
  }

  function over(){
    jQuery(this).addClass('ui-hover');
  }

  function out(){
    jQuery(this).removeClass('ui-hover');
  }

  $cats.hover(over, out);
  $cats.click(click);
});

jQuery(document).ready(function(){
  var $b2b = jQuery('.ui-item-b2b-image-contener');
  var animationTime = 600;
  function toggle() {
    var $this = jQuery(this);
    if($this.hasClass('ui-active')) {
      $this.removeClass('ui-active').animate(
        {height: 215},
        animationTime,
        'swing'
      ).next().addClass('ui-more-open').removeClass('ui-more-close');
    } else {
      $this.addClass('ui-active').animate(
        {height: $this.children().first().height()},
        animationTime, 
        '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;
  });
	
  jQuery('#NewsListPage .ui-item-b2b-image-contener').each(function(){
		jQuery(this).next().click(function(){
			jQuery(this).prev().click();
			return false;
		}).children();
	});
  
  jQuery('.rozwijany').hide().click(function(){
    var $this = jQuery(this);
    if($this.hasClass('ui-active')) {
      $this
        .removeClass('ui-active')
        .slideUp(animationTime)
        .next()
        .addClass('ui-more-open')
        .removeClass('ui-more-close')
      ;
    } else {
      $this
        .addClass('ui-active')
        .slideDown(animationTime)
        .next()
        .addClass('ui-more-close')
        .removeClass('ui-more-open')
      ;
    }
  });
  
  jQuery('.rozwijacz').click(function(){
    if(jQuery(this).prev().hasClass('rozwijany')) {
      jQuery(this).prev().click();
    } else if(jQuery(this).prev().size()) {
      jQuery(this).prev().toggle();      
    } else if(jQuery(this).parent().prev().size()) {
      jQuery(this).parent().prev().toggle();      
    }
    return false;
  });
  
  jQuery('.rozwijany').each(function(){
    jQuery(this).prev().click(function(){
      jQuery(this).next().click();
    });
  }).prev().attr('style', 'float: left;clear: both;width: 727px;').children().attr('style', 'float: left;');
});


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;
  var t;

  $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]);
		var $slide = jQuery($promotedProductsElements[num]);

    if(jQuery(this).hasClass('ui-active')) {
      return false;
    }
    $promotedProductsContener.stop(true, true);
		
    // $promotedProductsContener.animate(
      // {left: -1 * $singleWidth * num},
      // 600,
      // 'swing' //'easeOutBounce'
    // );
		
		$promotedProductsElements.css("z-index", 1);
    $promotedProductsElements.filter(".ui-active").css("z-index", 2).removeClass("ui-active");
    $slide.css("opacity", 0).css("z-index", 3).addClass("ui-active");
		
		$slide.animate({opacity: 1}, 1000);
		
    $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);
    }
  });
  
  function goNext() {
	$this = $bts.filter('.ui-active').next();
    if($this.size() && !$this.hasClass('ui-button-next')) {
      moveTo(parseInt($this.text()) - 1);
    }
	else {
		moveTo(0);
	}
	clearTimeout(t);
	t = setTimeout(goNext, 4000);
  }

  $bts.first().click();
  
  t = setTimeout(goNext, 4000);
  
  
 
  
});


(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(){
  jQuery('.ui-up').click(function(){
    jQuery( 'html, body' ).animate( {scrollTop: 0}, 300 );
  });
});

jQuery('#ProductFamilyContent').ready(function(){
  return false;
  var txts = jQuery('#ProductFamilyContent .ui-text, #ProductFamilyContent .ui-text p:first')
  .contents()
  .filter(function() {
    if(this.nodeType == 3) {
      return 0 != jQuery.trim(jQuery(this).text()).length;
    } 
    return false;
  })
  .each(function(){
    var txt = jQuery(this);
    var text = jQuery.trim(txt.text());
    if(text.charAt(0) == '\u2022') {
      txt.wrap('<li> </li>');
      txt.parent().text(text.replace('\u2022', ''));
    } else {
      txt.wrap('<p> </p>');
    }
  }); 
  jQuery('#ProductFamilyContent br').remove();

  jQuery('#ProductFamilyContent > .ui-text > li').wrap('<ul> </ul>');

  jQuery(txts[1]).before(jQuery('#ProductFamilyContent img').first());
});

jQuery('#SidePanelMenu .ui-list').ready(function(){
  var $last = jQuery('.ui-list-el-level-1.ui-list-el-last');
  if($last.children('ul').size() == 0) {
    $last.children('a').attr('style', 'border:none;');
  }
});


/**
 * Boze wybacz, ale nie bede recznie poprawial wszystkich zaimportowanych 
 * dokumentów w celu usuniecia style="margin=x x x x". 
 */
jQuery(document).ready(function(){
  jQuery('.ui-text > *:not(.ui-clear):last').css('margin-bottom', 0);
});



