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

var Domain_Custom_SImage = "http://img.gamekiss.com/dg/gsp";
var BANNER_URL = "http://bannersys.gamekiss.com";
var currentUrl = window.location.href;
var flash_expressInstall = "http://img.gamekiss.com/dg/gsp/flash/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+(\.gamekiss\.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;');//background-color:#DCDCDC;
					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><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);}
}

//	롤링 배너 노출 :: 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 : URLEncode(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='+arrItem[i].sectionIdx+'&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,"+arrItem[0].defImgWidth+","+arrItem[0].defImgHeight+"\" />"+
							"<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()\" />" +
							//"<area shape=\"rect\" coords=\"213,246,266,265\" href=\"#\" onclick=\"self.close()\"/>"+
						"</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:0;" }	
	);	

}

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)		location.href = destinationUrl;
	else if(linkType == 2)	window.open(destinationUrl);
}

//	팝업 스킨용 setMachine.
function popupSkin_setMachine(siteCode) {

	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'
	});
	if(siteCode == 20) {
		jQuery.setMachine({
			bgColor : '#714dbe',
			border : '2px solid #482b81',
			titleColor : '#c4fe1c',
			title : 'Gamekiss',
			notseeColor : '#d1c7e4',
			marginTop : 37,
			marginBottom : 31		
		});
	}
	else if(siteCode == 30) {
		jQuery.setMachine({
			bgColor : '#ffa800',
			border : '2px solid #ff6000',
			titleColor : '#fff',
			title : 'Freestyle',
			notseeColor : '#fff',
			marginTop : 37,
			marginBottom : 31	
		});		
	}
}