

/*------------------------------------------------------------
# Author: Maxim Zetser, Ivan Chernykh  
# Created: 27/7/10                                                                   
# Description: This file includes all the general scripts that should be loaded in every page of the site.   
------------------------------------------------------------*/

var hebrew_arr=new Array();
hebrew_arr["books"]="עברית וספרות";
hebrew_arr["math"]="מתמטיקה";
hebrew_arr["english"]="אנגלית";
hebrew_arr["science"]="מדע וטכנולוגיה";
hebrew_arr["lands"]="גיאוגרפיה ומולדת";
hebrew_arr["environment"]="אני ילד ירוק";
hebrew_arr["lifestyle"]="חברה וכישורי חיים";
hebrew_arr["history"]="היסטוריה";
hebrew_arr["art"]="אמנות ותרבות";
hebrew_arr["holidays"]="חגים ומורשת";
hebrew_arr["computers"]="מידענות ומחשבים";

var channel_search_type = "";
var move = 0;

$(document).ready(function(){
						   
		$("#renew").click(function(){
			Renew();						   
		});
		
		if($("body").hasClass("homepage") == false){
			$("#acum_text , #acum_img").hide()
			}
		
		if(location.href.indexOf("english")!=-1 || location.href.indexOf("Field=3")!=-1){
		$("#search_button").attr("src","/images/search_btn.png");
		$("#search_button").attr("alt","Search");
		$("#search_button").attr("title","Search");
		
		$("#bread_crumbs img").each(function(i){
			$(this).attr("src" , "http://www.galim.org.il/images/channel/bread_crumbs_arrows_en.png");
			});	
		}
	
	
		$("#container").show();

		if($("body").attr("class") == "" && location.href.indexOf("reports") == -1){

			url=location.href;

			new_fieldId=url.substring(url.indexOf(".il/")+4, url.indexOf("/",url.indexOf(".il/")+4));
			new_fieldId_plus_subid=url.substring(url.indexOf(".il/")+4, url.indexOf(pagesOrEmpages(url),url.indexOf(".il/")+4));
			
	site_navigation_str="";
	
	$.each($(this).find("."+fieldId), function() 
		{
		if(fieldId!=new_fieldId)
			{
				
				old_href=$(this).attr("href");
				
				id_index=old_href.indexOf(".il/")+4;			
	
				new_href="";
	
				if(pagesOrEmpages(old_href)!="") // ... .il/holidays/miriam_net/empages/17  --> ... .il/computers/miriam_net/empages/17
				{			
					fieldId_plus_subid=old_href.substring(id_index, old_href.indexOf(pagesOrEmpages(old_href),id_index));
					new_href=old_href.replace(fieldId_plus_subid,new_fieldId_plus_subid);
					
				}
				else 
				if(old_href.indexOf(fieldId)==old_href.length-fieldId.length || old_href.indexOf(fieldId+"/")==old_href.length-fieldId.length+1) // ... .il/holidays/  --> ... .il/computers/
				{							
					new_href=old_href.replace(fieldId,new_fieldId);				
				}
				else 	 // ... .il/holidays/miriam_net  --> ... .il/computers/miriam_net
				{							
					fieldId_plus_subid=old_href.substring(id_index, old_href.length);
					new_href=old_href.replace(fieldId_plus_subid, new_fieldId_plus_subid);				
				}

				$(this).attr("href",new_href);

				if($(this).hasClass("id_title"))
				{
					
					old_title=$(this).html();
					new_title=old_title.replace(hebrew_arr[fieldId],hebrew_arr[new_fieldId]);
					$(this).html(new_title);
				}
			
				
			}

			if(window.sites_arr==undefined){}
			else{
				$(".act_site_header img:eq(0)").attr("src","http://www.galim.org.il/pools/files/GalimGifs/GalimGifPicture/"+sites_arr[new_fieldId_plus_subid.split("/")[1]]);
				
				if((new_fieldId_plus_subid.split("/")[1] != "pages") && (new_fieldId_plus_subid.split("/")[1] != "empages")) {

					$(".act_site_header img:eq(0)").attr("alt",sites_arr[new_fieldId_plus_subid.split("/")[1]+"_name"]);
					$(".act_site_header img:eq(0)").attr("title",sites_arr[new_fieldId_plus_subid.split("/")[1]+"_name"]);
					
					$("#bread_crumbs a:eq(2)").html(sites_arr[new_fieldId_plus_subid.split("/")[1]+"_name"]);
				   
					var bc = $("#bread_crumbs a:eq(2)").attr("href");

					bc = bc.split(".il/");	
					bc[1]= bc[1].split("/");
					
					var new_minisite_url = $("#bread_crumbs a:eq(2)").attr("href").replace(bc[1][1],new_fieldId_plus_subid.split("/")[1])
					$("#bread_crumbs a:eq(2)").attr("href",new_minisite_url);
					
					$.each($(".menu a") , function(){
						var tmp = $(this).attr("href");	
						var new_minisite_url = tmp.replace(bc[1][1],new_fieldId_plus_subid.split("/")[1]);
						$(this).attr("href",new_minisite_url);
						});
				}
			}
			 
		active_site=false;
	
		gal_str="gallery";			
		href_str=$(this).attr("href");
		sub_id=href_str.substring(href_str.indexOf("/",href_str.indexOf(new_fieldId)),href_str.indexOf(pagesOrEmpages(href_str)));
		
		if(url==href_str || ((href_str.indexOf(gal_str)==href_str.length-gal_str.length)&& url.indexOf(href_str)!=-1) || url==href_str.split(sub_id).join(""))
			{	
				$(this).parent().parent().parent().parent().parent().addClass("current");
				//alert($(this).parent().parent().parent().parent().parent().find("img").attr("src"))
				$(this).parent().parent().parent().parent().parent().find("img").attr("src",$(this).parent().parent().parent().parent().parent().find("input").attr("value"));
				active_site=true;
			}
		//alert($(this).parent().parent().parent().parent().parent().find("b").attr("counter"))
		if($(this).parent().parent().parent().parent().parent().find("b").attr("counter")) 
			var pag_value = $(this).parent().parent().parent().parent().parent().find("b").attr("counter");
		else
			var pag_value = $(this).parent().parent().parent().parent().parent().find("b").html();
			
			//alert(pag_value)
		if($(this).parent().parent().parent().parent().parent().hasClass("menu_item"))
			site_navigation_str+='<a '+((active_site)?'class="active_site"':'')+' href="'+$(this).attr("href")+'">'+pag_value+'</a> '
	
	   });
		
		$(".site_navigation .content").html("<div>"+site_navigation_str+"</div>");
		
		if($(".site_navigation .content div").children("a").length == 1) $(".site_navigation").hide();
		var prev_act = "http://www.galim.org.il/images/channel/prev.png";
		var next_act = "http://www.galim.org.il/images/channel/next.png";
		if(location.href.indexOf("english")!=-1){
			var prev_act = "http://www.galim.org.il/images/channel/prev_en.png";
			var next_act = "http://www.galim.org.il/images/channel/next_en.png";
			}
			
		if(!$(".menu_item:eq(0)").hasClass("current"))
			$(".site_navigation .content div:eq(0)").prepend('<a href="'+$(".menu_item.current").prev().find("a").attr("href")+'"><img src="'+prev_act+'" /></a>');
	
		if(!$(".menu_item:last").hasClass("current"))
			$(".site_navigation .content div:eq(0)").append('<a href="'+$(".menu_item.current").next().find("a").attr("href")+'"><img src="'+next_act+'" /></a>');
	
		$("#MapFieldLogo area:eq(1)").attr("href",String($("#MapFieldLogo area:eq(1)").attr("href")).replace(fieldId,new_fieldId));
	
		fieldId=new_fieldId; 
	
		document.body.className=(fieldId);
		
		if(location.href.indexOf("pages") != -1){
		var searchContent = $("#search").html();
			searchContent = searchContent.replace('<!--Replace %%_3_%% -->','<div class="box grey search_type_container" ><div class="content"><img src="/images/top/search_type_off_arrow.png" id="search_type_arrow" /><div id="search_types" ><span>בגלים</span><br /><span id="search_type_channel">ב'+hebrew_arr[fieldId]+'</span></div></div></div><span id="search_type_selected"> בגלים</span>')
			$("#search").html(searchContent);
			BuildBoxes();
		}
		
		$.get("http://www.galim.org.il/cgi-bin/galim/ajaxScript/openNavigation.pl?FieldName="+fieldId, function(data){
				var tmp = $("#content").html();

				if(location.href.indexOf("pages") == -1){
					$("#content").prepend(data);
					BuildBoxes();
					SetFloatHeight();
					if(window.SetNavigationHeight!=undefined) SetNavigationHeight();
					$("#search_type_channel").html("ב"+hebrew_arr[fieldId]);
					
					
					$(".bread_crumbs_active").html(hebrew_arr[fieldId])
					var bread_crumbs_active_href =  $(".bread_crumbs_active").attr("href");
					if(bread_crumbs_active_href!=undefined)bread_crumbs_active_href = bread_crumbs_active_href.replace(".html",fieldId+".html");
					$(".bread_crumbs_active").attr("href",bread_crumbs_active_href)
					$("#MapFieldLogo area:eq(1)").attr("href",bread_crumbs_active_href);
					$("#MapFieldLogo area:eq(1)").attr("title",hebrew_arr[fieldId]);
					$("#MapFieldLogo area:eq(1)").attr("alt",hebrew_arr[fieldId]);
				}
				else{
					if(location.href.indexOf("empages") != -1 || location.href.indexOf("wgallery") != -1 || location.href.indexOf("pages") != -1){
						$("#bread_crumbs a:eq(1)").attr("href","/fields/"+fieldId+".html");	
						var new_url = location.href;
						
						if(location.href.indexOf("empages") != -1) new_url = new_url.split("empages");
						
						if(location.href.indexOf("wgallery") != -1) new_url = new_url.split("pages");
						
					    //alert(sites_arr[new_fieldId_plus_subid.split("/")[1]])
						$(".bread_crumbs_active").attr("href",new_url[0]+"sites/");
						if(location.href.indexOf("pages") != -1 && ($("#bread_crumbs a:eq(2)").html()!=undefined && $("#bread_crumbs a:eq(2)").html().indexOf("sites")!=-1)){
							if(new_url.split!=undefined) new_url = new_url.split("pages");
							$("#bread_crumbs a:eq(2)").attr("href",new_url[0]+"sites/");
							
								
							}
						}
					
					}
			});	
		}			   

		$("#cse-search-results iframe").height(parseFloat($("#cse-search-results iframe").height())-400);
		
		//Search type//	
		
		var search_type_flag = false;
		var old_mode;
		var new_mode;
		var h;
		if($(".search_type_container")) 

		$(".search_type_container , #search_type_selected").click(function(){
			var arrow_src = $("#search_type_arrow").attr("src");
			if(search_type_flag == false){
				old_mode = "off";
				new_mode="on";
				h="25px";
				search_type_flag = true;
				$("#search_type_channel").show();
				}
			else{
				old_mode = "on";
				new_mode="off";
				h="6px";
				search_type_flag = false;
				}
				
			arrow_src = arrow_src.replace(old_mode,new_mode);
			$("#search_type_arrow").attr("src",arrow_src);
			
			$(".search_type_container .content , .search_type_container .left.line , .search_type_container .right.line").animate({
				height: h
			  }, 200, function() {
				// Animation complete.
				if(search_type_flag == true){
					$("#search_type_selected").hide();
					$("#search_types").show();
				}
				else{
					$("#search_types").hide();
					$("#search_type_selected").show();
					}
			  });	 
		});
		
		$("#search_types span").click(function(){
			$("#search_type_selected").html($(this).html());
			channel_search_type = $(this).html();
			channel_search_type = channel_search_type.substr(1,channel_search_type.length)
			if(channel_search_type.indexOf(" ") != -1) 
				channel_search_type = channel_search_type.replace(" ","_");	
		});
		
		$("#q").focus(function(){
			$("#q").attr("value","");				   
		});

		//End of Search type//

		if(document.getElementById("login_status_0") != null){			   
			if(document.getElementById("login_status_0").style.display != "none") document.getElementById("user").focus();
		}	
		BuildBoxes();
		$(".sitemap > .left.line , .sitemap > .right.line").height($(".sitemap >.content").height()+parseFloat($(".sitemap >.content").css("padding-bottom")));
		SetFloatHeight();
		SetFloatWidth();
		

		/////////////////Navigation Top Menu /////////////////
		
		var link_tmp;				
		$("#links div").hover(function(e){  
		   $('#marker').show();
		   $(this).find("h5").css("color","#fff");
		   $('#marker').stop();
		    link_tmp = $(this);
			
			new_y = $(this).position().top;
			$('#marker').animate({
				marginTop: new_y-30
								
			  }, 100, function() {	
				
			  });				
		},
		function(){		
			$(this).find("h5").css("color","#3d3f3f");
			$(link_tmp).children().each(function(index){	
 			 });
			 $('#marker').hide();
		});
		
		/////////////////End of Navigation Top Menu /////////////////	
		$('body').append('<div id="writeus" class="window"></div><div id="mask"></div>')

		if(typeof(constructButtons)=="function")	constructButtons($(".button")) 
		
		if(window.documentReady!=undefined){window.documentReady()}
		
		if(location.href.indexOf("report")!=-1){
			$("#reports").width($(".reports").width());
			window.onload = function(){
				SetFloatHeight()
			}
			
		}	
	$(window).resize(function() {
		SetFloatHeight();
		SetFloatWidth();
	});
});


