


// functions


function isFormValid() {

	return $j(".input.incorrect", "form#contact").length > 0 ? false : true;

}

function validateInput(obj) {

	var id = obj.attr("id");
	var correct = false;

	if (id == "email") { // email validator
		if (obj.val().match(/^[a-zA-Z0-9_\.\-]+\@([a-zA-Z0-9\-]+\.)+[a-zA-Z0-9]{2,4}$/))
			correct = true;
	}
	else if (id == "message") { // message validator
		if (obj.val().replace(/(^\s+)|(\s+$)/g, "") != "")
			correct = true;
	}

	obj.removeClass("correct incorrect"); // clearing
	if (correct) {
		obj.addClass("correct");
	}
	else {
		obj.addClass("incorrect");
	}

}

//

function navigation() {
	$j("li a", "#nav").
		mousemove(function(e) {
			var relativeY = (e.pageY - this.offsetTop - 45) / 4;
			$j(this).css("backgroundPosition", "center " + relativeY + "px");
		}).
		mouseout(function(e) {
			$j(this).css("backgroundPosition", "center center");
		});
}

//

function folio(time, opacity) {
	$j("img", "#folio")
		.css("opacity", 1)
		.hover(
			function() {
				$j(this).stop().animate({ opacity: opacity }, time);
			},
			function() {
				$j(this).stop().animate({ opacity: 1 }, time);
			}
		);
}

function newsline() {
	var $ph = $j("#newsline a.ph");

	if ($ph.length == 0) { // prepare basement
		var $first = $j("#newsline a:first");
		$j("#newsline p").append("<br />").append($first.clone());
		$ph = $first.addClass("ph");
	}

	var $active = $j("#newsline a.active");

	if ($active.length == 0) {
		$active = $j("#newsline a:last");
	}

	var $next = $active.next().next().length ?
			$active.next().next() :
			$j("#newsline a:not(.ph):first");

	$next.addClass("active");
	$active.removeClass("active");

	if (ie6) { // bha! another ie6 opacity fail
		$ph.html($next.html());
		$ph.attr("href", $next.attr("href"));
	}
	else {
		$ph.animate({ opacity: 0 }, 500, function() { // hiding
			$ph.html($next.html());
			$ph.attr("href", $next.attr("href"));
			$ph.animate({ opacity: 1 }, 300); // revealing
		});
	}
}

//

var ie6 = jQuery.browser.msie && parseInt(jQuery.browser.version) < 7;


function convert_char(rot_algo, range_start, range_end, input_char)
{
  // rot_algo: 5 for ROT5, 13 for ROT13, etc...
  // range_start, range_end: position in ASCII code table
  // input_char: character to be converted
  //
  // 1. Get position of character in a list that contains only the characters
  //    to be converted. Counting starts with 0:
  //    => position_in = input_char.charCodeAt(0) - range_start;
  // 2. Get new position after applying ROT algorithm:
  //    => position_out = (position_in + rot_algo) % (rot_algo * 2);
  // 3. Return converted character:
  //    => return String.fromCharCode(range_start + position_out);

  return String.fromCharCode(range_start + (((input_char.charCodeAt(0) - range_start) + rot_algo) % (rot_algo * 2)));
}


function rot13(input)
{
  var output = '';

  for(var i = 0; i < input.length; i++ )
  {
    /* Letters A-Z (code 65-90) */
    if(input.charCodeAt(i) >= 65 && input.charCodeAt(i) <= 90)
    {
      output = output + convert_char(13, 65, 90, input.charAt(i));
    }

    /* Letters a-z (code 97-122) */
    else if(input.charCodeAt(i) >= 97 && input.charCodeAt(i) <= 122)
    {
      output = output + convert_char(13, 97, 122, input.charAt(i));
    }
    else
    {
      output = output + input.charAt(i);
    }
  }

  return output;
}