if(typeof(VHV)=='undefined')
{
	VHV = {
		modules:{},
		loadCount:{},
		blocks:{},
		version:1,
		lib:{},
		currentBlockId:0,
		oldConfig:{},
		href:location.href,
		config:{},
		skin:'portals/default/skins/default',
		defaultPortalId:'vinbuy',
		portalId:'vinbuy',
		ajaxIndicator:false,
		ready:false,
		actionQueue:[],
		actionQueueRunning:false,
		allOrderedLinks:[],
		init:function()
		{
			$(function(){
				$('body').append('<div id="loading-layer" style="display:none"><div id="loading-layer-text"><img src="'+staticURL+'3rdparty/jQuery/plugins/ajax/images/ajax-loader.gif" align="absmiddle" width="16" height="16" hspace="5" class="displayIn">Đang tải dữ liệu ...</div></div>');
				$('#loading-layer').ajaxStart(function(a,request) {
					if(VHV.ajaxIndicator)
					{
						$(this).attr('top', document.body.scrollTop);
						$(this).show();
					}
				}).ajaxStop(function() {
					$(this).hide();				
				});
				if(typeof(VHV) != 'undefined')
				{
					VHV.ajaxIndicator = true;
				}
			});
		},
		makeLinksCache:function(pageName)
		{
			if(VHV.User.isAdmin())
			{
				VHV.addActionQueue({
					isLinkCache:true,
					condition:function(){
						for(var i = 0; i < VHV.actionQueue.length; i++)
						{
							if(typeof(VHV.actionQueue[i].isLinkCache)=='undefined' && typeof(VHV.actionQueue[i].run)=='undefined' && !VHV.actionQueue[i].condition.call(this))
							{
								return false;
								break;
							}
						}
						return true;
					},
					success:function(){
						var allLinks = {};
						for(var i in VHV.allOrderedLinks)
						{
							allLinks['links['+i+']'] = VHV.allOrderedLinks[i];
						}
						
						$.post(this.buildURL({module:'MakeLinksCache', page_name:pageName}), allLinks);
					}
				});
			}
		},
		image:function(url)
		{
			if(typeof(url) == 'string')
			{
				if(/http:\/\//.test(url))
				{
					return url;
				}
				if(/\/upload\//.test(url))
				{
					return VHV.FILE_SERVER_URL+url;
				}
				else
				if(/upload\//.test(url))
				{
					return VHV.FILE_SERVER_URL+'/'+url;
				}
			}
			if(typeof(url) == 'undefined' || url == '' || url == false)
			{
				return staticURL+VHV.skin+'images/no-image.jpg';
			}
			return url;
		},
		url: function(param, defaultValue) {
			if(typeof(defaultValue) == 'undefined')
			{
				defaultValue = '';
			}
		    var regex = '[?&]' + param + '=([^&#]*)';
		    var results = (new RegExp(regex)).exec(VHV.href);
		    if(results) return results[1];
		    return defaultValue;
		},
		buildURL:function(options){
			if(typeof(options['portal']) == 'undefined')
			{
				options['portal'] = VHV.portalId;
			}
			var url = rootURL+'?';
			for(var i in options)
			{
				url += '&'+i+'='+encodeURI(options[i]);
			}
			return url;
		},
		session:function(name, value)
		{
			if(typeof(value) != 'undefined')
			{
				jQuery.cookie(name, value, {
					domain:'.vimua.com'
				});
			}
			else
			{
				return jQuery.cookie(name);
			}
		},
		xml:function(xmlNode, parentNode)
		{
			var result = {}, count = 0, isArray = false;
			xmlNode.children().each(function(){
				var children = $(this).children(), key;
				if(count == 0 && typeof(result[this.tagName]) == 'undefined' && !isArray)
				{
					if(typeof(parentNode) != 'undefined' && parentNode == this.tagName+'s')
					{
						key = 0;
						isArray = true;
						result = [];
					}
					else
					{
						key = this.tagName;
					}
				}
				else
				{
					if(count == 0 && typeof(result[this.tagName]) != 'undefined')
					{
						result = [result[this.tagName]];
					}
					key = ++count;
				}
				if(children.length > 0)
				{
					result[key] = VHV.xml($(this), key);
				}
				else
				{
					result[key] = $(this).text();
				}
				children = null;
				//result[key]._index = count;
			});
			
			return result;
		},
		load:function()
		{
			if(typeof(VHV.allLinks) == 'undefined')
			{
				VHV.allLinks = {
					'3rdparty/jQuery/jquery-1.2.6.min.js':2,
					'3rdparty/jQuery/plugins/jquery.cookie.js':2,
					'3rdparty/jQuery/plugins/jquery.json/jquery.json.min.js':2,
					'lib/js/VHV/VHV.js':2,
					'lib/js/VHV/VHV.User.js':2,
					'lib/js/VHV/VHV.Module.js':2,
					'lib/js/VHV/VHV.Form.js':2,
					'3rdparty/jQuery/plugins/pureJSTemplate.js':2
				};
				VHV.allLinks['publish/'+VHV.portalId+'/modules.js'] = 2;
				for(var i in VHV.allLinks)
				{
					VHV.allOrderedLinks.push(i);
				}
			}
			if(arguments.length>0)
			{
				if(typeof(arguments[0]) == 'function')
				{
					arguments[0].call(this);
				}
				else
				{
					var length = arguments.length;
					if(typeof(arguments[arguments.length - 1]) == 'function')
					{
						length--;
					}
					for(var i = 0; i < length; i++)
					{
						if(typeof(arguments[i]) == 'string')
						{
							VHV.loadURL(arguments[i]);
						}
					}
					if(typeof(arguments[arguments.length - 1]) == 'function')
					{
						VHV.afterLoad.apply(this, arguments);
					}
				}
			}
		},
		loadURL:function(fileName)
		{
			if(fileName.indexOf('.css') != -1)
			{
				if(!VHV.allLinks[fileName])
				{
					VHV.allLinks[fileName] = 1;
					var headID = document.getElementsByTagName("head")[0];
					var cssNode = document.createElement('link');
					cssNode.type = 'text/css';
					cssNode.rel = 'stylesheet';
					cssNode.href = staticURL+fileName+(/\?/.test(fileName)?'':'?')+'v='+VHV.version;
					cssNode.media = 'screen';
					headID.appendChild(cssNode);
					headID = cssNode = null;
					VHV.allOrderedLinks.push(fileName);
				}
			}
			else
			{
				if(!VHV.allLinks[fileName])
				{
					VHV.allLinks[fileName] = 1;
					$.ajax({
						url: rootURL+fileName+(/\?/.test(fileName)?'':'?')+'v='+VHV.version,
						dataType: 'html',
						cache:true,
						error:function()
						{
							VHV.allLinks[fileName] = 3;
						},
						success:function(data)
						{
							VHV.allLinks[fileName] = 4;
							eval(data);
							VHV.allLinks[fileName] = 2;
							VHV.allOrderedLinks.push(fileName);
						}
					});
				}
			}
		},
		addActionQueue:function(action)
		{
			VHV.actionQueue.push(action);
			if(!VHV.actionQueueRunning)
			{
				VHV.execActionQueue();
			}
		},
		execActionQueue:function()
		{
			VHV.actionQueueRunning = true;
			var run = false;
			for(var i = 0; i < VHV.actionQueue.length; i++)
			{
				if(typeof(VHV.actionQueue[i].run)=='undefined')
				{
					run = true;
					if(VHV.actionQueue[i].condition.call(this))
					{
						VHV.actionQueue[i].run = true;
						VHV.actionQueue[i].success.call(this);
					}
				}
			}
			if(run)
			{
				setTimeout('VHV.execActionQueue();',50);
			}
			else
			{
				VHV.actionQueueRunning = false;
				VHV.actionQueue = [];
			}
		},
		afterLoad:function()
		{
			var args = arguments;
			VHV.addActionQueue({
				condition:function(){
					var ok = true;
					for(var i = 0; i < args.length-1; i++)
					{
						if((args[i] && typeof(args[i]) == 'string') && (args[i].indexOf('.css') == -1))
						{
							if(VHV.allLinks[args[i]] != 2)
							{
								if(typeof(VHV.loadCount[args[i]]) == 'undefined')
								{
									VHV.loadCount[args[i]] = 0;
								}
								VHV.loadCount[args[i]] ++;
								if(VHV.loadCount[args[i]] > 40)
								{
									
									if(VHV.allLinks[args[i]] == 4)
									{
										VHV.allLinks[args[i]] = 2;
									}	
								}
								ok = false;
								if(VHV.allLinks[args[i]] == 3)
								{
									return;
								}						
							}
						}
					}
					return ok;
				},
				success:function(){
					if(typeof(args[args.length - 1]) == 'function')
					{
						var func = args[args.length - 1];
						func.call(this);
						
					}
					args = null;
				}
			});
		},
		number:function(st)
		{
			if(typeof(st) != 'undefined' && st != null)
			{
				return parseInt(st.toString().replace(/\./g,''));
			}
			return 0;
		},
		numberFormat:function(number, decimals, dec_point, thousands_sep)
		{  
    // *     example 1: number_format(1234.5678, 2, '.', '');
    // *     returns 1: 1234.57   
			var n = number, c = isNaN(decimals = Math.abs(decimals)) ? 0 : decimals;
			var d = dec_point == undefined ? "," : dec_point;
			var t = thousands_sep == undefined ? "." : thousands_sep, s = n < 0 ? "-" : "";
			var i = parseInt(n = Math.abs(+n || 0).toFixed(c)) + "", j = (j = i.length) > 3 ? j % 3 : 0;    
			return s + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : "");
		},
		require:function(className, success)
		{
			if(VHV.modules[className])
			{
				VHV.load(VHV.modules[className].path+'/class.js', function(){
					VHV.requireSuccess(className, success);
				});
			}
			else
			{
				VHV.load('lib/js/VHV/'+className+'.js', success);
			}
		},
		requireSuccess:function(className, success)
		{
			VHV.addActionQueue({
				condition:function(){
					return (typeof(VHV[className]) != 'undefined');
				},
				success:function(){
					if(typeof(success) == 'function')
					{
						success.call(this);
					}
				}
			});
		},
		echo:function(st)
		{
			if(VHV.ready)
			{
				$('body').append(st);
			}
			else
			{
				document.write(st);
			}
		},
		using:function(className, blockId, options, onReady)
		{
			if(VHV.modules[className])
			{
				VHV.load(VHV.modules[className].path+'/class.js',function(){
					if(typeof(VHV.blocks[blockId]) == 'undefined')
					{
						
						VHV.usingClass(className, blockId, options, onReady);
					}
					else
					{
						
					}
				});
			}
			else
			{
				VHV.load('lib/js/VHV/'+className+'.js', onReady);
			}
		},
		moduleReady:function(blockId,onReady)
		{
			VHV.addActionQueue({
				condition:function(){
					return (typeof(VHV.blocks[blockId]) != 'undefined' && VHV.blocks[blockId].isReady);
				},
				success:function(){
					onReady.call(this);
				}
			});
		},
		usingClass:function(className, blockId, options, onReady)
		{
			VHV.addActionQueue({
				condition:function(){
					return (typeof(VHV[className]) != 'undefined');
				},
				success:function(){
					eval('var module = new VHV.'+className+'(\''+className+'\','+blockId+',options, onReady);');
				}
			});
		},
		extend:function(oldClass, properties)
		{
			var newClass = function()
			{
				if(typeof(oldClass) != 'undefined')
				{
					this.parent = oldClass.prototype;
					oldClass.apply(this,arguments);
				}
			}
			if(typeof(oldClass) != 'undefined')
			{
				$.extend(newClass.prototype, oldClass.prototype, properties);
			}
			return newClass;
		},
		addslashes:function(st)
		{
			if(typeof(st) == 'string')
			{
				return st.replace(/'/g,'\\\'').replace(/"/g,'&quot;');
			}
			return st;
		},
		sizeof:function(obj)
		{
			if(typeof(obj) == 'object')
			{
				var size = 0;
				for(var i in obj)
				{
					if(typeof(obj[i])!='function')
					{
						size ++;
					}
				}
				return parseInt(size);
			}
			else
			if(typeof(obj.length) != 'undefined')
			{
				return obj.length;
			}
			return 0;
		},
		alert:function(message, options)
		{
			VHV.load('3rdparty/jQuery/plugins/jqimpromptu.css');
			VHV.load('3rdparty/jQuery/plugins/jquery.corner.js',
				'3rdparty/jQuery/plugins/jquery-impromptu.1.5.js', function(){
					$.prompt(message,$.extend({ show:'slideDown' }, options));
			});
		},
		parse:function(id, template, params)
		{
			$.fn.pureJSTemplate.setDelimiters("<.", ".>");
			$('#'+id).pureJSTemplate({id:template, data:params});
		},
		clone:function (myObj)
		{
			if(typeof(myObj) != 'object') return myObj;
			if(myObj == null) return myObj;
			var myNewObj = new Object();
			for(var i in myObj)
				myNewObj[i] = VHV.clone(myObj[i]);
			return myNewObj;
		},
		date:function(time)
		{
			var date=new Date(time * 1000); 
			var d = date.getDate()+'/'+(date.getMonth()+1)+'/'+date.getYear();
			delete date;
			return d;
		},
		time:function(time)
		{
			var date=new Date(time * 1000); 
			var d = date.getHours()+':'+date.getMinutes()+' '+date.getDate()+'/'+(date.getMonth()+1)+'/'+date.getYear();
			delete date;
			return d;
		}
	}	
}