﻿function PageScroller(id, pages, height)
{
	this.id = id;
	this.height = height;
	this.pages = [];
	this.current_page = 0;
	this.animation = null;

	// set up container
	this.container = document.getElementById(this.id);
	this.container.style.overflow = 'hidden';
	this.container.style.height = this.height + 'px';

	// set up prev link
	this.prev = document.createElement('a');
	this.prev.href = '#';
	YAHOO.util.Dom.addClass(this.prev, 'previous');
	this.prev.appendChild(document.createTextNode('Previous'));

	this.prev_insensitive = document.createElement('span');
	YAHOO.util.Dom.addClass(this.prev_insensitive, 'previous');
	this.prev_insensitive.appendChild(document.createTextNode('Previous'));

	YAHOO.util.Event.addListener(this.prev, 'click',
		function (e)
		{
			YAHOO.util.Event.preventDefault(e);
			this.prevPage();
		},
		this, true);


	// set up next link
	this.next = document.createElement('a');
	this.next.href = '#';
	YAHOO.util.Dom.addClass(this.next, 'next');
	this.next.appendChild(document.createTextNode('Next'));

	this.next_insensitive = document.createElement('span');
	YAHOO.util.Dom.addClass(this.next_insensitive, 'next');
	this.next_insensitive.appendChild(document.createTextNode('Next'));

	YAHOO.util.Event.addListener(this.next, 'click',
		function (e)
		{
			YAHOO.util.Event.preventDefault(e);
			this.nextPage();
		},
		this, true);

	// add pagination to page
	var divider = document.createElement('span');
	divider.appendChild(document.createTextNode('|'));
	YAHOO.util.Dom.addClass(divider, 'divider');
	var pagination = document.getElementById('pagination');
	pagination.appendChild(this.prev);
	pagination.appendChild(divider);
	pagination.appendChild(this.next);

	// add pages
	for (var i = 0; i < pages.length; i++)
		this.addPage(new Page(pages[i]));

	// initialize to first page
	this.setPage(0);
}

PageScroller.prototype.addPage = function(page)
{
	var page_number = this.pages.length;
	page.setPageHeight(this.height);
	this.pages.push(page);

	if (page.nav) {
		YAHOO.util.Event.addListener(page.nav, 'click',
			function (e)
			{
				YAHOO.util.Event.preventDefault(e);
				this.setPage(page_number);
			},
			this, true);
	}
}

PageScroller.prototype.prevPage = function()
{
	this.setPage(this.current_page - 1);
}

PageScroller.prototype.nextPage = function()
{
	this.setPage(this.current_page + 1);
}

PageScroller.prototype.setPage = function(page_number)
{
	// deselect current (old) page
	this.pages[this.current_page].setNavHighlight(false);

	// wrap page number
	if (page_number >= this.pages.length)
		this.current_page = 0;
	else if (page_number < 0)
		this.current_page = this.pages.length - 1;
	else
		this.current_page = page_number;
	
	// select current (new) page
	this.pages[this.current_page].setNavHighlight(true);

	// set prev link sensitivity
	this.setPrevLinkSensitivity(this.current_page != 0);

	// set next link sensitivity
	this.setNextLinkSensitivity(this.current_page != this.pages.length - 1);

	// scroll to the page
	this.scrollToCurrentPage();
}

PageScroller.prototype.setPrevLinkSensitivity = function(sensitive)
{
	if (sensitive) {
		if (this.prev_insensitive.parentNode) {
			this.prev_insensitive.parentNode.replaceChild(
				this.prev, this.prev_insensitive);
		}
	} else {
		if (this.prev.parentNode) {
			this.prev.parentNode.replaceChild(
				this.prev_insensitive, this.prev);
		}
	}
}

PageScroller.prototype.setNextLinkSensitivity = function(sensitive)
{
	if (sensitive) {
		if (this.next_insensitive.parentNode) {
			this.next_insensitive.parentNode.replaceChild(
				this.next, this.next_insensitive);
		}
	} else {
		if (this.next.parentNode) {
			this.next.parentNode.replaceChild(
				this.next_insensitive, this.next);
		}
	}
}

PageScroller.prototype.scrollToCurrentPage = function()
{
	var old_scroll_pos = this.container.scrollTop;

	// works because all pages are the same height
	var new_scroll_pos = this.height * this.current_page;

	if (this.animation && this.animation.isAnimated())
		this.animation.stop();

	this.animation = new YAHOO.util.Scroll(this.container,
		{ scroll: { from: [0, old_scroll_pos], to: [0, new_scroll_pos] } },
		0.5, YAHOO.util.Easing.easeOut);

	this.animation.animate();
}

/**
 * Page
 */
function Page(id)
{
	this.id = id;

	this.nav = null;
	var nav_element = document.getElementById('nav-page-' + this.id);
	for (var i = 0; i < nav_element.childNodes.length; i++) {
		if (nav_element.childNodes[i].nodeName == 'A') {
			this.nav = nav_element.childNodes[i];
			break;
		}
	}

	this.page = document.getElementById('pc-page-' + this.id);
	this.page.style.overflow = 'hidden';
}

