/* Simple gallery plugin
 * author:solveo.pl
 */
(function( $ ){
	  $.fn.simpleGallery = function( options ) {  
	    return this.each(function() {
	    	var settings = {
	    		'menuContainer'  : null,
	        	'speed' : 4000,
	        	'itemSelector' : 'div.widgetAd'
	      	};
	      	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 = $(settings.itemSelector, galleryContainer).size();
		  		for(var i = 0; i<size; i++){
		  			$(galleryMenu).append('<a href="javascript:;" class="pos'+i+'"><img src="'+$(settings.itemSelector+' img:eq('+i+')', galleryContainer).attr('data-thumb-url')+'" /></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');
		  		$(settings.itemSelector+':visible', galleryContainer).fadeOut('slow');
		  		$(settings.itemSelector, 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 );




$(function() {
	
	
	$('#headerContainerGallery').simpleGallery({
		'menuContainer':'#headerContainerContentGallerymenu', 
		'speed':4000
	});

	
	//
	// wygaszone zdjęcia w stopce
	//
	$('#footer div.widgetAdImage a')
		.fadeTo('fast', 0.4)
		.hover(function(){
			$(this).fadeTo('fast', 1.0);
			},function(){
			$(this).fadeTo('fast', 0.4);
			}
		);
	
	
	
	$('.widgetSearchInput, .widgetNewsletter .input_text').each(function() {
		$(this).addClass('grayed').focus(function() {
			if (this.value == this.defaultValue) {
				$(this).removeClass('grayed').val("");
			}
		}).blur(function() {
			if(!this.value.length) {
				$(this).addClass('grayed').val(this.defaultValue);
			}
		});
	});


	//
	// karuzela
	//
	function createListOfItems(which) {
		var results = [];

		if (createListOfItems.cache[which]) {
			return createListOfItems.cache[which];
		}

		$(which + ' > .container a').each(function() {
			var $img = $("img", this)
				,item = {}
			;
			item.url = $img.attr('src');
			item.title = $img.attr('title');
			item.href = $(this).attr('href');
			item.target = $(this).attr('target');
			results.push(item);
		});

		createListOfItems.cache[which] = results;
		return results;
	}
	createListOfItems.cache = {};

	function partners_itemVisibleInCallback(carousel, item, i, state, evt) {
		var idx = carousel.index(i, partners_itemList.length);
		carousel.add(i, getItemHTML(partners_itemList[idx - 1]));
	};

	function customers_itemVisibleInCallback(carousel, item, i, state, evt) {
		var idx = carousel.index(i, customers_itemList.length);
		carousel.add(i, getItemHTML(customers_itemList[idx - 1]));
	};

	function itemVisibleOutCallback(carousel, item, i, state, evt) {
		carousel.remove(i);
	};

	function getItemHTML(item) {
		if (item) {
			var target = 'target="'+item.target+'"' || '';
			return '<a href="'+item.href+'" '+target+'><img src="' + item.url + '" alt="' + item.title + '" /></a>';
		}
		else
			return '<img src="" alt="" />';
	};

	if (jQuery.isFunction(jQuery.fn.jcarousel)) {
		partners_itemList = createListOfItems('#partners');
		partners_idx = 1;

		jQuery('#partners ul').jcarousel({
			 wrap: 'circular'
			,scroll: 1
			,auto: 5
			,itemVisibleInCallback: {onBeforeAnimation: partners_itemVisibleInCallback}
			,itemVisibleOutCallback: {onAfterAnimation: itemVisibleOutCallback}
		});


		customers_itemList = createListOfItems('#customers');
		customers_idx = 1;

		jQuery('#customers ul').jcarousel({
			 wrap: 'circular'
			,scroll: 1
			,auto: 7
			,itemVisibleInCallback: {onBeforeAnimation: customers_itemVisibleInCallback}
			,itemVisibleOutCallback: {onAfterAnimation: itemVisibleOutCallback}
		});
	}

});

