(function($) {

	$(document).ready(function(){

		/**
		* AUTOMATIC ZIP CODES ANCHORS
		* działa, ale jednak inaczej będę musiał to rozwiązać (wytyczne)
		*
		if ( $('.redesignedDistributors').size() == 1 ){
		
			$('.redesignedDistributors h3:eq(0)').after('<div><table id="dynamicZipCodes"><tr></tr></table></div>');
		
			var baseAdress = window.location.href;
			var parts = baseAdress.split('#');
			baseAdress = parts[0];
		
			$('.redesignedDistributors tbody td.zip').each( function(){
			
				$(this).html('<a name="'+$(this).text()+'">'+$(this).text()+'</a>');
			
				$(this).attr( 'name', $(this).text() );
			
				var adress = baseAdress + '#' + $(this).text();
				var liItem = $('<td><a href="'+adress+'">'+$(this).text()+'</a></td>');
			
				$('#dynamicZipCodes tr').append( liItem );
			
			});

					
			if ( $('.redesignedDistributors table:eq(1) thead td').size() > 0 ){
				var theadTd = $('.redesignedDistributors table.slim thead td');
				var adress = baseAdress + '#' + theadTd.text();
				var liItem = $('<td><a href="'+adress+'">'+theadTd.text()+'</a></td>');
				theadTd.html('<a name="' + theadTd.text() + '">'+ theadTd.text() + '</a>');
				$('#dynamicZipCodes tr').append( liItem );
			}
		
		
		}
		*/
		
		$('.zipDistributors td.zip:last').addClass('last');
		
	});
	
	
	
	/**
	* ENGINE DLA AJAXOWYCH KOLEKCJI FLESZOWYCH
	*/
	$(document).ready(function(){
				
			/*
			$.ajax({
				url: ajaxurl, 
				data : {
					'action' : 'collection',
					'other' : 'P'
				},
				success: function(response){
					alert(response);
				}
			});
			*/	
	
	});
	
	$(document).ready(function(){
		if ( $('#contactForm').size() ){
			$('.post_page').addClass('contact');
		}
	});
	
	$('.closePopup').live('click', function(){
		hidePopup();
	});
	
	$('.coNext').live('click', function(){
	
		var rail = $('#coCataloguesRail');

		if ( $('.coCarouselContainer:last').hasClass('visible') ){
			return false;
		}
		
		var nextIndex = $('.coCarouselContainer.visible').index();
		nextIndex++;
		
		$('.coCarouselContainer.visible').removeClass('visible');
		$('.coCarouselContainer:eq('+nextIndex+')').addClass('visible');
		
		var newMargin = nextIndex * 960;
		newMargin = '-' + newMargin + 'px';
		
		$('#coCataloguesRail').animate({
			'left' : newMargin
		}, 500);
		
	});
	
	$('.coPrev').live('click', function(){
		var rail = $('#coCataloguesRail');

		if ( $('.coCarouselContainer:first').hasClass('visible') ){
			return false;
		}
		
		var nextIndex = $('.coCarouselContainer.visible').index();
		nextIndex--;
		
		$('.coCarouselContainer.visible').removeClass('visible');
		$('.coCarouselContainer:eq('+nextIndex+')').addClass('visible');
		
		var newMargin = nextIndex * 960;
		newMargin = '-' + newMargin + 'px';
		
		$('#coCataloguesRail').animate({
			'left' : newMargin
		}, 500);
	});
	
}(jQuery));



/**
* NOWY SYSTEM WYSWIETLANIA KATALOGOW
*/

var isPopupVisible = false;
var isPopupVirgin = true;

function popCollection( collectionPostId, season ){
	
	(function($) {
	
	if ( !isPopupVisible ){
		showPopup();
		window.scrollTo(0,0);
	}
		
	// return;
	
	$('#coCatalogues').html('');
	$('#coCataloguesRail').css('left', '0px');
	$('#coPagination').html('');
	
	$.ajax({
		url: ajaxurl, 
		data : {
			'action' : 'collection',
			'post_id' : collectionPostId,
			'season' : season,
			'language' : ajaxLanguage
		},
		beforeSend: function(){
			// $('#coCatalogues').addClass('loading');
		},
		success: function(response){
			
			$('#coCatalogues').removeClass('loading');
			
			var catalog = jQuery.parseJSON(response);
			
			// KOLEKCJE ( NAWIGACJA NA GORZE )
			// if ( isPopupVirgin ){
			if ( true ){
				$('#coSeasons').html( catalog.collections );
			}
			
			// KATALOG
			var newReplacement = '<div class="coCarouselContainer">';
			newReplacement += '<div class="coCatalogContainer">' + catalog.templates.catalog + '</div>';
			newReplacement += '<div class="coDownloadContainer">' + catalog.templates.catalogDownload + '</div>';
			newReplacement += '</div>';
			
			$('.coCatalogTitle').html( catalog.caption );
			
			// LINK DO POBIERANIA
			$('#coCatalogues').html( newReplacement );
			
			isPopupVirgin = false;
			
		}
	});	
	
	} (jQuery) );
	
}

function popPrevious( categoryId ){

	(function($) {
	
	if ( !isPopupVisible ){
		showPopup();
		window.scrollTo(0,0);
	}
	
	// return;
	
	$('#coCatalogues').html('');
	$('#coCataloguesRail').css('left', '0px');
	
	$.ajax({
		url: ajaxurl, 
		data : {
			'action' : 'collection_category',
			'category_id' : categoryId,
			'language' : ajaxLanguage
		},
		success: function(response){
			
			$('#coCatalogues').removeClass('loading');
						
			var catalogs = jQuery.parseJSON(response);
			
			// KOLEKCJE ( NAWIGACJA NA GORZE )
			// if ( isPopupVirgin ){
			if ( true ){
				$('#coSeasons').html( catalogs[0].collections );
			}
						
			var newReplacement = '';
						
			for ( var i=0; i < catalogs.length; i++ ){
				if ( (i+1) != catalogs.length ){
					newReplacement += '<div class="coCarouselContainer">';
					newReplacement += '<div class="coCatalogContainer">' + catalogs[i].templates.catalog + '</div>';
					newReplacement += '<div class="coDownloadContainer">' + catalogs[i].templates.catalogDownload + '</div>';
					newReplacement += '</div>';
				} else {
					$('#coPagination').html( catalogs[i].pagination );
					$('.coCatalogTitle').html( catalogs[i].title );
				}
			}
									
			// LINK DO POBIERANIA
			$('#coCatalogues').html( newReplacement );
						
			$('.coCarouselContainer:eq(0)').addClass('visible');
			
			isPopupVirgin = false;
			
		}
	});	
	
	} (jQuery) );

}

function hidePopup(){
	(function($) {
		$('#collectionOverall').hide();
		$('#collectionOverallBackground').hide();
		isPopupVisible = false;
	}(jQuery));
}
function showPopup(){
	(function($) {
		$('#collectionOverall').show();
		$('#collectionOverallBackground').show();
		isPopupVisible = true;
	} (jQuery) );
}