function BuildBoxes(){	
		var top = '<div class="right top corner"></div><div class="top line"></div><div class="left top corner"></div><div class="clear"></div><div class="right line"></div>';
		
		var bottom = '<div class="left line"></div><div class="clear"></div><div class="right bottom corner"></div><div class="bottom line"></div><div class="left bottom corner"></div>';
		
		$(".box").each(function(i){
			if($(".box:eq("+i+")").children().length == 1){
				$(".box:eq("+i+")").prepend(top);
				$(".box:eq("+i+")").append(bottom);
			}					
		});			
	}

		/////////////////End of Dictionary//////////

		function getCSSAttr(attr){	
			if(attr==undefined || isNaN(attr)) attr=0;
			return attr;
		}

		function SetFloatHeight(){
			
			$(".float_height").each(function(x){
		$(this).children (" .left.line:first ,  .right.line:first").height( $(this).children(" .content:first").height()+getCSSAttr(parseFloat($(this).children(" .content:first").css("padding-bottom")))+getCSSAttr(parseFloat($(this).children(" .content:first").css("padding-top")))) 
										 });
			}
			
		function SetFloatWidth(){
		$(".float_width").each(function(x){
		$(this).children (" .top.line:first ,  .bottom.line:first").width( calculateTotalWidth($(this))) ;	
		$(this).children(".content:first").width($(this).children (" .top.line:first").width());
		$(this).width(calculateTotalWidth($(this))+$(this).children(".left:first").width()+$(this).children(".right:first").width())
			});
			}

		function calculateTotalWidth(obj){
			return parseFloat( $(obj).children(".content:first").width()) + getCSSAttr(parseFloat($(obj).children(".content:first").css("padding-right"))) + getCSSAttr(parseFloat($(obj).children(" .content:first").css("padding-left")))
			
			}
