/*
	Podpowiedzi do inputów tekstowych
*/
/*
(function($) {
  $.fn.inputPrompt = function() {
		this.each( function() {
			this.orgValue = this.value;

			$(this).focus( function() {
				if ( this.value == this.orgValue ) {
					this.value = '';
				}
			});

			$(this).blur( function() {
				if ( this.value == '' ) {
					this.value = this.orgValue;
				}
			});
		});
  };
})(jQuery);
*/

/*
	Zamiana <hr /> na <div class="hr"><hr /></div>

	extendHR([options]) {
		...
	}

	className = String
*/
(function($) {
  $.fn.extendHR = function(className) {
		if ( className ) {
			this.wrap('<div></div>').parent().addClass( className );
		} else {
			this.wrap('<div class="hr"></div>');
		}
  };
})(jQuery);


/*
	Wyróżnianie kolejnych wierszy tabeli poprzez przypisanie (cykliczne) każdemu wierszowi jednej z podanych klas

	options = {
		cycle = new Array();
	};
*/
(function($) {
  $.fn.zebraTable = function(options) {
		if ( options && options.cycle && options.cycle.length > 1 ) {
			this.each( function() {
				var i = 0;
				var maxi = options.cycle.length-1;

				$('tbody tr', this).each( function() {
					$(this).addClass(options.cycle[i]);
					(i<maxi)?i++:i=0;
				});
			});
		}
  };
})(jQuery);


/*
	Wyróżnianie kolejnych elementów listy (ul) poprzez przypisanie (cykliczne) każdemu wierszowi jednej z podanych klas

	options = {
		cycle = new Array();
	};
*/
(function($) {
  $.fn.zebraUL = function(options) {
		if ( options && options.cycle && options.cycle.length > 1 ) {
			this.each( function() {
				var i = 0;
				var maxi = options.cycle.length-1;

				$('li', this).each( function() {
					$(this).addClass(options.cycle[i]);
					(i<maxi)?i++:i=0;
				});
			});
		}
  };
})(jQuery);


/*
expandTrigger
*/
(function($) {
  $.fn.receipesSearchPanel = function() {
		if ( this && this.length ) {
			$( $(this).attr('rel') ).hide();
			$(this).toggle(
				function() {
					$( $(this).attr('rel') ).show();
					$(this).addClass('expanded');

					return false;
				},
				function() {
					$( $(this).attr('rel') ).hide();
					$(this).removeClass('expanded');

					return false;
				}
			);
		}
  };
})(jQuery);



/*
expandableWhy
*/
(function($) {
  $.fn.expandableWhy = function() {

		if ( this && this.length ) {
			$('.whyMore', this).hide();

			$('.more-less', this).toggle(
				function() {
					var $this = $(this);
					$this.parents('.expandable').find('.whyMore').show();
					$this.addClass('expanded');

					return false;
				},
				function() {
					var $this = $(this);
					$this.parents('.expandable').find('.whyMore').hide();
					$this.removeClass('expanded');

					return false;
				}
			);
		}
  };
})(jQuery);


/*
 * wyrownanie bloczków
 */

(function($) {
  $.fn.equalize = function( children ) {
    if(!children){
      return this;
    }
    this.each(function(){
      if( typeof children == "string" ){
	var thisChildren = $(children, this);
      }
      else if(children instanceof jQuery){
	var thisChildren = children;
      }
      if(thisChildren instanceof jQuery && thisChildren.length > 0){
				var hMax = Math.max.apply( null, thisChildren.map(function(){ return $(this).height(); }).toArray() );
				thisChildren.height( hMax );
      }
    });

    return this;
  };
})(jQuery);



(function($) {
  $.fn.playerTriggerIco = function() {
    this.each( function() {
			var $this = $(this);

			$this.parents('a').css('background-image', 'url('+$this.attr('src')+')');
			$this.wrapAll("<span></span>").css('visibility','hidden').parent().addClass("playerIco");
		});
  };
})(jQuery);


/*
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// MAIN
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
*/

