var commentSend = false;

window.addEvent("load", function(){
	
	if($$(".gallery_image") && $('img_max')){
		
		$$(".gallery_image").each(function(el){
			
			el.addEvent("click", function(){
				var size = el.getParent().getAllNext().length;
				
				index = ($('img_max').innerHTML * 1) - size;
				
				if(index==1){
					$("prev_img").setStyle('opacity', 0.5);
					$("prev_img").setStyle('filter', 'alpha(opacity=50)');
					
					$("next_img").setStyle('opacity', 1);
					$("next_img").setStyle('filter', 'alpha(opacity=100)');
					
				}else{
					
					$("prev_img").setStyle('opacity', 1);
					$("prev_img").setStyle('filter', 'alpha(opacity=100)');
					
				}
				
				if (index == ($('img_max').innerHTML * 1)) {
					
					$("next_img").setStyle('opacity', 0.5);
					$("next_img").setStyle('filter', 'alpha(opacity=50)');
					
					$("prev_img").setStyle('opacity', 1);
					$("prev_img").setStyle('filter', 'alpha(opacity=100)');
				}else{
					
					$("next_img").setStyle('opacity', 1);
					$("next_img").setStyle('filter', 'alpha(opacity=100)');
				}
				
				
				$('img_count').innerHTML = index;
				$("main_image").src = el.src.replace(/thumbs\//, "thumbs\/big_");
				
				
			});
			
		
		});
		
	}
		
	if($("next_img") && $('img_max').innerHTML * 1>1)
	{
		$("next_img").addEvent("click", function(){
			
			if ($('img_count').innerHTML * 1 == $('img_max').innerHTML * 1 -1) {
				$("next_img").setStyle('opacity', 0.5);
				$("next_img").setStyle('filter', 'alpha(opacity=50)');
				
				
				
			}else{
				$("prev_img").setStyle('opacity', 1);
				$("prev_img").setStyle('filter', 'alpha(opacity=100)');
			}
		
			var count = $('img_count').innerHTML;
			if (count * 1 < ($('img_max').innerHTML * 1)) {
				$("main_image").src = $$(".gallery_image")[(count * 1)].src.replace(/thumbs\//, "thumbs\/big_");
				$('img_count').innerHTML = (count * 1) + 1;
			}
		});
		
		
	}
	
	if($("prev_img") && $('img_max').innerHTML * 1>1)
	{
		$("prev_img").addEvent("click", function(){
		
			if ($('img_count').innerHTML * 1 == 2) {
				
				
				$("prev_img").setStyle('opacity', 0.5);
				$("prev_img").setStyle('filter', 'alpha(opacity=50)');
				
			}else{
				$("next_img").setStyle('opacity', 1);
				$("next_img").setStyle('filter', 'alpha(opacity=100)');
			}
			
			var count = $('img_count').innerHTML;
			if (count * 1 > 1) {
				$("main_image").src = $$(".gallery_image")[(count * 1)-2].src.replace(/thumbs\//, "thumbs\/big_");
				$('img_count').innerHTML = (count * 1) - 1;
			}
		});
		
		
	}
	
	
	
	
	
	
	$$(".category_header").each(function(el){
		
		var tmpWidth = el.getChildren()[1].getChildren()[0].getChildren()[0].getSize().x;
		var width = (590 - tmpWidth + 1) * -1;
		el.getChildren()[1].getChildren()[0].setStyle("margin-left", width + 'px');

	});
	
	if ($("gallery_slider_box"))
	{
		var nS2 = new noobSlide({
			box: $('gallery_slider_in'),
			items: $$('#gallery_slider_in img'),
			size: 134,
			interval: 3000,
			fxOptions: {
				duration: 1000,
				transition: Fx.Transitions.Bounce.easeOut,
				wait: false
			},
			addButtons: {
				previous: $('arrow_left'),
				next: $('arrow_right')
			},
			onWalk: function(currentItem,currentHandle){

				if (this.currentIndex == 0) {
					$("arrow_left").setStyle("display", "none");
					$("arrow_left_disable").setStyle("display", "block");
				}
				else {
					$("arrow_left").setStyle("display", "block");
					$("arrow_left_disable").setStyle("display", "none");
				}
				
				if (this.currentIndex == this.items.length - 4) {
					$("arrow_right").setStyle("display", "none");
					$("arrow_right_disable").setStyle("display", "block");
				}
				else {
					$("arrow_right").setStyle("display", "block");
					$("arrow_right_disable").setStyle("display", "none");
				}
			}
		});
	}
	
	
	if ($("post_form")){
		
		$("post_form").addEvent("submit", function(e){
			
			new Event(e).stop();
			
			var post_title = $("post_title").value;
			var post_body  = $("post_body").value;
			var post_author  = $("post_author").value;
	
			
			if (post_title.length == 0)
			{
				alert("Podaj tytuł komentarza");
				return false;
			}
			
			if (!validTextFiled(post_title, 60))
			{
				alert("Podany tytuł jest za długi");
				return false;
			}
			
			if (post_body.length == 0)
			{
				alert("Podaj treść komentarza");
				return false;
			}
			
			if (!validTextFiled(post_body, 1024))
			{
				alert("Podana treść jest za długa");
				return false;
			}
			
			if (post_author.length == 0)
			{
				alert("Podaj podpis");
				return false;
			}
			
			if (!validTextFiled(post_author, 20))
			{
				alert("Podpis jest za długi");
				return false;
			}
			
		
				
			if (!commentSend) {
				commentSend = true;

				$('post_form').submit();
				$('post_submit').destroy();
				
			}
			else
			{
				return false;
			}
		
			
		});
	}

	
	if ($("player"))
	{
		if (!DetectFlashVer(9, 0, 0)) {
			$("player").setStyle("margin-bottom", "15px").setStyle("padding", "10px").setStyle("background-image", "url(/images/wide_bg.png)").setStyle("text-align", "center").innerHTML = 'Przypuszczalnie nie masz zainstalowanego odtwarzacza Flash lub zainstalowana wersja jest nieaktualna.<br /><br /> Jest on bezpłatnie dostępny na stronie producenta:<br /><br / ><a href="http://www.adobe.com/go/getflashplayer" style="color: #0076A3;"><b>Pobierz za darmo najnowszą wersję Flash Playera</b></a><br /><br />'
		}
	}
	
	if ($("no-flash"))
	{
			$("no-flash").setStyle("top", "0px");
	}
	
	if ($("no-player"))
	{
		if ($("adwert").getSize().y > 10)
			$("no-player").setStyle("top", "330px");
		else
			$("no-player").setStyle("top", "310px");
	}
	
});


function validTextFiled(field, chars)
{
	var num = field.length;

	if (num > chars) {

		return false;
	}
	return true;
}

function imposeMaxLength(Object, MaxLen)
{
  return (Object.value.length <= MaxLen);
}
