/* GoogleMap 用 */
/* <script src="/js/prototype/prototype.js"></script> */

	var MyFunc;
	var map;
	var gLng = 139.29413080215454;
	var gLat = 35.728886074410795;
	var gSiteid = 'akiruno';
	var gSC;
	var gLS; //localsearch obj

//	var gZoom = 14;
//	var gZoom = 10;
	var gZoom = 12;

var svrData = new Array();

    function CreateGmap(vJb) {
if (!vJb){
		map = new GMap2(document.getElementById("gmap"));
		map.setCenter(new GLatLng(gLat, gLng), eval(gZoom));
		ctrlObj = new GLargeMapControl();
		map.addControl(ctrlObj);
}

		if (vJb){
		    map.clearOverlays();
			CreateSearchControl();

			MyFunc = new MyFuncs();
			MyFunc.ReadAndParseXML(vJb);
		}
    }



    function CreateSearchControl() {
      // Create a search control
      	gSC = new GSearchControl();
      	// Add in a full set of searchers
      	gSC.addSearcher(new GwebSearch());
      	gSC.addSearcher(new GblogSearch());
    }

	CyGmap_OpenSearchWindow = function(arg_id){
		svrData[arg_id].OpenSearchWindow(svrData[arg_id]);
	}

	CyGmap_ShowMapBlowup = function(arg_id){
		svrData[arg_id].marker.showMapBlowup({ mapType: G_NORMAL_MAP, zoomLevel: 17 });
	}





/* イベント GoogleMap 用 */
/* <script src="/js/prototype/prototype.js"></script> */
/* <script src="/js/ObjTree.js"></script> */

function inherit(subClass, superClass) {
	var Temp = new Function();
	Temp.prototype = superClass.prototype;
	subClass.prototype = new Temp;
	subClass.prototype.constructor = subClass;
	subClass.prototype.superclass = superClass.prototype;
}

inherit(CyBaseIcon, GIcon);