// $(document).ready( function() { ... });
$(function() {

    /* wyrownanie wysokosci boksow */
    //$('.receipesList').equalize('.receipesListItem .content');
		$('.receipesList').equalize('.receipesListItem .receipesListItemContainer');
    $('.categories > ul').equalize('ul');
    $('.przygotowanie ol, ul.prizeOrderStepsList, ul.threeStepsList').equalize('li');
    $('.split-v').equalize('.groupbox');
		$('.magazineList').equalize('li .content');


		$('#playerTrigger img').playerTriggerIco();

      // wysokosci boksow w szkole gotowania
    $('#coockingSchool #movieList div.boxContainer').each( function(i){
      if(!(i%2)){
	var $this = $(this);
	var $next = $this.next();
	$this.add($next).height( Math.max( $this.height(), $next.height() ) );
      }
    });

    // linki w nowym oknie
    $('#fbfan, .FBlikebox a').attr('target','_blank');

	$("#photoWrapper .tabs").tabs("#photoWrapper div.items > div.item", {
		// enable "cross-fading" effect
		effect: 'fade',
		fadeOutSpeed: "slow",
		// start from the beginning after the last tab
		rotate: true
	// use the slideshow plugin. It accepts its own configuration
	}).slideshow({autoplay:true, interval:5000});

	// Zakładki - Ranking przepisów
	$("#receipesRankContainer .tabs").tabs("#receipesRankContainer div.tabContentContainer div.tabContent", {
		// enable "cross-fading" effect
		effect: 'fade',
		fadeOutSpeed: 0,
		// start from the beginning after the last tab
		rotate: false
	// use the slideshow plugin. It accepts its own configuration
	});


	$("#tabs-receipeWrapper .tabs-receipe").tabs("#tabs-content-receipeWrapper .tab-content-receipe-item", {
		// enable "cross-fading" effect
		current:'active',
		tabs:'li',
		effect: 'fade',
		fadeOutSpeed: 0,
		// start from the beginning after the last tab
		rotate: false
	// use the slideshow plugin. It accepts its own configuration
	});

	  // zakladki - dzieci - gry
    $('ul.gametabs').tabs('#gry-dla-dzieci > .tabContent', {effect:'fade'});

    // zakladki - szukaj klubowiczow
    $('#membersSearch ul.tabs').tabs('#membersSearch > div.collapsable > div > form', {effect:'fade'});

	$(".receipeDetailsTabsNav .receipeDetailsTabs").tabs(".receipeDetailsTabItems .receipeDetailsTabItem", {
		// enable "cross-fading" effect
		current:'active',
		tabs:'li',
		effect: 'fade',
		fadeOutSpeed: 0,
		// start from the beginning after the last tab
		rotate: false
	// use the slideshow plugin. It accepts its own configuration
	});

	$('#searchReceipes a.expandTrigger').receipesSearchPanel();

	$('table.eventsList,table.topReceipes,table.searchResults').zebraTable({cycle:['odd','even']});
	$('ul.zebra').zebraUL({cycle:['odd','even']});
	$('hr').extendHR();
//	$('input[type=text]').inputPrompt();

    /*
     * podmiana elementow <select> na listy a checkboxami
     */


	if ( $('a.overlayExpose').length ) {
		$('a.overlayExpose').overlay({
			// each trigger uses the same overlay with the id "gallery"
			target: '#overlayExposeContainer',
			// optional exposing effect
			expose: '#fcf5c2'
			// let the gallery plugin do its magic!
		}).gallery({
			// the plugin accepts its own set of configuration options
			speed: 800
		});
	}

	/* FLV Player */
	$('a[rel=#flvOverlay]').overlay({
			// optional exposing effect
			expose: '#fcf5c2',
			// let the gallery plugin do its magic!
			effect: 'default',

			onBeforeLoad: function() {
				// grab wrapper element inside content
				this.getContent().find(".contentWrap").load(this.getTrigger().attr("href"));
			}

		});
	/**/


		$('div.carousel')
    $('div.carousel').prepend('<a class="prevPage browse left"></a>').append('<a class="nextPage browse right"></a>').find("div.scrollable").scrollable({size:3,keyboard:false});

    $('div.carousel').each(function(){
      $(this).height( $('ul', this).height() );
    });

    $("#hometabs > ul").tabs("#hometabs div.panes > div", {effect:'fade'});

	//DD_roundies.addRule('.rounded3px', '3px', true);
    DD_roundies.addRule('.r3', '3px');
    DD_roundies.addRule('.r5', '5px');

    DD_roundies.addRule('#tab-first.active', '5px 0 0 0', true);
    DD_roundies.addRule('#tab-last.active', '0 5px 0 0', true);

    $('li.category')
      .find('div.title')
      .append('<span></span>')
	.find('span')
	.click(
	  function(){
	    $(this).closest('div').toggleClass('box').closest('li').toggleClass('collapsed').toggleClass('r5');
	  })
	.end()
      .end()
      .filter('li + li')
	.addClass('collapsed')
	.removeClass('r5')
	  .find('div.title')
	  .addClass('box');


    $('#subNavigation .active a[href*=#]').click(
      function(){
				var id = $(this).attr('href').replace(/^[^#]*/,'');

				$(id).find('.title.box span').trigger('click');
      });

    $('select').dropdown();
    $('.append').appender();
    $('form a.cleanForm').click(function(){
      var F = $(this).closest('form');
      F.find('.target').empty();
      F.find(':checkbox, :radio').attr('checked','checked');
      F.find(':text').val('').trigger('blur');
      F.find('div.dropdown select').val('').trigger('change');
      return false;
    });

    if(window.location.hash!=''){
      $('a[href$='+ window.location.hash +']').trigger('click');
      if(window.location.hash.indexOf('comment-')>-1){
	$('a[href$=#tab-komentarze]').trigger('click');
	var posTop = $('li.' + window.location.hash.replace('#','') ).addClass('highlight').position().top;
	window.scrollTo(0, posTop);
      }
    }

    $('.expandable').expandableWhy();

    $('#searchReceipes form').submit( function(){
	$(this).find('.append input:text').remove();
    });

    var origProducts = $('select[name=productID]'), filteredProducts = origProducts.parent();
    filteredProducts.hide();
    $('select#ProductCategories').change( function(){
      var groupID = $(this).val();
      var filteredList = $('<select name="productID"></select>').html( '<option value="">Wybierz produkt</option>'+origProducts.find('#group-'+groupID).html() );
      filteredProducts.empty().append( filteredList ).show().find('select').dropdown();
    });

	$('.shortNavi .expandableInfo').hover(
		function() {
			$(this).addClass('expandableInfo-expanded');
		},
		function() {
			$(this).removeClass('expandableInfo-expanded');
		}
	);

    /*
     *
     *
     */
    $('#membersSearch a.btn-zwin-rozwin').click( function(){
      $(this).toggleClass('btn-zwiniety');
      $('#membersSearch .collapsable').toggle();
    });
    /*
     * zlaszanie do moderatora
     */
    var modForm = $('#modForm').css({'position':'absolute'}).hide();
    if(modForm.length>0){
      $('a.abuse, a[href$=#zglos-naduzycie]').click( function(){
	var xy = $(this).offset();
	modForm.toggle().css({ top: xy.top, right:'30px' });
	return false;
      });
      $('a.default', modForm).click( function(){
	modForm.toggle();
	return false;
      });
    }

    $('body.ie6 table.searchResults img.thumb').parent().hover(
      function(){
	$(this).addClass('hover');
      },
      function(){
	$(this).removeClass('hover');
      }
    );

    $('body.ie6 div.filter').hover(
      function(){
	$(this).addClass('hover');
      },
      function(){
	$(this).removeClass('hover');
      }
    );


    $('input[type=text]').focus(function(){
		var temp = $(this).data('placeholder');
		if(temp){ this.value = (this.value==temp) ? '' : this.value; }
    }).blur(function(){
		var temp = $(this).data('placeholder');
		if(temp){ this.value = (this.value=='') ? temp : this.value; }
    });

    $('input:text[name=mainKeyword]').data('placeholder','Szukaj w serwisie').trigger('blur');
    $('input:text[name=keyword]').data('placeholder', 'Szukane słowo...').trigger('blur');
    $('input:text[name=component]').data('placeholder', 'Nazwa składnika...').trigger('blur');
    $('#coockingSchool input:text[name=phrase]').data('placeholder', 'Szukaj porady kulinarnej...').trigger('blur');
    $('input#email').data('placeholder', 'Twój adres e-mail...').trigger('blur');
    $('input#pomyslna-szukaj').data('placeholder', 'Wpisz nazwę składnika').trigger('blur');
    $('input#favSoup').data('placeholder', 'Szukaj...').trigger('blur');

	if(typeof window.pageTrackPlayer == "undefined"){ window.pageTrackPlayer = function(){} };

	$('a#quizAnswersTrigger').click( function() {
		$this = $(this);
		$( $this.attr('rel') ).show();
		$this.remove();

		return false;
	});
	$( $('a#quizAnswersTrigger').attr('rel') ).hide();

    /* kącik razosnych zakupów */
    if( $('#klubKRZ').length == 1 ){
      function forceRecalculate(){
	var changed = false;
	$('.basket-order input.number-short')
	  .each( function(){
		   if( $(this).val() != $(this).data('origVal') ){ changed = true; }
		 });
	return changed;
      }
      $('.basket-order input.number-short')
	.keyup( function(){
		  if( forceRecalculate() ){
		    $('a.dalej').hide();
		    $('#btnprzelicz').fadeIn();
		  }
		  else {
		    $('#btnprzelicz').hide();
		    $('a.dalej').fadeIn();
		  }
		})
	.blur( function(){
		 $(this).val( parseInt( $(this).val(), 10 ) || 1 );
	       })
	.each( function(){
		 $(this).data('origVal', $(this).val() );
	       });

      $('#deliveryaddress input[type=text]')
	.change(function(){
		  $('#agree').attr('checked', false);
		})
	.blur( function(){
		  $('.field-validation-valid:empty').parent().remove();
		})
	.each( function(){
		 $(this).data( 'origVal', $(this).val() );
	       });
      
      $('#agree')
	.change( function(){
		   if(!$(this).is(':checked')){
		     $('#deliveryaddress input[type=text]').val('');
		   }
		   else {
		     $('#deliveryaddress input[type=text]')
		       .each( function(){
				$(this).val( $(this).data('origVal') );
			      });
		   }
		 });
      
      $('#btnkoszyk')
	.hover( function(){
		  $('#klubInfoBar .lista').show()
		});
      $('#klubInfoBar').bind('mouseleave', function(){ $('#klubInfoBar .lista').hide() });
      
      if($('.basket-order').length > 0){
	window.setTimeout( function(){
			     $('.actioninfo').animate({ 'opacity' : 0 }, 10000);
			   }, 5000 );
      }
    }

});

var addthis_config = {
	services_compact: 'email, favorites, facebook, google, twitter, blip'
};