
jQuery.noConflict();
var j$ = jQuery;


var DEF_INTERVAL = 1000;
var GETMAP_CGI = "../cgi/getmap.cgi?map=";// + "map=" + mapValue + "&kind=1&cam=1&t=" + nw ;

var toggle = 0;
var areaname;
var mapList = new Array();

// image typpe
var imgType = '';
  

j$(document).ready(function() {

if (jQuery.browser.msie) {
  j$("#map_scroll").css("position", "relative");
}
			      

	setInterval(function() {
		if (toggle) {
		    j$("#png_img_o").hide();
		    toggle = 0;
		} else {
		    j$("#png_img_o").show();
		    toggle = 1;
		}

		blink();

	    }, DEF_INTERVAL);
    });



/********************************************************************************/
/*		起動																																			*/
/********************************************************************************/
function traffic_init( an ) {

	areaname = an ;
	
	/* 地図内でマウスをクリックした時のイベント */
/*
	var divs = document.getElementsByTagName("div");
	for (i=0;i<divs.length;i++) {
		if (divs[i].id == 'map_img') {
			divs[i].onmousedown = mouse_position_detect;
			break;
		}
	}
*/

	//↓を組み込むとOperaで映像が出なくなる
	//if (window.opera) { // Operaの全画面Event拾えない対策
	//	op_dmydoc ='<div id="dmy"'
	//	          +'     style="position : absolute ;'
	//	          +'            z-index  : 0        ;'
	//	          +'            left:100%; top:100%">'
	//	          +'</div>'
	//	document.write(op_dmydoc)
	//}

	/* 点滅表示 */
	//showPic();
}

/********************************************************************************/
/*		点滅																																			*/
/********************************************************************************/
function showPic() {

	// document.images['png_img'].src = document.images['png_img_' + counter ].src;
	document.images['png_img'].src = mapList[ counter ].src;

	counter++;

	if (counter > 1) {
		counter = 0;
	}

	blink() ;

	setTimeout('showPic()', interval);
}

var VorH = "hidden" ;
if(document.getElementById('BlinkArea')) {
    var objid=document.getElementById('BlinkArea');
}

function blink() {

	if ( VorH == "visible" ) {
		if(document.getElementById('BlinkArea')) {
			document.getElementById('BlinkArea').style.visibility='hidden';
		}
		VorH = "hidden" ;
	} else {
		if(document.getElementById('BlinkArea')) {
			document.getElementById('BlinkArea').style.visibility='visible';
		}
		VorH = "visible" ;
	}
}

/********************************************************************************/
/*		マウスクリック																														*/
/********************************************************************************/
function mouse_position_detect(e) {

	var Xpos_Map;
	var Ypos_Map;
	var Xpos_Wrap;
	var Ypos_Wrap;

	var paramList;

	/* 座標軸取得(渋滞情報) */
	if (document.all) { // IE
		Xpos_Map = event.offsetX + 2;
		Ypos_Map = event.offsetY + 2;
	}
	else {
		Xpos_Map = getMouseX(e) - this.offsetLeft - this.offsetParent.offsetLeft;
		Ypos_Map = getMouseY(e) - this.offsetTop  - this.offsetParent.offsetTop;
	}

	/* 座標軸取得(ふきだし表示) */
	Xpos_Wrap = getMouseXClient(e);
	Ypos_Wrap = getMouseYClient(e);

	var url = "../ic/get_ic_name.cgi";
	paramList = "area=" + areaname +"&x="+Xpos_Map + "&y=" + Ypos_Map;

	/* サーバーとAjax通信 */
	new Ajax.Request(url,
		{
			method: 'get',        // get通信
			onSuccess: getData,   // データ取得完了
			onFailure: getNone,   // データ取得エラー
			parameters: paramList // パラメーター
		}
	);

	/****************************************/
	/*		データ取得												*/
	/****************************************/
	function getData(data) {
		var traffic_info = data.responseXML.getElementsByTagName("traffic_info");
		if (traffic_info[0].childNodes.length != 0) {
			var information_code = data.responseXML.getElementsByTagName("traffic_information_code");
			var informationCodeTmp = information_code[0].firstChild.nodeValue;
			var information = data.responseXML.getElementsByTagName("traffic_information");
			var informationTmp = information[0].firstChild.nodeValue;
			var route_name = data.responseXML.getElementsByTagName("route_name");
			var routeNameTmp = route_name[0].firstChild.nodeValue;
			var area_name = data.responseXML.getElementsByTagName("area_name");
			var areaNameTmp = area_name[0].firstChild.nodeValue;
			var contents = data.responseXML.getElementsByTagName("contents");
			var contentsTmp = contents[0].firstChild.nodeValue;
			showToolTip(Xpos_Wrap, Ypos_Wrap, informationCodeTmp, informationTmp, routeNameTmp, areaNameTmp, contentsTmp);
		}
		else {
		}
	}

	/****************************************/
	/*		受信取得エラー										*/
	/****************************************/
	function getNone() {
	}
}

