﻿

$DL(
	function(){
		Site.init();
		QuickSearch.init();	
		MoreInfo.init();
		if($("page")){
			var heightFixed=$("page").getElement(".heightFixed");	
			if(heightFixed){			
				//var el = new Element("div", {"style":"height:100px;width:100px;background-color:red"}).injectInside(heightFixed);			
				var el=document.createElement("div");
				el.style.height="100px";
				el.style.width="100px";
				heightFixed.appendChild(el);
			}
		}
		if(Config.IsHomePage){
			HomePage.init();
		}
	},5
);

var Config={
	SiteUrl:null,
	UploadPath:null,
	IsHomePage:false
};

var Resources={
	RegisterCssFile:function(href){
		var headID = document.head;
		var cssNode = document.createElement("link");
		cssNode.type = "text/css";
		cssNode.rel = "stylesheet";
		cssNode.href = href;	
		headID.appendChild(cssNode);
	}
};


var Site = {	
	init: function(){
		if($("page")){
			var print=$("page").getElement(".print");
			if(print){
				print.addEvent("click",function(){
					window.print();
				});
			}
			
			$$("#page .header .listMenu li").each(function(item){
				item.addEvent("mouseenter",function(){
					this.setStyle("background-color","#e4e4e4");
				});
				item.addEvent("mouseleave",function(){
					this.setStyle("background-color","");
				});
			});
		}
		
		
		//var init = function(){
			Note.init();
		//}
		//init.delay(1000);
	}
};

var Note={
	_element:null,
	init:function(){
		//alert(notePos);
		if($("note")){ 
			this._element=$("note");
			var obj={};
			
			switch(notePos){
				case "1":
					obj={"top":150,"right":20,"opacity":0};
					break;
				case "2":
					obj={"top":535,"right":40,"opacity":0};
					break;
				case "3":
					obj={"top":495,"left":40,"opacity":0};
					break;
			}
			this._element.setStyles(obj);
			this._element.getElement(".close").addEvent("click",function(){
				Note._element.hide();	
			});
			var fx = new Fx.Style(Note._element, 'opacity',{duration:1000});
			var time;
			if(Config.IsHomePage) time=2000;
			else time=1000;
			fx.start.delay(time, fx, [0, .8]);
			fx.start.delay(noteTimeDisplay*1000, fx, [.8, 0]);
		}
	}
}

var HomePage={
	_sideBar:null,
	_footer:null,
	_search:null,
	init: function(){		
		if($("default")){			
			this._footer=$("default").getElement(".footer");
			this._search=$("default").getElement(".search");
			this._sideBar=$("sidebar");
			if (this._sideBar){				
				HomePage.appearText();
			}
		}
	},
	appearText: function(){
		var timer = 0;
		var sideblocks = $$('#sidebar li.item');
		
		var slidefxs = [];
		var colorfxs = [];
		
		sideblocks.each(function(el, i){			
			HomePage.createOver(el, i);
		}, this);
	},	
	createOver: function(el, i){		
		var first = el.getFirst();
		if (!first || first.getTag() != 'a') return;
		var overfxs = new Fx.Styles(first, {'duration': 200, 'wait': false});
		var tocolor, fromcolor;
		
		tocolor = 'f0c929';
		fromcolor = '000000';		
		toBg = 'f6f6f6';
		fromBg = 'f6f6f6';
		
		el.mouseouted = true;
		el.addEvent('mouseenter', function(e){
			overfxs.start({
				'color': tocolor,
				'margin-left': 5,				
				'width':165
			});
		});
		el.addEvent('mouseleave', function(e){
			overfxs.start({
				'color': fromcolor,
				'margin-left': 0,				
				'width':178
			});
		});
	},
	toggleMenu:function(){
		//alert(this._sideBar);
		if(this._sideBar) this._sideBar.toggle();
		if(this._footer) this._footer.toggle();
		if(this._search) this._search.toggle();
	}
}

