//////////////////////////////////////////////
// Program name : navicast.js
// $Rev$
// $Date$
//////////////////////////////////////////////

//////////////////////////////////////////////
// This function is called by Users.
// Execute Navicast
function execNaviPlus(userId) {
	navicast_follow_flg = 0;
	execNavicast_main( userId, 0, 0 );
	new Draggable( 'NaviPlus' );
	window.onscroll = navicast_scroll_follow;
}
// From Stac URL Execute Navicast
function execNaviPlus_stac(userId) {
	navicast_follow_flg = 0;
	execNavicast_main( userId, 1, 0 );
}

// for debug
function execNaviPlusDbg(userId, dbgMode) {
	navicast_follow_flg = 0;
	execNavicast_main( userId, 0, dbgMode );
}

//////////////////////////////////////////////
// Main
function execNavicast_main(userId, stacFlg, dbgMode) {
	var from_url = escape( document.referrer );
	var buf =  document.location + "";
	var aryAccessUrl = buf.split("?");
	buf = null;
	userId = parseInt(userId, 10);

	// get now scroll position
	navicast_get_scroll_position();

	// get nowtime
	var nowtime = new Date();
	var nowtimestr = getDatetimeString( nowtime );

	// get Cookie
	myCookie = "@NaviPlusInfo-" + userId + "-" + escape(aryAccessUrl[0]) + "=";
	aryAccessUrl = null;

	acCnt = null;
	myStr = document.cookie + ";";
	myOfst = myStr.indexOf( myCookie );
	if( myOfst != -1 ) {
		// There is Cookie
		myStart = myOfst + myCookie.length;
		myEnd   = myStr.indexOf(";" , myStart);
		acCnt = unescape(myStr.substring(myStart,myEnd));
		if( isNaN(escape(acCnt)) )	return;	// Illegal access
		acCnt = eval(acCnt) + 1;
	} else {
		// There is no cookie
		acCnt = 1;
	}
	myStr = null;
	acCnt = parseInt( acCnt, 10 );

	// Write Cookie
	writeCookie( nowtime, myCookie, acCnt, 90 );

	// Get browser infomations.
	var browser_w;
	var browser_h;
	if( stacFlg==0 ) {
		if( document.all ) {
			var ua = navigator.userAgent;
			var nHit = ua.indexOf("MSIE");
			var bIE = (nHit >=  0);
			var bVer6 = (bIE && ua.substr(nHit+5, 1) == "6");
			var bVer7 = (bIE && ua.substr(nHit+5, 1) == "7");
			var bVer8 = (bIE && ua.substr(nHit+5, 1) == "8");
			if( (bVer6||bVer7||bVer8) && navicast_get_compatmode() ) {
				browser_w = document.documentElement.clientWidth;
				browser_h = document.documentElement.clientHeight;
			} else {
				browser_w = document.body.clientWidth;
				browser_h = document.body.clientHeight;
			}
		} else {
			browser_w = window.innerWidth;
			browser_h = window.innerHeight;
		}
	} else {
		browser_w = 0;
		browser_h = 0;
	}

	// Get DB information for making uri
	var uri = getNavicastDomain() + "/access/getAccessInfo.php"
		+ "?uid=" + escape(userId)
		+ "&frm=" + escape(from_url)
		+ "&uri=" + escape(document.location)
		+ "&br_w=" + escape(browser_w)
		+ "&br_h=" + escape(browser_h)
		+ "&acc=" + acCnt
		+ "&scx=" + navicast_scr_x
		+ "&scy=" + navicast_scr_y
		+ "&stc=" + stacFlg
		+ "&dt=" + nowtimestr;
	if( dbgMode>0 )
		uri += "&dbg=" + dbgMode;

	from_url = null;
	browser_w = null;
	browser_h = null;

	// MacOS
	if( getOSName()=="Mac" && stacFlg==0 )	return;
	// Safari
	if( getBrowserName()=="Safari" && stacFlg==0 )	return;

	var navicast = document.getElementById("NaviPlus");
	var out = document.createElement("script");
	out.src = uri;
	uri = null;
	navicast.appendChild( out );

	return;
}

//////////////////////////////////////////////
// Write cookie
function writeCookie( nowtime, myCookie, myValue, myDay ) {
	myExp = nowtime;
	myExp.setTime(myExp.getTime()+(myDay*24*60*60*1000));
	myItem = myCookie + escape(myValue) + ";";
	myExpires = "expires="+myExp.toGMTString();
	document.cookie =  myItem + myExpires;

	myExp = null;
	myItem = null;
	myExpires = null;
}

//////////////////////////////////////////////
// Clicked FLASH(normal link)
function clickFlashLink( userId, manuscriptId, toUrl ) {
	clickFlashLinkAppoint( userId, manuscriptId, "0", toUrl, "0" );
}