/********************************************************************************/
/*		マウスX軸																																	*/
/********************************************************************************/
function getMouseX(e) {
	if (window.opera) { // Opera
		return(e.clientX);
	}
	else if (document.all) { // IE(ここには来ないけど一応ゥォ
		return(document.body.scrollLeft+event.clientX);
	}
	else if (document.layers||document.getElementById) { // Firefox、Safari、etc...
		return(e.pageX);
	}
}

/********************************************************************************/
/*		マウスY軸																																	*/
/********************************************************************************/
function getMouseY(e) {
	if (window.opera) { // Opera
		return(e.clientY);
	}
	else if (document.all) { // IE(ここには来ないけど一応ゥォ
		return(document.body.scrollTop+event.clientY);
	}
	else if (document.layers||document.getElementById) { // Firefox、Safari、etc...
		return(e.pageY);
	}
}

/********************************************************************************/
/*		マウスX軸(クライアント)																										*/
/********************************************************************************/
function getMouseXClient(e) {
	if (window.opera) { // Opera
		return(e.clientX);
	}
	else if (document.all) { // IE
		return(document.body.scrollLeft+event.clientX);
	}
	else if (document.layers||document.getElementById) { // Firefox、Safari、etc...
		return(e.clientX);
	}
}

/********************************************************************************/
/*		マウスY軸(クライアント)																										*/
/********************************************************************************/
function getMouseYClient(e) {
	if (window.opera) { // Opera
		return(e.clientY);
	}
	else if (document.all) { // IE
		return(document.body.scrollTop+event.clientY);
	}
	else if (document.layers||document.getElementById) { // Firefox、Safari、etc...
		return(e.clientY);
	}
}

/********************************************************************************/
/*		ツールチップ																															*/
/********************************************************************************/
function showToolTip(posX, posY, informationCode, information, routeName, areaName, contents) {

	jPrompt_traffic(posX, posY, informationCode, information, routeName, areaName, contents, function(nop)
		{
		}
	);
}

function closeToolTip() {
	jClose();
}


function loadFunc( mapValue ) {
	var dt = new Date() ;
	var nw =
		"" + dt.getYear() + formatZero( dt.getMonth() ) + formatZero( dt.getDate() ) +
		formatZero( dt.getHours() ) + formatZero( dt.getMinutes() ) + formatZero( dt.getSeconds() ) ;

	document.images['png_img'].src   = "../cgi/getmap.cgi" + "?" + "map=" + mapValue + "&kind=1&" + nw ;
	mapList[0].src = "../cgi/getmap.cgi" + "?" + "map=" + mapValue + "&kind=1&" + nw ;
	mapList[1].src = "../cgi/getmap.cgi" + "?" + "map=" + mapValue + "&kind=2&" + nw ;

	// document.images['png_img_1'].src = "../cgi/getmap.cgi" + "?" + "map=" + mapValue + "&kind=1&" + nw ;
	// document.images['png_img_2'].src = "../cgi/getmap.cgi" + "?" + "map=" + mapValue + "&kind=2&" + nw ;
}

function ld( mapName ) {
    areaname = mapName;

    j$(function() {
	    var t = new Date().getTime();

	    mapList[0] = new Image();
	    mapList[0].src = GETMAP_CGI + areaname + "&kind=2&t=" + t;
  
	    mapList[1] = new Image();
	    mapList[1].src = GETMAP_CGI + areaname + "&kind=1&t=" + t;

	    // initial 
	    j$("#png_img").attr('src', mapList[0].src);
	    j$("#png_img_o").attr('src', mapList[1].src);
	});


    //traffic_init( mapName );
    //loadFunc( mapName );
}
function formatZero( num ) {
	if ( num.length == 2 ) {
		return num ;
	}
	return "0" + num ;
}


function switchMap(iType ) {
    if (iType == imgType) {
	return false;
    }

    if (iType == undefined) {
        iType = '';
    }

    var t = new Date().getTime();

    mapList[0].src = GETMAP_CGI + areaname + "&kind=2&type=" + iType + "&t=" + t;
    mapList[1].src = GETMAP_CGI + areaname + "&kind=1&type=" + iType + "&t=" + t;
    
    j$("#png_img").attr('src', mapList[0].src);
    j$("#png_img_o").attr('src', mapList[1].src);

    imgType = iType;

    // redraw each button image of status
    swapImage('traffic');
    swapImage('lv');
    swapImage('sapa');

    return false;
}


function swapImage(iType, ev) {
    var suffix = undefined;

    if (iType == 'traffic') {
	iType = '';
    }

    if (ev == 'over') {
	if (imgType == iType) {
	    // this is active type
	    return;
	} else {
	    suffix = '_over.png';
	}
    } else {
	suffix = imgType == iType ? '_on.png' : '_off.png';
    }

    iType = iType ? iType : 'traffic';

    var btnId = "#mcntl_" + iType;
    j$(btnId + "> a > img").attr('src', "../img/btns/camera/" + iType + suffix);
}



