/*
 * 2007/10/22 新規作成 竹原
 * ページ内容を変更する
 */
var firstLoad = true;
var currentPage = null;
var flash_menu = null;
var flashtags = null;
var shadowConfig = {};
var isHTTPS = false;
var requireHTTPS = new Array();

function getFlashMovie(movieName) {
	var isIE = navigator.appName.indexOf("Microsoft") != -1;
	return (isIE) ? window[movieName] : document[movieName];
}
 
onloadGetPage = function() {
	isHTTPS = location.href.toString().match(/^https:\/\//);
	requireHTTPS['contact'] = true;
	requireHTTPS['contact_valid'] = true;
	requireHTTPS['contact_process'] = true;
	requireHTTPS['contact_back'] = true;
	
	document.title = '博報堂DYメディアパートナーズ メディア環境研究所';
	Shadower.shadowWithClass('shadowed');
	flash_menu = getFlashMovie('bubbles_shrink');
	flashtags = document.getElementById('flashtags');
	flashtags.style.visibility = 'hidden';
	takeMeasures();
	
	// initialize RSH
	dhtmlHistory.initialize();
  
	// determine our current location so we can initialize ourselves at startup
	var initialLocation = dhtmlHistory.getCurrentLocation();
	
	// if no location specified, use the default
	if (!initialLocation) {
		initialLocation = "home";
	}
	
	// now initialize our starting UI
	pageSpecificProcess(initialLocation);
	updateGUI(initialLocation);
	
	// add ourselves as a listener for history change events
	dhtmlHistory.addListener(handleHistoryChange);
}

function handleHistoryChange(newLocation, historyData) {
	// use the history data to update our UI
	pageSpecificProcess(newLocation);
	updateGUI(newLocation);                           
}

//ページ内容URLの取得
function updateGUI(pageCode, settings) {
	document.title = '博報堂DYメディアパートナーズ メディア環境研究所';
	now = new Date();
	codeSplit = pageCode.split("@");
	if (codeSplit[0] == 'index' || codeSplit[0] == "") {
		codeSplit[0] = 'home';
		pageCode = 'home';
	}
	if ((!isHTTPS && requireHTTPS[pageCode]) || (isHTTPS && !requireHTTPS[pageCode])) {
		//protocol switch => replace location
		location.replace("http"+(requireHTTPS[pageCode]?"s":"")+"://"+location.host+"/#"+pageCode);
		return;
	}
		
	if (currentPage == pageCode && (!settings || !settings.forceLoad)) {
		//ignore
		return;
	}
	currentPage = pageCode;
	
	var page = "./site.php?p=" + codeSplit[0]+(codeSplit.length == 2?'&id='+codeSplit[1]:'')+"&rand="+now.getTime();

	urchinURL = "/"+codeSplit[0]+(codeSplit.length == 2?'_'+codeSplit[1]:'')+".html";
	urchinTracker(urchinURL);
	new Ajax.Request(
	page ,
	{
		method: (settings && settings.postData)?'post':'get' ,
		parameters: (settings && settings.postData)?settings.postData:null, 
		onSuccess: function(data){
			// 内容の変更
			setInnerHTML($('content'), data.responseText);
			//$('content').innerHTML = data.responseText;
			//alert('Response data size '+data.responseText.length + " innerHTML size" + $('content').innerHTML.length);
			found = true;
			idx = 0;
			while(found) {
				idx = data.responseText.indexOf("<script", idx);
				found = (idx > -1);
				if (found) {
					idx = data.responseText.indexOf(">", idx);
					script = data.responseText.substring(idx+1, data.responseText.indexOf("</script>", idx));
					eval(script);
				}
			}			
			firstLoad = false;
			scroll(0,0);
		}
	}
	);
}

function setInnerHTML( element, html, count ) {
	element.innerHTML = html;
	if( ! count ) {
		count = 1;
	}
	if( html != '' && element.innerHTML == '' && count < 10 ) {
		++count;
		setTimeout( function() {
			setInnerHTML( element, html, count );
		}, 50 );
	}
} 

function pageSpecificProcess(newPage) {
	if (firstLoad && !isWhitePage(newPage)) {
		flash_menu.themeload();
	}
	if(!isWhitePage(newPage)) {
		// back to blue state
		if (!firstLoad) {
			flash_menu.leaveinsight();
		}
	} else {
		// white state
		if (!hasTheme(newPage)) {
			flash_menu.sendTextToFlash();
		} 
	}
}

function hasTheme(pageCode) {
	return pageCode.indexOf("newsarticle") == -1  && (pageCode.indexOf("article@") > -1 || pageCode.indexOf("theme@") > -1);
}

function isWhitePage(pageCode) {
	return hasTheme(pageCode) || pageCode.indexOf("insight") > -1;
}

function pageLoad(url){
	//alert(url);
	pageCode = url.substr(url.lastIndexOf("#")+1);
	//alert("added to DHTML history : "+pageCode);
	updateGUI(pageCode)
	dhtmlHistory.add(pageCode, null);
}

function sendContactForm (mode) {
	var postData = {
		'subject' : $F('subject'),
		'msg' : $F('msg'),
		'companyName' : $F('companyName'),
		'name' : $F('name'),
		'mail' : $F('mail')
	};
	var settings = {
		'postData': postData,
	 	'forceLoad' : true
	 };
	updateGUI(mode, settings); 
}