function module(){
// module class : interface using http://module.ogplanet.com
// written by : jin kim, 2009.09.25
// last modified by : jin kim, 2009.10.16
// version : 1.0
// compressed using dojo shirnksafe and then JavaScript Compressor(http://dean.edwards.name/packer/)
/*
// below example is sample codeused in this class
$.getJSON( "http://module.ogplanet.com/main.og?callback=?", {id:"hlist"}, function(data){	//ok
    alert("Data Loaded: " + data.ErrorCode);
	// call back
	if ( typeof(moduleCallBack) != "undefined") {
	 	moduleCallBack( data );
	}
});
*/



	// class variables ///////////////////////////////////////////////////////////////////////////////////
	NA = "http://module.ogplanet.com/main.og?callback=?";	
	EU = "http://module.ogplanet.eu/main.og?callback=?";

	connect = NA;	
	this.bDebug = false;
	this.errMsg = "";
	this.callBack = "moduleCallBack";

	// initialize ////////////////////////////////////////////////////////////////////////////////////////
	if (typeof jQuery == 'undefined') {  
	    // jQuery is not loaded
		alert("Please load jQuery to use module.js.");
		return;		      
	} 

	// setServer
	this.setServer = function(ServerName){
		// to lower case
		//ServerName = ServerName.toString().toLowerCase();
		ServerName = ServerName.toString().toUpperCase();
		
		// set servers
		if ( ServerName=="NA" ){
			connect = NA;
		} else if ( ServerName=="EU") {
			connect = EU;		
		} else {
		 	this.errMsg += "setServer - parameter invalid.";
		}

		// return 
		return this.printDebug();
	}

	// methods //////////////////////////////////////////////////////////////////////////////////////////
	// test
	this.test = function(){
		alert("test is working");
	}

	// helpdesk list
	this.helpdeskList = function(PageNum, Status){
		
		// init & check param
		if ( PageNum == undefined || !isNumeric(PageNum) ) PageNum = 1;
		if ( Status == undefined || !isNumeric(Status) ) Status = -1;

		// call ajax
		this.CallJSON( {id:"hlist", PageNum:PageNum, Status:Status },  "helpdeskList");

		// return 
		return this.printDebug();
	}

	// helpdesk view
	this.helpdeskView  = function(Num){
		
		// init & check param
		if ( Num == undefined || !isNumeric(Num) ) this.errMsg +="helpdeskView - parameter invalid.";
 
		// call ajax
		this.CallJSON( {id:"hview", Num:Num},  "helpdeskView");
		
		// return 
		return this.printDebug();
	}

	// helpdesk Category
	this.helpdeskCategory  = function(Cat){
		
		// init & check param
 		Cat = Cat.toString();
		if ( Cat.length < 1 ) this.errMsg += "helpdeskCategory - parameter invalid.";
		
		// call ajax
		this.CallJSON( {id:"hcategory", Cat:Cat},  "helpdeskCategory");
		
		// return 
		return this.printDebug();
	}
	
	// news list
	this.newsList = function(BoardID, Category, PageNum, PageSize, CallBackOrder ){
		if ( connect == NA ){

			// init & check param
			BoardID = BoardID.toString().toLowerCase();
			if ( !(BoardID=="ogp_notice" || BoardID=="ogp_event" || BoardID=="ogp_patch" || BoardID=="ogp_all") ) 
				this.errMsg += "newsList - parameter invalid.";
			if ( Category == undefined || !isNumeric(Category) ) this.errMsg += "newsList - parameter invalid.";
			if ( PageNum == undefined || !isNumeric(PageNum) ) PageNum=1;
			if ( PageSize == undefined || !isNumeric(PageSize) ) PageSize=20;				

			// call ajax
			this.CallJSON( {id:"nlist", BoardID:BoardID, Category:Category, PageNum:PageNum, PageSize:PageSize },  "newsList", CallBackOrder);
	
			// return 
			return this.printDebug();
		}
		else {	// if (connect == EU)
		
			// init & check param
			if ( BoardID == undefined || !isNumeric(BoardID) ) this.errMsg += "newsList - parameter invalid.";			
			if ( Category == undefined || !isNumeric(Category) ) this.errMsg += "newsList - parameter invalid.";
			if ( PageNum == undefined || !isNumeric(PageNum) ) PageNum=1;
			if ( PageSize == undefined || !isNumeric(PageSize) ) PageSize=20;				
	
			// call ajax
			this.CallJSON( {id:"nlist", BoardID:BoardID, GameID:Category, PageNum:PageNum, PageSize:PageSize },  "newsList", CallBackOrder);
	
			// return 
			return this.printDebug();
		}
	
	}

	// news view
	this.newsView = function(Num){
		
		// init & check param
		if ( Num == undefined || !isNumeric(Num) ) this.errMsg += "newsList - parameter invalid.";
	
		// call ajax
		this.CallJSON( {id:"nview", Num:Num},  "newsView");
	
		// return 
		return this.printDebug();
	}

	
	// main news list
	this.mainnewsList = function( Category, PageSize ){

		if ( connect == NA ){
		
			// init & check param
			if ( Category == undefined || !isNumeric(Category) ) this.errMsg += "newsList - parameter invalid.";
			if ( PageSize == undefined || !isNumeric(PageSize) ) PageSize=10;				
	
			// call ajax
			this.CallJSON( {id:"mnlist", Category:Category, PageSize:PageSize },  "mainnewsList");
	
			// return 
			return this.printDebug();
		}
		else {	// if (connect == EU)
		
			// init & check param
			if ( Category == undefined || !isNumeric(Category) ) this.errMsg += "newsList - parameter invalid.";
			if ( PageSize == undefined || !isNumeric(PageSize) ) PageSize=20;				
	
			// call ajax
			this.CallJSON( {id:"mnlist", GameID:Category, PageSize:PageSize },  "mainnewsList");
	
			// return 
			return this.printDebug();
		}
	}

	// main topic list
	this.maintopicList = function( ServiceCode, PageSize ){
		// init & check param
		if ( ServiceCode == undefined || !isNumeric(ServiceCode) ) this.errMsg += "maintopicList - parameter invalid.";
		if ( PageSize == undefined || !isNumeric(PageSize) ) PageSize=10;				

		// call ajax
		this.CallJSON( {id:"mtlist", ServiceCode:ServiceCode, PageSize:PageSize },  "maintopicList");

		// return 
		return this.printDebug();
	}

	// main screenshot list
	this.mainscreenshotList = function( ServiceCode, PageSize ){
		// init & check param
		if ( ServiceCode == undefined || !isNumeric(ServiceCode) ) this.errMsg += "mainscreenshotList - parameter invalid.";
		if ( PageSize == undefined || !isNumeric(PageSize) ) PageSize=10;				

		// call ajax
		//this.CallJSON( {id:"mslist", ServiceCode:ServiceCode, PageSize:PageSize },  "mainscreenshotList");
		this.CallJSON( {id:"mslist", ServiceCode:ServiceCode },  "mainscreenshotList");

		// return 
		return this.printDebug();
	}

	// screenshot list
	this.screenshotList = function( ServiceCode, PageSize, PageNum ){
		// init & check param
		if ( ServiceCode == undefined || !isNumeric(ServiceCode) ) this.errMsg += "screenshotList - parameter invalid.";
		if ( PageSize == undefined || !isNumeric(PageSize) ) PageSize=10;
		if ( PageNum == undefined || !isNumeric(PageNum) ) PageNum=1;						

		// call ajax
		this.CallJSON( {id:"slist", ServiceCode:ServiceCode, PageSize:PageSize, PageNum:PageNum },  "screenshotList");

		// return 
		return this.printDebug();
	}
	
	// screenshot view
	this.screenshotView  = function(Num){
		
		// init & check param
		if ( Num == undefined || !isNumeric(Num) ) this.errMsg +="screenshotView - parameter invalid.";
 
		// call ajax
		this.CallJSON( {id:"sview", Num:Num},  "screenshotView");
		
		// return 
		return this.printDebug();
	}	
	
	// main item list
	this.mainitemList = function( ServiceCode ){
		// init & check param
		if ( ServiceCode == undefined || !isNumeric(ServiceCode) ) this.errMsg += "mainitemList - parameter invalid.";

		// call ajax
		this.CallJSON( {id:"milist", ServiceCode:ServiceCode },  "mainitemList");

		// return 
		return this.printDebug();
	}
	
	// item list
	this.itemList = function( ServiceCode, PageSize, PageNum ){
		// init & check param
		if ( ServiceCode == undefined || !isNumeric(ServiceCode) ) this.errMsg += "itemList - parameter invalid.";
		if ( PageSize == undefined || !isNumeric(PageSize) ) PageSize=10;
		if ( PageNum == undefined || !isNumeric(PageNum) ) PageNum=1;
		
		// call ajax
		this.CallJSON( {id:"ilist", ServiceCode:ServiceCode },  "itemList");

		// return 
		return this.printDebug();
	}	
	
	// comment list
	this.commentList = function( BoardName, PageSize, PageNum ){
		// init & check param
		
		BoardName = BoardName.toString();
		if ( BoardName.length < 1 ) this.errMsg += "commentList - parameter invalid.";
		if ( PageSize == undefined || !isNumeric(PageSize) ) PageSize=10;
		if ( PageNum == undefined || !isNumeric(PageNum) ) PageNum=1;
		
		// call ajax
		this.CallJSON( {id:"clist", BoardName:BoardName, PageSize:PageSize, PageNum:PageNum },  "commentList");

		// return 
		return this.printDebug();
	}

	// poll list
	this.pollList = function(Category, PageSize, PageNum ){

		// init & check param
		Category = Category.toString().toLowerCase();
		if ( Category.length <1 || Category.length > 21 ) this.errMsg += "pollList - parameter invalid.";
		if ( PageNum == undefined || !isNumeric(PageNum) ) PageNum=1;
		if ( PageSize == undefined || !isNumeric(PageSize) ) PageSize=10;				
	
		// call ajax
		this.CallJSON( {id:"plist", Category:Category, PageSize:PageSize, PageNum:PageNum },  "pollList");
	
		// return 
		return this.printDebug();
	}
		
	// poll view
	this.pollView  = function(Num){
		
		// init & check param
		if ( Num == undefined || !isNumeric(Num) ) this.errMsg +="pollView - parameter invalid.";
 
		// call ajax
		this.CallJSON( {id:"pview", Num:Num},  "pollView");
		
		// return 
		return this.printDebug();
	}

	// main poll list
	this.mainpollList = function(Category, CallBackOrder ){

		// init & check param
		Category = Category.toString().toLowerCase();
		if ( Category.length <1 || Category.length > 21 ) this.errMsg += "mainpollList - parameter invalid.";
	
		// call ajax
		this.CallJSON( {id:"mplist", Category:Category },  "mainpollList", CallBackOrder);
	
		// return 
		return this.printDebug();
	}

	// poll submit
	this.pollSubmit = function(Num, Answer ){

		// init & check param
		if ( Num == undefined || !isNumeric(Num) ) this.errMsg += "pollSubmit - parameter invalid.";
		if ( Answer == undefined || !isNumeric(Answer) ) this.errMsg += "pollSubmit - parameter invalid.";	
	
		// call ajax
		this.CallJSON( {id:"psubmit", Num:Num, Answer:Answer },  "pollSubmit");
	
		// return 
		return this.printDebug();
	}
	
	// rank list
	this.rankList = function(gameName, PerPage, CallBackOrder){

		if ( gameName == undefined || !isNumeric(CallBackOrder) ) this.errMsg += "RankList - parameter invalid.";
		// call ajax
		this.CallJSON( {id:"rklist", gameName:gameName, PerPage:PerPage}, "rankList", CallBackOrder);
		
		// return
		return this.printDebug();
	}
	
	// log in check
	this.logCheck = function(CallBackOrder){
		if (!isNumeric(CallBackOrder) ) this.errMsg += "logCheck - parameter invalid.";
		this.CallJSON( {id:"logCheck"}, "logCheck", 0);
	}
	
	//////////////////////////////////////////////////////////////////////////////////////////////////

	// call getJSON function
	this.CallJSON = function(param, fname, CallBackOrder){
	
		var callBack = this.callBack + CallBackOrder;
		//alert(callBack);
		//alert(this.errMsg);
		var msg = "";		
		if ( this.errMsg==""){
			try{
				$.getJSON( connect, param, function(data){
	        		// call back
					eval ("if ( typeof("+ callBack +") != 'undefined') { "+ callBack +"( data ); }; ");
				});
			}
			catch(err){
				msg = fname + " - error occured : "+ err.description + "\n";
			}
			if ( msg != "" ) this.errMsg += msg ;
		}
	}


	// printDebug function
	this.printDebug = function(){
		if ( this.bDebug == true ) {
			if ( this.errMsg != "" ) {
				alert(this.errMsg);
				this.errMsg = "";
			}
		}
		return (this.errMsg==""?true:false);
	}

	// show Error Message
	this.showErrMsg = function(){
		if (this.errMsg == ""){
			alert("No error message.");
		}
		else{
			alert(this.errMsg);
		}
	}

	// inNumeric
	function isNumeric(strString){
		return (Number(strString) == strString?true:false);
	}
}



