﻿//onContent(function () {});
var Categories={
	List:[],
	_clientCache:{},

	Init:function () {
		if ($("CategoryBackground")) Categories.HomeBackground=$("CategoryBackground").src;

		Categories.AssignToElements();
		Categories.AssignEventsToTabs();
		Categories.Hashtableize();
		Categories.PreloadImages();
	},
	AssignEventsToTabs:function () {
		$("Categories").getElementsByClassName("category").each(function (tab) {

			var productCategory=tab.ProductCategory;
			/*
			tab.observe("click",function (e) {
				Categories.TabClick(productCategory);
			});
			*/
			tab.observe("mouseover",function (e) {
				var related=Event.relatedElement(e,false);
				if (!related || (related && related.childOf && related.childOf(tab))) return;
				Categories.TabOver(productCategory);
				//alert(productCategory.id);
			});
			tab.observe("mouseout",function (e) {
				var related=Event.relatedElement(e,true);
				if (!related || (related && related.childOf && related.childOf(tab))) return;

				Categories.TabOut(productCategory);
			});

		});
	},
	AssignToElements:function () {
		// assign each category to an HtmlDivElement, back and forth
		this.List.each(function (cat) {
			$("MainTab"+cat.ProductCategoryId).ProductCategory=cat;
			cat.Element=$("MainTab"+cat.ProductCategoryId);
		});
	},
	Hashtableize:function () {
		this.List.ById={};
		var l=this.List.ById;
		this.List.each(function (cat) { l[cat.ProductCategoryId]=cat; });
	},
	PreloadImages:function () {
		var images=[];
		this.List.each(function (cat) {
			images.push(cat.OpenedTabImage);
			images.push(cat.BackgroundImage);
		});
		// preload them all
		images.each(function (src) { document.createElement("img").src=src; /*alert(src);*/ });

	},
	LastTab:null,
	TabOver:function (productCategory) {

		if (productCategory==Categories.LastTab) return;
		var tab=productCategory.Element;
		var bg=tab.getElementsByClassName("bg")[0];
		var queueOut=Effect.Queues.get("catOut"+productCategory.ProductCategoryId);

		if(tab.hasClassName("lastCategory") && $('monthSpecial'))
			$('monthSpecial').style.display = 'none';

		if (queueOut.effects.length) Categories.CancelEffects(queueOut.effects);

		if(!tab.hasClassName("firstCategory")){
			tab.addClassName("opened");
		}else{
			tab.addClassName("openedFirst");
		}

		if (bg.filters && bg.filters[0]){
			 bg.filters[0].src=productCategory.OpenedTabBackground.replace("//","/");
		}else{
				//alert(productCategory.OpenedTabBackground.replace("//","/"));
			bg.style.backgroundImage="url('"+productCategory.OpenedTabBackground.replace("//","/")+"')";
			//alert(bg.style.backgroundImage);
			//bg.style.backgroundImage = "productCategory.OpenedTabBackground";
		}

		if(!tab.hasClassName("firstCategory")){
			new Effect.Move(bg,{y:-6,duration:.2,mode:"absolute",queue:{position:"end",scope:"catOver"+productCategory.ProductCategoryId}});
		}else{
			new Effect.Move(bg,{y:-5,duration:.2,mode:"absolute",queue:{position:"end",scope:"catOver"+productCategory.ProductCategoryId}});
		}

	},
	TabOut:function (productCategory) {
		if (productCategory==Categories.LastTab) return;
		var tab=productCategory.Element;
		var bg=tab.getElementsByClassName("bg")[0];
		var queueOver=Effect.Queues.get("catOver"+productCategory.ProductCategoryId);

		if (queueOver.effects.length) Categories.CancelEffects(queueOver.effects);

		if(!tab.hasClassName("firstCategory")){
			new Effect.Move(bg,{
				y:0,
				duration:.1,mode:"absolute",
				queue:{position:"end",scope:"catOut"+productCategory.ProductCategoryId},
				afterFinish:function () {
					if (bg.filters && bg.filters[0]) bg.filters[0].src=productCategory.ClosedTabBackground.replace("//","/");
					else bg.style.backgroundImage="url('"+productCategory.ClosedTabBackground.replace("//","/")+"')";
					tab.removeClassName("opened");

					if(tab.hasClassName("lastCategory") && $('monthSpecial'))
						$('monthSpecial').style.display = 'block';
				}
			});
			}else{
				new Effect.Move(bg,{
					y:0,
					duration:.1,mode:"absolute",
					queue:{position:"end",scope:"catOut"+productCategory.ProductCategoryId},
					afterFinish:function () {
						if (bg.filters && bg.filters[0])
							bg.filters[0].src=productCategory.ClosedTabBackground.replace("//","/");
						else
							bg.style.backgroundImage="url('"+productCategory.ClosedTabBackground.replace("//","/")+"')";

						tab.removeClassName('openedFirst');
					}
				});

		}
		//if(tab.hasClassName("lastCategory") && $('monthSpecial'))
		//	setTimeout("$('monthSpecial').style.display = 'block'",100);

	},

	TabCloseAll:function () {
		this.TabClose();
	},
	TabClose:function (tab) {
		if (!this.LastTab) return;
		this.LastTab.Element.removeClassName("active");
		var last=this.LastTab;
		this.LastTab=null;
		this.TabOut(last);
		$("CategoryBackground").src=Categories.HomeBackground;
		if (Categories.OpenSequence) Categories.OpenSequence.Abort();
	},
/*
	TabClick:function (productCategory) {

		var tab=productCategory.Element;

		isCategoryLobby=true;

		// click on one when already open - close it
		if (Categories.LastTab==productCategory) {
			this.TabClose(Categories.LastTab);
			return;
		}

		if (Categories.OpenSequence) {
			Categories.OpenSequence.Abort();
		}

		var sequence=new Devign.Sequence();

		var last=Categories.LastTab;
		Categories.LastTab=0;

		// click on one when the other is already open - close the current
		if (last) {
			sequence.Add(function (currSequence) {
				Categories.TabOut(last);
				last.Element.removeClassName("active");
				Categories.LastTab=productCategory;
				currSequence.Next();
			});
		}
		else Categories.LastTab=productCategory;

		tab.addClassName("active");

		sequence.Add(function (currSequence) {
			if (productCategory.NavCssClass) {
				$("Page").className="category"+productCategory.NavCssClass;
			}
			currSequence.Next();
		});

		// will be assigned in the sequence before use
		var categoryData;

		// texts
		sequence.Add(function (currSequence) {
			var cached=Categories._clientCache["GetCategory("+productCategory.ProductCategoryId+",true)"];
			if (cached) {
				categoryData=cached;
				setTimeout(function () {
					//1
					currSequence.Next();
				},100);
			}


		});


		sequence.Add(function (currSequence) {
			var cacheKey="Category.ChildProductCategoriesLinks(ProductCategoryId="+productCategory.ProductCategoryId+")";

			var cached=Categories._clientCache[cacheKey];
			var productsElement;

			if (cached) productsElement=cached;
			else {

				productsElement=document.createElement("div");
				productsElement.innerHTML=str;

				Categories._clientCache[cacheKey]=productsElement;
			}
		});


		Categories.OpenSequence=sequence;

		sequence.OnEnd=function () {
			Categories.OpenSequence=null;
		};

		sequence.Start();
	},

	SubTabClick:function (clickedProductCategoryId) {
		var categoryData=Categories.List.ById[clickedProductCategoryId];
	},
*/
	CancelEffects:function (effects) {
		effects.each(function (effect) { effect.cancel(); });
		effects.length=0;
	}
};


