/**
* @author Remy Sharp, additions by nachokb
* @url http://remysharp.com/2007/01/25/jquery-tutorial-text-box-hints/
*/

(function ($) {

/* $d("....")
 *
 *  Easy access to jQuery's per-element data cache.
 *
 *  Examples
 *
 *    $d("#foo").age = 12
 *    $d("#foo").age //=> 12
 *    $d("#foo").age = $d("#foo").age + 1
 *
 *  Slightly adapted from http://yehudakatz.com/2009/04/20/evented-programming-with-jquery/ by Yehuda Katz.
 */
if (typeof($d) != "function") {
  $d = function(param) {
    var node = jQuery(param)[0];
    var id   = jQuery.data(node);

    jQuery.cache[id] || (jQuery.cache[id] = {});
    jQuery.cache[id].node = node;

    return jQuery.cache[id];
  };
}

$.fn.hint = function (blurClass) {
  if (!blurClass) { 
    blurClass = 'blur';
  }
    
  return this.each(function () {
    // get jQuery version of 'this'
    var $input = $(this),
    
    // capture the rest of the variable to allow for reuse
      title = $input.attr('title'),
      isPassword = $input.attr('type') == 'password',
      $form = $(this.form),
      $win = $(window);

    var strategies = {
      changeValue: {
        init: function() {},
        add: function() {
          if ($input.val() === '') {
            $input.addClass(blurClass)
              .val(title);
          }
        },
        remove: function() {
          if ($input.val() === title && $input.hasClass(blurClass)) {
            $input.val('')
              .removeClass(blurClass);
          }
        },
        submit: function() {
        	 if ($input.val() === title && $input.hasClass(blurClass)) {
            $input.val('')
              .removeClass(blurClass);
          }
        }
      },

      replaceElement: {
        init: function() {
          // create alternative text input element with title as value
    	  if($.browser.msie){
          	var $alt = $('<input type="text" name="password" class="fText password hint" title="Wpisz swoje hasło..." />');
    	  }else {
    		var $alt = $input.clone();
    		$alt.attr("type", "text");
    	  }
          
          $input.addClass("replaced-for-title");
          $alt.attr("id", null)
            .val(title)
            .insertBefore($input)
            .addClass("password-title")
            .addClass(blurClass)
            .hide()
            .focus(function() { // delegate to original
              $d($alt).original.focus();
            });
          $d($input).alternative = $alt;
          $d($alt).original = $input;
        },
        add: function() {
          if ($input.val() === '') {
            $input.hide();
            var id = $input.attr("id");
            $input.attr("id", null);
            $d($input).alternative.attr("id", id).show();
          }
        },
        remove: function() {
          if ($input.is(":hidden")) {
            var id = $d($input).alternative.attr("id");
            $d($input).alternative.attr("id", null).hide();
            $input.attr("id", id).show();
          }
        },
        submit: function() {
          $d($input).alternative.remove();
        }
      }
    };

    // only apply logic if the element has the attribute
    if (title) { 
      var strategyName = isPassword ? "replaceElement" : "changeValue";
      var strategy = strategies[strategyName];

      strategy.init();
      // on blur, set value to title attr if text is blank
      $input.blur(strategy.add)
        .focus(strategy.remove)
        .blur(); // now change all inputs to title

      // clear the pre-defined text when form is submitted
      $form.submit(strategy.submit);
      $win.unload(strategy.submit); // handles Firefox's autocomplete
    }
  });
};

})(jQuery);



/*
(function ($) {
	$.fn.hint = function (blurClass) {
		if (!blurClass) { 
			blurClass = 'blur';
		}
		return this.each(function () {
			var $input = $(this),
			title = $input.attr('title'),
			$form = $(this.form),
			$win = $(window);
			
			function remove() {
				if ($input.val() === title && $input.hasClass(blurClass)) {
					$input.val('').removeClass(blurClass);
				}
			}
			if (title) { 
				$input.blur(function () {
					if (this.value === '') {
						$input.val(title).addClass(blurClass);
					}
				}).focus(remove).blur();
				$form.submit(remove);
				$win.unload(remove);
			}
		});
	};
})(jQuery);
*/