//////////////////////////////////////////////
// Clicked FLASH(appoint frame or mailto)
function clickFlashLinkAppoint( userId, manuscriptId, toUrlType, toUrl, toUrlFrame ) {
	var uri = getNavicastDomain() + "/access/setClickData.php"
		+ "?uid=" + escape(userId)
		+ "&mid=" + escape(manuscriptId)
		+ "&tourl=" + escape(toUrl);

	var navicast = document.getElementById("NaviPlus");
	navicast.removeChild( navicast.lastChild );
	var out = document.createElement("script");
	out.src = uri;
	uri = null;
	navicast.appendChild( out );

	if( toUrlType=="1" )
		toUrl = "mailto:" + toUrl;
		
	if( getBrowserName()=="MSIE" ) {
		var in_html = "";
		if( toUrlFrame=="0" ) {
			// target=_self
			in_html = "<a href='" + toUrl + "' target='_self' id='NaviPluslink' style='display:none;'></a>";
		} else {
			// target=_blank
			in_html = "<a href='" + toUrl + "' target='_blank' id='NaviPluslink' style='display:none;'></a>";
		}
		navicast.innerHTML = in_html;
		document.getElementById('NaviPluslink').click();
	} else {
		var ua = navigator.userAgent;
		var nHit = ua.indexOf("Firefox");
		var bFF = (nHit >=  0);
		var bVer20 = (bFF && ((ua.substr(nHit+8, 2) == "2.") || (ua.substr(nHit+8, 2) == "3."))); //NDV-374
		var d1 = new Date().getTime();
		var d2 = new Date().getTime();
		if( bVer20 ) {	// FireFox2.x
			while( d2<d1+500 ) {	//0.3sec
				d2 = new Date().getTime();
			}
		} else {		// FireFox1.x
			while( out.src.length<=0 && d2<d1+500 ) {
				d2 = new Date().getTime();
			}
		}
		if( toUrlFrame=="0" ) {	// target=_self
			location.href = toUrl;
		} else {				// target=_blank
			window.open( toUrl );
		}
	}
}

//////////////////////////////////////////////
// Close FLASH
function closeWindow( objId ) {
	new Effect.Fade( objId );
	// if not IE, go outer frame
	if( getBrowserName()!="MSIE" ) {
		new Effect.MoveBy(objId,-10000,-10000,
			{
				delay: 0,
				fps:10000,
				transition: Effect.Transitions.full
			}
		);
	}
}

//////////////////////////////////////////////
// XmlHttp request
function createXmlHttp(){
	if (window.XMLHttpRequest) {			// Mozilla, Firefox, Safari, IE7
		return new XMLHttpRequest();
	} else if (window.ActiveXObject) {		// IE5, IE6
		try {
			return new ActiveXObject("Msxml2.XMLHTTP");    // MSXML3
		} catch(e) {
			return new ActiveXObject("Microsoft.XMLHTTP"); // to MSXML2
		}
	} else {
		return null;
	}
}

//////////////////////////////////////////////
// get OS name
function getOSName() {
	var BrowserName = navigator.userAgent;

	if( BrowserName.indexOf('Mac')>=0 ) {
		return "Mac";
	} else if( BrowserName.indexOf('Windows')>=0 ) {
		return "Win";
	}
}

//////////////////////////////////////////////
// get browser
function getBrowserName() {
	var BrowserName = navigator.userAgent;
	if (BrowserName.indexOf("Safari") > -1) {
		return "Safari";
	} else if (BrowserName.indexOf("Opera") > -1) {
		return "Opera";
	} else if (BrowserName.indexOf("Firefox") > -1) {
		return "Firefox";
	} else if (BrowserName.indexOf("Gecko") > -1) {
		return "Netscape / Mozilla";
	} else if (BrowserName.indexOf("MSIE") > -1) {
		return "MSIE";
	} else if (BrowserName.indexOf("Mozilla") > -1) {
		return "Netscape";
	} else {
		return "unknown";
	}
}

////////////////////////////////////////////////////////////
// if browser was scrolled, banner is followed scrolls
function navicast_scroll_follow() {
	if( navicast_follow_flg!=2 )	return;	// It is not follow status

	// get position before scroll
	navicast_old_scr_x = navicast_scr_x;
	navicast_old_scr_y = navicast_scr_y;

	// get now scroll position
	navicast_get_scroll_position();

	// get scroll volumes
	navicast_scr_x_val = navicast_scr_x - navicast_old_scr_x;
	navicast_scr_y_val = navicast_scr_y - navicast_old_scr_y;
	
	new Effect.MoveBy("NaviPlus", navicast_scr_y_val, navicast_scr_x_val,
		{
			delay: -1,
			fps:10000,
			transition: Effect.Transitions.full
		}
	);
}

