var ContentSummary = {
	language : "en",
	
	switchTab : function(tabName) {
		document.getElementById('selectedTab').value = tabName;
		document.getElementById('startIndex').value = '-99999';
		this.updateContent();
	},
	
	setLanguage : function(lang){
		this.language = lang;
	},
	
	setStartIndex : function(index) {
		document.getElementById('startIndex').value = index;
		this.updateContent();
	},

	updateBanner : function() {		
		try {
			$.ajax({
		        type: 'POST',
		        url: bannerURL,
		        data: {language:this.language, type:"skyscraper"},
		        success: renderBanner
		    });			
		} catch(err) {};
	},

	updateAds : function() {
		var langParams = {language: this.language};
		try {
			if(document.getElementById('hometextlinks') != null) {				
				$.ajax({
			        type: 'POST',
			        url: textLinksHomepageURL,
			        data: {language:this.language},
			        success: renderTextLinks
			    });				
			}
			if(document.getElementById('textlinks') != null) {				
				$.ajax({
			        type: 'POST',
			        url: textLinksLeftBarURL,
			        data: {language:this.language},
			        success: renderLeftbarTextLinks
			    });				
			}
		} catch(err) {};
	},

	updateContent : function() {
		// update the skyscraper and text links for other languages then en and zh. For en and zh we use GAM		
		if(this.language!='en' && this.language!='zh'){
			this.updateBanner();
			this.updateAds();
		}		
		try {
			// get the params
			var startIndex = $('#startIndex').val();
			var selectedTab = $('#selectedTab').val();			
			$.ajax({
		        type: 'POST',
		        url: rightbarURL,
		        beforeSend: function() { $('#disabledZone').show();},
		        complete: function() { $('#disabledZone').hide();},
		        data: {language: this.language, startIndex: startIndex, selectedTab: selectedTab},
		        dataType: 'html',
		        success: doOnResult
		    });			
		} catch(err) {};
	},

	setSelectedTab : function(lang) {
		if(lang) {
			this.language = lang;
		}
		var tabs = document.getElementById('tabsmenu').getElementsByTagName('LI');
		for(var i = 0; i < tabs.length; i++) {
			if(tabs[i].className.indexOf("-on") != -1) {
				tabs[i].className = tabs[i].className.replace(/-on/, "-off");
			}
		}
		// set the active one
		var selectedTabName = document.getElementById('selectedTab').value;
		document.getElementById('tab_' + selectedTabName).className = 'm-' + selectedTabName + '-on';
		// update the 300x250 banner for other languages then en. For en we use GAM		
		if(this.language!='en' && this.language!='zh'){
			this.update300x250Banner();
		}
	},
	
	determine300x250BannerPosition : function() {
		var position = 2;
/*
		// script to dynamically determine the banner position
		var height = 60;
		try{
			for(var i=1;i<=8;i++){
				if(null != document.getElementById("entry"+i)){
					height += document.getElementById("entry"+i).clientHeight;
					if(height<document.body.clientHeight - 100) {
						position = i;
					}
				}
		}
		}
		catch(err){
			if(document.body.clientHeight>1000){
				position=4;
			} 
			else {
				position=3;
			}
		};
		if(height==60){
			// unable to count height
			position=3;
			if(document.body.clientHeight>1000){
				position=4;
			} 
			else {
				position=3;
			}
		}*/
		// determine if this is a valid position
/*		while(document.getElementById('adIndex'+position) == null && position > 0){
			position --;
		}
		if(position > 0){
			document.getElementById('adIndex'+position).style.height="300px";
		}*/
		ContentSummary.bannerPosition=position;
	},

	update300x250Banner : function() {
		if(this.bannerPosition <= 0){return;}
		var bParams = {language: this.language, type: "300x250", forceBannerDisplay: ContentSummary.forceBannerDisplay};
		try {
			$.ajax({
		        type: 'POST',
		        url: bannerURL,
		        data: {language:this.language, type:"300x250", forceBannerDisplay: ContentSummary.forceBannerDisplay},
		        success: this.render300x250Banner
		    });
			
			//DWRActionUtil.execute({namespace: '/ads', action: 'skyscraper', executeResult: 'true'}, bParams, ContentSummary.render300x250Banner);
		} catch(err) {};
	},
	
	render300x250Banner : function(text) {		
		if(text.replace(/^\s+|\s+$/g,"") != "" && document.getElementById('adIndex'+ContentSummary.bannerPosition) != null && document.getElementById('banner300x250')!=null) {
			document.getElementById('adIndex'+ContentSummary.bannerPosition).innerHTML = text;
		} else {
			if(document.getElementById('adIndex'+ContentSummary.bannerPosition)!=null){
				document.getElementById('adIndex'+ContentSummary.bannerPosition).style.height="0";
			}
		}
	}
	
};