/* Simple gallery plugin
 * author:solveo.pl
 */
(function( $ ){
	  $.fn.simpleGallery = function( options ) {  
	    return this.each(function() {
	    	var settings = {
	    		'menuContainer'  : null,
	        	'speed' : 4000
	      	};
	      	if ( options ) { 
	      		$.extend( settings, options );
	      	}
	      	if(!settings.menuContainer){
	      		settings.menuContainer = $('<div style="display:none;" ></div>');
	      	}
	      	var galleryContainer = $(this);
		  	var galleryMenu = settings.menuContainer;
		  	
		  	var intervalGallery = null;
		  	function setGalleryInterval(){
		  		intervalGallery = setInterval(galleryClickNext, settings.speed);
		  	}
		  	function clearGalleryInterval(){
		  		clearInterval(intervalGallery);
		  	}
		  	function galleryCreateMenu(){
		  		var size = $('div', galleryContainer).size();
		  		for(var i = 0; i<size; i++){
		  			$(galleryMenu).append('<a href="javascript:;" class="pos'+i+'"></a>');
		  		}
		  		$('a', galleryMenu).click(function(){galleryClick($(this));});
		  	}
		  	function galleryClick(a){
		  		clearGalleryInterval();
		  		var index = a.index();
		  		var currentIndex = $('a.active', galleryMenu).index();
		  		if(index == currentIndex){ return; }
		  		$('a.active', galleryMenu).removeClass('active');
		  		$('a', galleryMenu).eq(index).addClass('active');
		  		$('div:visible', galleryContainer).fadeOut('slow');
		  		$('div', galleryContainer).eq(index).fadeIn();
		  		setGalleryInterval();
		  	}
		  	function galleryClickNext(){
		  		var currentIndex = $('a.active', galleryMenu).index();
		  		if(currentIndex < 0 || $('a', galleryMenu).size() <= currentIndex+1){
		  			currentIndex = 0;
		  		}else {
		  			currentIndex += 1;
		  		}
		  		galleryClick($('a', galleryMenu).eq(currentIndex));
		  	}
		  	galleryCreateMenu();
		  	galleryClickNext();
	    });
	  };
})( jQuery );



$(document).ready(function(){
	
	Cufon.replace("#headerContainerContentMenu a", {hover: true});
	Cufon.replace("div.widgetHeader h3, .bodyPage .pageItem .widgetContent h1.pageItemName, .bodyPage .pageList .widgetContent h1.pageItemName, .bodyIndex .productListPromoted h1.pageItemName");
	
	$('#headerContainerGallery').simpleGallery({
		'menuContainer':'#headerContainerContentGallerymenu', 
		'speed':4000
	});
	
        /*LEFT MENU - */
	$('.bodyPage .columnLeft .widgetLeftMenu ul li.active').each(function(){
		if($('li.active', $(this)).size()){
			$(this).removeClass('active');
		}
	});

	if (jQuery.isFunction($.fn.fancybox)) {
		var fancyboxOptions = {
			 transitionIn: 'elastic'
			,transitionOut: 'elastic'
			,titlePosition: 'inside'
			,speedIn: 250
			,speedOut: 250
			,changeSpeed: 150
			,changeFade: 150
		};
		$(".pageItemImages a[rel=fancybox], #productGallery a, a.fancybox").fancybox(fancyboxOptions);
		$(".pageItemGalleryImages a[rel=fancybox]").fancybox($.extend({}, fancyboxOptions, {overlayOpacity: 0.8}));
	}
	
	
	
	if (jQuery.isFunction($.fn.jcarousel)) {
		jQuery('.bodyBottom .productListPromoted div.product').wrap('<li />');
		jQuery('.bodyBottom .productListPromoted div.products li').wrapAll('<ul class="jcarousel-skin-tango" />');
		
		jQuery('.bodyBottom .productListPromoted div.products ul').jcarousel({
			auto: 4,
	        scroll: 1,
	        wrap: 'circular',
	        animation:'slow',
	        buttonNextHTML:null,
	        buttonPrevHTML:null,
	    });
	}

	
	
	$('input[title!=""].hint').hint();

});

