var adTitleCache = new Array();
var adUrlCache = new Array();
var adDesc1Cache = new Array();
var adDesc2Cache = new Array();
var adCategoryCache = new Array();
var adIdCache = new Array();
var adUseGraphicCache = new Array();
var curWidgetAdIndex = new Array();
var adMoveCache = new Array();
var counter = 0;
var checkBannerStatus = false;
var adPanelIndex = 0;
var adAdTimer = 0;
var adPanelActive = false;
var adPosIndex = 3;

function getWidgetAdInfo(widgetType, widgetId) {
	curWidgetAdIndex = new Array();
	var adCategory = "";
	if(widgetType == 6) {
		switch(widgetId) {
			case 7:
				adCategory = "Weather";
			break;
			case 16:
			case 15:
				adCategory = "Video";
			break;
			case 61:
			case 62:
			case 63:
			case 64:
			case 65:
			case 52:
				adCategory = "Game";
			break;
			case 53:
				adCategory = "Survey";
			break;
			case 50:
			case 38:
				adCategory = "Education";
			break;
			case 12:
				adCategory = "Meals";
			break;
			case 17:
				adCategory = "Adult";
			break;
			case 3:
				adCategory = "Stock";
			break;
			case 4:
				adCategory = "Currency";
			break;
			
		}
	} else {
		switch(widgetType) {
			case 7:
				adCategory = "Weather";
			break;
			case 16:
			case 15:
				adCategory = "Video";
			break;
			case 20:
				adCategory = "Game";
			break;
			case 2:
				adCategory = "Survey";
			break;
			case 13:
			case 14:
				adCategory = "Education";
			break;
			case 12:
				adCategory = "Meals";
			break;
			case 17:
				adCategory = "Adult";
			break;
		}
	}
	getWidgetAdIndex(adCategory);
}

function getWidgetAdIndex(adCategory) {
	if(adCategory.length == 0) {
		return;
	}
	var count = 0;
	for(var i =0; i < adCategoryCache.length; i++) {
		if(adCategory == adCategoryCache[i]) {
			curWidgetAdIndex[count] = i;
			++count;
		}
	}
	changeAdArray(2);
	if(curWidgetAdIndex.length > 0) {
		showAdsPanel();
	}
}

function getAdInfo(category) {
	var params = "method=getAdInfo" + "&serial=" + serial + "&category=" + category;
	createAjaxRequest();
	if(ajaxRequest == null) {
		return;
	}
	ajaxRequest.onreadystatechange = function() { 
		if (ajaxRequest.readyState == 4) {
	    	if(ajaxRequest.status == 200) {
	    		if(ajaxRequest.responseText == "recall") {
	    			getAdInfo();
	    		} else {
	    			var response = ajaxRequest.responseText;
	    			if(response.length > 0) {
	    				var data = response.split("-?#-");
    					adTitleCache = data[0].split("-@#-");
    					adUrlCache = data[1].split("-@#-");
    					adDesc1Cache = data[2].split("-@#-");
    					adDesc2Cache = data[3].split("-@#-");
    					adCategoryCache = data[4].split("-@#-");
    					adIdCache = data[5].split("-@#-");
    					adUseGraphicCache = data[6].split("-@#-");
    					changeAdArray(1);
						showAdsPanel();
						curWidgetAdIndex = new Array();
						adPanelActive = true;
    				}
	    		}
			} else if(ajaxRequest.status == 500) { 
				failureInfo();
			}
		} 
	 };
 
	ajaxRequest.open("POST", serverUrl, true);
	ajaxRequest.setRequestHeader("Content-Type" , "application/x-www-form-urlencoded");
	ajaxRequest.send(params);
	return false;
	
}

function exitAdPanel() {
	adPanelActive = false;
	clearAdBackground();
	clearAdContent();
	hideAdPanel();
	counter=0;
}

function changeAdArray(index) {
	counter = 0;
	adMoveCache = new Array();
	adPanelIndex = 0;
	if(index == 1) {
		var count = -1;
		for(var i = 0; i < adTitleCache.length; i++) {
			++count;
			adMoveCache[count] = i;
			if(i == adTitleCache.length - 1) {
				for(var j = 0; j < adTitleCache.length; j++) {
					if(adUseGraphicCache[j] == 1) {
						++count;
						adMoveCache[count] = j;
					}
				}
			}
		}
	} else {
		var count = -1;
		for(var i = 0; i < curWidgetAdIndex.length; i++) {
			++count;
			adMoveCache[count] = curWidgetAdIndex[i];
			if(i == curWidgetAdIndex.length - 1) {
				for(var j = 0; j < curWidgetAdIndex.length; j++) {
					var curIndex = curWidgetAdIndex[j];
					if(adUseGraphicCache[curIndex] == 1) {
						++count;
						adMoveCache[count] = curWidgetAdIndex[j];
					}
				}
			}
		}
	}
}

