/*
 * Subject : Banner UI Control Script
 * Author  : javakiller(Lee, hyo-min)
 * Date    : 2009-02-10
 * */

var Domain_Custom_SImage = "http://imgdg.joycity.com/th";
var BANNER_URL = "http://bannersys.joycity.com";
var currentUrl = window.location.href;
var flash_expressInstall = "http://imgdg.joycity.com/gx/real/images/multimedia/expressInstall.swf";
var flash_version = "9.0.124.0";

function bannerInfo_Alert(arrItem){
	return false;
}

//section preview.
function bannerSecDisplay(secID, secNM){
	var param;
	//var domain_valid = /(http(s)?:\/\/)?\S+(\.joycity\.com+)+/gi
	
	//if(domain_valid.test(currentUrl)) {
		if(currentUrl.indexOf('isBannerSecDisplay') > 0) {
			param = currentUrl.substring(currentUrl.indexOf('isBannerSecDisplay')).split('=');
			if(param.length == 2) {
				if(param[1] == 1) {
					//	boundary style.
					jQuery('#'+secID).append(decodeURI(secNM)+'<br/>');
					jQuery('#'+secID).attr('style','border: 2px solid #CD1039;font-weight:bold;color:#FF5675;');
					jQuery('#'+secID).attr('align','center');
				}
			}
		}
	//}
}

//	확률 노출:: LS
function bannerUI_Type_LS(arrItem) {
	try{
	
		var sectionID = arrItem[0].bannerSecID;

		bannerSecDisplay(sectionID, arrItem[0].sectionName);

		//	banner file set.
		if(arrItem[0].bannerIdx > 0) {
			if(arrItem[0].imgType == 2) {	
				jQuery('#'+sectionID).append('<div id=\"'+sectionID+'_'+arrItem[0].bannerIdx+'\"></div>');
				banner_embed_flash(sectionID+'_'+arrItem[0].bannerIdx, arrItem[0].imgUrl, arrItem[0].imgWidth, arrItem[0].imgHeight);
			}
			else {
				jQuery('#'+sectionID).append(
					'<a href=\"#\" onclick=\"th_banner_goUrl('+arrItem[0].siteCode+','+arrItem[0].bannerIdx+',\''+arrItem[0].linkUrl+'\','+arrItem[0].linkType+')\" >'+
					'<img src=\"'+arrItem[0].imgUrl+'\" title=\"'+arrItem[0].eventName+'\" alt=\"'+arrItem[0].eventName+'\" border=\"0\" ></a>'
				);
			}
			
			//	show logging.
			jQuery.getJSON(BANNER_URL+"/show_log.jce?bannerIdxGroup="+arrItem[0].bannerIdx+"&callback=?");
			
		}else {	//	default file set.
			if(arrItem[0].defImgType == 2) {
				jQuery('#'+sectionID).append('<div id=\"'+sectionID+'_'+arrItem[0].bannerIdx+'\"></div>');
				banner_embed_flash(sectionID+'_'+arrItem[0].bannerIdx, arrItem[0].defImgUrl, arrItem[0].defImgWidth, arrItem[0].defImgHeight);
			}
			else if(arrItem[0].defImgType == 1) {
				jQuery('#'+sectionID).append(
					'<img src=\"'+arrItem[0].defImgUrl+'\" title=\"'+arrItem[0].sectionName+'\" alt=\"'+arrItem[0].sectionName+'\" border=\"0\" >'
				);
			}
		}
	}catch(e){alert(e.message);}
}

