var ci_error_msg = new Array('US', 'EP');
ci_error_msg['US'] = new Array(7);
ci_error_msg['EP'] = new Array(7);

ci_error_msg['US'] = {
		"bbsDelete_1":"Do you want to delete?",
		"bbsVoteResult_1":"It voted already.",
		"bbsVoteResult_2":"It is successfully recommended.",
		"pollVoteResult_1":"Invalid Parameters",
		"pollVoteResult_2":"It is successfully recommended.",
		"pollVoteResult_3":"It voted already.",
		"ciCommentWrite_1":"Please type the comment."
	 };

ci_error_msg['EP'] = {
		"bbsDelete_1":"¿Deseas borrar?",
		"bbsVoteResult_1":"Ya ha sido votado.",
		"bbsVoteResult_2":"Ha sido exitosamente recomendado.",
		"pollVoteResult_1":"Parámetros inválidos",
		"pollVoteResult_2":"Ha sido exitosamente recomendado.",
		"pollVoteResult_3":"Ya ha sido votado.",
		"ciCommentWrite_1":"Por favor escribe el comentario."
	 };

var ci_country_code = getCookie("MGUSALANGUAGE") ? getCookie("MGUSALANGUAGE") : 'US';

function getCookie(name) {
	var nameOfCookie = name + "=";
	var x = 0;

	while(x <= document.cookie.length) {
		var y	=	(x+nameOfCookie.length);
		if(document.cookie.substring(x,y) == nameOfCookie) {
			if((endOfCookie = document.cookie.indexOf(";",y)) == -1) {
				endOfCookie	=	document.cookie.length;
			}

			return unescape( document.cookie.substring(y,endOfCookie));
		}

		x	=	document.cookie.indexOf(" ",x) + 1;
		if(x == 0)
		break;
	}

	return "";
}

ciLoopContent = function(divID, totalPage, nowPage, interval){
	this.divID = divID ? divID : null;
	this.totalPage = totalPage ? totalPage : 0;
	this.nowPage = nowPage ? nowPage : 1;
	this.interval = interval ? interval : 3000;
}

ciLoopContent.prototype.start = function(){
	
	var pageObj;
	var self = this;
	
	if(this.nowPage > this.totalPage) this.nowPage = 1;

	for(var i=1;i<=this.totalPage;i++){
		try{
			pageObj = document.getElementById(this.divID + i);

			if(this.nowPage == i){
				pageObj.style.display = '';
			}else{
				pageObj.style.display = 'none';
			}
		}catch(e){
			//page obj error
		}
	}
	
	this.nowPage++;
	setTimeout(function(){self.start();},this.interval);
}

function ciLoadMainContent(ajax)
{
	if(ajax.responseXML){
		var rsDom = ajax.responseXML;
		try{ 
			var contentList = rsDom.getElementsByTagName('contentlist')[0].firstChild.nodeValue;
			var arrContentList = contentList.split(',');

			var contentOBJ;
			var contentHTML	=	' ';
			
			for(var i=0;i<arrContentList.length;i++){
				try{ contentHTML = rsDom.getElementsByTagName(arrContentList[i])[0].firstChild.nodeValue; }catch(e){ contentHTML = ' '; }
				try{
					contentOBJ = document.getElementById('div_main_'+arrContentList[i]);
					contentOBJ.innerHTML+=contentHTML; 

					if(arrContentList[i].indexOf('banner') >= 0)
					{
						if(document.getElementById('div_main_'+arrContentList[i]+'_ul'))
						{
							document.getElementById('div_main_'+arrContentList[i]+'_ul').style.display = "block";
						}
					}
					
					if(document.getElementById('div_main_'+arrContentList[i]))
					{
						if(document.getElementById('div_main_'+arrContentList[i]).style.display != "block")
						{
							document.getElementById('div_main_'+arrContentList[i]).style.display = "block";
						}
					}
					
				}catch(e){
					//content object error
				}
			}
		}catch(e){
			//contentList load error
		}
	}else{
		//ajax response error
	}
}

function ciGameStart(gameSub)
{
	ajaxRequest('ajaxGameStart','/common/gameStart',(gameSub?'gameSub=1':''),false);
}

function ciLoginCheck(loginSub)
{	
	ajaxRequest('ajaxlogin','/common/login',(loginSub?'loginSub=1':''),false);
}
function ciCash()
{
	ajaxRequest('ajaxcash','/common/cash','',false);
}
function bbsList(type) 
{
	uri = makeCIUri('list');

	location.href = SITE_PAGE_URL[type]+'/'+uri;
}

function bbsWrite(type) 
{
	if(!netgameUserCheck())return;
	
	location.href = SITE_PAGE_URL[type]+'/mode/write';
}

function bbsEdit(type,idx) 
{
	if(!netgameUserCheck())return;
	
	location.href = SITE_PAGE_URL[type]+'/mode/edit/idx/'+idx;
}

function bbsDelete(type,idx) 
{
	if(!netgameUserCheck())return;
	
	if(!confirm(ci_error_msg[ci_country_code]['bbsDelete_1'])) {
		return;
	}
	
	var f			=	document.procForm;

	f.idx.value		=	idx;
	f.mode.value	=	'delete';
	
	f.action		=	'/forum/proc';
	f.submit();
	
}


function bbsSort(v, type) 
{
	var f	=	document.procForm;
	
	f.sort.value	=	v;	
	
	uri = makeCIUri('sort');
	location.href = SITE_PAGE_URL[type]+'/'+uri;
}

