﻿$(document).ready (init);
function init()
{
    if ($("#tweetList").length > 0) {
        var tweeturl = "http://twitter.com/status/user_timeline/neil_osman.json?count=5&callback=?";
        $.getJSON(tweeturl, function(data) {
            $.each(data, function(i, item) {
                var txt = item.text
	      .replace(/(https?:\/\/[-a-z0-9._~:\/?#@!$&\'()*+,;=%]+)/ig, '<a href="$1">$1</a>')
	      .replace(/@+([_A-Za-z0-9-]+)/ig, '<a href="http://twitter.com/$1">@$1</a>')
	      .replace(/#+([_A-Za-z0-9-]+)/ig, '<a href="http://search.twitter.com/search?q=$1">#$1</a>');
                $('<p></p>').html(txt).appendTo('#tweetList');
            });
        });
    }
    if ($("div#blogNav").length > 0) {
    	$("div#blogNav").localScroll({ hash: true });
    }
	/*
    $('.boxgrid.slidedown').hover(function() {
    	$(".cover", this).stop().animate({ top: '-296px' }, { queue: false, duration: 660 });
    }
    , function() {
    	$(".cover", this).stop().animate({ top: '0px', easing: 'swing' }, { queue: false, duration: 2000 });
    });
    */
	$("div#mainMenu ul li.selected a").focus();

	$("div#mainMenu ul li").not(".selected").hover( 
		function () {
			$(this).addClass("over");
		}, 
		function () {
			$(this).removeClass("over");
		}
    );
    /*
	$("div#mainMenu ul li a").not("div#mainMenu ul li.selected a").focus(
		function() {
			$(this).parent().addClass("over");
		}
	);
	$("div#mainMenu ul li a").not("div#mainMenu ul li.selected a").blur(
		function() {
			$(this).parent().removeClass("over");
		}
	);
	*/
	$("input#searchSubmit").bind("mouseover", function() {
		$(this).attr('src', function() {
			return this.src.replace('.gif', '-hover.gif');
		});
	});
	$("input#searchSubmit").bind("focus", function() {
		$(this).attr('src', function() {
			return this.src.replace('.gif', '-hover.gif');
		});
	});
	$("input#searchSubmit").bind("mouseout", function() {
		$(this).attr('src', function() {
			return this.src.replace('-hover.gif', '.gif');
		});
	});
	$("input#searchSubmit").bind("blur", function() {
		$(this).attr('src', function() {
			return this.src.replace('-hover.gif', '.gif');
		});
	});
	$("input#searchSubmit").bind("click", function() {
		var el = document.getElementById("v");
		if(el.value.indexOf("...") > 0){
			$("input#v").focus()
			return false
		}
		else {
			el.value = unescape(el.value);
			return true
		}
	});
	$("input#v").bind("focus",function(){
		if(this.value.indexOf("...") > 0) {
			this.value = "";
		}
	});
	$("input#v").bind("blur",function(){
		if(this.value == "") {
			this.value = "חפש...";
		}
	});

	$("a.external").bind("click",function(){
		tempStr = this.href;
		window.open (tempStr,"","");
		return false;
	});
	
	$("input.subFrm").bind("click", subFrm, false);
	$("input.subPost").bind("click", subPost, false);

	if (document.location.href != document.location.href) {
		document.location.href = document.location.href;
	}
	if (document.getElementById("fullName")) {
		document.getElementById("fullName").value = getCookie("fullName").toString();
	}
	if (document.getElementById("emailStr")) {
		document.getElementById("emailStr").value = getCookie("emailStr").toString();
	}
	if (document.getElementById("urlStr")) {
		document.getElementById("urlStr").value = getCookie("urlStr").toString();
	}
	var fnArr = document.getElementsByName("fullName");
	var emlArr = document.getElementsByName("emailStr");
	var urlArr = document.getElementsByName("urlStr");
	for (i = 0; i < fnArr.length; i++) {
		if (fnArr[i].value == "") {
			fnArr[i].value = getCookie(fnArr[i].name).toString();
			emlArr[i].value = getCookie(emlArr[i].name).toString();
		}
	}
	for (i = 0; i < urlArr.length; i++) {
		if (urlArr[i].value == "") {
			urlArr[i].value = getCookie(urlArr[i].name).toString();
		}
	}
	
	checkURI();
};



function checkURI() {
    var tmp = document.location.toString();
    if (tmp.indexOf("suc=1") > 0) {
        $("#topLinks").append("<div id='msg'>תודה על פנייתך</div>");
        $("div#msg").fadeIn(6000, msgOut);
    }
}
function msgOut() {
    $("div#msg").fadeOut(6000);
}

String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/gm, '');
}

function subFrm() {
	var el;
	el = document.getElementById("fullName");
	if (isBlank(el.value)) {
		setError(el);
		return false;
	}
	else {
		clearError(el);
	}
	el = document.getElementById("emailStr");
	if (!isValidEmail(el)) {
		setError(el);
		return false;
	}
	else {
		clearError(el);
	}
	el = document.getElementById("semantics");
	if (isBlank(el.value)) {
		setError(el);
		return false;
	}
	else {
		var v = el.value;
		if (v.length != 6 && v.length != 2) {
			setError(el);
			return false;
		}
		else {
			clearError(el);
		}
	}
	return true
}

function subPost(event) {
	if (typeof event == "undefined") {
		event = window.event;
	}
	var target = getEventTarget(event);
	var tmp = target.id.split("-");
	var row = tmp[1];
	var el;
	el = document.getElementById("fullName-" + row);
	if (isBlank(el.value)) {
		setError(el);
		return false;
	}
	else {
		setCookie("fullName", el.value, 365)
		clearError(el);
	}
	el = document.getElementById("emailStr-" + row);
	if (!isValidEmail(el)) {
		setError(el);
		return false;
	}
	else {
		clearError(el);
	}
	el = document.getElementById("semantics-" + row);
	if (isBlank(el.value)) {
		setError(el);
		return false;
	}
	else {
		var v = el.value;
		if (v.length != 6) {
			setError(el);
			return false;
		}
		else {
			clearError(el);
		}
	}
	return true
}

function setErrorNoFocus(el) {
	cssjs("add", el, "validateError", "")
}
function setError(el) {
	cssjs("add", el, "validateError", "")
	el.focus();
}
function clearError(el) {
	cssjs("remove", el, "validateError", "")
}

function setCookie(c_name, value, expiredays) {
	var exdate = new Date();
	exdate.setDate(exdate.getDate() + expiredays);
	document.cookie = c_name + "=" + escape(value) +
    ((expiredays == null) ? "" : ";expires=" + exdate.toGMTString());
}

function getCookie(c_name) {
	if (document.cookie.length > 0) {
		c_start = document.cookie.indexOf(c_name + "=");
		if (c_start != -1) {
			c_start = c_start + c_name.length + 1;
			c_end = document.cookie.indexOf(";", c_start);
			if (c_end == -1) c_end = document.cookie.length;
			return unescape(document.cookie.substring(c_start, c_end));
		}
	}
	return "";
}

function getEventTarget(event) {
	var targetElement = null;
	if (typeof event == "undefined") {
		event = window.event;
	}
	if (typeof event != "undefined") {
		targetElement = event.target;
	}
	else {
		event = window.event;
		targetElement = event.srcElement;
	}

	while (targetElement.nodeType == 3 && targetElement.parentNode != null) {
		targetElement = targetElement.parentNode;
	}
	return targetElement;
}
function cssjs(a, o, c1, c2) {
	switch (a) {
		case 'swap':
			o.className = !cssjs('check', o, c1) ? o.className.replace(c2, c1) : o.className.replace(c1, c2);
			break;
		case 'add':
			if (!cssjs('check', o, c1)) { o.className += o.className ? ' ' + c1 : c1; }
			break;
		case 'remove':
			var rep = o.className.match(' ' + c1) ? ' ' + c1 : c1;
			o.className = o.className.replace(rep, '');
			break;
		case 'check':
			return new RegExp('\\b' + c1 + '\\b').test(o.className)
			break;
	}
}

function checkEmail(val) {
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(val)) {
		return true;
	}
	else {
		return false;
	}
}

function isValidEmail(el) {
	var reEmail = /^([\w]+)(.[\w]+)*@([\w]+)(.[\w]{2,3}){1,2}$/;
	if (el) {
		var re = new RegExp(reEmail);
		el.value = el.value.trim();
		if (el.value == "" || !el.value.match(re)) {
			return false
		}
		else {
			return true
		}
	}
}


function LTrim(str) {
	if (str == null) { return null; }
	for (var i = 0; str.charAt(i) == " "; i++);
	return str.substring(i, str.length);
}
function RTrim(str) {
	if (str == null) { return null; }
	for (var i = str.length - 1; str.charAt(i) == " "; i--);
	return str.substring(0, i + 1);
}
function Trim(str) { return LTrim(RTrim(str)); }
function LTrimAll(str) {
	if (str == null) { return str; }
	for (var i = 0; str.charAt(i) == " " || str.charAt(i) == "\n" || str.charAt(i) == "\t"; i++);
	return str.substring(i, str.length);
}
function RTrimAll(str) {
	if (str == null) { return str; }
	for (var i = str.length - 1; str.charAt(i) == " " || str.charAt(i) == "\n" || str.charAt(i) == "\t"; i--);
	return str.substring(0, i + 1);
}
function TrimAll(str) {
	return LTrimAll(RTrimAll(str));
}
function isNull(val) { return (val == null); }
function isBlank(val) {
	if (val == null) { return true; }
	for (var i = 0; i < val.length; i++) {
		if ((val.charAt(i) != ' ') && (val.charAt(i) != "\t") && (val.charAt(i) != "\n") && (val.charAt(i) != "\r")) { return false; }
	}
	return true;
}
function isInteger(val) {
	if (isBlank(val)) { return false; }
	for (var i = 0; i < val.length; i++) {
		if (!isDigit(val.charAt(i))) { return false; }
	}
	return true;
}
function isNumeric(val) { return (parseFloat(val, 10) == (val * 1)); }
function isArray(obj) { return (typeof (obj.length) == "undefined") ? false : true; }
function isDigit(num) {
	if (num.length > 1) { return false; }
	var string = "1234567890";
	if (string.indexOf(num) != -1) { return true; }
	return false;
}
function setNullIfBlank(obj) { if (isBlank(obj.value)) { obj.value = ""; } }
function setFieldsToUpperCase() {
	for (var i = 0; i < arguments.length; i++) {
		arguments[i].value = arguments[i].value.toUpperCase();
	}
}