function OnFlashButtonClick(){
	//window.open('PopUp.aspx','pauzapopup','status=yes,width=382,height=451,left=0,top=0');
	//lightBox.show();

	var block = $('formLightBox');
	var content = $('formLightBoxContent');
	var width = Math.max(document.documentElement.clientWidth,Math.max(window.innerWidth || 0,document.documentElement.scrollWidth));
	var height = Math.max(document.documentElement.clientHeight,Math.max(window.innerHeight || 0,document.documentElement.scrollHeight))-50;

	block.show();
	content.show();
	content.style.left = (width - content.offsetWidth) /2 + 'px';
	content.style.top = '50px';//(height - content.offsetHeight) /2 + 'px';

}

function closeFormLightbox(){

	var block = $('formLightBox');
	var content = $('formLightBoxContent');

	block.hide();
	content.hide();
}

function submitFormLightbox(){

	var errCount = 0;
	if ($("fullname").value.length==0){
		$("fullname").focus();
		errCount++;
	}

	if ($("phone").value.length==0 && errCount==0){
		$("phone").focus();
		errCount++;
	}

	if (errCount>0)return false;

	$("lightboxSubmit").hide();
	$("lightboxSubmiting").show();


	new Ajax.Request('lightbox_form_action.aspx', {parameters:'fullname='+encodeURIComponent($("fullname").value)+'&email='+encodeURIComponent($("email").value)+'&phone='+encodeURIComponent($("phone").value),
		onSuccess:function(t) {
						//var xmlDoc = t.responseXML.documentElement;
						//alert (t.responseText);
						$("lightboxForm").hide();
						$("lightboxFormResponse").show();
						if (t.responseText=="ok"){
							$("lightboxFormResponse").innerHTML = "<p>תודה על פנייתך. <br/>נציגנו ייצור עמך קשר.</p>";
						}else{
							$("lightboxFormResponse").innerHTML = "<p>חלה שגיאה בשליחת המידע. <br/>אנא נסה שנית מאוחר יותר.</p>";
						}

					},
		onFailure:function(t) {
						$("lightboxForm").hide();
						$("lightboxFormResponse").show();
						//$("lightboxFormResponse").innerHTML ='Error ' + t.status + ' -- ' + t.statusText;
						$("lightboxFormResponse").innerHTML = "<p>חלה שגיאה בשליחת המידע. <br/>אנא נסה שנית מאוחר יותר.</p>";
					}
		});

	return false;

}