//	리스트형 세로 노출 :: TS
function bannerUI_Type_TS(arrItem) {
	try{
		var sectionID = arrItem[0].bannerSecID;
		var bannerIdxGroup = '';
		
		bannerSecDisplay(sectionID, arrItem[0].sectionName);

		//	banner file set.
		if(arrItem[0].bannerIdx > 0) {
			
				jQuery('#'+sectionID).append(jQuery("<ul>"))
				
				jQuery(arrItem).each(function(i){
				if(arrItem[i].imgType != 0){
					if(arrItem[i].imgType == 2) {
						jQuery('#'+sectionID+' ul').append('<li style=\"list-style-type:none;\"><div id=\"'+sectionID+'_'+arrItem[i].bannerIdx+'\"></div></li>');
						banner_embed_flash(sectionID+'_'+arrItem[i].bannerIdx, arrItem[i].imgUrl, arrItem[i].imgWidth, arrItem[i].imgHeight);
					}
					else {
						jQuery('#'+sectionID+' ul').append(
						'<li style=\"list-style-type:none;\">'+
							'<a href=\"#\" onclick=\"th_banner_goUrl('+arrItem[i].siteCode+','+arrItem[i].bannerIdx+',\''+arrItem[i].linkUrl+'\','+arrItem[i].linkType+')\" >'+
							'<img src=\"'+arrItem[i].imgUrl+'\" title=\"'+arrItem[i].eventName+'\" alt=\"'+arrItem[i].eventName+'\" border=\"0\" >'+
						'</a></li>');
					}
					bannerIdxGroup += arrItem[i].bannerIdx+'|';
				}
				});
	
				//	show logging.
				jQuery.getJSON(BANNER_URL+"/show_log.jce?bannerIdxGroup="+bannerIdxGroup+"&callback=?");
		
		}else {	//	default file set.
			if(arrItem[0].defImgType == 2) {
				jQuery('#'+sectionID).append('<div id=\"'+sectionID+'_'+arrItem[0].bannerIdx+'\"></div>');
				banner_embed_flash(sectionID+'_'+arrItem[0].bannerIdx, arrItem[0].defImgUrl, arrItem[0].defImgWidth, arrItem[0].defImgHeight);
			}
			else if(arrItem[0].defImgType == 1) {
				jQuery('#'+sectionID).append(
					'<img src=\"'+arrItem[0].defImgUrl+'\" title=\"'+arrItem[0].sectionName+'\" alt=\"'+arrItem[0].sectionName+'\" border=\"0\" >'
				);
			}
		}
		
	}catch(e){bannerInfo_Alert(arrItem);}
}

//	리스트형 가로 노출 :: TS2
function bannerUI_Type_TS2(arrItem) {
	try{
		var sectionID = arrItem[0].bannerSecID;
		var bannerIdxGroup = '';
		
		bannerSecDisplay(sectionID, arrItem[0].sectionName);

		//	banner file set.
		if(arrItem[0].bannerIdx > 0) {
			
				jQuery('#'+sectionID).append(jQuery("<ul>"))
				
				jQuery(arrItem).each(function(i){
				if(arrItem[i].imgType != 0){
					if(arrItem[i].imgType == 2) {
						jQuery('#'+sectionID+' ul').append('<li style=\"list-style-type:none;float:left;\"><div id=\"'+sectionID+'_'+arrItem[i].bannerIdx+'\"></div></li>');
						banner_embed_flash(sectionID+'_'+arrItem[i].bannerIdx, arrItem[i].imgUrl, arrItem[i].imgWidth, arrItem[i].imgHeight);
					}
					else {
						jQuery('#'+sectionID+' ul').append(
						'<li style=\"list-style-type:none;float:left;\">'+
							'<a href=\"#\" onclick=\"th_banner_goUrl('+arrItem[i].siteCode+','+arrItem[i].bannerIdx+',\''+arrItem[i].linkUrl+'\','+arrItem[i].linkType+')\" >'+
							'<img src=\"'+arrItem[i].imgUrl+'\" title=\"'+arrItem[i].eventName+'\" alt=\"'+arrItem[i].eventName+'\" border=\"0\" >'+
						'</a></li>');
					}
					bannerIdxGroup += arrItem[i].bannerIdx+'|';
				}
				});
	
				//	show logging.
				jQuery.getJSON(BANNER_URL+"/show_log.jce?bannerIdxGroup="+bannerIdxGroup+"&callback=?");
		
		}else {	//	default file set.
			if(arrItem[0].defImgType == 2) {
				jQuery('#'+sectionID).append('<div id=\"'+sectionID+'_'+arrItem[0].bannerIdx+'\"></div>');
				banner_embed_flash(sectionID+'_'+arrItem[0].bannerIdx, arrItem[0].defImgUrl, arrItem[0].defImgWidth, arrItem[0].defImgHeight);
			}
			else if(arrItem[0].defImgType == 1) {
				jQuery('#'+sectionID).append(
					'<img src=\"'+arrItem[0].defImgUrl+'\" title=\"'+arrItem[0].sectionName+'\" alt=\"'+arrItem[0].sectionName+'\" border=\"0\" >'
				);
			}
		}
		
	}catch(e){bannerInfo_Alert(arrItem);}
}

