var SNSHTML = {};
SNSHTML.win = {
	counter : new Array(),
	inst    : {},
	build : function(title, content, id, attrs, styles, callBack )
	{
		SNSHTML.win.counter ++;

		if( ! id )
		{
			id = 'snsPopUp.' + SNSHTML.win.counter;
		}

		var container = sns.get_obj('vBody');

	    var winScene = sns.createElement('div', { id : id, key : SNSHTML.win.counter, className : 'winScene' }, { width : '100px' }, container );

		if( attrs )
		{
			for( var x in attrs )
			{
				winScene[x] = attrs[x];
			}
		}

		if( styles )
		{
			for( var x in styles )
			{
				winScene.style[x] = styles[x];
			}
		}

		winScene.style.zIndex   = 1000 + SNSHTML.win.counter * 2;
		winScene.style.position = 'absolute';

		var titleContainer = sns.createElement('div', { id : id + '.dragBar', key : SNSHTML.win.counter, className : 'winTitleBar' }, {}, winScene );

		sns.createElement('img', { src      : sns.res_root_server + '/img/buttons/closeWin.gif',
								   key      : SNSHTML.win.counter,
								   onclick  : SNSHTML.win.destroy },
								 { cssFloat : 'right', styleFloat : 'right', marginLeft : '10px', cursor : 'pointer' }, titleContainer );

		if( typeof title == 'object' )
		{
		    titleContainer.appendChild(title);
		}
		else
		{
		    titleContainer.appendChild(sns.createElement('span', { innerHTML : title } ) );
		}

		var winContainer = sns.createElement('div', {}, { margin : '7px', background : '#fff' }, winScene );

		if( typeof content == 'object' )
		{
		    winContainer.appendChild(content);
		}
		else
		{
		    winContainer.innerHTML = content;
		}

		sns.move_to_center(winScene);

		SNSHTML.win.inst[SNSHTML.win.counter] = { obj : winScene, id : id, callBack : callBack, disableScene : null };

		sns.dragger.init({ dragBar  : id + '.dragBar',
						   dragObj  : id,
						   callback : { start : SNSHTML.win.swapDepths } } );

		return winScene;
	},

	swapDepths : function(params)
	{
		var id = params.dragBar.id.replace(/\.[^\.]+$/, '' );
		var depth = 1000;

		var inst  = null;

		for( var x in SNSHTML.win.inst )
		{
			if( SNSHTML.win.inst[x] != null )
			{
				if( SNSHTML.win.inst[x].id != id )
				{
					depth += 2;

					SNSHTML.win.inst[x].obj.style.zIndex = depth;

					if( SNSHTML.win.inst[x].disableScene != null )
					{
						SNSHTML.win.inst[x].disableScene.style.zIndex = depth + 1;
					}
				}
				else
				{
					inst = SNSHTML.win.inst[x];
				}
			}
		}

		if( inst != null )
		{
			inst.obj.style.zIndex = depth + 2;

			if( inst.disableScene != null )
			{
				inst.disableScene.style.zIndex = depth + 3;
			}
		}
	},

	disable : function(id)
	{
		var inst = SNSHTML.win.getInstByID(id);

		if( inst === false )
		{
			return false;
		}

		var disableWrapper = sns.createElement('div', { id : inst.id + '.disableWrapper' }, { position   : 'absolute',
															top        : inst.obj.style.top,
															left       : inst.obj.style.left,
															zIndex     : inst.obj.style.zIndex + 1,
															background : 'url("' + sns.res_root_server + '/img/global/white75.png")' }, sns.get_obj('vBody') );

		disableWrapper.style.width  = inst.obj.offsetWidth  + 'px';
		disableWrapper.style.height = inst.obj.offsetHeight + 'px';

		inst.disableScene = disableWrapper;

		sns.dragger.init({ dragBar  : disableWrapper,
						   dragObj  : inst.obj,
						   callback : { start : SNSHTML.win.swapDepths, drag : function(params){ disableWrapper.style.top = params.y + 'px'; disableWrapper.style.left = params.x + 'px'; } } } );

		return true;
	},

	available : function(id)
	{
		var inst = SNSHTML.win.getInstByID(id);

		if( inst === false )
		{
			return false;
		}

		if( inst.disableScene != null )
		{
			inst.disableScene.parentNode.removeChild(inst.disableScene);
			inst.disableScene = null;
		}

		return true;
	},

	getInstByID : function(id)
	{
		for( var x in SNSHTML.win.inst )
		{
			if( SNSHTML.win.inst[x] != null && SNSHTML.win.inst[x].id == id )
			{
				return SNSHTML.win.inst[x];
			}
		}

		return false;
	},

	destroy : function(e,winID)
	{
		if( winID )
		{
			var obj = sns.get_obj(winID);
		}
		else
		{
			var obj = this;
		}

		if( SNSHTML.win.inst[obj.key].callBack && SNSHTML.win.inst[obj.key].callBack.destroy )
		{
			if( ! SNSHTML.win.inst[obj.key].callBack.destroy() )
			{
				return false;
			}
		}

		sns.get_obj('vBody').removeChild(SNSHTML.win.inst[obj.key].obj);

		SNSHTML.win.inst[obj.key] = null;
	}
}
SNSHTML.page = {

	/**
     * Build up page span links
     *
     * @param id string  unique ID
     * @param current integer Current page
     * @param section integer Section
     * @param total integer Total result
     * @param perPage integer Number result per page
     * @param callBack object  onclick EVENT execute object
     * @param attrs object attributes
     * @param simple boolean simple mode
     *
     * @return String
     */

	build : function(config)
	{
		if( config.total < 1 )
		{
			return false;
		}

    	config.simple  = config.simple  ? true : false;
    	config.current = config.current >= 1 ? config.current : 1;

    	config.perPage = config.perPage > 0 ? config.perPage : SNSHTML.page.result_per_page;
    	config.section = config.section > 0 ? config.section : SNSHTML.page.page_section;

		if( config.total % config.perPage == 0 )
		{
		    totalPAGE = parseInt(config.total/config.perPage);
		}
	    else
	    {
		    totalPAGE = parseInt(config.total/config.perPage+1);
	    }

	    if( totalPAGE < 2 )
	    {
	    	return false;
	    }

	    first = '';
	    last  = '';
	    jump  = '';

	    start = config.current - config.section;

	    if( start <= 1 )
	    {
	    	start = 1;
	    }
	    else if( start > totalPAGE )
	    {
	    	start = totalPAGE;
	    }
	    else if( totalPAGE > config.section * 2 + 1 )
	    {
	    	last = SNSHTML.page.last(config.callBack,config.simple,config.attrs);
	    }

	    end = start + config.section * 2;

	    if( end >= totalPAGE )
	    {
	    	end = totalPAGE;

	    	start = end - config.section * 2;

	    	if( start < 1 )
	    	{
	    		start = 1;
	    	}
	    }
	    else
	    {
	    	first = SNSHTML.page.first(config.callBack,totalPAGE,config.simple,config.attrs);
	    }

		pages = new Array();

	    for( var p = start; p <= end; p ++ )
	    {
	    	pages[pages.length] = p == config.current ? SNSHTML.page.current(p,config.attrs) :
	    	                              				SNSHTML.page.page(config.callBack,p,config.attrs);
	    }

        next_page     = '';
	   	previous_page = '';

	   	next     = start - ( config.section + 1 );
	   	previous = end + ( config.section + 1 );

	   	if( next > config.section + 1 )
		{
	    	next_page = SNSHTML.page.next(config.callBack,next,config.simple,config.attrs);
	   	}

	   	if( previous < totalPAGE - config.section )
	   	{
	   		previous_page = SNSHTML.page.previous(config.callBack,previous,config.simple,config.attrs);
	    }

	    if( totalPAGE > config.section * 2 + 1 )
	    {
	    	jump = SNSHTML.page.jump(totalPAGE,config.callBack,config.id,config.simple,config.attrs);
	    }

	    return SNSHTML.page.complete(last, next_page, pages, previous_page, first, config.total, totalPAGE, config.simple );
	},

	current : function(p,params)
	{
		var obj = sns.createElement('div', { p         : p,
				   						     innerHTML : p,
				   						     className : 'curPage' } );

		if( typeof params == 'object' )
		{
			for( var i in params )
			{
				obj[i] = params[i];
			}
		}

		return obj;
	},

	last : function(e,simple,params)
	{
		var obj = sns.createElement('div', { className   : 'otherPage',
										     p           : 1,
				   						     innerHTML   : simple ? '<<' : 'Trang đầu',
										     onclick     : e } );

		if( typeof params == 'object' )
		{
			for( var i in params )
			{
				obj[i] = params[i];
			}
		}

		return obj;
	},

	first : function(e,p,simple,params)
	{
		var obj = sns.createElement('div', { className   : 'otherPage',
											 p           : p,
				   						     innerHTML   : simple ? '>>' : 'Trang cuối',
											 onclick     : e } );

		if( typeof params == 'object' )
		{
			for( var i in params )
			{
				obj[i] = params[i];
			}
		}

		return obj;
	},

	page : function(e,p,params)
	{
		var obj = sns.createElement('div', { p         : p,
											 innerHTML : p,
											 className : 'page',
											 onclick   : e } );

		if( typeof params == 'object' )
		{
			for( var i in params )
			{
				obj[i] = params[i];
			}
		}

		return obj;
	},

	next : function(e,p,simple,params)
	{
		var obj = sns.createElement('div', { className : 'otherPage',
											 p         : p,
				   						     innerHTML : simple ? '<' : 'Trang tiếp',
											 onclick   : e } );

		if( typeof params == 'object' )
		{
			for( var i in params )
			{
				obj[i] = params[i];
			}
		}

		return obj;
	},

	previous : function(e,p,simple,params)
	{
		var obj = sns.createElement('div', { className : 'otherPage',
											 p         : p,
				   						     innerHTML : simple ? '>' : 'Trang sau',
											 onclick   : e } );

		if( typeof params == 'object' )
		{
			for( var i in params )
			{
				obj[i] = params[i];
			}
		}

		return obj;
	},

	jump : function()
	{
	},

	complete : function(last,next,pages,previous,first,t,totalPAGE,simple)
	{
		var container = sns.createElement('div', { className : 'pages' } );

		if( ! simple )
		{
			sns.createElement('div', {  className : 'pageLine', innerHTML : sns.lang_build_string('Tìm thấy tất cả <%1> kết quả trong <%2> trang', t, totalPAGE ) }, {}, container );
		}

		var scene = sns.createElement('div', { className : 'pageLine' }, {}, container );

		if( ! simple )
		{
			sns.createElement('div', {  className : 'pageDesc', innerHTML : 'Đi tới trang:' }, {}, scene );
		}
		else
		{
			sns.createElement('div', {  className : 'pageDesc', innerHTML : 'Trang:' }, {}, scene );
		}

		var main = sns.createElement('div', { className : 'pageEntry' }, {}, scene );

		if( typeof last == 'object' )
		{
			main.appendChild(last);
		}

		if( typeof next == 'object' )
		{
			main.appendChild(next);
		}

		for( var i = 0; i < pages.length; i ++ )
		{
			main.appendChild(pages[i]);
		}

		if( typeof previous == 'object' )
		{
			main.appendChild(previous);
		}

		if( typeof first == 'object' )
		{
			main.appendChild(first);
		}

		return container;
	}
}
SNSHTML.rating = {
	inst : {},

	createInstance : function(config)
	{
		if( typeof config.container != 'object' )
		{
			config.container = sns.get_obj(config.container);
		}

		config.container.innerHTML = '';

	    var averagePoint = Math.ceil(config.point/config.totalRated);

	    for( var x = 1; x <= 10; x ++ )
	    {
	        className = config.size + 'Point' + ( x % 2 ? 'Left' : 'Right' );

	        if( x <= averagePoint )
	        {
	            className += 'Rated';
	        }

	        sns.createElement('img', { src : sns.res_root_server + '/img/global/null.gif', className : className }, {}, config.container );

	        if( config.ratingEnabled )
	        {
	            config.container.lastChild.id = config.container.id + '.' + x;

	            config.container.lastChild.style.cursor = 'pointer';

	            config.container.lastChild.onmouseout  = SNSHTML.rating.listener.ratingOut;
	            config.container.lastChild.onmouseover = SNSHTML.rating.listener.ratingOver;
	            config.container.lastChild.onmousedown = config.form ? SNSHTML.rating.listener.saveRating : SNSHTML.rating.listener.rating;
	        }
	    }

	    SNSHTML.rating.inst[config.container.id] = config;
	},

	drawRatingPoint : function(point, totalRated, size, ratingEnabled, id, ratingReq )
	{
	    var prefix = size == 1 ? 'small' : 'large';

	    var container = this.createElement('div', { className : prefix + 'PointBar' } );

	    return container;
	},

	drawRatingForm : function( id, form, size )
	{
		var point = parseInt(form.value);

		if( isNaN(point) || point < 1 )
		{
			point = 0;
		}

	    var prefix = size == 1 ? 'small' : 'large';

	    var container = this.createElement('div', { className : prefix + 'PointBar' } );

	    for( var x = 1; x <= 10; x ++ )
	    {
	        className = prefix + 'Point' + ( x % 2 ? 'Left' : 'Right' );

	        if( x <= point )
	        {
	            className += 'Rated';
	        }

	        this.createElement('div', { className : className }, {}, container );

	        container.lastChild.id = id + '.' + x;

	        container.lastChild.style.cursor = 'pointer';

	        container.lastChild.onmouseout  = function(){ sns.ratingOut(this) }
	        container.lastChild.onmouseover = function(){ sns.ratingOver(this) }
	        container.lastChild.onmousedown = function()
	        {
	        	form.value = this.id.replace(/^.+\.(\d+)$/, '$1' );

				container.parentNode.insertBefore(sns.drawRatingForm( id,
																	  form,
																	  size ), container );

				container.parentNode.removeChild(container);
	        }
	    }

	    return container;
	},

	listener : {
		saveRating : function()
		{
			var id    = this.id.replace(/^(.+)\.\d+$/, '$1' );
			var point = this.id.replace(/^.+\.(\d+)$/, '$1' );

	        SNSHTML.rating.inst[id].form.value = point;

			SNSHTML.rating.inst[id].point      = point;
			SNSHTML.rating.inst[id].totalRated = 1;

			SNSHTML.rating.createInstance(SNSHTML.rating.inst[id]);
		},

		rating : function()
		{
			var id    = this.id.replace(/^(.+)\.\d+$/, '$1' );
			var point = this.id.replace(/^.+\.(\d+)$/, '$1' );

			var xmlobj = new SNSAjax();

			xmlobj.onreadystatechange(function()
			{
				if( ! xmlobj.readystate_ready_and_ok() )
				{
					return false;
				}

				sns.hideLoadingForm();

				var _return  = xmlobj.xmlhttp.responseXML.getElementsByTagName('return')[0].firstChild.nodeValue;

				if( _return == 'OK' )
				{
					SNSHTML.rating.inst[id].point         = xmlobj.xmlhttp.responseXML.getElementsByTagName('point')[0].firstChild.nodeValue;
					SNSHTML.rating.inst[id].totalRated    = xmlobj.xmlhttp.responseXML.getElementsByTagName('totalRated')[0].firstChild.nodeValue;
					SNSHTML.rating.inst[id].ratingEnabled = false;

					SNSHTML.rating.createInstance(SNSHTML.rating.inst[id]);
				}
				else
				{
					alert(xmlobj.xmlhttp.responseXML.getElementsByTagName('message')[0].firstChild.nodeValue);
				}
			} );

			sns.displayLoadingForm(sns.lang.waitForExecData);

			xmlobj.process(sns.base_url + '/' + SNSHTML.rating.inst[id].request, 'POST', { point : point } );
		},

		ratingOver : function()
		{
		    var pointInRated = (/^.+Rated$/).test(this.orgClass);

		    var point = this.id.replace(/^.+\.(\d+)$/g, '$1' );
		    var id    = this.id.replace(/^(.+)\.\d+$/g, '$1' );

		    for( var x = 1; x <= 10; x ++ )
		    {
		        var obj = sns.get_obj(id + '.' + x );

		        obj.orgClass = obj.className;

		        if( (/^.+Rated$/).test(obj.orgClass) )
		        {
		            if( x > point )
		            {
		                className = obj.className;
		            }
		            else
		            {
		                className = obj.className.replace(/^(.+?)Rated$/, '$1' ) + 'Hover';
		            }
		        }
		        else
		        {
		            if( x > point )
		            {
		                className = obj.className;
		            }
		            else
		            {
		                className = obj.className + 'Hover';
		            }
		        }

		        obj.className = className;
		    }
		},

		ratingOut : function()
		{
		    var id    = this.id.replace(/^(.+)\.\d+$/g, '$1' );

		    for( var x = 1; x <= 10; x ++ )
		    {
		        var obj = sns.get_obj(id + '.' + x );

		        obj.className = obj.orgClass;
		    }
		}
	}
};
