// JavaScript Document


function setOutputNews(){
	if(httpObject.readyState == 4){
		temp_str = httpObject.responseText;
		var newsInfo = temp_str.split("~");
		document.getElementById('news_bar_headline').innerHTML = newsInfo[0];
		$('#news_bar_headline').fadeIn(300);
		document.getElementById('btn_news_bar_previous').href = "/ajax/news_bar.php?action="+newsInfo[1];
		document.getElementById('btn_news_bar_next').href = "/ajax/news_bar.php?action="+newsInfo[2];
		ajaxHomepage.overrideButtonsInThePage();
	}
} 

function setOutputSpotlight(){
	if(httpObject.readyState == 4){
		temp_str = httpObject.responseText;
		var spotlightInfo = temp_str.split("~");
		document.getElementById('bottom_box_center_content').innerHTML = spotlightInfo[0];
		$('#bottom_box_center_content').fadeIn(300);
		document.getElementById('btn_spotlight_box_previous').href = "/ajax/spotlight_box.php?action="+spotlightInfo[1];
		document.getElementById('btn_spotlight_box_next').href = "/ajax/spotlight_box.php?action="+spotlightInfo[2];
		ajaxHomepage.overrideButtonsInThePage();
	}
} 

function setOutputHomepage(){
	if(httpObject.readyState == 4){
		if(httpObject.responseText!="default"){
			var industryFormat1 = httpObject.responseText;
			var industryFormat2 = industryFormat1.replace("-", "/");
			var industryFormat3 = industryFormat2.replace("_", " ");
			document.getElementById('industry_display').innerHTML = "<strong>AirMagnet for "+industryFormat3+"</strong>&nbsp;&nbsp;<span style=\"font-size:11px;\">[ <a href=\"#\" id=\"selectIndustry\">change industry</a> ]</span>";

			//homepage ajax begin
			if(slocation == "/home/www/airmagweb/contents/index/default.php") {
				
				//set homepage flash
				httpObjectHomepageFlash = getHTTPObject();
				if (httpObjectHomepageFlash != null) {
					posturlHomepageFlash = "/ajax/content.php?industry="+httpObject.responseText+"&slocation="+slocation+"&jsection=homepage_flash";
					httpObjectHomepageFlash.open("GET", posturlHomepageFlash, true);
					httpObjectHomepageFlash.onreadystatechange = function () {
						if(httpObjectHomepageFlash.readyState == 4) {
							string1=document.getElementById('homepage_flash').innerHTML;
							string2=httpObjectHomepageFlash.responseText;
							v1position1=string1.indexOf('src="',document.getElementById('homepage_flash').innerHTML)+5;
							v1position2=string1.indexOf('.jpg"',document.getElementById('homepage_flash').innerHTML)+4;
							v2position1=string2.indexOf('src="',httpObjectHomepageFlash.responseText)+5;
							v2position2=string2.indexOf('.jpg"',httpObjectHomepageFlash.responseText)+4;
							v1=string1.substring(v1position1,v1position2);
							v2=string2.substring(v2position1,v2position2);
		
							if(v1 != v2){
								$('#homepage_flash').fadeOut(500);
								if(httpObject.responseText == "Federal_Government") location.reload(true);
								window.setTimeout(function(){
									document.getElementById('homepage_flash').innerHTML = httpObjectHomepageFlash.responseText;
									$('#homepage_flash').fadeIn(500);
								},1000);
							}
						}
					}
					httpObjectHomepageFlash.send(null);
				}
			//homepage ajax end
			} else {
			//all other pages refreshed
				window.location.reload(true);
			}
			
		} else {
			document.getElementById('industry_display').innerHTML = "[ <a href=\"#\" id=\"selectIndustry\">select industry</a> ]";
		}
	}
	$('#selectIndustryBox').slideUp(400);
	grayOut(false);
	ajaxHomepage.overrideButtonsInThePage();
}


	
function eNews(eURL){
	$('#news_bar_headline').fadeOut(300);
	httpObject = getHTTPObject();
	if (httpObject != null) {
		posturl = eURL+"&"+Date();
		httpObject.open("GET", posturl, true);
		httpObject.onreadystatechange = setOutputNews;
		httpObject.send(null);
	}
	
}