function MyFuncs(){

	this.ReadAndParseXML = function(vJb){
	    if (!vJb){
	        return;
	    }

        if (vJb == 'Sname' && document.SearchForm.word.value == ''){
            alert('店名の一部を入力後、「検索」ボタンをクリックしてください。');
            return;
        }

		try {
			$("list").innerHTML = '<div class="messages">検索中です。</div>';
		} catch(e){
		}

        var param = vJb.split(":");
        
		var url = '/cgi/gmap.cgi?siteid=' + gSiteid + '&jb=' + param[0];
		if (param[1]){
		    url += '&' + param[1];
		}
		if (vJb == 'Sname'){
		    url += '&word=' + document.SearchForm.word.value;
		    if (document.SearchForm.sname_limit.value){
			    url += '&limit=' + document.SearchForm.sname_limit.value;
		    }
		}
//alert(url);
		new Ajax.Request(url, { method: 'get', onComplete: this.parseData});
	}



	this.parseData = function(httpObj){

		var xotree = new XML.ObjTree();
		xotree.force_array = [ "unit" ];
		var tree;

		try {
			tree = xotree.parseXML( httpObj.responseText ); 

		} catch(e){
			$("list").innerHTML = '検索できませんでした。';
		}
//		$("list").innerHTML = '<div class="target_label">▼&nbsp;' + tree.doc.target_label + '</div>';






		var count = tree.doc.units["-count"];


        $("list").innerHTML = '';
        if (tree.doc.jb == 'ListAct'){
            if (tree.doc.actid == 'EVT'){
                tree.doc.jb_label = 'イベント';
            } else if (tree.doc.actid == 'PST'){
                tree.doc.jb_label = 'プレゼント';
            } else if (tree.doc.actid == 'SAL'){
                tree.doc.jb_label = 'セール';
            } else if (tree.doc.actid == 'CPN'){
                tree.doc.jb_label = 'クーポン';
            } else if (tree.doc.actid == 'RCM'){
                tree.doc.jb_label = 'おすすめ';
            } else if (tree.doc.actid == 'MMY'){
                tree.doc.jb_label = '耳よりNEWS';
            } else if (tree.doc.actid == '_MTI'){
                tree.doc.jb_label = 'マル得情報';
            } else {
                tree.doc.jb_label = 'その他';
            }
            $("list").innerHTML = '<div class="result_label">' + tree.doc.jb_label + ' ：' + tree.doc.navi.total + '件</div>';

        } else if (tree.doc.jb == 'ListUnits'){
            tree.doc.jb_label = '掲載店一覧';
            $("list").innerHTML = '<div class="result_label">' + tree.doc.jb_label + ' ：' + tree.doc.navi.total + '件</div>';

        } else if (tree.doc.jb == 'ListJob'){
            tree.doc.jb_label = '求人情報';
            $("list").innerHTML = '<div class="result_label">' + tree.doc.jb_label + ' ：' + tree.doc.navi.total + '件</div>';

        } else if (tree.doc.jb == 'Sname'){
            tree.doc.jb_label = '名前検索';
//            $("list").innerHTML = '<div class="result_label"><div>' + tree.doc.jb_label + ' ：' + tree.doc.navi.total + '件</div>';
//            $("list").innerHTML += '⇒検索ワード：' + tree.doc.word;
            $("list").innerHTML = '<div class="result_label"><div>' + tree.doc.jb_label + ' ：' + tree.doc.navi.total + '件</div><div class="result_keyword">⇒検索ワード：' + tree.doc.word;
            $("list").innerHTML += '</div></div>';

        } else if (tree.doc.jb == 'ListCat' || tree.doc.jb == 'ListNewCat'){
            tree.doc.jb_label = 'カテゴリ検索';
            $("list").innerHTML = '<div class="result_label">' + tree.doc.jb_label + ' ：' + tree.doc.navi.total + '件<br>⇒カテゴリ：' + tree.doc.cat_label + '</div>';
//            $("list").innerHTML += '⇒カテゴリ：' + tree.doc.cat_label;
//            $("list").innerHTML += '</div>';
        }


        if (tree.doc.navi.lastpage > 1){
            $("list").innerHTML += '<div class="navi">' + NaviHtml(tree.doc) + '</div>';
        }



        if (count > 0){
        	var uniqid;
        	var w_city_label = '';
        	var w_group_label = '';
			for(var i=0; i<count; i++){
				var vTree;
				if (count > 1){
					vTree = tree.doc.units.unit[i];
				} else {
//2007/07/27 hojo
//					vTree = tree.doc.units.unit;
					vTree = tree.doc.units.unit[i];
				}
				uniqid = vTree["-uniqid"];

				svrData[uniqid] = new GmapFunc();
				svrData[uniqid].setData(tree.doc, vTree);
				svrData[uniqid].addMarker();

                var w_class = '0';
                if (vTree["-flg_appeal"] == 't'){
                    w_class = '1';
                }
                var list_html = '';
                if (vTree.group_label && vTree.group_label != w_group_label){
                    list_html += '<div class="group_label">[' + vTree.group_label + ']</div>';
                    w_group_label = vTree.group_label;
                    w_city_label = '';
                }
                if (vTree.city_label && vTree.city_label != w_city_label){
                    list_html += '<div class="city_label">&lt;' + vTree.city_label + '&gt;</div>';
                    w_city_label = vTree.city_label;
                }
                list_html += '<div class="list_unit list_unit' + w_class + '"><!--span class="point">■</span-->';
				list_html += '<a href="javascript:onClickListUnit(' + "'" + uniqid + "'" + ');">' + vTree.name + '</a>';
				list_html += '</div>';

                $("list").innerHTML += list_html;

			}
			if(count == 1){
				svrData[uniqid].OpenInfoWindow();
			}
			var lng = tree.doc.center["-longitude"];
			var lat = tree.doc.center["-latitude"];
			var zoom = tree.doc.zoom["-level"];

//			map.setCenter(new GLatLng(lat, lng), eval(gZoom));
			map.setCenter(new GLatLng(lat, lng), eval(zoom));
        } else {
	        $("list").innerHTML += '<div class="alerts">該当するお店は見つかりませんでした。</div>';
        }
	}
}