Page.prototype.setPageHeight = function(height)
{
	this.page.style.height = height + 'px';
}

Page.prototype.setNavHighlight = function(highlight)
{
	if (this.nav) {
		if (highlight)
			YAHOO.util.Dom.addClass(this.nav, 'current');
		else
			YAHOO.util.Dom.removeClass(this.nav, 'current');
	}
}
//--------------------------------------------------------------------------------

function openPopUp(width,height,url,target){
		window.open(url,target,'toolbar=0,location=0, directories=0, status=0, menubar=0, scrollbars=1, resizable=0, width=' + width + ', height=' + height + ', left=' + (screen.width - width)/2 + ', top=' + (screen.height-height)/2 );
}
function openWin(width,height,url,target){
		window.open(url,target,'toolbar=0,location=0, directories=0, status=0, menubar=0, scrollbars=0, resizable=0, width=' + width + ', height=' + height + ', left=' + (screen.width - width)/2 + ', top=' + (screen.height-height)/2 );
}
function popup_win(){
        openPopUp(600,400,'about:blank','_pwin');
}
function op_m_i(url){
    openPopUp(392,450,'about:blank','_pimage');
}
function time_params(){
            var a = new Date();
            var ms = a.getMilliseconds();
            var s = a.getSeconds();
            var mn = a.getMinutes();
            var h = a.getHours();
            var d = a.getDay();
            var m = a.getMonth();
            var y = a.getFullYear();
            var t = y+'.'+m+'.'+d+'-'+h+':'+mn+':'+s+':'+ms;
            return t;
}

function counter(id){
    if(typeof(document.getElementById('CountGamePlay'))=='undefined' || document.getElementById('CountGamePlay')==null){
        document.write('<img id="CountGamePlay" src="/backend/client/CountGamePlay.aspx?g_id='+id+'&sid='+time_params()+'" width="1" height="1" border="0" />');
    }
    else{
        document.getElementById('CountGamePlay').src='/backend/client/CountGamePlay.aspx?g_id='+id+'&sid='+time_params();
    }
    if(id!='' && id!=null)
        var t=setTimeout('counter("'+id+'")',300000);//load lai sau 5 phut
}


AO = function(url,query)
{
	this.url 			= url;
	this.query 			= query;
	this.uri			= url + "?" + query;
	this.init 			= false;
	this.http 			= false;
	this.setup();
}
AO.prototype.debug = false;
AO.prototype.setup = function()
{
	if (window.XMLHttpRequest)
	{
		this.http = new XMLHttpRequest();
	}
	if (!this.http && window.ActiveXObject)
	{
		try
		{
			this.http = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try
			{
				this.http = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) { this.http = false; }
		}
	}
	if (this.http) this.init = true;
	if (this.debug) alert(this.http);
}

AO.handler = function(o)
{
	var obj = o;
	if (obj.debug) alert("readyState: " + obj.http.readyState);
	
	if (obj.http.readyState == 4)
	{
		// properties
		obj.txt				= obj.http.responseText;
		obj.xml 			= obj.http.responseXML;
		obj.readyState 		= obj.http.readyState;
		obj.status	 		= obj.http.status;
		obj.statusText		= obj.http.statusText;
		obj.headers 		= obj.http.getAllResponseHeaders();
		
		// methods
		obj.getHeader		= function(aHeader) { return obj.http.getResponseHeader(aHeader); }
		
		//if (obj.status != 200) obj.init = false;
		
		if (obj.debug) alert("status: " + obj.status + "\ncalling onload()");
		
		obj.onload();
	}
}

// early utility method ... need to come up with better ones
AO.prototype.putHere = function(itemID, what)
{
	if (what != null) document.getElementById(itemID).innerHTML = what;
	else document.getElementById(itemID).innerHTML = this.txt;
}

AO.prototype.go = function(v)
{
	if (!this.init) return false;
	var me = this;
	try
	{
		this.http.onreadystatechange = function() { AO.handler(me); }
		
		if (v == "GET")
		{
			if (this.debug) alert("getting: " + this.uri);
			this.http.open("GET", this.uri, true);
			this.http.send(null);
		}
		else if (v == "POST")
		{
			if (this.debug) alert("posting: " + this.url + "\npost data: " + this.query);
			this.http.open("POST", this.url, true);
			// todo: set this as an option so we can actually do a web-service type thing
			this.http.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
			this.http.send(this.query);
		}
	}
	catch(e)
	{
		if (this.debug) alert("failed to connect");
		this.init = false;
		this.onload();
	}
}

AO.prototype.get = function() { this.go("GET"); }
AO.prototype.post = function() { this.go("POST"); }
AO.prototype.onload = function() { };


var iframe_box;


