// JavaScript Document
var baseURL = window.location.protocol + "//" + window.location.host;

$(document).ready(function(){
	$('#leftMenu li a[href^="#"]').click(toggleSubmenu);
	$('.miniaturyRenderow>img').click(toggleRenders);
	$('.miniaturyRenderow .hide').click(hideMinis);
	$('.renderyProjektu>.show').click(showMinis);
	$("#tabs").tabs();
	
	$('a[rel="lightbox"]').fancybox({
		'transitionIn'	:	'elastic',
		'transitionOut'	:	'elastic',
		'easingIn'      : 'easeOutBack',
		'easingOut'     : 'easeInBack',
		'speedIn' : 600,
		'speedOut' : 500,
		'overlayOpacity': 0.6,
		'overlayColor'	: '#000',
		'titlePosition' : 'over',
		'titleFormat'    : function(title, currentArray, currentIndex, currentOpts) {
		    return '<span id="fancybox-title-over">Zdjęcie ' +  (currentIndex + 1) + ' z ' + currentArray.length + '</span>';
		}
	});
	$('a.thickbox').fancybox({
		'transitionIn'	:	'elastic',
		'transitionOut'	:	'elastic',
		'easingIn'      : 'easeOutBack',
		'easingOut'     : 'easeInBack',
		'speedIn' : 600,
		'speedOut' : 500,
		'overlayOpacity': 0.6,
		'overlayColor'	: '#000',
		'titlePosition' : 'over',
		'titleFormat'    : function(title, currentArray, currentIndex, currentOpts) {
		    return '<span id="fancybox-title-over">Zdjęcie ' +  (currentIndex + 1) + ' z ' + currentArray.length + '</span>';
		}
	});
	$('a[rel^="iframe"]').fancybox({
		'width'				: 650,
		'height'			: '85%',
        'autoScale'     	: false,
        'transitionIn'		: 'none',
		'transitionOut'		: 'none',
		'type'				: 'iframe'
	});

	
	/*
	$("#roller").click(toggleSearch);
	if($.cookie('projekty') == null){
		setTimeout("toggleSearch()", 1000);
		$.cookie('projekty', 'hidden', { expires: 0, path: '/'});
	}else{
		if($.cookie('projekty') == 'hidden'){
			$('#searchBar').attr('style', 'height: 180px').find('#roller').removeClass('visible').text('schowaj');
		}else{
			$('#searchBar').attr('style', 'height: 346px').find('#roller').addClass('visible').text('pokaż');
		}
	}
	*/
	
	$("form[name='search'] input[type='text']").blur(searchResultsCount);
	$("form[name='search'] input[type='checkbox']").change(searchResultsCount);
	$("form[name='search'] select").change(searchResultsCount);
	$('#addToCase').click(addToCase);
	$('#removeFromCase').click(removeFromCase);
	$('#clearCase').click(clearCase);
	
	$(".fltr_container a.more").click(showMore);
	$(".fltr_container a.less").click(hideMore);
	searchResultsCount();
});

	function showMore(){
		$(this).hide().parent().addClass('currentTop').find('.fltr_seg:gt(0)').each(function(){$(this).show('normal');}).end().find('.less').show();
	}
	
	function hideMore(){
		$(this).hide().parent().removeClass('currentTop').find('.fltr_seg:gt(0)').each(function(){$(this).hide('normal');}).end().find('.more').show();
	}

	function toggleSubmenu(){	
		if($(this).next().hasClass('visible') == false){
			$('.subMenu').slideUp().removeClass('visible');
			$(this).next().slideDown().addClass('visible');
			
		}		
		return false;
	}
	
	function toggleRenders(){
		var klik = $(this).attr('class');
		$(this).hide();
		$('.miniaturyRenderow img[class!="'+klik+'"]').show();
		
		$('.renderyProjektu>img').hide();
		$('.renderyProjektu>img.'+klik).show();
	}
	
	function hideMinis(){
		$('.miniaturyRenderow').hide();
		$('.renderyProjektu>.show').show();
		return false;
	}
	
	function showMinis(){
		$('.miniaturyRenderow').show();
		$('.renderyProjektu>.show').hide();
		return false;
	}
	
	function toggleSearch(){
		if($('#roller').hasClass('visible')){
			$('#searchBar').animate({"height": "180px"}, "slow").find('#roller').removeClass('visible').text('schowaj');
			$.cookie('projekty', 'hidden', { expires: 0, path: '/'});
		}else{
			$('#searchBar').animate({"height": "346px"}, "slow").find('#roller').addClass('visible').text('pokaż');
			$.cookie('projekty', 'visible', { expires: 0, path: '/'});
		}
	}
	
	function searchResultsCount(){
		var formData = $("form[name='search']").serialize();
		
		$.ajax({
				type: "POST",
				url: baseURL + "/inc/ajax.php",
				data: formData + "&case=1",
				success: function(msg){
						$('#searchPlace span').html(msg);
					},
				error: function(XMLHttpRequest, textStatus, errorThrown){
					alert(XMLHttpRequest.status + "\n" + textStatus + "\n" + errorThrown);
				}
			 });
	}
	
	function addToCase(){
		var formData = 'id=' + $(this).attr('rel');
		var current = $(this);
		
		$.ajax({
				type: "POST",
				url: baseURL + "/inc/ajax.php",
				data: formData + "&case=2",
				async: false,
				success: function(msg){
						current.hide();
						$('#teczkaLeft').find('#teczkaInfo').hide().end().find('.akcje').show().before(msg);
						$('#removeFromCase').show();
					},
				error: function(XMLHttpRequest, textStatus, errorThrown){
					alert(XMLHttpRequest.status + "\n" + textStatus + "\n" + errorThrown);
				}
			 });
		
		return false;
	}
	
	function removeFromCase(){
		var formData = 'id=' + $(this).attr('rel');
		var current = $(this);
		
		$.ajax({
				type: "POST",
				url: baseURL + "/inc/ajax.php",
				data: formData + "&case=3",
				async: false,
				success: function(msg){
						current.hide();
						$('#addToCase').show();
						$('#teczkaLeft #'+msg).remove();
					},
				error: function(XMLHttpRequest, textStatus, errorThrown){
					alert(XMLHttpRequest.status + "\n" + textStatus + "\n" + errorThrown);
				}
			 });
		
		return false;
	}
	
	function clearCase(){	
		$.ajax({
				type: "POST",
				url: baseURL + "/inc/ajax.php",
				data: "case=6",
				async: false,
				success: function(msg){
						$('#addToCase').show();
						$('#teczkaLeft').html('');
						location.reload();
					},
				error: function(XMLHttpRequest, textStatus, errorThrown){
					alert(XMLHttpRequest.status + "\n" + textStatus + "\n" + errorThrown);
				}
			 });
		
		return false;
	}
