// JavaScript by Łukasz Kurzyniec

function IsValidEmail(text) {
	var v = new RegExp();
	v.compile("\\w+([-+.\']\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*");
	if(v.test(text))
		return true;
	else
		return false;
}

function IsValidUrl(text) {
	var v = new RegExp();
	v.compile("(http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?");
	if(v.test(text))
		return true;
	else
		return false;
}

function InitIframes()
{
	$('a.iframe').fancybox({
		'opacity'			:	'true',
		'scrolling'			:	'auto',
		'autoDimensions'	:	'false',
		'width'				:	'10',
		'height'			:	'8',
		'centerOnScroll'	:	'true',
		'titlePosition'		:	'over'
	});
}

function InitImg(id)
{
	$('a#' + id).fancybox({
		'opacity'			:	'true',
		'scrolling'			:	'auto',
		'centerOnScroll'	:	'true',
		'titlePosition'		:	'over'
	});
}

function CheckFieldsForAddComment() {
	var check = new Array('textarea[name=Comment]', 'input[name=Name]');
	var isGood = true;
	for(var i = 0; i < check.length; i++) {
		if(trim($('form#addComment ' + check[i]).val()) == ''){
			isGood = false;
			$(check[i]).css({backgroundColor:'#FFB3B3'});
			$('form#addComment span#errComment').html('[Wypełnij wymagane pola]');
		}
		else {
			$(check[i]).css({backgroundColor:''});
			$('form#addComment span#errComment').html('');
		}
	}
	
	var tb = $('input[name=Email]');
	if(tb.val() != '' && !IsValidEmail(tb.val())) {
		isGood = false;
		tb.css({backgroundColor:'#FFB3B3'});
	}
	else {
		tb.css({backgroundColor:''});
	}
//	tb = $('input[name=Www]');
//	if(tb.val() != '' && tb.val() != 'http://' && !IsValidUrl(tb.val())) {
//		isGood = false;
//		tb.css({backgroundColor:'#FFB3B3'});
//	}
//	else {
//		tb.css({backgroundColor:''});
//	}
	return isGood;
}

function CheckFieldsForFeedBack() {
	var isGood = true;
	if(trim($('form#feedback textarea[name=feedBackMessage]').val()) == ''){
		isGood = false;
		$('form#feedback textarea[name=feedBackMessage]').css({backgroundColor:'#FFB3B3'});
	}
	else {
		$('form#feedback textarea[name=feedBackMessage]').css({backgroundColor:''});
	}
	
	var tb = $('form#feedback input[name=feedBackEmail]');
	if(trim(tb.val()) == '' || !IsValidEmail(tb.val())) {
		isGood = false;
		tb.css({backgroundColor:'#FFB3B3'});
	}
	else {
		tb.css({backgroundColor:''});
	}
	
	return isGood;
}

function showTime() {
   myDate = new Date();

   hours   = myDate.getHours();
   minutes = myDate.getMinutes();
   seconds = myDate.getSeconds();
   
   if (hours < 10)   
		hours   = "0" + hours;
   if (minutes < 10) 
		minutes = "0" + minutes;
   if (seconds < 10) 
		seconds = "0" + seconds;
   
   $('#maxtime').html(hours + ":" + minutes + ":" + seconds);
   setTimeout("showTime()", 1000);
}

function startup() {
	//img opacity
	$('img').hover(function() {
			if($(this).attr('no') != 'no')
				$(this).animate({opacity: '1'}, 'slow');
		}, function() {
			if($(this).attr('no') != 'no')
				$(this).stop().animate({opacity: '0.5'}, 'slow');
		}
	);
	$('img[no!=no]').css( {opacity: '0.5'} );
	
	//loading page
	$('div#load').fadeOut(1000);
	$('div.wrapper').fadeIn(1500);
	
	//comments length
	comments();
	
	//BBCode();
	
	$('img#feedbackBtn').click(feedBack);
	
	//font size
	$('.reader').css( {fontSize: parseInt($('#readerFontSize').val())} );
	
	//to top
	$('img#topPage').click(function(){
		$('html, body').animate( { scrollTop: 0 }, 'slow' );
	})
}

function rotate()
{
	$('.rotate').each(function(){
		var $this = $(this);
		var r = Math.floor(Math.random() * 39) - 20;
		$this.css({
			'-moz-transform':'rotate(' + r + 'deg)',
			'-webkit-transform':'rotate(' + r + 'deg)',
			'transform':'rotate(' + r + 'deg)'
			});
	});
}

function feedBack() {
	var form = $('form#feedback');
	if(form.attr('vis') == null) {
		form.stop().animate({ 'marginLeft': '0px' }, 1000);
		form.attr('vis', 'true');
		$('img#feedbackBtn').attr('no', 'no');
	}
	else {
		form.stop().animate({ 'marginLeft': '-315px' }, 1000);
		form.removeAttr('vis');
		$('img#feedbackBtn').removeAttr('no');
	}
}

function comments() {
	var obj = $('input#commentSpam[type=hidden]');
	if(obj.length)
	{
		$('span#commentSpamMsg').html(obj.val());
	}
	
	$('form#addComment input[type=text]').each(function() {
		$(this)//.attr('onkeyup', 'countMax(this)')
			.after('<span id="span' + this.name + '"> ' + $(this).attr('maxlength') + '</span>');
	});
	//$('form#addComment textarea').attr('onkeyup', 'countComment(this)');
}

function BBCode() {
	$('td.tdComment').each(function(){
		var text = $(this).html();
		text = text.replace('[code]', '<br /><span>CODE:</span><div class="myCode">').replace('[/code]', '</div>');
		text = text.replace('[u]', '<u>').replace('[/u]', '</u>').replace('[b]', '<b>').replace('[/b]', '</b>');
		text = text.replace('[i]', '<i>').replace('[/i]', '</i>');
		$(this).html(text);
	});
}

function countComment(obj){
	$('form#addComment span#countComment').html('<b>' + obj.value.length + '</b>');
}

function countMax(obj){
	var max = parseInt($(obj).attr('maxlength'));
	var current = parseInt(obj.value.length);
	$('form#addComment span#span' + obj.name).html(' ' + (max - current));
}

function gup( name )
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
	return "";
  else
	return results[1];
}

function curPageName()
{
	return window.location.pathname.substring(window.location.pathname.lastIndexOf('/') + 1);
}
	
function LTrim(value) {
    var re = /\s*((\S+\s*)*)/;
    return value.replace(re, "$1");
}
function RTrim(value) {
    var re = /((\s*\S+)*)\s*/;
    return value.replace(re, "$1");
}
function trim(value) {
    return LTrim(RTrim(value));
}