/** tab content update */
function doOnResult(text) {	
	if(text != "" && document.getElementById('rightbarcontentbox') != null) {
		// for en and zh use GAM for the rest the old solution		
		if(ContentSummary.language=='en' || ContentSummary.language=='zh'){			
			// clear and hide all divs on the rightbar
			for(i=1; i<9; i++){
				$("#entry"+i).html("");
				$("#entry"+i).hide();
			}
			// hide the 2 banners
			$('#banner300x250_top').hide();
			$('#banner300x250_bottom').hide();
			
			// get the results from the response and populate the right bar divs
			var obj = $('<div>' + text + '</div>');
			obj.find('div[id^="entry"]').each(function(i){
				$("#entry"+(i+1)).html($(this).html());
				$("#entry"+(i+1)).show();
			    //console.log(i, " = ", $(this).html());
				if(i==1){
					if($('#adIndex2').html().toLowerCase().indexOf("<div id=")!=-1){
						$('#banner300x250_top').show();
					}				
				}
				if(i==5){
					if($('#adIndex6').html().toLowerCase().indexOf("<div id=")!=-1){
						$('#banner300x250_bottom').show();
					}				
				}
			});
			// replace the form and pagination contents with what we have in the response
			$('#tabstateformelements').html($('#tabstateformelements', obj).html());
			$('#paginationRightbar').html($('#paginationRightbar', obj).html());
		}else{		
			$('#rightbarcontentbox').html(text);
			// update ContentSummary.forceBannerDisplay according to the result of the action.
			if(text.indexOf("banner300x250")!=-1){
				ContentSummary.forceBannerDisplay=true;
			}else{
				ContentSummary.forceBannerDisplay=false;
			}
			ContentSummary.determine300x250BannerPosition();
		}
		ContentSummary.setSelectedTab();
	}
	DWRErrorHandlerUtil.resetErrorHandler();
}
/** banner update */
function renderBanner(text) {
	if(text != "" && document.getElementById('leftskyscraper') != null) {
		document.getElementById('leftskyscraper').innerHTML = text;
	}
}
/** textlinks update */
function renderTextLinks(text) {
	if(text != "" && document.getElementById('hometextlinks') != null) {
		document.getElementById('hometextlinks').innerHTML = text;
	}
}
/** leftbar textlinks update */
function renderLeftbarTextLinks(text) {
	if(text != "" && document.getElementById('textlinks') != null) {
		document.getElementById('textlinks').innerHTML = text;
	}
}
function showPresentations() {
	document.getElementById('rightbarcontentbox').innerHTML =
		'<form id="tabstateform" action="">\r\n' +
		'\t<input id="startIndex" name="startIndex" value="0" type="hidden">\r\n' +
		'\t<input id="selectedTab" name="selectedTab" value="PRESENTATION" type="hidden">\r\n' +
		'\t<input id="rightBarLanguage" name="language" value="' + JSResource.LANG + '" type="hidden">\r\n' +
		'</form>\r\n' +
		'<div class="entry"><h1 class="ex-presentations">'+JSResource.contentSummary_showPresentations_1+'</h1>' +
		'<p>'+JSResource.contentSummary_showPresentations_2+'</p>' +
		'<p>'+JSResource.contentSummary_showPresentations_3+'</p>' +
		'</div>';
	ContentSummary.setSelectedTab(JSResource.LANG);
}
function showInterviews() {
	document.getElementById('rightbarcontentbox').innerHTML = 
		'<form id="tabstateform" action="">\r\n' +
		'\t<input id="startIndex" name="startIndex" value="0" type="hidden">\r\n' +
		'\t<input id="selectedTab" name="selectedTab" value="INTERVIEW" type="hidden">\r\n' +
		'\t<input id="rightBarLanguage" name="language" value="' + JSResource.LANG + '" type="hidden">\r\n' +
		'</form>\r\n' +
		'<div class="entry"><h1 class="ex-interviews">'+JSResource.contentSummary_showInterviews_1+'</h1>' +
		'<p>'+JSResource.contentSummary_showInterviews_2+'</p>' +
		'<p>'+JSResource.contentSummary_showInterviews_3+'</p>' +
		'</div>';
	ContentSummary.setSelectedTab(JSResource.LANG);	
}
function showMinibooks() {
	document.getElementById('rightbarcontentbox').innerHTML = 
		'<form id="tabstateform" action="">\r\n' +
		'\t<input id="startIndex" name="startIndex" value="0" type="hidden">\r\n' +
		'\t<input id="selectedTab" name="selectedTab" value="MINIBOOK" type="hidden">\r\n' +
		'\t<input id="rightBarLanguage" name="language" value="' + JSResource.LANG + '" type="hidden">\r\n' +
		'</form>\r\n' +
		'<div class="entry"><h1 class="ex-interviews">'+JSResource.contentSummary_showMinibooks_1+'</h1>' +
		'<p>'+JSResource.contentSummary_showMinibooks_2+'</p>' +
		'<ul>' +
		'<li>Scrum Checklists</li>' +
		'<li>Domain Driven Design Quickly</li>' +
		'<li>Visual Studio .NET Tips and Tricks</li>' +
		'</ul>' +
		'</div>';
	ContentSummary.setSelectedTab(JSResource.LANG);	
}