//	롤링 배너 노출 :: RS
function bannerUI_Type_RS(arrItem) {
	try {
		var bannerIdxGroup = '';
		var sectionID = arrItem[0].bannerSecID;
		
		bannerSecDisplay(sectionID, arrItem[0].sectionName);
		
		//	defImgType : 0(no use), 1(img), 2(flash)
		if(arrItem[0].defImgType == 2) {
			var param = {bnnXML : encodeURIComponent(BANNER_URL+"/getBanner.jce?siteCode="+arrItem[0].siteCode+"&secIdGroup="+sectionID.split('_')[2]+"&dataType=xml")};
			
			jQuery('#'+sectionID).append(jQuery('<div>').attr('id',sectionID+'_'+arrItem[0].bannerIdx));
			banner_embed_flash(sectionID+'_'+arrItem[0].bannerIdx, arrItem[0].defImgUrl, arrItem[0].defImgWidth, arrItem[0].defImgHeight, param);
		
			jQuery(arrItem).each(function(i){
				bannerIdxGroup += arrItem[i].bannerIdx+'|';
			});
	
			//	show logging.
			jQuery.getJSON(BANNER_URL+"/show_log.jce?bannerIdxGroup="+bannerIdxGroup+"&callback=?");
		}

	}catch(e){bannerInfo_Alert(arrItem);}
}

//	레이어 팝업:: LP
function bannerUI_Type_LP(arrItem) {
	try {
		var bannerIdxGroup = '';
		var sectionID = arrItem[0].bannerSecID;

		jQuery(arrItem).each(function(i){
			//	banner file set.
			if((arrItem[i].defImgType == 0) && (arrItem[i].bannerIdx > 0)) {	

				popupSkin_setMachine(arrItem[i].siteCode);

				jQuery.pushCorn(sectionID+'_'+arrItem[i].bannerIdx, BANNER_URL+'/popup.jce?siteCode='+arrItem[i].siteCode+'&secIdGroup='+sectionID.split('_')[2]+'&bannerIdx='+arrItem[i].bannerIdx, Number(arrItem[i].imgWidth), Number(arrItem[i].imgHeight), 1);
				if((arrItem.length-1) == i)
					jQuery.popCorn();	
				
			}			
		});
		
	}catch(e){alert(e.message);}
}