function changeAdPanel(index) {
	checkBannerStatus = false; 
	var curIndex = 0;
	++counter;
	if(curWidgetAdIndex.length == 0) {
		if(counter > adTitleCache.length) {
			checkBannerStatus = true;
		}
	} else {
		if(counter > curWidgetAdIndex.length) {
			checkBannerStatus = true;
		}
	}
//	alert(adMoveCache.length + "adMoveCache.length: " + counter);
	adPanelIndex = adPanelIndex < 0 ? adMoveCache.length-1 : adPanelIndex;
	adPanelIndex = adPanelIndex >= adMoveCache.length ? 0 : adPanelIndex;
	if(counter > adMoveCache.length) {
		clearAdInterval();
		exitAdPanel();
		adAdTimer = window.setInterval("recallAdMethod();", 10*1000);
		return;
	} else {
		curIndex = adMoveCache[adPanelIndex];
	}
	if(checkBannerStatus == true) {
		clearAdContent();
		var useGraphic =  adUseGraphicCache[curIndex];
		var adBanner = '<object height="70" width="322">'
			+'<param value="/ImageHandlerMobi?value='+ adIdCache[curIndex] + '&icon=-1&imageType=6" clsid="d27cdb6e-ae6d-11cf-96b8-444553540000" name="movie"/>'
			+'<param name="allowScriptAccess" value="sameDomain" />'
			+'<param name="allowFullScreen" value="true" />'
			+'<param name="quality" value="high" />'
			+'<param name="wmode" value="transparent" />'
			+'<param name="bgcolor" value="#000000" />'
			+'<embed height="70" width="322" clsid="d27cdb6e-ae6d-11cf-96b8-444553540000" wmode="transparent" src="/ImageHandlerMobi?value='+ adIdCache[curIndex] + '&icon=-1&imageType=6" quality="high" bgcolor="#000000" align="middle" allowScriptAccess="sameDomain" allowFullScreen="true" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />'
			+'</object>'
		document.getElementById("adsBanner").innerHTML = (useGraphic == 1 ? adBanner : 'banner not available' );
	} else {
		document.getElementById("adsBanner").innerHTML = "";
		document.getElementById("adTitlePanel").innerHTML = adTitleCache[curIndex];// + " : " + counter;// + " : " + adCategoryCache[curIndex];
		document.getElementById("adDescPanel").innerHTML = adDesc1Cache[curIndex];
		document.getElementById("adUrlPanel").innerHTML = "<u>" + adUrlCache[curIndex] + "</u>";
	}
	if(adAdTimer) {
		window.clearInterval(adAdTimer);
		adAdTimer = 0;
	}
	adAdTimer = window.setInterval("changeAdPanel(2);", 5000);
	index == 1 ? --adPanelIndex : ++adPanelIndex;
}

function recallAdMethod() {
	adPanelIndex=0;
	checkBannerStatus = false;
	clearAdContent();
	counter=0;
	adPanelActive = true;
	changeAdContent();
	document.getElementById('adsPanel').style.visibility = 'visible';
}

function clearAdContent() {
	document.getElementById("adTitlePanel").innerHTML = "";
	document.getElementById("adDescPanel").innerHTML = "";
	document.getElementById("adUrlPanel").innerHTML = "";
}

function clearAdBackground() {
	document.getElementById("adControl1").style.background = "";
	document.getElementById("adControl2").style.background = "";
	document.getElementById("adControl3").style.background = "";
}
function changeAdContent() {
	if(adAdTimer) {
		window.clearInterval(adAdTimer);
		adAdTimer = 0;
	}
	changeAdPanel(2);
}
function clearAdInterval() {
	if(adAdTimer) {
		window.clearInterval(adAdTimer);
		adAdTimer = 0;
	}
}
function showAdArrow(index) {
	clearAdBackground();
}

function hideAdArrow(index) {
	document.getElementById("adControl"+index).style.background = "";
}

function adKeyHandler(keyCodeNum) {
	if(adUrlCache.length == 0) {
		clearAdCache();
		clearAdInterval();
		launchViewer();
		return 0;
	}
	switch(keyCodeNum) {
		case rcUP:
			clearAdCache();
			hideAdPanel();
			clearAdInterval();
			showMiniPanel();
			break;
		case rcDOWN:
			clearAdCache();
			hideAdPanel();
			clearAdInterval();
			showMiniPanel();
			break;
		case rcLEFT:
			adPosIndex = 1;
			showAdArrow(1);
			changeAdPanel(1);
			break;
		case rcRIGHT:
			adPosIndex = 2;
			showAdArrow(2);
			changeAdPanel(2);
			break;
		case rcOK:
			if(adPosIndex == 3) {
				adPanelActive = false;
				clearAdCache();
				clearAdInterval();
				exitAdPanel();
			} else {
				clearAdInterval();
				hideAdPanel();
				clearAdCache();
				showMiniPanel();
			}
			break;
	}
}
function openAdUrl() {
	if(adUrlCache.length > 0) {
		curIndex = adMoveCache[adPanelIndex];
		window.open(adUrlCache[curIndex]);
	}
}

function clearAdCache() {
	adTitleCache = new Array();
	adUrlCache = new Array();
	adDesc1Cache = new Array();
	adDesc2Cache = new Array();
	adCategoryCache = new Array();
	adIdCache = new Array();
	adUseGraphicCache = new Array();
	curWidgetAdIndex = new Array();
	adMoveCache = new Array();
	counter = 0;
	checkBannerStatus = false;
	adPanelIndex = 0;
	adAdTimer = 0;
	adPanelActive = false;
	adPosIndex = 3;
}
	
	