﻿	//General JavaScript & Ajax file
	//Copyright EuroGraf.pl 2009 - All rights reserved

	//Setup some variables
   	var opened_comment_id = '';
	var opened_comment_form_id = '';
	var opened_post_id = '';
	var post_display_effect = 1;

	//Helpers Functions
	function random_string()
	{
		var list = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
		var string = '';
		for (i = 0; i < 40; i++)
		string += list.charAt(Math.floor(Math.random()*list.length));
		return string;
	}

	function rand_value()
	{
		var rand_value = Math.round(Math.random() * 10000000000000000);
		return rand_value;
	}

	function count_array(array)
	{
		var count = 0;
		for(key in array){count++;}
		return count;
	}

	function remove_characters(string)
	{
		var search_array = new Array('<', '>', '~', '`', '!', '?', '/', '\\', '\'', '"', '@', '#', '€', '$', '%', '^', '&', '*', '(', ')', '=', '+', '-', '[', ']', '{', '}', ':', ';', '|', ',', '.', ' ', 'ą', 'ę', 'ż', 'ź', 'ć', 'ń', 'ó', 'ł', 'ś', 'Ą', 'Ę', 'Ż', 'Ź', 'Ć', 'Ń', 'Ó', 'Ł', 'Ś', '¶', '±', 'Ľ', 'ˇ', '¬', '¦');
		var replace_array = new Array('', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '_', '', '', '', '', '_', '_', '', '', '', '', '', '', '', '', '', '_', 'a', 'e', 'z', 'z', 'c', 'n', 'o', 'l', 's', 'A', 'E', 'Z', 'Z', 'C', 'N', 'O', 'L', 'S', 's', 'a', 'z', 'Z', 'A', 'S');
		var search = [].concat(search_array);
		var replace = [].concat(replace_array)
		var i = (string = [].concat(string)).length;
		while(j = 0, i--)
		{
			while(string[i] = string[i].split(search[j]).join(replace[j] || ""), ++j in search);
		}
		return string;
	}

	//Main functions
	var userAgent = navigator.userAgent.toLowerCase();
	var client = {
		isOpera: userAgent.indexOf('opera') > -1,
		isFirefox: userAgent.indexOf('firefox') > -1,
		isChrome: userAgent.indexOf('chrome') > -1,
		isIE: userAgent.indexOf('msie') > -1,
		isSafari: /webkit|khtml/.test(userAgent)
	};

	function onLoadInit()
	{
		imagesPreloader();
		timer();
	}

	function imagesPreloader()
	{
		if (document.images)
		{
			var imagesEmots = '/application/images/emots/, icon_biggrin.gif, icon_confused.gif, icon_cool.gif, icon_eek.gif, icon_evil.gif, icon_lol.gif, icon_mad.gif, icon_mrgreen.gif, icon_neutral.gif, icon_razz.gif, icon_redface.gif, icon_rolleyes.gif, icon_sad.gif, icon_smile.gif, icon_twisted.gif, icon_wink.gif';
			var imagesSite = '/application/images/layout/, loading.gif, loading2.gif, loading_small.gif, ';
			var imagesEmotsArray = new Array();
			var imagesEmotsObject = new Array();
			var imagesSiteArray = new Array();
			var imagesSiteObject = new Array();
			imagesEmotsArray = imagesEmots.split(", ");
			imagesSiteArray = imagesSite.split(", ");
			var imagesEmotsCount = count_array(imagesEmotsArray);
			var imagesSiteCount = count_array(imagesSiteArray);
			for(var i = 1; i <= (imagesEmotsCount - 1); i++)
			{
				imagesEmotsObject[i] = new Image();
				imagesEmotsObject[i].src = imagesEmotsArray[0] + imagesEmotsArray[i];
			}
			for(var i = 1; i <= (imagesSiteCount - 1); i++)
			{
				imagesSiteObject[i] = new Image();
				imagesSiteObject[i].src = imagesSiteArray[0] + imagesSiteArray[i];
			}
		}
	}

	function displayImage(url, width, height)
	{
		var newWindow = window.open(url, rand_value(), 'width=' + width + ', height='+ height + ', resizable=no, scrollbars=no, menubar=no, status=no, toolbar=no');
		newWindow.focus();
	}

	function displayWindow(url, width, height)
	{
		var newWindow = window.open(url, rand_value(), 'width=' + width + ', height=' + height + ', resizable=no, scrollbars=yes, menubar=no, status=no, toolbar=no');
		newWindow.focus();
	}

	function displayPopup(url)
	{
		var newWindow = window.open(url, rand_value(), 'width=1, height=1, resizable=no, scrollbars=yes, menubar=no, status=no, toolbar=no');
		newWindow.focus();
	}

	function windowResize(width, height)
	{
		window.resizeTo(width + 30, height + 60 - ((navigator.appName == 'Netscape')? 10 : 0));
		self.focus();
	}

	function comments_display(comment_id, order_marker)
	{
		if ((opened_comment_id != '') && (opened_comment_id == comment_id))
		{
			$('comment_for_id_' + opened_comment_id).innerHTML = '';
			opened_comment_id = '';
		}
		else	if ((opened_comment_id != '') && (opened_comment_id != comment_id))
		{
			$('comment_for_id_' + opened_comment_id).innerHTML = '';
			comments_get(comment_id, order_marker);
			opened_comment_id = comment_id;
		}
		else
		{
			comments_get(comment_id, order_marker);
			opened_comment_id = comment_id;
		}
	}

	function comments_parse(xml)
	{
		var output_tags = '';
		var comments = xml.getElementsByTagName("comment_item");
		for(i = 0; i < comments.length; ++i)
		{
			output_tags += '<div class="comment_content"> <b class="comment_who">Napisany przez: ' + comments[i].getElementsByTagName("author")[0].childNodes[0].nodeValue + ' | Dodano: ' + comments[i].getElementsByTagName("add_time")[0].childNodes[0].nodeValue + '</b>';
			output_tags += '<div class="comment_tresc">' + comments_insert_emoticons(comments[i].getElementsByTagName("comment")[0].childNodes[0].nodeValue.replace(/\n/g, "<br>")) + '</div></div>';
			output_tags += '<div style=\"height: 1px; padding: 5px 0px 5px 0px; float:none; margin-bottom:10px; \"></div>';
		}
		return output_tags;
	}

	function comments_get(id, order_marker)
	{
		new Ajax.Request('/ajax/comments/index/' + id + '/' + order_marker,
		{
			method:'get',
			onSuccess: function (response) {$('comment_for_id_' + id).innerHTML = comments_parse(response.responseXML); comments_anchor_jump(id);},
			onLoading: function () {$('comment_for_id_' + id).innerHTML = '<div align="center">Ładowanie komentarzy<br>Proszę czekać...<br><img border="0" src="/application/images/layout/loading2.gif" width="160" height="5"></div>';},
			onFailure: function () {$('comment_for_id_' + id).innerHTML = '<div align="center">Brak połączenia z serwerem.</div>';}
		});
	}

	function comments_anchor_jump(anchor_id)
	{
		window.location.hash = 'post_jump_' + anchor_id;
	}

	function comments_insert_emoticons(string)
	{
		var text_emoticons = new Array(":D",":d",":?","8)",":shock:",":evil:",":lol:",":x",":mrgreen:",":|",":P",":p",":oops:",":roll:",":(",":)",":twisted:",":wink:");
		var img_emoticons = new Array("icon_biggrin.gif","icon_biggrin.gif","icon_confused.gif","icon_cool.gif","icon_eek.gif","icon_evil.gif","icon_lol.gif","icon_mad.gif","icon_mrgreen.gif","icon_neutral.gif","icon_razz.gif","icon_razz.gif","icon_redface.gif","icon_rolleyes.gif","icon_sad.gif","icon_smile.gif","icon_twisted.gif","icon_wink.gif");
		var new_img_emoticons = new Array(img_emoticons.length);
		var preload_img_emoticons = new Array(img_emoticons.length);
		for(a = 0; a < img_emoticons.length; a++)	
		{
			preload_img_emoticons[a] = new Image();
			preload_img_emoticons[a].src = '/application/images/emots/' + img_emoticons[a];
			new_img_emoticons[a] = '<img border="0" src="/application/images/emots/' + img_emoticons[a] + '" width="19" height="19" align="absmiddle">';
		}
		var search = [].concat(text_emoticons);
		var replace = [].concat(new_img_emoticons)
		var i = (string = [].concat(string)).length;
		while(j = 0, i--)
		{
			while(string[i] = string[i].split(search[j]).join(replace[j] || ""), ++j in search);
		}
		return string;
	}

	function comment_add(comment_form_id, comment_form_order)
	{
		if ((opened_comment_form_id != '') && (opened_comment_form_id == comment_form_id))
		{
			$('comment_form_id_' + opened_comment_form_id).innerHTML = '';
			opened_comment_form_id = '';
		}
		else	if ((opened_comment_form_id != '') && (opened_comment_form_id != comment_form_id))
		{
			$('comment_form_id_' + opened_comment_form_id).innerHTML = '';
			comment_insert_form(comment_form_id, comment_form_order);
			opened_comment_form_id = comment_form_id;
		}
		else
		{
			comment_insert_form(comment_form_id, comment_form_order);
			opened_comment_form_id = comment_form_id;
		}
	}

	function comment_insert_form(comment_form_id, comment_form_order)
	{
		var output_form = '';
		output_form += '<form id="send_form_ajax" name="send_comment" action="/ajax/add_comment/index" method="post">\n';
		output_form += '<input type="hidden" name="code_value">\n';
		output_form += '<input type="hidden" name="item_id\" value="' + comment_form_id + '">\n';
		output_form += '<input type="hidden" name="item_order\" value="' + comment_form_order + '">\n';
		output_form += '<div class="emots">\n';
		output_form += '<img border="0" src="/application/images/emots/icon_biggrin.gif" width="19" height="19" onclick="javascript:comment_form_emoticon(\':)\');" style="cursor: pointer">\n';
		output_form += '<img border="0" src="/application/images/emots/icon_confused.gif" width="19" height="19" onclick="javascript:comment_form_emoticon(\':?\');" style="cursor: pointer">\n';
		output_form += '<img border="0" src="/application/images/emots/icon_cool.gif" width="19" height="19" onclick="javascript:comment_form_emoticon(\'8)\');" style="cursor: pointer">\n';
		output_form += '<img border="0" src="/application/images/emots/icon_eek.gif" width="19" height="19" onclick="javascript:comment_form_emoticon(\':shock:\');" style="cursor: pointer">\n';
		output_form += '<br>\n';
		output_form += '<img border="0" src="/application/images/emots/icon_evil.gif" width="19" height="19" onclick="javascript:comment_form_emoticon(\':evil:\');" style="cursor: pointer">\n';
		output_form += '<img border="0" src="/application/images/emots/icon_lol.gif" width="19" height="19" onclick="javascript:comment_form_emoticon(\':lol:\');" style="cursor: pointer">\n';
		output_form += '<img border="0" src="/application/images/emots/icon_mad.gif" width="19" height="19" onclick="javascript:comment_form_emoticon(\':x\');" style="cursor: pointer">\n';
		output_form += '<img border="0" src="/application/images/emots/icon_mrgreen.gif" width="19" height="19" onclick="javascript:comment_form_emoticon(\':mrgreen:\');" style="cursor: pointer">\n';
		output_form += '<br>\n';
		output_form += '<img border="0" src="/application/images/emots/icon_neutral.gif" width="19" height="19" onclick="javascript:comment_form_emoticon(\':|\');" style="cursor: pointer">\n';
		output_form += '<img border="0" src="/application/images/emots/icon_razz.gif" width="19" height="19" onclick="javascript:comment_form_emoticon(\':P\');" style="cursor: pointer">\n';
		output_form += '<img border="0" src="/application/images/emots/icon_redface.gif" width="19" height="19" onclick="javascript:comment_form_emoticon(\':oops:\');" style="cursor: pointer">\n';
		output_form += '<img border="0" src="/application/images/emots/icon_rolleyes.gif" width="19" height="19" onclick="javascript:comment_form_emoticon(\':roll:\');" style="cursor: pointer">\n';
		output_form += '<br>\n';
		output_form += '<img border="0" src="/application/images/emots/icon_sad.gif" width="19" height="19" onclick="javascript:comment_form_emoticon(\':(\');" style="cursor: pointer">\n';
		output_form += '<img border="0" src="/application/images/emots/icon_smile.gif" width="19" height="19" onclick="javascript:comment_form_emoticon(\':)\');" style="cursor: pointer">\n';
		output_form += '<img border="0" src="/application/images/emots/icon_twisted.gif" width="19" height="19" onclick="javascript:comment_form_emoticon(\':twisted:\');" style="cursor: pointer">\n';
		output_form += '<img border="0" src="/application/images/emots/icon_wink.gif" width="19" height="19" onclick="javascript:comment_form_emoticon(\':wink:\');" style="cursor: pointer">\n';
		output_form += '</div>\n';
		output_form += '<div class="text_comments">\n';
		output_form += '<b>Nick:</b><br /><input class="form_comments" type="text" name="author" value="" maxlength="32"><br />\n';
		output_form += '<b>Treść komentarza:</b><br /><textarea class="form_comments_text" name="comment" wrap="virtual"></textarea><br />';
		output_form += '</div>';
		output_form += '<table class="comments_code">';
		output_form += '<tr>';
		output_form += '<td><b>Przepisz kod: </b></td>';
		output_form += '<td><div id="code_image" style="width:69px; height:17px;"></div></td>';
		output_form += '<td>\n';
		output_form += '<input class="form_code_text" type="text" name="code" maxlength="6"></td>';
		output_form += '<td><input class="submit" style="cursor: pointer;" type="button" name="submit" value="Wyślij" onclick="comment_form_send();"></td>';
		output_form += '</tr>\n';
		output_form += '</table>\n';
		output_form += '<div id="comment_info" class="txt_alert" style="clear: both; text-align: center; padding-top: 10px"></div>\n';
		output_form += '</form>\n';
		output_form += '<div class="line" />\n';
		$('comment_form_id_' + comment_form_id).innerHTML = output_form;
		setTimeout("comment_form_code_reload();", 200);
	}

	function comment_form_response(responseXML)
	{
		var comment_attributes = responseXML.getElementsByTagName("comment")[0];
		var item_id = comment_attributes.getAttribute("item_id");
		var item_order = comment_attributes.getAttribute("item_order");
		var errors = comment_attributes.getAttribute("errors");
		if(errors == 1)
		{
			var errors_string = responseXML.getElementsByTagName("error")[0].childNodes[0].nodeValue.replace(/@/g, "<br />");
			$('comment_info').innerHTML = '<span class="red">' + errors_string + '</span>';
			comment_form_code_reload();
		}
		else
		{
			$('comment_info').innerHTML = '';
			$('comment_form_id_' + item_id).innerHTML = '';
			opened_comment_form_id = '';
			var item_counter = parseInt($('comment_counter_id_' + item_id).innerHTML) + 1;
			$('comment_link_id_' + item_id).innerHTML = '<a href="javascript:void(0);" onclick="javascript:comments_display(' + item_id + ', \'' + item_order + '\')\"">Komentarze (<span id="comment_counter_id_' + item_id + '">' + item_counter + '</span>)</a>';
			opened_comment_id = '';
			comments_display(item_id, item_order);
		}
	}

	function comment_form_code_reload()
	{
		var randon_code_value = random_string();
		document.forms.send_comment.code_value.value = randon_code_value;
		document.forms.send_comment.code.value = '';
		$('code_image').style.backgroundImage = 'url(/ajax/image_code/index/' + randon_code_value + ')';
	}

	function comment_form_send()
	{
		new Ajax.Request('/ajax/add_comment/index/',
		{
			method:'post',
			postBody: $('send_form_ajax').serialize(),
			onSuccess: function (response) {comment_form_response(response.responseXML);},
			onLoading: function () {$('comment_info').innerHTML = '<div align="center">Proszę czekać...<br><img border="0" src="/application/images/layout/loading2.gif" width="160" height="5"></div>';},
			onFailure: function () {$('comment_info').innerHTML = '<div align="center">Brak połączenia z serwerem.</div>';}
		});
	}

	function comment_form_emoticon(text)
	{
		var txtarea = document.send_comment.comment;
		text = ' ' + text + ' ';
		if (txtarea.createTextRange && txtarea.caretPos)
		{
			var caretPos = txtarea.caretPos;
			caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? text + ' ' : text;
			txtarea.focus();
		}
		else
		{
			txtarea.value  += text;
			txtarea.focus();
		}
	}

	function post_display(post_id)
	{
		if ((opened_post_id != '') && (opened_post_id == post_id))
		{
			post_display_effect ? Effect.toggle('post_id_' + opened_post_id, 'slide', { delay: 0.1, duration: 1.0 }) : $('post_id_' + opened_post_id).hide();
			$('post_display_label_' + opened_post_id).innerHTML = 'Czytaj całość';
			opened_post_id = '';
		}
		else	if ((opened_post_id != '') && (opened_post_id != post_id))
		{
			post_display_effect ? Effect.toggle('post_id_' + opened_post_id, 'slide', { delay: 0.1, duration: 1.0 }) : $('post_id_' + opened_post_id).hide();
			$('post_display_label_' + opened_post_id).innerHTML = 'Czytaj całość';
			post_display_effect ? Effect.toggle('post_id_' + post_id, 'slide', { delay: 0.1, duration: 1.0 }) : $('post_id_' + post_id).show();
			$('post_display_label_' + post_id).innerHTML = 'Ukryj treść';
			post_anchor_jump(post_id);
			opened_post_id = post_id;
		}
		else
		{
			post_display_effect ? Effect.toggle('post_id_' + post_id, 'slide', { delay: 0.1, duration: 1.0 }) : $('post_id_' + post_id).show();
			$('post_display_label_' + post_id).innerHTML = 'Ukryj treść';
			post_anchor_jump(post_id);
			opened_post_id = post_id;
		}
	}
	
	function post_anchor_jump(anchor_id)
	{
		window.location.hash = 'post_jump_' + anchor_id;
	}

	function timer()
	{
		if(document.getElementById('date') != null)
		{
			var date = new Date();
			var minutes = (date.getMinutes() < 10)? '0' + date.getMinutes() : date.getMinutes();
			var secounds = (date.getSeconds() < 10)? '0' + date.getSeconds() : date.getSeconds();
			document.getElementById('date').innerHTML= 'dziś jest: '+ date.getDate() + '.' + (date.getMonth() + 1) + '.' + date.getFullYear() + ', godz. ' + date.getHours() + ':' + minutes + ':' + secounds;
			setTimeout('timer()', 1000);
		}
	}

	function subscribe_form_response(status)
	{
		var subscribe_status_array = new Array();
		subscribe_status_array[0] = 'Adres dodany do bazy - dziękujemy!';
		subscribe_status_array[1] = 'Niepoprawny adres e-mail!';
		subscribe_status_array[2] = 'Adres email już istnieje w bazie!';
		subscribe_status_array[100] = 'Błąd zapisu adresu do bazy prosimy spróbować ponownie!';
		$('note').innerHTML = (subscribe_status_array[status] != undefined)? subscribe_status_array[status] : subscribe_status_array[100];
	}

	function subscribe_form_send()
	{
		new Ajax.Request('/ajax/subscribe/index/',
		{
			method:'post',
			postBody: $('email_form').serialize(),
			onSuccess: function (response) {subscribe_form_response(response.responseText);},
			onFailure: function () {$('note').innerHTML = 'Brak połączenia z serwerem.';}
		});
	}

	function subscribe_form_swap_message(action)
	{
		if(action == 'hide')
		{
			$("email").value = ($("email").value == labels['type_email'])? '' : $("email").value;
		}
		else if(action == 'check')
		{
			$("email").value = ($("email").value != '')? $("email").value : labels['type_email'];
		}
	}

