/**
 * for network's store feature
 *
 * @package Application
 * @category JavaScript
 * @subpackage store
 * @author Louis
 * @version $0.1$
 * @copyright 14, July 2008
 */

var store = {
	showGroupMenu : function(o,sdomain)
	{
		if(sdomain.length<=0) return; 
		var floatGroupMenu = getx('floatGroupMenu');
		var ShortHandNav = getx('ShortHandNav');
		if(!floatGroupMenu || !ShortHandNav)
			return ;
		var showGroupMenuTimer;
		ShortHandNav.style.position = 'relative';
		floatGroupMenu.style.left = ShortHandNav.offsetLeft + 'px';
		floatGroupMenu.style.top = ShortHandNav.offsetTop + ShortHandNav.offsetHeight + 'px';
		var select=document.getElementsByTagName('select');
		if (Version.ie && !Version.ie7 && select.length)
		{
			var div=makex('div');
			with (div.style)
			{	position='absolute';
				//visibility='hidden';
				zIndex=2;
				top = floatGroupMenu.offsetTop + 'px';
				left = floatGroupMenu.offsetLeft + 'px';
				width= floatGroupMenu.offsetWidth + 'px';
				height= floatGroupMenu.offsetHeight + 'px';
				background = 'white';
			}
			document.body.appendChild(div);
			var ifm = addx(div,makex('iframe'));
			with(ifm.style){border='none';width='100%';height='100%';top='0px';left='0px';filter="Alpha(Opacity=0)";}
			o._cover = div;
			floatGroupMenu._cover = div;
			div.style.display = 'none';
		}
		if(!this.GroupMenu) this.initGroupMenu(sdomain);
		o._holder=floatGroupMenu;
		o.ShortHandNav = ShortHandNav;
		o.onmouseover=function(){
			cleartimer(this._holder._timer);
			this.ShortHandNav.style.position = 'relative';
			this._holder.style.left = this.ShortHandNav.offsetLeft + 'px';
			this._holder.style.top = this.ShortHandNav.offsetTop + this.ShortHandNav.offsetHeight + 'px';
			if (this._cover)
			{
				cleartimer(this._cover._timer);
				this._cover.style.left = this.ShortHandNav.offsetLeft + 'px';
				this._cover.style.top = this.ShortHandNav.offsetTop + this.ShortHandNav.offsetHeight + 'px';
				this._cover.style.display = 'block';
			}
			this._holder.style.display='block';
			if(!store.GroupMenu) store.initGroupMenu(sdomain);
		};
		o.onmouseout=function(){
			cleartimer(this._holder._timer);
			var _holder=this._holder;
			this._holder._timer=setTimeout(function(){_holder.style.display='none';},500);
			if(this._cover)
			{
				cleartimer(this._cover._timer);
				var _cover = this._cover;
				this._cover._timer=setTimeout(function(){_cover.style.display='none';},500);
			}
		};
		floatGroupMenu.onmouseover=function(){
			cleartimer(this._timer);
			if(this._cover) cleartimer(this._cover._timer);
		};
		floatGroupMenu.onmouseout=function(){
			cleartimer(this._timer);
			var _holder=this;
			this._timer=setTimeout(function(){_holder.style.display='none';},500);
			if(this._cover)
			{
				cleartimer(this._cover._timer);
				var _cover = this._cover;
				this._cover._timer=setTimeout(function(){_cover.style.display='none';},500);
			}
		};
		floatGroupMenu.style.display = 'none';
	},
	hideGroupMenu : function()
	{
		var floatGroupMenu = getx('floatGroupMenu');
		if(floatGroupMenu)
		{
			floatGroupMenu.style.display = 'none';
		}
	},		
	initGroupMenu : function(sdomain){
		//Ext.QuickTips.init();
	    Ext.BLANK_IMAGE_URL = '/scripts/ext21/resources/images/default/s.gif';
	    var Tree = Ext.tree;
	    
	    var Tree_Group_Loader = new Tree.TreeLoader({
	        dataUrl   :"/"+sdomain+"/store/loadgroupshaslink"
	    });
	    
	    var Tree_Group = new Tree.TreePanel({
	        collapsible      : false,
	        animCollapse     : false,
	        id               : "groupTree",
	        el               : "groupTree",
	        autoScroll       : true,
	        animate          : false,
	        enableDD         : false,
	        containerScroll  : true,
	        rootVisible 	 : false,
	        width			 : 230,
	        loader			 : Tree_Group_Loader
	        
	    });
	
	    // set the root node
	    var Group_Root = new Tree.AsyncTreeNode({
	        text: getJs(1555),
	        draggable:false,
	        id:'0'
	    });
	    
	    Tree_Group.setRootNode(Group_Root);	
	    
	    Tree_Group.on("click",myClick,this);
		
	    // render the tree
	    Tree_Group.render();
	    
	    //expand the root node.
	    Group_Root.expand();
	    this.GroupMenu = true;
	    
	    function myClick(node,e){
			if(node.attributes.urltitle.length>0)
	    		gotoUrl('/'+sdomain+'/store/group/'+node.id+'/'+node.attributes.urltitle);
	    	else
	    		gotoUrl('/'+sdomain+'/store/group/'+node.id);
		}
	},
	contact : function(email,storeId)
	{
		if(email.trim().length>0)
		{
			var para = {'target_email':email.trim(),'storeId':storeId};
			$.ajax({
				beforeSend:function(){
					var _box=makex('div',{'id':'FloatStore'});
					var content = '<p class="myLoading"></p>';
					_box.innerHTML = content;
					FloatBox.draw(_box,'false');
				},
				success:function(data){
					FloatBox.cancel();
					var json=eval('(' + data + ')');
					if(json.flag == 'successful')
					{
						var _box=makex('div',{'id':'FloatStore'});
						_box.innerHTML = json.html;
						FloatBox.draw(_box,'false');
						var myemail = getx('email');
						if(myemail) myemail.focus();
					}
					
				},
				type:'POST',
				data: para,
				url : '/networkstore/showcontact'
			});
		}
	},
	sendMail : function(o)
	{
		
		var target_email = getx('target_email');
		if(target_email)
			var targetEmail = target_email.value.trim();
		else
			var targetEmail = '';
		var storeId = getx('storeId');
		if(storeId)
			var sId = storeId.value.trim();
		else
			var sId = '';
		var email = getx('email');
		if(email)
			var emailVal = email.value.trim();
		else
			var emailVal = '';
		var messgae = getx('message');
		if(messgae)
			var msgVal = messgae.value.trim();
		else
			var msgVal = '';
			
		var para = {'target_email':targetEmail,'storeId':sId,'email':emailVal,'message':msgVal};
		FloatBox.cancel();
		$.ajax({
			beforeSend:function(){
				var _box=makex('div',{'id':'FloatStore'});
				var content = '<p class="myLoading"></p>';
				_box.innerHTML = content;
				FloatBox.draw(_box,'false');
			},
			success:function(data){
				FloatBox.cancel();
				var json=eval('(' + data + ')');
				if(json.flag == 'successful')
				{
					Alert({'Title':getJs(1734),'Question':getJs(1908)});
				}
				else
				{
					var _box=makex('div',{'id':'FloatStore'});
					_box.innerHTML = json.html;
					FloatBox.draw(_box,'false');
				}
				
			},
			type:'POST',
			data: para,
			url : '/networkstore/sendusermail'
		}); 
	}
};