
var romanIndex = function(){
	var asxURL = "http://eonet.jp/eohikari-ch/asx/rekishi";
	var dbURL = "xml/playlist.xml";
	var dbXml;
	
	var defLat = 34.69871932453233;
	var defLng = 135.49713134765625;
	var defLevel = 7;
	
	var gmap;
	
	var listAc = 0;
	
	var listTa = 0;
	
	var ua = navigator.userAgent.toLowerCase();
	
	var client = {
		ie    :ua.indexOf('msie') > -1,
		ie6   :ua.indexOf('msie 6') > -1,
		ie7   :ua.indexOf('msie 7') > -1,
		gecko :ua.indexOf('gecko') > -1 && ua.indexOf('safari') == -1,
		webkit:ua.indexOf('applewebkit/') > -1,
		win   :ua.indexOf('windows') > -1 || ua.indexOf('win32') > -1,
		mac   :ua.indexOf('macintosh') > -1 || ua.indexOf('mac os x') > -1,
		linux :ua.indexOf('linux') > -1
	};
	
	var intervalID = 0;
	
	var listScrollHeight = 58 * 4;
	
	var autoTime = 1000 * 10;
	
	R = {
		init : function(){
			
			gmap = new GMap2( $("#gmap").get(0));
			gmap.setCenter(new GLatLng(defLat, defLng), defLevel, G_PHYSICAL_MAP);
			gmap.addControl(new GLargeMapControl());
			//gmap.addControl(new GSmallMapControl());
			//gmap.addControl(new GMapTypeControl());
			//gmap.enableScrollWheelZoom();
			
			$.ajax({
				url : dbURL,
				dataType : "xml",
				timeout : 1000*10,
				success : loadXmlSuccess
			});
		},
		
		listOpen : function(i){
			/*if (!popupParser) return;
			var x = $(dbXml).find("movie").eq(i);
			var t = x.find("title").text();
			var id = x.attr("id");
			var u = asxURL + id + "_" + popupParser.size() + ".asx";
			*/
			var url = 'http://bcove.me/' + $(dbXml).find("movie").eq(i).find("bcove").text();
			//window.open(url, 'player', 'width=600, height=415, menubar=no, toolbar=no, scrollbars=no');
			$.colorbox({
				//width:"960",
				innerWidth:"960",
				//height:"445",
				innerHeight:"445",
				href:url,
				scrolling:false,
				iframe:true
			});
			
			
			//popupParser.openEoPlayer(t, u);
			
			//return false;
		},
		
		listOver : function(i){
			var b = $("#pickup_list div").eq(listAc);
			var n = $("#pickup_list div").eq(i);
			var id = $(dbXml).find("movie").eq(i).attr("id");
			
			b.removeClass("on").addClass("off");
			n.removeClass("off").addClass("on");
			
			$("#pickup_img").attr("src", "img/320/" + id + ".jpg");
			
			listAc = i;
		},
		
		openGmapInfo : function(m){
			m.openInfoWindow(m.html);
		},
		
		listAutoNext : function(){
			var i = listAc;
			
			if ($("#pickup_list div").length == i + 1) {
				i = 0;
				scrollTop(0);
			} else {
				++i;
			}
			
			if (i%4 == 0 && i != 0) {
				listDown(null);
			}
			
			this.listOver(i);
		}
	};
	
	function loadXmlSuccess(xml){
		dbXml = xml;
		
		initList();
		initMap();
		initArchives();
	}
	
	/*
	* ピックアップリスト
	*****************************************************************/
	
	function initList(){
		var html = '';
		
		$(dbXml).find("movie").each(function(i){
			var th = $(this);
			var id = th.attr("id");
			var t = th.find("title").text();
			
			html += '<div id="' + id + '" ';
			html += 'onmouseover="romanIndex.listOver(' + i + ')" ';
			if (i == 0) {
				html += 'class="on">';
				$("#pickup_img").attr("src", "img/320/" + id + ".jpg");
			} else {
				html += 'class="off">';
			}
			html += '<img src="img/140/' + id + '.jpg" />';
			html += '<h3>' + t + '</h3>';
			html += '<p>' + th.find("desc").text() + '</p>';
			html += '<span></span><a href="javascript:romanIndex.listOpen(' + i + ')"></a>';
			html += '</div>';
		});
		
		$("#pickup_list").append(html).mouseover(autoStop).mouseout(autoStart);
		$("#pickup_list_navi_up").click(listUp).mouseover(autoStop).mouseout(autoStart);
		$("#pickup_list_navi_down").click(listDown).mouseover(autoStop).mouseout(autoStart);
		
		autoStart();
	}
	
	function listUp(e){
		scrollTop(-listScrollHeight);
	}
	
	function listDown(e){
		scrollTop(listScrollHeight);
	}
	
	function scrollTop(s){
		var wh = $("#pickup_list").height();
		var sh = $("#pickup_list").attr("scrollHeight") - wh;
		var se = listTa + s;
		
		if (se > sh) {
			se = sh;
		} else if (se < 0) {
			se = 0;
		}
		
		if (s == 0) se = 0;
		
		listTa = se;
		$("#pickup_list").stop()
		.animate({scrollTop:se}, {duration:300, easing:"easeOutCubic"});
	}
	
	function autoStop(){
		clearInterval(intervalID);
	}
	
	function autoStart(){
		clearInterval(intervalID);
		intervalID = setInterval("romanIndex.listAutoNext();", autoTime);
	}
	
	/*
	* Google map
	*****************************************************************/
	
	function initMap(){
		$(dbXml).find("movie").each(function(i){
			var th = $(this);
			var lat = th.attr("lat");
			var lng = th.attr("lng");
			var id = th.attr("id");
			var h = th.find("title").text();
			
			var iconPoint = new GIcon();
			iconPoint.image = "img/gmapicon/" + id + ".png";
			iconPoint.iconSize = new GSize(26, 21);
			iconPoint.iconAnchor = new GPoint(11, 9);
			iconPoint.infoWindowAnchor = new GPoint(11, 0);
			
			var marker = new GMarker(new GLatLng(lat, lng), {icon:iconPoint, title:h});
			
			var html = '<div id="gmap_info"><img src="img/140/' + id + '.jpg" />';
			html += '<h3>' + h + '</h3>';
			html += '<span><a href="javascript:romanIndex.listOpen(' + i + ')"></a></span>';
			html += '</div>';
			
			marker._Rinfo = html;
			GEvent.addListener(marker, 'click', gmarkerClick);
			gmap.addOverlay(marker);
		});
	}
	
	function gmarkerClick(overlay, point){
		this.openInfoWindowHtml(this._Rinfo);
	}
	 
	 /*
	 * アーカイブリスト
	 *****************************************************************/
	
	function initArchives(){
		var movie = $(dbXml).find("movie");
		var html = '';
		var i = 0;
		
		movie.each(function(m){
			var th = $(this);
			if (th.attr("pickup") == "true" && i < 5) {
				var t = th.find("title").text();
				var id = th.attr("id");
				var p = th.find("desc").text();
				
				html += '<li class="off"><img src="img/140/' + id + '.jpg" width="140" height="112" />';
				html += '<h3>' + t + '</h3>';
				html += '<p>' + p + '</p>';
				html += '<span></span><a href="javascript:romanIndex.listOpen(' + m + ')"></a></li>';
				++i;
			}
		});
		
		$("#archives_list").append(html);
		$("#archives_list li").mouseover(archivesListOver)
		.mouseout(archivesListOut);
	}
	
	function archivesListOver(e){
		$(e.currentTarget).removeClass("off").addClass("on");
	}
	
	function archivesListOut(e){
		$(e.currentTarget).removeClass("on").addClass("off");
	}
	
	
	return R;
}();