// get paging
function getPaging(TotalCnt , PerPage, PageNum){

	// lastPage, startPage, endPage, previousPage, nextPage
	var lPage = parseInt(TotalCnt/PerPage)+(TotalCnt%PerPage==0?0:1);
	var sPage = PageNum - (PageNum%10==0?10:PageNum%10) + 1;
	var ePage = lPage < sPage+9 ? lPage : sPage + 9;
	var pPage = PageNum < 11 ? sPage : sPage-1;
	var nPage = sPage+10 < lPage ? sPage+10 : lPage;

	// parameter
	var strParam = "";
	var strHref = window.location.href;
	if ( strHref.indexOf("?") > -1 ){
		var strQueryString = strHref.substr(strHref.indexOf("?"));
		var aQueryString = strQueryString.split("&");
		for ( i = 0; i < aQueryString.length; i++ ){
			if ( aQueryString[i].toLowerCase().indexOf("pn=") < 0 ){
				strParam = strParam + ( strParam==""?"":"&");
				strParam = strParam + aQueryString[i];
			}
		}
	}
	strParam = strParam + ( strParam.indexOf("?")==0?"&pn=":"?pn=");

	// paging
	var paging = "";
	paging = paging + ( sPage==pPage?"":"<li><a href='"+strParam+pPage+"'>Prev</a></li> | ");
	for ( i=sPage; i<=ePage; i++ ){
		paging = paging + (sPage==i?"":" | ");
		paging = paging + (PageNum==i?"<li><a href='#' class='current'>"+i+"</a></li>":"<li><a href='"+strParam+i+"'>"+i+"</a></li>")
	}
	paging = paging + (ePage==nPage?"":" | <li><a href='"+strParam+nPage+"'>Next</a></li>");
	paging = "<ul>" + paging + "</ul>"

	// return
	return paging;
}

// get url param
function getURLParam(strParamName){
    var strReturn = "";
    var strHref = window.location.href;
    if ( strHref.indexOf("?") > -1 ){
		var strQueryString = strHref.substr(strHref.indexOf("?")).toLowerCase();
		var aQueryString = strQueryString.split("&");
		for ( var iParam = 0; iParam < aQueryString.length; iParam++ ){
		    if ( aQueryString[iParam].indexOf(strParamName.toLowerCase() + "=") > -1 ){
			var aParam = aQueryString[iParam].split("=");
			strReturn = aParam[1];
			break;
		    }
		}
    }
    return unescape(strReturn);
}
	

// un- fitJSON : change &quot; to "
function unfitJSON(str){
	str = str.replace(/&quot;/g, "\"");
	return str;
}