var QuickSearch={
	_control:null,
	_button:null,
	_tree:null,
	_treeHeight:null,
	init:function(){
		this._control=$("quick-search");
		if(this._control){
			this._relatedBarHeight=$("page").getElement(".related-bar").getStyle("height").toInt();
			this._topPlus=this._relatedBarHeight==0 ? 24 : 35;			
			this._control.setStyles({top:this._relatedBarHeight+this._topPlus});
			this._rcElement=$("page").getElement(".related");
			this._rcHeight=this._rcElement.getStyle("height").toInt();
			this._button=this._control.getElement(".top");
			this._tree=this._control.getElement(".tv");		
			this._treeHeight=this._tree.getStyle("height").toInt();
			this._tree.setStyles({'height':0});
			
			var fxSlide = new Fx.Styles(this._tree, {wait: false,duration: 400, transition: Fx.Transitions.Quart.easeOut});
			var fxTop = new Fx.Styles(this._control, {wait: false,duration: 500, transition: Fx.Transitions.Quart.easeOut});
			var fxContent = new Fx.Styles(this._rcElement, {wait: false,duration: 400, transition: Fx.Transitions.Quart.easeOut});
			
			if(this._button){
				this._button.addEvent("click",function(){
					if(QuickSearch._tree.getStyle("height").toInt()==0){
						
						QuickSearch._tree.setStyles({'border-bottom':'solid 2px #f0c929','overflow':'hidden'});
						QuickSearch._control.setStyles({'z-index':600});						
						
						fxSlide.start({'height': 150});
						fxTop.start({'top':24});						
					}
					else{						
						QuickSearch._tree.setStyles({'border-bottom':'none'});
						
						if(QuickSearch._rcHeight>=0)fxContent.start({'height':QuickSearch._rcHeight});
						fxSlide.start({'height': 0}).chain(function(){
							QuickSearch.collapseTree();
							QuickSearch._control.setStyles({'z-index':500});
						});						
						fxTop.start({'top':QuickSearch._relatedBarHeight+QuickSearch._topPlus}).chain(function(){
							//QuickSearch._control.setStyles({'position':''});							
						});
					}	
				});
				this._button.addEvent('mouseenter', function(e){
					this.addClass("top-over");
				});
				this._button.addEvent('mouseleave', function(e){					
					this.removeClass("top-over");
				});
			}
		}
		this.initProductsTree();
	},
	initProductsTree:function(){		
		var	last=null;
		var	lastDepth=0;
		var depth=0;
		var treeElement=null;
		var relatedElement=null;
		if($("quick-search")){
			treeElement=$("quick-search").getElement(".tv");
		}
		if($("page")){
			relatedElement=$("page").getElement(".related")
		}
		$$("#products-tv ul").hide();
		$$("#products-tv li span").each(function(el,index){		
			el.addEvent("click",function(){			
				if(this.getParent().getElement("ul")){
					depth=this.getParent().className.replace("depth-","").replace(" sel","").replace(" collapse","");				
					this.getParent().getElement("ul").toggle();
					this.getParent().toggleClass("collapse");
					if(depth==0) if(treeElement!=null) treeElement.setStyles({'overflow':'','height':''});
					if(depth==1) this.getParent().toggleClass("sel");											
					
					var self=this;
					//alert(depth)
					if(depth>=0){
						$$("#products-tv li.depth-"+depth).each(function(item){
							if(item!=self.getParent() && item.hasClass("collapse")){
								item.getElement("ul").toggle();
								item.toggleClass("collapse");
								if(depth==1)item.toggleClass("sel");
							}
						});
					}
					if(relatedElement!=null){
						relatedElement.setStyle('height',treeElement.getStyle('height').toInt()+47);
					}
				}
			});
		});
	},
	collapseTree:function(){
		$$("#products-tv ul").hide();
		$$("#products-tv li").each(function(el){
			el.removeClass("sel");
			el.removeClass("collapse");
		});
		//$("products-tv").getElement(".root").getFirst().toggleClass("collapse");
	}
};
var MoreInfo={
	_control:null,
	_button:null,
	_tree:null,
	_treeHeight:null,
	init:function(){
		this._control=$("more-info");
		if(this._control){
			this._relatedBarHeight=$("page").getElement(".related-bar").getStyle("height").toInt();			
			this._rcElement=$("page").getElement(".related");
			this._rcHeight=this._rcElement.getStyle("height").toInt();
			this._topPlus=this._relatedBarHeight==0 ? 80 : 92;
			this._control.setStyles({top:this._relatedBarHeight+this._topPlus});
			this._button=this._control.getElement(".top");
			this._tree=this._control.getElement(".tv");		
			this._treeHeight=this._tree.getStyle("height").toInt();
			this._tree.setStyles({height:0});
			
			var fxSlide = new Fx.Styles(this._tree, {wait: false,duration: 400, transition: Fx.Transitions.Quart.easeOut});
			var fxTop = new Fx.Styles(this._control, {wait: false,duration: 500, transition: Fx.Transitions.Quart.easeOut});
			var fxContent = new Fx.Styles(this._rcElement, {wait: false,duration: 400, transition: Fx.Transitions.Quart.easeOut});
			
			if(this._button){
				this._button.addEvent("click",function(){
					if(MoreInfo._tree.getStyle("height").toInt()==0){
						MoreInfo._control.setStyles({'z-index':600});
						MoreInfo._tree.setStyles({'border-bottom':'solid 2px #f0c929'});
						fxSlide.start({'height': 350});						
						if(MoreInfo._rcHeight<400) fxContent.start({'height':400});
						fxTop.start({'top':24});
					}
					else{
						MoreInfo._control.setStyles({'z-index':500});
						MoreInfo._tree.setStyles({'border-bottom':'none'});												
						if(MoreInfo._rcHeight<0) MoreInfo._rcHeight=0;
						if(MoreInfo._rcHeight>=0) fxContent.start({'height':MoreInfo._rcHeight});
						fxSlide.start({'height': 0}).chain(function(){
							MoreInfo.toggle(true);
						});
												
						fxTop.start({'top':MoreInfo._relatedBarHeight+MoreInfo._topPlus}).chain(function(){
							//MoreInfo._control.setStyles({'position':''});							
						});
					}	
				});
				
				this._button.addEvent('mouseenter', function(e){
					this.addClass("top-over");
				});
				this._button.addEvent('mouseleave', function(e){					
					this.removeClass("top-over");
				});
			}
		}		
	},
	toggle:function(showForm){
		if(this._control){
			var f=this._control.getElement(".form-panel");
			var sent=this._control.getElement(".sent-panel");
			if(showForm){
				f.show();
				sent.hide();
			}else{
				f.hide();
				sent.show();
			}
			f.getElements(".txt").each(function(inp){
				inp.value="";
			});
						
		}
	}
};