function NaviHtml(vDoc){
    var spage = vDoc.navi.page - 2;
    if (spage < 1){
        spage = 1;
    }
    var epage;
    if (spage == 1){
        epage = 5;
    } else {
        epage = parseInt(vDoc.navi.page) + 2;
    }
    if (epage > parseInt(vDoc.navi.lastpage)){
        epage = vDoc.navi.lastpage;
    }
    if (spage > vDoc.navi.lastpage - 4 ){
        spage = vDoc.navi.lastpage - 4;
        if (spage < 1){
            spage = 1;
        }
    }
    var param = '';
    if (vDoc.param){
        param = vDoc.param + '&';
    }

    var navi_html = '';
    var w_html = '<a href="javascript:onClickButton(\'' + vDoc.jb + ':' + param + 'limit=' + vDoc.navi.limit;
    if (vDoc.navi.page > 1){
        navi_html += w_html + '&page=' + (vDoc.navi.page-1) + '\');">[←]</a>';
    } else {
        navi_html += '[←]';
    }
    navi_html += '&nbsp;';
    
    if (spage > 1){
        navi_html += w_html + '&page=1\');">1</a>';
        navi_html += '・・・';
        navi_html += '&nbsp;';
    }

    for (var i=spage; i <= epage; i++){
        if (i == parseInt(vDoc.navi.page)){
            navi_html += '<font color="#ff0000">' + i + '</font>';
        } else {
            navi_html += w_html + '&page=' + i + '\');">' + i + '</a>';
        }
        navi_html += '&nbsp;';
    }

    if (epage < parseInt(vDoc.navi.lastpage)){
        navi_html += '・・・';
        navi_html += w_html + '&page=' + vDoc.navi.lastpage + '\');">' + vDoc.navi.lastpage + '</a>';
        navi_html += '&nbsp;';
    }

    if (parseInt(vDoc.navi.page) < parseInt(vDoc.navi.lastpage)){
        navi_html += w_html + '&page=' + (parseInt(vDoc.navi.page)+1) + '\');">[→]</a>';
    } else {
        navi_html += '[→]';
    }
    return navi_html;
}