//	게임 엔딩 :: EB
function bannerUI_Type_EB(arrItem) {
	
	try {
		var bannerIdxGroup = '';
		var sectionID = arrItem[0].bannerSecID;
		
		bannerSecDisplay(sectionID, arrItem[0].sectionName);

		//	banner file set.
		if(arrItem[0].bannerIdx > 0) {		
			
			if(arrItem[0].imgType == 2) {
				jQuery('#'+sectionID).append('<div id=\"'+sectionID+'_'+arrItem[0].bannerIdx+'\"></div>');
				banner_embed_flash(sectionID+'_'+arrItem[0].bannerIdx, arrItem[0].imgUrl, arrItem[0].imgWidth, arrItem[0].imgHeight);
			}else {
				jQuery('#'+sectionID).append(
					'<img src=\"'+arrItem[0].imgUrl+'\" title=\"'+arrItem[0].eventName+'\" alt=\"'+arrItem[0].eventName+'\" border=\"0\" usemap=\"#Bannersys_Map\" >'
				);
				jQuery("#"+sectionID).append(
						"<map name=\"Bannersys_Map\" id=\"Bannersys_Map\">"+
							"<area shape=\"rect\" coords=\"0,0,280,250\" href=\"#\" onfocus=\"this.blur()\" />" +
							"<area shape=\"rect\" coords=\"0,0,230,280\" href=\"#\" onfocus=\"this.blur()\" />" +
							"<area shape=\"rect\" coords=\"230,250,280,280\" href=\"#\" onfocus=\"this.blur()\" />" +
						"</map>"
					);			
				jQuery('#Bannersys_Map area:eq(0)').click(function(){
					th_banner_goUrl(arrItem[0].siteCode,arrItem[0].bannerIdx,''+arrItem[0].linkUrl+'',2);
				});
			}
			//	show logging. 
			jQuery.getJSON(BANNER_URL+"/show_log.jce?bannerIdxGroup="+arrItem[0].bannerIdx+"&callback=?");
		
		}else {	//	default file set.
			if(arrItem[0].defImgType == 2) {
				jQuery('#'+sectionID).append(jQuery('<div>').attr('id',sectionID+'_'+arrItem[0].bannerIdx));
				banner_embed_flash(sectionID+'_'+arrItem[0].bannerIdx, arrItem[0].defImgUrl, arrItem[0].defImgWidth, arrItem[0].defImgHeight);
			}
			else if(arrItem[0].defImgType == 1) {
				jQuery('#'+sectionID).append(
					'<img src=\"'+arrItem[0].defImgUrl+'\" title=\"'+arrItem[0].sectionName+'\" alt=\"'+arrItem[0].sectionName+'\" border=\"0\" >'
				);
			}
		}
	}catch(e){bannerInfo_Alert(arrItem);}
}

function banner_embed_flash(pk, fileUrl, width, height, flashVars) {
	
	swfobject.embedSWF(
		fileUrl, pk, width, height, flash_version, flash_expressInstall, 
		flashVars,
		{ allowScriptAccess : "always", allowNetworking : "all", wmode : "transparent" }, 
		{ id : pk, name : pk, style : "position:relative;z-index:5;" }	
	);	

}

function th_banner_goUrl(siteCode, bannerIdx, linkUrl, linkType) {
	var destinationUrl = BANNER_URL+"/click_log.jce?siteCode="+siteCode+"&bannerIdx="+bannerIdx+"&redirectUrl="+encodeURIComponent(linkUrl);
	if(linkType == 1)		top.location.href = destinationUrl;
	else if(linkType == 2)	window.open(destinationUrl);
}

//	Popcorn Popup Skin Config
function popupSkin_setMachine(siteCode) {

	if (Number(siteCode) == 301 || Number(siteCode) == 302 ||Number(siteCode) == 303 )
		siteCode = 30;
	
	var popup_skin_url = Domain_Custom_SImage+"/bannersys/js/klib/";
	jQuery.setMachine({
		bg : popup_skin_url+'images/'+siteCode+'/bg_popup.gif',
		close : popup_skin_url+'images/'+siteCode+'/btn_close.gif',
		x : popup_skin_url+'images/'+siteCode+'/btn_x.gif',
		logo : popup_skin_url+'images/'+siteCode+'/img_logo.gif'
	});

	switch(Number(siteCode)){
		case 50:
			jQuery.setMachine({
				bgColor : '#714dbe',
				border : '2px solid #482b81',
				titleColor : '#c4fe1c',
				title : '고스트X',
				notseeColor : '#d1c7e4',
				marginTop : 37,
				marginBottom : 31		
			});
			break;
		case 30:
			jQuery.setMachine({
				bgColor : '#d9dadb',
				border : '2px solid #afafaf',
				titleColor : '#848484',
				title : '프리스타일',
				notseeColor : '#828282',
				marginTop : 37,
				marginBottom : 31		
			});
			break;
		case 0:
			jQuery.setMachine({
				bgColor : '#ffa800',
				border : '2px solid #ff6000',
				titleColor : '#fff',
				title : '통합 레이어 팝업',
				notseeColor : '#fff',
				marginTop : 37,
				marginBottom : 31	
			});	
			break;
	}	
}