function eSpotlight(eURL){
	$('#bottom_box_center_content').fadeOut(300);
	//httpObject = getHTTPObject();
	//if (httpObject != null) {
	//	posturl = eURL+"&"+Date();
	//	httpObject.open("GET", posturl, true);
	//	httpObject.onreadystatechange = setOutputSpotlight;
	//	httpObject.send(null);
	//}
	
	eURLp1=eURL.indexOf('action=',eURL)+7;
	eURLp2=eURL.length;
	vAction=eURL.substring(eURLp1,eURLp2);
	
	arrLength = arrSpotlight.length-1;
	
	if(vAction == "next") vAction = 1;
	if(vAction == "previous") vAction = arrLength;
	
	vAction=vAction*1;
	
	if(vAction == arrLength) nextNew = 0;
	else nextNew = vAction + 1;

	if(vAction > 0) previousNew = vAction-1;
	else previousNew = arrLength;
	
	window.setTimeout(function(){
		document.getElementById('bottom_box_center_content').innerHTML = arrSpotlight[vAction];
		$('#bottom_box_center_content').fadeIn(300);
	},600);
	
	document.getElementById('btn_spotlight_box_previous').href = "/ajax/spotlight_box.php?action="+previousNew;
	document.getElementById('btn_spotlight_box_next').href = "/ajax/spotlight_box.php?action="+nextNew;
	ajaxHomepage.overrideButtonsInThePage();
}

function eSelectIndustry(){
	posY = getScreenCenterY()-100;
	posX = getScreenCenterX();
	
	grayOut(true);
	$('#selectIndustryBox').slideDown(400);
	document.getElementById("selectIndustryBox").style.top = posY+"px";
	document.getElementById("selectIndustryBox").style.left = posX+"px";
	document.getElementById("selectIndustryBox").style.zIndex = 100;
}

function setIndustry(selectedIndustry) {
	if(selectedIndustry!="") {
		httpObject = getHTTPObject();
		if (httpObject != null) {
			posturl = "/ajax/set_industry.php?industry="+selectedIndustry+"&"+Date()+"&a=1";
			httpObject.open("GET", posturl, true);
			httpObject.onreadystatechange = setOutputHomepage;
			httpObject.send(null);
			//alert("pass");
		}else{
			//alert("failed_1");
		}
		
		
	}
	return false;
}
	
	
var ajaxHomepage = {
	overrideButtonsInThePage : function(){
		$('.ajax_news_bar').unbind('click').click(function(){
			eNews($(this).attr('href'));
			return false;
		});
		
		$('.ajax_spotlight_box').unbind('click').click(function(){
			clearInterval(SpotlightInterval);
			eSpotlight($(this).attr('href'));
			return false;
		});
		
		$('#selectIndustry').unbind('click').click(function(){
			eSelectIndustry($(this).attr('href'));
			return false;
		});
		
		$('.industrySelect_link').unbind('click').click(function(){
			var myIndustry = ($(this).attr('href')).replace("/ajax/set_industry.php?industry=","");
			setIndustry(myIndustry);
			$('#selectIndustryBox').slideUp(400);
			grayOut(false);
			return false;
		});
		
		$('#idustry_prompt_cancel').unbind('click').click(function(){
			setIndustry("default");
			$('#selectIndustryBox').slideUp(400);
			grayOut(false);
			return false;
		});
		
		$('#industrySelect').unbind('submit').submit(function(){
			setIndustry(document.industrySelect.industry.value);
			return false;
		});
	}
}


/*$(document).ready(function(){
	ajaxHomepage.overrideButtonsInThePage();
});*/