function GmapFunc(){
	this.marker = null;


	this.setData = function(vDoc, vTree){
		this.uniqid = vTree["-uniqid"];
		this.areaid = vTree["-areaid"];
		this.unitid = vTree["-unitid"];
		this.flg_appeal = vTree["-flg_appeal"];
		this.flg_search = vTree["-flg_search"];
		this.group = vTree["-group"];
		this.lat = vTree["-lat"];
		this.lng = vTree["-lng"];
		this.name = vTree.name;
        this.tabs_label = '';

        if (vDoc.jb == 'ListAct'){
            this.tabs = this.setActData(vTree);
            this.tabs_label = vDoc.jb_label;
        } else if (vDoc.jb == 'ListJob'){
            this.tabs = this.setJobData(vTree);
            this.tabs_label = vDoc.jb_label;
        } else if (vDoc.jb == 'ListNew' || vDoc.jb == 'Sname' || vDoc.jb == 'ListUnits'){
            this.tabs = this.setUnitData(vTree);
        } else if (vDoc.jb == 'ListCat' || vDoc.jb == 'ListNewCat'){
            this.tabs = this.setCatData(vTree);
        }


//        if (vDoc.jb == 'ListAct'){
//            this.tabs = this.setActData(vTree);
//            if (vDoc.actid == 'EVT'){
//                this.tabs_label = 'イベント';
//            } else {
//                this.tabs_label = 'お得情報';
//            }
//        } else if (vDoc.jb == 'ListJob'){
//            this.tabs = this.setJobData(vTree);
//            this.tabs_label = '求人情報';
//        } else if (vDoc.jb == 'ListNew' || vDoc.jb == 'ListUnits'){
//            this.tabs = this.setUnitData(vTree);
//        } else if (vDoc.jb == 'ListLCat'){
//            this.tabs = this.setLCatData(vTree);
//        }




	}

    this.setActData = function(vTree){
        var count = vTree.act.length;
        if (!count){
             count = 1;
        }
        var htmlStrDiv = this.htmlUnitClass(vTree);
        var htmlUnitName = this.htmlUnitName(vTree);
        var htmlUnitData = this.htmlUnitData(vTree);
        var htmlEndDiv = '</div>';

        var tabs = new Array(count);
        for (var i=0; i<count; i++){
        
            tabs[i] = htmlStrDiv;
            tabs[i] += htmlUnitName;

            var act;
            if (count == 1){
                act = vTree.act;
            } else {
                act = vTree.act[i];
            }

            tabs[i] += '<table class="content_table"><tr><td>';
            tabs[i] += '<div class="title">' + act.title + '</div>';
            var content_class = '';
            if (act.content.length > 100){
                content_class = 'scroll_content';
            }
            tabs[i] += '<div class="' + content_class + '">';
            if (act.act_image){
                if (vTree.unit_url){
                    tabs[i] += '<a href="' + vTree.unit_url + '" target="_blank"><img src="'+ act.act_image + '"></a>';
                } else {
                    tabs[i] += '<img src="' + act.act_image + '">';
                }
            }
            tabs[i] += act.content;
            tabs[i] += '<br clear="both"></div></td></tr></table>';

            tabs[i] += htmlUnitData;

            tabs[i] += htmlEndDiv;
        }

        return tabs;
    }

    this.setJobData = function(vTree){
        var count = vTree.job.length;
        if (!count){
             count = 1;
        }
        var htmlStrDiv = this.htmlUnitClass(vTree);
        var htmlUnitName = this.htmlUnitName(vTree);
        var htmlUnitData = this.htmlUnitData(vTree);
        var htmlEndDiv = '</div>';

        var tabs = new Array(count);
        for (var i=0; i<count; i++){
        
            tabs[i] = htmlStrDiv;
            tabs[i] += htmlUnitName;

            var job;
            if (count == 1){
                job = vTree.job;
            } else {
                job = vTree.job[i];
            }

            tabs[i] += '<div class="scroll_content">';

            tabs[i] += '<table class="data_table">';
            tabs[i] += '<caption><div class="title" style="text-align:left;">' + job.title + '</div></caption>';
            tabs[i] += this.htmlTableRow('区分', job.kubun);
            tabs[i] += this.htmlTableRow('職種', job.shokushu);
            tabs[i] += this.htmlTableRow('資格', job.qualifications);
            tabs[i] += this.htmlTableRow('勤務時間', job.hour);
            tabs[i] += this.htmlTableRow('給与', job.pay);
            tabs[i] += this.htmlTableRow('休日', job.holiday);
            tabs[i] += this.htmlTableRow('待遇', job.treatment);
            tabs[i] += this.htmlTableRow('勤務地', job.adr_work);
            tabs[i] += this.htmlTableRow('応募方法', job.proc);
            tabs[i] += this.htmlTableRow('電話番号', job.tel);
            tabs[i] += this.htmlTableRow('FAX番号', job.fax);
            if ( job.email ){
                tabs[i] += this.htmlTableRow('E-mail', '<a href="mailto:' + job.email + '">' + job.email + '</a>');
            }
            tabs[i] += this.htmlTableRow('募集期限', job.edate);
            tabs[i] += '</table></div>';

            tabs[i] += htmlEndDiv;
        }
        return tabs;
    }

    this.setUnitData = function(vTree){
        var tabs = new Array(1);
        var i = 0;
        tabs[i] = this.htmlUnitClass(vTree);
        tabs[i] += this.htmlUnitName(vTree);
        if (vTree.image || vTree.copy){
            tabs[i] += '<table class="content_table"><tr><td>';
            if (vTree.image){
                if (vTree.unit_url){
                    tabs[i] += '<a href="' + vTree.unit_url + '" target="_blank"><img src="'+ vTree.image + '"></a>';
                } else {
                    tabs[i] += '<img src="' + vTree.image + '">';
                }
            }
            if (vTree.copy){
                tabs[i] += vTree.copy;
            }
            tabs[i] += '<br clear="both"></td></tr></table>';
        }
        tabs[i] += this.htmlUnitData(vTree);
        tabs[i] += this.htmlGoogleLink(vTree);
        tabs[i] += '</div>';
        return tabs;
    }


    this.setLCatData = function(vTree){

        var tabs = new Array(1);
        var i = 0;
        tabs[i] = this.htmlUnitClass(vTree);
        tabs[i] += this.htmlUnitName(vTree);
        if (vTree.image || vTree.copy){
            tabs[i] += '<table class="content_table"><tr><td>';

            if (vTree.image){
                if (vTree.unit_url){
                    tabs[i] += '<a href="' + vTree.unit_url + '" target="_blank"><img src="'+ vTree.image + '" align="right"></a>';
                } else {
                    tabs[i] += '<img src="' + vTree.image + '" align="right">';
                }
            }

            if (vTree.copy){
                tabs[i] += vTree.copy;
            }
            tabs[i] += '<div style="clear:all;"/></td></tr></table>';
        }

        tabs[i] += '<table class="data_table">';
        tabs[i] += this.htmlTableRow('住所', vTree.address);
        tabs[i] += this.htmlTableRow('TEL', vTree.tel);

        var count = vTree.cat.length;
        var label = '';
        if (!count){
            label = vTree.cat.label;
            if (vTree.cat.label == 'その他'){
                label += '（' + vTree.cat.plabel + '）';
            }
        } else {
            for (var j=0; j<count; j++){
                if (j > 0){
                    label += '／';
                }
                label += vTree.cat[j].label;
                if (vTree.cat[j].label == 'その他'){
                    label += '（' + vTree.cat[j].plabel + '）';
                }
            }
        }
        tabs[i] += this.htmlTableRow('カテゴリー', label);
        tabs[i] += '</table>';
        tabs[i] += this.htmlGoogleLink(vTree);

        tabs[i] += '</div>';
        return tabs;
    }


    this.htmlUnitClass = function(vTree){
        var w_class = '0';
        if (vTree["-flg_appeal"] == 't'){
            w_class = '1';
        }

        var html = '<div class=" unit unit' + w_class + '">';
        return html;
    }

    this.htmlUnitName = function(vTree){
        var html = '<h2>';
        if (vTree.pre){
            html += '<div class="pre">' + vTree.pre + '</div>';
        }
        var unit_html = vTree.name;
        if (vTree.unit_url){
            unit_html = '<a href="' + vTree.unit_url + '" target="_blank">'+ vTree.name + '</a>';
        }
        html += '<div class="name">' + unit_html + '</div>';
        html += '</h2>';
        return html;
    }

    this.htmlUnitData = function(vTree){
        var html = '';
        if (vTree.address || vTree.tel){
            html += '<table class="data_table">';
            html += this.htmlTableRow('住所', vTree.address);
            html += this.htmlTableRow('TEL', vTree.tel);
            html += '</table>';
        }
        return html;
    }

	this.htmlTableRow = function(vTh, vTd){
		var html = '';
		if ( vTd ){
			html += '<tr><th nowrap="nowrap">' + vTh + '</th><td>' + vTd + '</td></tr>';
		}
		return html;
	}


    this.htmlGoogleLink = function(vTree){
        var html = '';
        if (vTree["-flg_appeal"] != 't'){
            return html;
        }

//2007/08/30 hojo delete
//        html += '<div class="google_link">';
//        if (this.flg_appeal == 't'){
////2007/07/26 hojo delete
////            html += '<a href="javascript:CyGmap_OpenSearchWindow(' + "'" + vTree["-uniqid"] + "'" + ');">Google検索</a>&nbsp;&nbsp;&nbsp;&nbsp;';
//            html += '<a href="javascript:CyGmap_ShowMapBlowup(' + "'" + vTree["-uniqid"] + "'" + ');">拡大表示</a>';
//        }
//        html += '</div>';

        return html;
    }

	this.baseicon = new CyBaseIcon();
	this.icon = new GIcon(this.baseicon);
	this.alert = function(){
		alert('alert func');
	}

	this.OpenInfoWindow = function(){

        var w_tabs;
        var w_tabs_label;
        if (this.parent){
            w_tabs = this.parent.tabs;
            w_tabs_label = this.parent.tabs_label;
        } else {
            w_tabs = this.tabs;
            w_tabs_label = this.tabs_label;
        }
        var count = w_tabs.length;
        if (!count){
            count = 1;
        }
        var tabs = new Array();
        for (var i=0; i<count; i++){
            tabs.push(new GInfoWindowTab(w_tabs_label + (i+1), w_tabs[i]));
        }

        if (this.parent){
            this.openInfoWindowTabsHtml(tabs);
        } else {
            this.marker.openInfoWindowTabsHtml(tabs);
        }
	}

	this.addMarker = function(){
//        this.icon.image = 'icon/red.png';
//        this.icon.image = 'icon/' + this.group + '.png';
        if (this.flg_appeal == 't'){
            this.icon.image = 'icon/red.png';
        } else {
            this.icon.image = 'icon/black.png';
        }
		this.marker = new GMarker(new GLatLng(this.lat,this.lng), { icon:this.icon });
		this.marker.uniqid = this.uniqid;
		this.marker.parent = this;
		GEvent.addListener(this.marker, "click", this.OpenInfoWindow);

		map.addOverlay(this.marker);
	}

	this.OpenSearchWindow = function(vObj){
		var mapsize = map.getSize();
		var mapwidth = Math.floor(eval(mapsize.width)*0.9);
		var html = '<div id="searchcontrol"></div>';
		this.marker.openInfoWindow(html);

		// create a drawOptions object
		var drawOptions = new GdrawOptions();

		drawOptions.setDrawMode(GSearchControl.DRAW_MODE_TABBED);
		var element = document.getElementById("searchcontrol");
		gSC.draw(element, drawOptions);

		// Execute an inital search
      	gSC.execute(vObj.name);
	}


}


function CyBaseIcon(){
	this.superclass.constructor(); 

    this.shadow = 'icon/shadow.png'
    this.iconSize = new GSize(40, 40);    
	this.shadowSize = new GSize(40, 40); 
    //アンカーポイントと画像の左上角からの相対オフセット値
	this.iconAnchor = new GPoint(10,40);
	this.infoWindowAnchor = new GPoint(15,10);
}