////////////////////////////////////////////////////////////
// get now scroll position
function navicast_get_scroll_position() {
	// get now scroll position
	if( document.all ) {
		// for IE
		if( navicast_get_compatmode() ) {
			navicast_scr_x = document.documentElement.scrollLeft;
			navicast_scr_y = document.documentElement.scrollTop;
		} else  {
			navicast_scr_x = document.body.scrollLeft;
			navicast_scr_y = document.body.scrollTop;
		}
	} else {
		// for FF1.5-2.0
		if( navicast_get_compatmode() ) {
			navicast_scr_x = document.documentElement.scrollLeft;
			navicast_scr_y = document.documentElement.scrollTop;
		} else {
			navicast_scr_x = document.body.scrollLeft;
			navicast_scr_y = document.body.scrollTop;
		}		
	}
}

////////////////////////////////////////////////////////////
// if browser is IE, get compat mode
function navicast_get_compatmode() {
	var bStd = (document.compatMode && document.compatMode=="CSS1Compat");
	return bStd;
}

////////////////////////////////////////////////////////////
// get next flash banner, and then redraw
function redrawBanner(userId,nextManuscriptPriority,stacFlg) {
	// check display count
	if( navicast_display_count>=navicast_max_display_count )	return;

	// get now scroll position
	navicast_get_scroll_position();

	// Get browser infomations.
	var browser_w;
	var browser_h;

	if( stacFlg==0 ) {
		if( document.all ) {
			var ua = navigator.userAgent;
			var nHit = ua.indexOf("MSIE");
			var bIE = (nHit >=  0);
			var bVer6 = (bIE && ua.substr(nHit+5, 1) == "6");
			var bVer7 = (bIE && ua.substr(nHit+5, 1) == "7");
			var bVer8 = (bIE && ua.substr(nHit+5, 1) == "8");
			if( (bVer6||bVer7||bVer8) && navicast_get_compatmode() ) {
				browser_w = document.documentElement.clientWidth;
				browser_h = document.documentElement.clientHeight;
			} else {
				browser_w = document.body.clientWidth;
				browser_h = document.body.clientHeight;
			}
		} else {
			browser_w = window.innerWidth;
			browser_h = window.innerHeight;
		}
	} else {
		browser_w = 0;
		browser_h = 0;
	}

	// Get DB information for making uri
	var uri = getNavicastDomain() + "/access/getNextFlash.php"
		+ "?uid=" + userId
		+ "&nmp=" + nextManuscriptPriority
		+ "&frm=" + escape(document.referrer)
		+ "&uri=" + escape(document.location)
		+ "&br_w=" + escape(browser_w)
		+ "&br_h=" + escape(browser_h)
		+ "&scx=" + navicast_scr_x
		+ "&scy=" + navicast_scr_y
		+ "&ndc=" + navicast_display_count
		+ "&stc=" + stacFlg;
	var navicast_banner = document.getElementById("NaviPlus");
//	navicast_banner.removeChild(navicast_banner.lastChild);
	if( document.all ) {
		navicast_banner.innerHTML = "";
	} else {
		navicast_banner.textContent = "";
	}
	var out = document.createElement("script");
	out.src = uri;
	navicast_banner.appendChild( out );

	return;
}


////////////////////////////////////////////////////////////
// get Navicast server domain name
function getNavicastDomain()
{
	for (i = 0;i < document.getElementsByTagName('script').length;i++)
	{
		str = document.getElementsByTagName('script')[i].getAttribute('src');
		if ((str != null) && (str.search('NaviPlus.js') >= 0))
		{
			prtcl = 'http';
			if (str.substr(0,6) == 'https:')
				prtcl = 'https';
			str = str.substr(str.search('//') + 2);
			str = str.substr(0,str.search('/'));
			return prtcl + '://' + str;
		}
	}
	return null;
}

////////////////////////////////////////////////////////////
// get today string
function getDatetimeString( date )
{
	var yyyy = date.getFullYear();
	var mm   = date.getMonth() + 1;
	var dd   = date.getDate();
	var hh   = date.getHours();
	var mi   = date.getMinutes();
	var ss   = date.getSeconds();

	ret  = "" + yyyy;
	ret += Math.floor(mm/10)%10;
	ret += mm%10;
	ret += Math.floor(dd/10)%10;
	ret += dd%10;
	ret += Math.floor(hh/10)%10;
	ret += hh%10;
	ret += Math.floor(mi/10)%10;
	ret += mi%10;
	ret += Math.floor(ss/10)%10;
	ret += ss%10;

	return ret;
}
// End of file