////////////////////// getElementsByClassName /////////////////////////////////////

function getElementsByClassName(searchClass,node,tag) {
        var classElements = new Array();
        if (node == null )
                node = document;
        if (tag == null )
                tag = '*';
        var els = node.getElementsByTagName(tag);
        var elsLen = els.length;
        var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
        for (i = 0, j = 0; i < elsLen; i++) {
        	if(pattern.test(els[i].className) ) {
				classElements[j] = els[i];
				j++;
            }
        }
        return classElements;
}

function preSearchSubmit(){
	if(channel_search_type!="")
	$('#q').attr("value", $('#q').attr("value")+" more:"+channel_search_type+"");
	document.getElementById("form_search").submit();
	return true;
}

function pagesOrEmpages(url){
	return (url.indexOf("/pages/")==-1)?((url.indexOf("/empages/")==-1)?"":"/empages/"):"/pages/";
}

function Renew(e,x){	
	   // e.preventDefault();
		var id = $("#renew_form_back");
		var maskHeight = $(document).height();
		var maskWidth = $(window).width();	
		$('#mask_tools').css({'width':maskWidth,'height':maskHeight});
		$('#mask_tools').show();
		$('#mask_tools').css("opacity",0);
		$('#mask_tools').animate({    opacity: 0.6  }, 1000, function() {});
		var winH = 100;
		var winW = $(window).width();
		$(id).css('top',  -50);
		$(id).css('left', winW/3-$(id).width()/2);	
		$(id).fadeIn(2000); 
		$('#mask_tools , #renew_close').click(function () {
		$('#mask_tools').hide();
		$('.window').hide();
		$(id).hide();
		}); 
}
		
function SendFormRenew(){
	var counter = 0;
	$("#renew_form textarea").each(function(){								
		if($(this).attr("value") != "") counter++;							
	});
	if(counter!=0){
		$.post("http://www.galim.org.il/cgi-bin/galim/about/contact.pl", $("#renew_form").serialize());
		$('#mask_tools').hide();
		$('.window').hide();
		$("#renew_form_back").hide();
		}
	}
	