function YUI_iFrameBox(title, url, width, height){
    var handleClose = function() {
        this.hide();
    };
    var html = '<iframe src=\''+url+'\' width='+width+' height='+height+' marginwidth="0" marginheight="0" vspace="0" hspace="0" allowtransparency="true" borderwidth=0 frameborder=0></iframe>';
    iframe_box = new YAHOO.widget.SimpleDialog('iframe_box', 
    {width: (parseInt(width) + 20) + 'px',
    modal: true,
    fixedcenter: true,
    visible: false,
    draggable: true,
    constraintoviewport: true
    } );
    iframe_box.setHeader(title);
    iframe_box.setBody(html);
    iframe_box.render('yui_container'); 
    iframe_box.show();
}


//Ham hien thi cua so chuyen khoan
chuyenkhoan = function (to,soluong,lydo){
    YUI_iFrameBox("Chuyển khoản", "/xeng/chuyenkhoan_nhanh.aspx?den="+to+"&soluong="+soluong+"&lydo="+lydo, 700, 250);
}
sendPM = function(username){
    sendMail(username);
}
sendXeng = function(username){
    chuyenkhoan(username,'','');
}
//Ham hien thi menu cua nguoi dung
var _user_menu_current = "";
user_menu = function(e, username){
        username = username.replace("'","\'");
        var Dom = YAHOO.util.Dom;
	    var Event = YAHOO.util.Event;
	    if(_user_menu_current!=''){
	        var prev = new YAHOO.widget.Menu(_user_menu_current);
	        prev.hide();
	    }
	    _user_menu_current = "usermenu:"+username
        var oMenu = new YAHOO.widget.Menu(_user_menu_current, { fixedcenter: false });
        var items = oMenu.getItems();
        if(items==null || items.length==0){
            oMenu.addItem({ text: 'Gửi tin nhắn',classname:'sendpm',url:'javascript:sendMail("'+username+'");'},0);
            oMenu.addItem({ text: 'Gửi XÈNG',classname:'sendxeng',url:'javascript:sendXeng("'+username+'");'},0);
            oMenu.addItem({ text: 'Thông tin cá nhân',classname:'profile', url: '/thanhvien/profile.aspx?username='+ username },1);
            oMenu.addItem({ text: 'Trang riêng',classname:'mypage', url: 'http://my.gamevui.com/'+ username + '/default.aspx' },1);
            oMenu.addItem({ text: 'Trang blog', classname:'blog', url:'http://my.gamevui.com/'+username+'/blog/default.aspx' },1);
            oMenu.addItem({ text: 'Kết bạn',classname:'addfriend',url:'/friend/add.aspx?username=' + username },2);
            oMenu.render("yui_container");
        }
	    oMenu.show();
	    Dom.setXY(oMenu.element, Event.getXY(e)  ,true);
}

buy_click = function(id){
    YUI_iFrameBox('Mua hàng', '/fastbuy.aspx?mode=buy&prodId='+id, 500, 400);
}
give_click = function(id){
    YUI_iFrameBox('Mua và tặng quà', '/fastbuy.aspx?mode=give&prodId='+id, 500, 400);
}
sgd_click = function(pId, spId){
    YUI_iFrameBox('Rao bán sản phẩm', 'SGDAdd.aspx?mode=sgd&proId='+pId+'&sproId='+spId, 500, 450);
}
sgd_sell_click = function(title,id, mode){
    YUI_iFrameBox(title, '/SGD/SGDAction.aspx?mode=' + mode +'&id='+id, 500, 450);
}
thuhoi_click = function(title,id){
    YUI_iFrameBox(title, 'thuhoi.aspx?id='+id, 400, 200);
}
gian_lan = function(title,id){
    YUI_iFrameBox(title, '/cms/shop/_GianLan.aspx?id=' + id , 400, 200);
}
datmua = function(title,id){
    YUI_iFrameBox(title, '/SGD/Buy.aspx?id=' + id , 500, 450);
}
give_click2 = function(id){
            YUI_iFrameBox('Tặng quà', '/shopcontrol/fastgive.aspx?mode=give&prodId='+id, 500, 400);
        } 
        
function YUI_iFrameBoxHid(title, url, width, height){
    var handleClose = function() {
        this.hide();
    };
    var html = '<iframe src=\''+url+'\' width='+width+' height='+height+' marginwidth="0" marginheight="0" vspace="0" hspace="0" allowtransparency="true" borderwidth=0 frameborder=0></iframe>';
    iframe_box = new YAHOO.widget.SimpleDialog('iframe_box', 
    {width: (parseInt(width) + 20) + 'px',
    fixedcenter: true,
    modal: true,
    visible: false,
    draggable: true,
    constraintoviewport: true
    } );
    iframe_box.setHeader(title);
    iframe_box.setBody(html);
    iframe_box.render('yui_container'); 
    iframe_box.show();
}
        check_key= function()
        {
        YUI_iFrameBoxHid('Mở khoá cửa hàng','/shopcontrol/ShopKeySecurity.aspx', 350, 300);
        }