var LoginBox = Class.create({	
	
	initialize: function() 
	{
		/*if (!$('loginOverlay')) return;
		
		this.error = $$('#loginOverlay p.error .message').first();
		
		this.loginForm = $$('form[name=loginOverlayForm]').first();		
		this.button = this.loginForm.select('input[type=image]').first();
		
		this.username = this.loginForm.select('input[name=username]').first();
		this.password = this.loginForm.select('input[name=password]').first();
		
		this.loginForm.onsubmit = function(event) {
			Event.stop(event);
			this.login();
		}.bindAsEventListener(this);		
		
		var dimensions = $('loginOverlay').getDimensions();
		window.loginbox = new app.lightbox('loginOverlay', dimensions.width, dimensions.height, { 
			lightbox: 'loginbox',
			before: function() {
				this.error.hide();
			}.bind(this)
		});
										
		$$('#loginOverlay .close a').first().onclick = function(e)
		{
			Event.stop(e);
			delete window.loginbox.after;
			window.loginbox.hide();
		}
		
		var dimensions = $('permissionOverlay').getDimensions();
		window.permissionbox = new app.lightbox('permissionOverlay', dimensions.width, dimensions.height, { lightbox: 'permissionbox' });		
		
		$$('#permissionOverlay .close a').first().onclick = function(e)
		{
			Event.stop(e);
			window.permissionbox.hide();
		}	*/	
	},
	
	login: function()
	{
		if (this.pressed()) return;
	
		this.username = this.loginForm.select('input[name=username]').first();
		this.password = this.loginForm.select('input[name=password]').first();
	
		this.username.disabled = true;
		this.password.disabled = true;
	
		if (this.error.visible()) Effect.Fade(this.error);	
	
		var error = false;
		if (!this.username.value.strip() || this.username.value == this.username.defaultValue)
		{
			new Effect.Highlight(this.username);
			error = true;
		}
		if (!this.password.value.strip() || this.password.value == this.password.defaultValue)
		{
			new Effect.Highlight(this.password);
			error = true;
		}
		if (error) return;
		
		this.press();
		
		var callback = function(json)
		{
			this.unpress();
			this.password.value = '';
			this.username.disabled = false;
			this.password.disabled = false;
			
			if (json.success) {
				window.loginbox.hide();
			} else {
				Effect.Appear(this.error);
			}
		}.bind(this);
		
		var errorCallback = function()
		{
			this.unpress();
			this.password.value = '';
			this.username.disabled = false;
			this.password.disabled = false;				
			Effect.Appear(this.error);
		}.bind(this);
		
		var parameters = {
			username: this.username.value.strip(),
			password: this.password.value.strip()		
		};
		
		setTimeout(function() { 		
			app.jsonLoad('/login/', { 
				parameters: parameters,				
				callback: callback,
				errorCallback: errorCallback
			});
		}.bind(this), 500);
		
	},
	
	pressed: function()
	{
		return this.button.src.match(/\-on.(\w+)$/);
	},
	
	press: function()
	{
		this.button.src = this.button.src.replace(/(.*?)\.(\w+)$/, '$1-on.$2');		
	},
	
	unpress: function()
	{
		this.button.src = this.button.src.replace(/(.*?)-on\.(\w+)$/, '$1.$2');		
	}
	
});



var Panel = Class.create({	
	
	initialize: function(panel, options)
	{
		this.start(panel, options);
	},
	
	start: function(panel, options)
	{		
		this.options = options || {};
		
		this.panel = $(panel);
		if (!this.panel) return;

		this.button = $(this.options.button) || $(this.panel.id+'Chooser');
		this.on = this.options.on || 'on';
		
		this.button.onclick = function(e) {
			Event.stop(e);

			if (!this.visible()) {
				this.open();
			}
			else {
				this.close();
			}
		}.bindAsEventListener(this);		
		
		Event.observe(document, 'click', function(e) { 
			var x = Event.element(e);
			if (x == this.panel || x.descendantOf(this.panel)) return;
			if (this.visible()) this.close();
		}.bindAsEventListener(this));	
		
		if (this.onInit) this.onInit(); // FOR CHILD CLASSES
		if (this.options.onInit) this.options.onInit(); // RUNTIME
	},

	open: function()
	{
		this.panel.show();
		if (this.options.on !== false)
			this.button.addClassName(this.on);

		if (this.onOpen) this.onOpen(); // FOR CHILD CLASSES
		if (this.options.onOpen) this.options.onOpen(); // RUNTIME
	},

	close: function()
	{
		Effect.Fade(this.panel, { duration: 0.2 });
		if (this.options.on !== false)		
			this.button.removeClassName(this.on);	

		if (this.onClose) this.onClose(); // FOR CHILD CLASSES			
		if (this.options.onClose) this.options.onClose(); // RUNTIME
	},
	
	visible: function()
	{
		return this.panel.visible();
	}
});

