// Global checkmail function:

function checkMail(field)
{
	var field;
	var mailFilter = /^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$|(\[?((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\]?)$/i

	if (mailFilter.test($(field).attr("value")))
	return true;
	else {
	return false;
	}
}

// Border height fix widget:

function fixHeight()
{
	// Find maximum value in array.
	Array.prototype.max = function()
	{
		var max = this[0];
		var len = this.length;
		for (var i = 1; i < len; i++) if (this[i] > max) max = this[i];
			return max;
	}

	var barArray = new Array();

	barArray[0] = $(".middleBar").height();
	barArray[1] = $(".barLeft").height();
	barArray[2] = $(".barRight").height();

	var maxHeight = barArray.max();

	//$(".middleBar").height(maxHeight); // Problem with opening hidden divs! overflows!
	$(".barLeft").height(maxHeight);

	if (barArray[2] == null)
	{
		$(".middleBar").css("width", "642px");
		var borderProp = "1px #d6d6d6 dashed";

		$(".bottomBanner").css("width", "100%");
		$(".bottomBanner").css("border-right", "0");

		if (barArray[0] > barArray[1])
		{
			$(".middleBar").css("border-left", borderProp);
			$(".barLeft").css("border-right", "0");
		}
	}
	else
	{
		$(".barRight").height(maxHeight);
	}
}

$(document).ready(function(){

// Make date:

var currentTime = new Date(); var month2 = currentTime.getMonth()+1; var date2 = currentTime.getDate(); var year2 = currentTime.getFullYear(); year2 = ""+year2+""; year3 = year2.substr(2,2); var day = currentTime.getDay(); month3=""+month2; if (month3.length==1) { month3 = "0"+month2 }; date3=""+date2; if (date2.length==1) { date2 = "0"+date2 };switch(day){case 0: hebDay = "ראשון"; break; case 1: hebDay = "שני"; break; case 2: hebDay = "שלישי"; break; case 3: hebDay = "רביעי"; break; case 4: hebDay = "חמישי"; break; case 5: hebDay = "שישי"; break; case 6: hebDay = "שבת"; break;}switch(month2) {case 1: hebMonth = "ינואר"; break; case 2: hebMonth = "פברואר"; break;case 3: hebMonth = "מרץ"; break; case 4: hebMonth = "אפריל"; break; case 5: hebMonth = "מאי"; break; case 6: hebMonth = "יוני"; break; case 7: hebMonth = "יולי"; break; case 8: hebMonth = "אוגוסט"; break; case 9: hebMonth = "ספטמבר"; break; case 10: hebMonth = "אוקטובר"; break;case 11: hebMonth = "נובמבר"; break; case 12: hebMonth = "דצמבר"; break;} var makeDate = "יום "+hebDay+", ה-"+date3+" ב"+hebMonth+", "+year2; $(".indexDate").text(makeDate);

	// Clear Login Form label (Username):

	$("form[name='connectMain'] input[type='password'], input[type='text']").focus(function() {
		if ($(this).attr("value") == "שם משתמשת")
		{
			$(this).attr("value", "");
		}
	});


	// Clear Login Form label (Password):

	function clearPassLabel()
	{
		$("#passMask").css("display", "none");
		$("form[name='connectMain'] input[type='password']").css("display", "inline");
		$("form[name='connectMain'] input[type='password']").focus();
	}

	$("#passMask").focus(clearPassLabel);


	// Show errors if exist:

	$("#errors div").fadeIn("slow");
	$("#errors div").fadeOut(15000);

	// Make homepage FF/IE:

	$(".homePage").click(function () {
		title = $(this).attr("title");
		url = $(this).attr("href");

		if (window.sidebar) { // Mozilla Firefox Bookmark
			$("<div class=\"ffHomePage\" style=\"position: absolute; z-index: 100; left: 60px; width: 120px; border: 1px #a9a9a9 solid; background: #fff; text-align: center; padding: 2px 4px 2px 4px;\"><p>יש למשוך את הלוגו של \"גולדה\", לשחרר אותו על האייקון של הבית, ואז ללחוץ \"כן\".</p></div>").appendTo(".homePage");
		} else if( window.external ) { // IE Favorite
		this.style.behavior='url(#default#homepage)';this.setHomePage(url);}

		function hide()
		{
			$(".ffHomePage").hide();
		}

		var t=setTimeout(hide, 4000);

		return false;
	});

	// Add to favorites FF/IE:

	$(".addFav").click(function () {
		title = $(this).attr("title");
		url = $(this).attr("href");

		if (window.sidebar) { // Mozilla Firefox Bookmark
		window.sidebar.addPanel(title, url,"");
		} else if( window.external ) { // IE Favorite
		window.external.AddFavorite( url, title); }
		else if(window.opera && window.print) { // Opera Hotlist
		return true; }

		return false;
	});

	// Add emoticons to latest forum messages, right bar:

	$(".forumBox div div span").each(function() {
		var innerText = $(this).text();
		var subtitle = addemoticons(innerText);
		var subtitle = ngbbcode(subtitle);
		
		$(this).html(subtitle);
	});
	
	fixHeight();

	// Toggle

	$(".toggle").click(function () {
		var show = $(this).attr("href");
		$("#"+show).toggle();
		return false;
	});

	// Newsletter submit

	$("#newsletterSign").submit(function() {
		var Elem = "#newsletterSign [name=nMail]";
		var Elem2 = "#newsletterSign [name=nUsername]";
		var error = 0;
		
		if ($(Elem2).val().length == 0)
		{
			alert(".חובה לספק שם");
			error++;
		}

		if ($(Elem).val().length == 0 || checkMail(Elem) == false)
		{
			alert(".חובה לספק כתובת דוא\"ל תקינה");
			error++;
		}

		if (error > 0)
		{
			return false;
		}

		// Success

		else
		{
			var mail = $(Elem).val();
			var name = $(Elem2).val();

			// Submit newsletter.
			$.ajax({
				type: "POST",
				cache: false,
				url: "ajax/newsletter.php",
				data: "name="+name+"&mail="+mail,
				dataType: "text",

				success: function(msg){
					$(".newsletterBox").html(msg);
				}
			});
		}
		
		return false;
	});


	// Open pop-up

	$(".popMeUp").click(function () {
		var href = $(this).attr("href");
		var rel = $(this).attr("rel");
		var size = $(this).attr("rev");
		
		if (size == "big")
		{
			var width = 800;
			var height = 600;
		}
		
		if (size == "small")
		{
			var width = 350;
			var height = 210;
		}
		
		if (size == "medium")
		{
			var width = 600;
			var height = 430;
		}

		var x = (1024 - width)/2;
		var y = (768 - height)/2;

		if (screen)
		{
			y = (screen.availHeight - height)/2;
			x = (screen.availWidth - width)/2;
		}

		window.open(""+href+"", rel, "width="+width+", height="+height+", top="+y+", left="+x+", resizable=yes, status=no, scrollbars=1");
		return false;
		}
	);

	// Make sure search is more than 3 letters.
	$(".searchBox form").submit(function () {
		var t = $(".searchBox form input[name='data']").attr("value");
		var ln = t.length;

		if (ln == 0 || ln < 3)
		{		
			alert("יש לספק מילת חיפוש ארוכה מ-3 תווים");
			return false;
		}
	});

	// Select Menu for search.
	$("#searchSelectMenu a").click(function(){
		$("#searchSelectMenu div").toggle();
		$("#searchIn").text($(this).text());
		$("#section").val($(this).attr("href"));
		return false;
	});

	// Manage tag sizes.

	var tagLinkArray = jQuery.makeArray($(".tagBox span a"));
	var lenTagArray = tagLinkArray.length-1;

	var i = 0;

	for (i = 0; i <= lenTagArray/3; i++)
	{
		var rand = Math.floor(Math.random()*lenTagArray);
		var rand2 = Math.floor(Math.random()*lenTagArray);
		$(".tagBox span a:eq("+rand+")").css("font-size", "1.3em");
		$(".tagBox span a:eq("+rand2+")").css("font-size", "1.6em");
	}
});