function bbsSearch(type) 
{
	var uri;
	var uriType = 'search';
	var p	=	document.procForm;
	
	var searchWord = document.getElementById('search_searchWord').value;
	var searchName = document.getElementById('search_searchName').value;
	
	p.searchName.value = searchName;
	p.searchWord.value = searchWord;
	
	if(type == 'notice') {
		uriType = 'noticeSearch';
	}

	if(type == 'announcement') {
		uriType = 'announcementSearch';
	}	
	
	uri = makeCIUri(uriType);

	location.href = SITE_PAGE_URL[type]+'/'+uri;
}


function bbsView(idx, type, no) 
{
	var noUri = '';
	var p	=	document.procForm;
	
	p.idx.value			=	idx;
	p.mode.value		=	'view';
	
	if(no > 0)
	{
		noUri = 'no/'+no;
	}

	uri = makeCIUri('view');

	location.href = SITE_PAGE_URL[type]+'/'+uri+noUri;
}

function bbsVote(cate)
{
	if(!netgameUserCheck())return;
	
	var idx	=	document.procForm.idx.value;		
	ajaxRequest("bbsVoteResult",SITE_PAGE_URL["communityProc"],"mode=recom&idx="+idx+"&cate="+cate,true);
}

function bbsVoteResult(html){
	if(html.responseText=='login'){
		error();
	}else if(html.responseText=='error'){
		error(ci_error_msg[ci_country_code]['bbsVoteResult_1']);
	}else{	
		error(ci_error_msg[ci_country_code]['bbsVoteResult_2']);
		document.getElementById('ajaxvote').innerHTML	=	html.responseText;
	}
}

function pollVote(idx)
{
	var obj = document.getElementsByName("poll");	
	for(var i=0;i<obj.length;i++){
		if(obj[i].checked){
			var poll	=	obj[i].value;
		}
	}
	ajaxRequest("pollVoteResult",SITE_PAGE_URL["pollProc"],"mode=poll_vote&idx="+idx+"&poll="+poll,true);
}

function pollVoteResult(html)
{
	
	if(html.responseText==0)
	{
		netgameLayer('', '', '', 3);
	}
	else if(html.responseText==1)
	{
		netgameLayer(ci_error_msg[ci_country_code]['pollVoteResult_1'],  '', '', 3);
	}
	else if(html.responseText==2)
	{
		netgameLayer(ci_error_msg[ci_country_code]['pollVoteResult_2'],  '', '', 3);
	}
	else
	{
		netgameLayer(ci_error_msg[ci_country_code]['pollVoteResult_3'],  '', '', 3);
	}
}


//make codeigniter type url
function makeCIUri(ID)
{
	var uri = '';
	var	elementObject;
	var obj = SEARCH_OBJECT[ID];
	obj = obj.split(",");
	
	for(var i=0;i<obj.length;i++) 
	{
		elementObject = document.getElementById(obj[i].replace(/^\s+|\s+$/g,""));

		if( "undefined" != typeof(elementObject)  && elementObject != null)
		{
			if( "hidden" == elementObject.type || "textarea" == elementObject.type ||
					"file" == elementObject.type || "password" == elementObject.type ||
					"text" == elementObject.type )
			{
				var strValue = elementObject.value.replace(/^\s+|\s+$/g,"");
	
				if( "" != strValue &&  strValue.length > 0)
				{
					uri += obj[i]+'/'+strValue+'/';
				}
			}
			else if( "select-one" == elementObject.type || "select" == elementObject.type )
			{
				if( 0 <= elementObject.options.length )
				{
					if (elementObject.options[elementObject.selectedIndex].value != null && elementObject.options[elementObject.selectedIndex].value != "")
					{
						uri += obj[i]+'/'+elementObject.options[elementObject.selectedIndex].value+'/';
					}
				}
			}
			else if( "checkbox" == elementObject.type )
			{
				if( false != elementObject.checked )
				{
					uri += obj[i]+'/'+elementObject.value+'/';
				}
			}
			else if( "radio" == elementObject.type )
			{
				var selectedObject = getCheckedRadioObject( elementObject );

				if( null != selectedObject )
				{
					uri += obj[i]+'/'+selectedObject.value+'/';
				}
			}
		}
	}

	return uri;
}

function getCheckedRadioObject( radioObject )
{
	if( "undefined" == typeof(radioObject) )
		return null;

	if( 1 < radioObject.length )
	{
		for(i=0; i<radioObject.length; i++)
		{
			if( true == radioObject[i].checked )
				return radioObject[i];
		}
	}
	else
	{
		if( true == radioObject.checked )
			return radioObject;
	}

	return null;
}

function bbsImgReSize(imgObj) {	
	var maxWidth = 550;
    var imgWidth = imgObj.width;
    var imgHeight = imgObj.height;
	
	//document.img_frm.imgwidth.value = imgWidth;
    //document.img_frm.imgheight.value = imgHeight;

 	if(imgWidth > maxWidth) {
  		var widthRatio = maxWidth/imgWidth;
		imgObj.width = maxWidth;
		imgObj.height = imgHeight * widthRatio;
 	}
}

function ciCommentWrite() {

	if(!netgameUserCheck()) return false;

	var f	=	document.commentForm;
	
	f.comment.value	=	netgameIlligalWordsCheck(f.comment.value);

	if(!f.comment.value.trim()) {
		error(ci_error_msg[ci_country_code]['ciCommentWrite_1']);
		f.comment.focus();
		return false;
	}

	ajaxRequest(ajaxId(),ajaxProc(),ajaxVariable() +"&mode=write&comment="+ encodeURIComponent(f.comment.value));
	f.reset();
	ajaxResize();
	
	return false;
}