Panel.editor = Class.create(Panel, {
	
	initialize: function(panel, options)
	{	
		this.options = options || {};
		
		this.editor = this.options.editor;
		this.start(panel, this.options);
	},
	
	onOpen: function()
	{
		this.__onOpen();
	},
	
	onClose: function()
	{
		this.__onClose();
	},
	
	__onOpen: function()
	{
		if (!this.editor) return;

		this.editor.bookmark = this.editor.selection.getBookmark();
		this.editor.selection.moveToBookmark(this.editor.bookmark);
	},
	
	__onClose: function()
	{
		if (!this.editor) return;

		this.editor.selection.moveToBookmark(this.editor.bookmark);
		this.editor.bookmark = null;	
	}
});

Panel.editor.link = Class.create(Panel.editor, {	

	onInit: function()
	{
		this.input = this.panel.select('input').first();
		this.edit = this.panel.select('.edit').first();
		this.save = this.panel.select('.save').first();
		this.cancel = this.panel.select('.cancel').first();
		
		this.input.onkeyup = function(e) {
			var keycode = e.charCode || e.keyCode;
			if (keycode == 13 || keycode == 10 || keycode == 3)
			{
				this.link();
				return;
			}
			var esc = window.event ? 27 : e.DOM_VK_ESCAPE;	      
			if (keycode == esc)
			{
				this.close();
				return;
			}
		}.bindAsEventListener(this);		
		
		this.input.onkeydown = function(e) {
			var keycode = e.charCode || e.keyCode;
			if (keycode == 13 || keycode == 10 || keycode == 3)
			{
				Event.stop(e);
				return;
			}			
		}.bindAsEventListener(this);
		
		this.save.onclick = this.edit.onclick = function(e) {
			Event.stop(e);
			this.link();
		}.bindAsEventListener(this)
		
		this.cancel.onclick = function(e) {
			Event.stop(e);
			this.unlink();
		}.bindAsEventListener(this)
	},

	onOpen: function()
	{
		this.__onOpen();		
		this.input.focus();
		this.input.value = this.input.value;		
		
		if (this.input.value && this.input.value != this.input.defaultValue)
		{ 
			this.save.hide();
			this.edit.show();
			this.cancel.show();			
		}
		else
		{
			this.save.show();
			this.edit.hide();	
			this.cancel.hide();						
		}
	},

	fill: function(x)
	{
		if (this.halt) return;
		this.input.value = '';
		if (x && x.href) this.input.value = x.href;	
		if (this.visible())
		{
			this.save.hide();
			this.edit.show();
			this.cancel.show();			
		}
	},

	clear: function()
	{
		if (this.halt) return;		
		this.input.value = '';		
		this.input.value = this.input.defaultValue;
		if (this.visible())
		{		
			this.save.show();
			this.edit.hide();	
			this.cancel.hide();	
		}
	},

	unlink: function()
	{
		this.editor.selection.moveToBookmark(this.editor.bookmark);		
		var node = this.editor.dom.getParent(this.editor.selection.getNode(), 'A');
		if (node)
		{
			this.editor.execCommand("UnLink", false);
		}
		
		new Effect.Highlight(this.input, { afterFinish: function() {
			this.close();
		}.bind(this) });				
	},

	link: function()
	{
		this.input.value = this.input.value.strip();
		if (this.input.value == '' || this.input.value == this.input.defaultValue) return;
		this.input.value = this.input.value.replace(/^http:\/\/(\w+:\/\/)/, '$1');
		if (!this.input.value.match(/^\w+:\/\//)) this.input.value = 'http://' + this.input.value;
		
		this.halt = true;
		this.editor.selection.moveToBookmark(this.editor.bookmark);
		var node = this.editor.dom.getParent(this.editor.selection.getNode(), 'A');

		if (!node)
		{
			this.editor.execCommand("CreateLink", false, this.input.value);	
			node = this.editor.dom.getParent(this.editor.selection.getNode(), 'A');
			this.editor.dom.setAttribs(node, {
				target : '_blank'
			});
		}
		else
		{
			this.editor.dom.setAttribs(node, {
				href : this.input.value,
				target : '_blank'
			});
		}		
				
		this.halt = false;	
		this.fill(node);
		
		new Effect.Highlight(this.input, { afterFinish: function() {
			this.close();
		}.bind(this) });		
	}
});

var Toggle = Class.create({	

	initialize: function(where, type)
	{
		switch(where) { 
			case 'article': case 'user': case 'topic': break;
			default: return;
		}		

		switch(type) { 
			case 'recommend': case 'bookmark': break;
			default: return;
		}

		this.button = $(type);
		this.buttonBottom = $(type+'Bottom');		
		
		if (!this.button) return;
		
		this.type = type;		
		this.where = where;
		this.halt = false;	
		
		this.classes = { on: type+'On', loading: type+'Loading' };
				
		this.loading =
		{
			show: function() {
				this.button.addClassName(this.classes.loading);
				this.button.removeClassName(this.classes.on);

				if (this.buttonBottom) {
					this.buttonBottom.addClassName(this.classes.loading);
					this.buttonBottom.removeClassName(this.classes.on);		
				}
			}.bind(this),

			hide: function() {
				this.halt = false;
				this.button.removeClassName(this.classes.loading);
				if (this.buttonBottom)
					this.buttonBottom.removeClassName(this.classes.loading);		
			}.bind(this)
		}
			
	},

	callback: function(json)
	{
		if (json.on)
		{
			this.button.addClassName(this.classes.on);				
			if (this.buttonBottom)
				this.buttonBottom.addClassName(this.classes.on);				
		}
		else
		{
			this.button.removeClassName(this.classes.on);				
			if (this.buttonBottom)
				this.buttonBottom.removeClassName(this.classes.on);				
		}
	},

	toggle: function(id)
	{	
		if (this.halt) return;
		this.halt = true;
		
		var url = this.button.hasClassName(this.classes.on) ? 'un'+this.type : this.type;
		
		app.jsonLoad('/'+url+'/'+this.where+'/'+id+'/', { 
			loading: this.loading,
			callback: this.callback.bind(this)
		});
	}

});

var FormProcess = Class.create({	

	initialize: function(form) 
	{
		this.halt = false;
		this.loading = $('loading');
		this.form = $$('form#'+form).first();
		if (!this.form) return;
		
		this.form.getElements().each(function(x) {
			Event.observe(x, 'focus', function() {
				this.form.select('.error').each(function(y) {
					if (y.visible()) new Effect.Fade(y);
				})
			}.bindAsEventListener(this));
		}.bind(this));
	},
	
	callback: function(json)
	{
		if (json.error)
		{
			var error = this.form.select('.error.'+json.error).first();
			if (error) error.show();
		}
		
		if (json.success)
		{
			if (json.url) location.href = json.url;
		}
	},
	
	validate: function()
	{
		if (!this.form) return false;
		
		var parameters = {};

		this.form.getElements().each(function(x) {
				parameters[x.name] = x.value.strip();
				if (x.type == 'text' || x.type == 'textarea')
				{	
					if (!parameters[x.name] || (!x.hasClassName('active') && parameters[x.name] == x.defaultValue))
					{
						if (!x.hasClassName('optional'))
						{
							new Effect.Highlight(x);
							parameters = false;
							throw $break;							
						}
						parameters[x.name] = '';
					}						
				}
		});
				
		return parameters;
	},
	
	submit: function()
	{				
		var parameters = this.validate();		
		if (parameters === false) return;
		
		if (this.halt) return;
		this.halt = true;		
		
		Position.clone(this.form, this.loading);			
		this.loading.show();
				
		app.jsonLoad(this.form.action, {
			loading: this.loading,
			parameters: parameters,
			keepLoading: true,
			callback: this.callback.bind(this),
			commonCallback: function() { this.halt = false; }.bind(this)
		});
	}
	
});

var Paging = Class.create({
	
	initialize: function(url, block)
	{
		this.halt = false;
		
		this.url = url;
		this.block = block;
		this.container = $(block+'Container');
		this.paging = $(block+'PagingContainer');
		
		this.loading = $('loading');	
		this.section = '';
	},
	
	callback: function(json)
	{
		if (json.html !== undefined)
			this.container.update(json.html);

		if (json.paging !== undefined)
			this.paging.update(json.paging);
	},
	
	select: function(section)
	{
		this.section = section;
		this.page(1);
	},
	
	page: function(page)
	{		
		if (this.halt) return;
		this.halt = true;
				
		Position.clone(this.container, this.loading);
		this.loading.show();
				
		app.jsonLoad(this.url, { 
			loading: this.loading,
			callback: this.callback.bind(this),
			commonCallback: function() { this.halt = false; }.bind(this),
			parameters: { block: this.block, page: page, section: this.section }
		});
	}
});

window.paging = {};

var Folder = Class.create({
	
	initialize: function(folder)
	{
		this.folder = $(folder)
		if (!this.folder) return;
		
		this.arrow = this.folder.select('.expandCollapse a').first();		
		this.arrow.onclick = this.toggle.bindAsEventListener(this);
	},
	
	toggle: function(e)
	{
		Event.stop(e);
		if (this.isClosed()) this.open();
		else this.close();
	},
	
	isClosed: function()
	{
		return this.folder.hasClassName('closed');
	},
	
	open: function()
	{
		this.folder.removeClassName('closed');
	},
	
	close: function()
	{
		this.folder.addClassName('closed');		
	}
});




var PreloadImages = [
	'/img/global/nav/home-on.png',
	'/img/global/nav/articles-on.png',
	'/img/global/nav/networks-on.png',
	'/img/global/nav/groups-on.png',
	'/img/global/nav/publish-on.png',
	'/img/global/nav/admin-on.png',
	'/img/global/nav/support-on.png',
	'/img/global/nav/expo-on.png',

	'/img/global/loading.gif',
	'/img/global/paging-prev.gif',
	'/img/global/paging-next.gif',
	'/img/global/paging-bg.gif',
	'/img/global/bookmark.gif',
	'/img/global/bookmarked.gif',	
	'/img/global/search-orangeonorange.gif',	
	'/img/global/productstar.gif',	
	'/img/global/icon_ans.gif',	
	'/img/global/icon_comment.gif',	
	'/img/global/icon_ques.gif',	
	'/img/global/icon_recommend.gif',	
	'/img/global/icon_review.gif',	
	'/img/global/icon_unrecommend.gif',	
	'/img/global/icon_vendor_update.gif',	
	'/img/global/icon-shared.gif',	
	'/img/global/icon_vendor_update.png',	
	'/img/global/paging-next.gif',	
	'/img/global/paging-prev.gif',	
	'/img/global/save.gif',	
	'/img/global/stargrey.gif',	
	'/img/global/favoriteactive_product.gif',	
	'/img/global/favoriteinactive_product.gif',	
	'/img/global/btn_track.gif',	
	'/img/global/btn_track_h.gif',
	'/img/global/btn-saving.gif',
	

	'/img/leftcol/stargrey.gif',	
	'/img/leftcol/starblank.gif',	

	'/img/articles/remove-link.gif',
	'/img/articles/create-link.gif',
	'/img/articles/comments-submit.gif',
	'/img/articles/attach-image.gif',
	'/img/articles/attach-video.gif',
	'/img/articles/insert-link.gif',
	'/img/articles/insert-email.gif',
	'/img/articles/insert-youtube.gif',
	'/img/articles/choose-file.gif',
	'/img/articles/comments-upload.gif',
	'/img/articles/edit-link.gif',
	'/img/articles/delete.gif',
	'/img/articles/edit.gif',
	'/img/articles/comment-divider.gif',
	'/img/articles/reply.gif',
	'/img/articles/unrecommended.gif',
	'/img/articles/recommend1.gif',
	'/img/articles/sharethisreview.gif',
	'/img/articles/askthereviewer.gif',
	'/img/articles/btn_answerthisquestion.gif',
	'/img/articles/askthereviewer_inactive.gif',
	'/img/articles/recommended-comment2.gif',


	
	'/img/profiles/add-colleague.gif',
	'/img/profiles/adding-colleague.gif',
	'/img/profiles/your-colleague.gif',
	'/img/profiles/follow.gif',
	'/img/profiles/following.gif',
	'/img/profiles/follow-pressed.gif',	
	'/img/global/less-blueonblue.gif'
];




function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

// Functions for search inline text START 
function overlay_focus_handler(event){
	 var element = Event.element(event);
	
	 if(element){
	 		if(element.value == element.getAttribute('title')){
				element.removeClassName('overlayed');
				element.value = '';				
			}
	 }	
}

function overlay_attacher(){
	$$('.overlay').each(function(item){
		item.observe('blur', overlay_blur_handler);
		item.observe('focus', overlay_focus_handler);
	});	
}

function overlay_blur_handler(event){
	 var element = Event.element(event);
	
	 if(element){
	 		if(element.value == ''){
				element.addClassName('overlayed');
				element.value = element.getAttribute('title');				
			}
	 }	
}

function overlay_submit_handler(event){
	$$('.overlay').each(function(item){
		if(item.value == item.getAttribute('title')){
			item.removeClassName('overlayed');
			item.value = '';						
		}
	});
}

 Event.observe(window, 'load', overlay_attacher);


function check_if_blank(elementId, frmId){
	//alert(document.getElementById(elementId).value);
		if (document.getElementById(elementId).value == ''){
			msg = 'Please enter '+ elementId;
			alert (msg);
			document.getElementById(elementId).focus();
			return;
		}else{
			document.getElementById(frmId).submit();
		}
	}

// Functions for search inline text END