function evenCols() {
	if($.browser.msie) {
		
		if(document.getElementById("comments") != null) {
			var commentsHeight = $('div#comments').height();
				
			$('div#col-right-comm').css("height", commentsHeight + 17);
		}
		
		// komentarz padding div.comm-text div.main p:last-child { padding-bottom: 0; }
		
		$('div.main p:last-child').css("paddingBottom", 0);
		
		
	} else {
		// komentarzy
			
		if(document.getElementById("comments")) {
			var commentsHeight = $('div#comments').height();
		
			$('div#col-right-comm').css("height", commentsHeight + 17);
		}
	}
	
}

function answer() {
	$('a.answer').click(function() {
		
		var href = $(this).attr('href');
		re = /\w+-\d+/;
		getId = re.exec(href);
		
		var nick = $('li#' + getId).find('p.nick').text();
		var wpis = "<strong>" + nick + ",</strong> ";
		var base = $('#comment').val();
		
		
		$('#comment').val(base + wpis).focus();
		
		return false;
	});

}

var message = '';

function validate() {
	$('#submit').click(function() {
		$('form#add-comment label').each(function() {
			$(this).css("color", "#fff");
		});
		
		var lengthAuthor = $('#author').val().length;
		if(lengthAuthor < 2) {
			message += "Wpisz nick/imię.\n"; 
			$('label[for="author"]').css("color", "#f02400");
		}
					
		var email = $('#email').val();
		var emailLength = $('#email').length;
		re = /[\w]+@[\w]+\.[\w]+/;
		emailChecked = re.exec(email);
		if(emailLength == 0) {
			message += "Wpisz email.\n"; 
			$('label[for="email"]').css("color", "#f02400");
		} else if(emailChecked == null) {
			message += "Wpisz poprawny email.\n";
			$('label[for="email"]').css("color", "#f02400");
		}
		
		/*
		var link = $('#url').val();
		var linkLength = $('#url').length;
		reL = /^http:\/\/|^www\.[\w-\?\/\%\s]+\.[\w]+/;
		linkChecked = reL.exec(link);
		if(linkLength != 0) {
			if(linkChecked == null) {
				message += "<p>Link, który wpisałeś jest niepoprawny.</p>";
			}
		}
		*/
		
		var textareaLength = $('#comment').val().length;
		if(textareaLength == 0) {
			message += "Wpisz treść komentarza.\n";
			$('label[for="comment"]').css("color", "#f02400");
		}
		
		if(message.length > 0) {
			alert(message);
			message = '';
			return false;
		}
	});
}

function floatLi() {
	$('div#col-right ul').each(function() {
		if(!$(this).hasClass('text') && !$(this).hasClass('delicious')) {
			$(this).find('li:odd').addClass('right');
		}
	});
}

function moreSnippet() {
	$('ul.text li').each(function() {
		if(!$(this).parent().hasClass('no-toggle')) {
			$(this).find('p:first').css("height", "16px");
			
			if($(this).find('p > a').text().length - $(this).find('p > a > strong').text().length - $(this).find('p > a > span.nick').text().length > 110) {
				$(this).find('p:first > a').append('...');
			}
			
			$(this).hover(function() {
				$(this).find('p:first').css("height", "auto");
			}, function() {
				$(this).find('p:first').css("height", "16px");
			});

			
		}
	});
}

function manageSidebar() {

	$cookie = $.cookie('active_right_col');
	
	if($cookie != null) { 
		$('a.label-' + $cookie).addClass('selected');
	} else {
		$('a.label-sidebar').addClass('selected');
	}		

	$sidebar = $('a.label-sidebar');
	$info = $('a.label-info');
	
	$divInfo = $('div#l-info');
	$divSidebar = $('div#l-sidebar');
	
	divInfoHeight = $divInfo.height();
	divSidebarHeight = $divSidebar.height();
	
	if($sidebar.hasClass('selected')) {
		$divInfo.animate({ height: "0px" }, 500);
	} else if($info.hasClass('selected')) {
		$divSidebar.animate({ height: "0px" }, 500);
	}
	
	$sidebar.click(function() {
		if($(this).hasClass('selected')) {
			return false;
		} else {
			$(this).addClass('selected');
			$info.removeClass('selected');
			$divInfo.animate({ height: "0px" }, 500);
			$divSidebar.animate({ height: divSidebarHeight }, 600);
			
			$.cookie('active_right_col', 'sidebar', { expires: 90 });

			return false;
		}
	});
	
	$info.click(function() { 
		if($(this).hasClass('selected')) {
			return false;
		} else {
			$(this).addClass('selected');
			$sidebar.removeClass('selected');
			$divSidebar.animate({ height: "0px" }, 500);
			$divInfo.animate({ height: divInfoHeight }, 600);
			
			$.cookie('active_right_col', 'info', { expires: 90 });
			
			return false;
		}
	});
		
	
}

function noSzukaj() {
	$('input#s').click(function() {
		$(this).val('');
	});
}

function contactFormDiv() {
	$thisdiv = $('div.wpcf7-response-output');
	
	if($thisdiv.text().length == 0) {
		$thisdiv.css("display", "none");
	}
}
	
$(document).ready(function(){
	evenCols();
	answer();
	validate();
	floatLi();
	moreSnippet();
	manageSidebar();
	noSzukaj();
	contactFormDiv();
});