/* *********************
Seattle Times: livemaps.js
*******************  */
 	
 	var map;
   var POIs = [];
 	var marker;
 	var i = 0;
 	var baseIcon;
 	var timeoutId;
 	var status_div = document.getElementById("st_map_status");
 	var play_button_div;
 	var pause_button_div;
 	var splash_div;
 	var icon_div;
 	
 	var st_ads = [];
 		st_ads[0] = 'Grow your small business with search marketing. <a href="http://adsearch.seattletimescompany.com/" target="_new">Learn more about AdSearch from The Seattle Times.</a>';
 		st_ads[1] = '<a href="http://www.seattletimescompany.com/advertise/" target="_new">Advertise with us!</a> Learn more about Newspapers, Online Network, Special Sections and other advertising opportunities.';
 		st_ads[2] = 'Get the latest headlines, traffic and weather on your mobile device at <a href="http://mobile.seattletimes.com" target="_new">http://mobile.seattletimes.com</a>';
 		st_ads[3] = 'Looking for information about placing a classified ad? <a href="http://www.nwsource.com/classifieds/postlisting.html" target="_new">Learn more</a>';
 	var st_ad_number = 0;
 		
 	function getAnAd() {
 		if (st_ad_number >= st_ads.length) {
 			st_ad_number = 0;
 		}
 		current_ad_number = st_ad_number;
		st_ad_number = st_ad_number + 1;
		return st_ads[current_ad_number];
 	}
 	
  	function GetWindowWidthHeight() {
        var myWidth = 0, myHeight = 0;
        if (typeof window.innerWidth == "number") {
            myWidth = window.innerWidth;
            myHeight = window.innerHeight;
        } else if (document.documentElement &&
            (document.documentElement.clientWidth ||
            document.documentElement.clientHeight)) {
            myWidth = document.documentElement.clientWidth;
            myHeight = document.documentElement.clientHeight;
        } else if (document.body &&
            (document.body.clientWidth || document.body.clientHeight)) {
            myWidth = document.body.clientWidth;
            myHeight = document.body.clientHeight;
        }
        return {width: myWidth, height: myHeight};
    }
    
     function initMap(s) {
     	  splash_div = document.getElementById("st_splash");
        splash_div.style.display = "block";
     	  play_button_div = document.getElementById("st_play_button");
		  pause_button_div = document.getElementById("st_pause_button");
		  icon_div = document.getElementById("st_icon");
		  play_button_div.style.display = "none";
     	  sizeMap();
        map = new GMap2(document.getElementById("map"));
        map.setCenter(new GLatLng(47.619920, -122.335212),4);
        map.addControl(new GLargeMapControl);
        
        baseIcon = new GIcon(G_DEFAULT_ICON);
        baseIcon.image = "/art/maps/icons/othervenue_bkgd.png";
        baseIcon.iconSize = new GSize(22, 32);
    }
    
    function sizeMap() {
		  var w = GetWindowWidthHeight().width;
        var h = GetWindowWidthHeight().height;
        center = w / 2;
        center_h = h / 2;
        map_div = document.getElementById("map");
        footer_div = document.getElementById("st_map_footer");
        footer_right_div = document.getElementById("st_map_footer_right");
        status_div = document.getElementById("st_map_status");
        control_div = document.getElementById("st_control_bar");
        max_zh = (h * 0.8) - 100;
        max_zw = (w * 0.55) - 100;
        if (map_div) {
            map_div.style.top = 0;
            map_div.style.left = 0;
            map_div.style.width = w;
            map_div.style.height = h-22;
        }
        if (footer_div) {
            footer_div.style.top = h - 20;
        }
        if (footer_right_div) {
        	footer_right_div.style.right = -w + 20;
        }
        if (status_div) {
        		status_div.style.top = center_h - 32;
         	status_div.style.left = center - 32;
        }
        if (control_div) {
        	control_div.style.top = h - 20;
        	control_div.style.right = center;
        }
        if (splash_div) {
        	splash_div.style.top = center_h - 200;
        	splash_div.style.left = center - 231;
        }
        if (icon_div) {
        	icon_div.style.top = h - 100;
        	icon_div.style.left = w - 70;
        }
    }
    
        window.onload = function () {
        	window.onresize = sizeMap;
        	if (GBrowserIsCompatible()) {
        		initMap();
        	} else {
        		alert("Sorry, your browser isn't compatible with Seattle Times: Mapping the News");
        	}
        	hitPOIs();
        };
        
    
    
    
	function hitPOIs() {
		//alert("--");
		if (POIs.length <= 1) {
				slurpPOIs();
		}
		currentPOI = POIs.shift();
		if (currentPOI) {
			displayMarker(currentPOI.latitude, currentPOI.longitude, currentPOI.source, currentPOI.logo, currentPOI.title, currentPOI.summary, currentPOI.link, currentPOI.age);
		}
		timeoutId = setTimeout("hitPOIs();", 6000);
	}

    function displayMarker(xlat, xlong, source, logo, title, summary, link, age) {
    		splash_div.style.display = "none";

       	var point = new GLatLng(xlat,xlong);
       	
       	if (marker) {map.removeOverlay(marker);}

			var options = { 
				title: title,
				icon: baseIcon,
				draggable: false
			};

       	marker = new GMarker(point,options);
       	map.addOverlay(marker);
       	
       	html = "<div class=\"bubble_main\" onmouseover=\"st_pause();\" onmouseout=\"st_resume();\"><div class=\"bubble_logo\"><img src=\""+logo+"\" title=\""+source+"\" height=\"60\" border=\"0\"></div><div class=\"bubble_text\"><span class=\"bubble_time\">"+age+"</span><br><span class=\"bubble_headline\"><a target=\"_blank\" href=\""+link+"\"><strong>"+title+"</strong></a></span><br>"+summary+"</div><div class=\"text\"><img src=\"http://local.ads.nwsource.com/ads/adv.gif\"><br>"+getAnAd();
			
		// marker.openInfoWindowHtml(decodeURIComponent(html));
		marker.openInfoWindowHtml(html);
    }
    
    function st_fastforward() {
    	clearTimeout(timeoutId);
    	hitPOIs();
    }
    
    function st_pause() {
    	clearTimeout(timeoutId);
    	status_div.innerHTML = "<img id=\"pause_img\" src=\"images/pause64.png\" height=\"64\" width=\"64\" border=\"0\" onclick=\"javascript:st_resume();\">";
    	status_div.style.display = "block";
    	play_button_div.style.display = "inline";
    	pause_button_div.style.display = "none";
    }
    
    function st_resume() {
    	splash_div.style.display = "none";
    	timeoutId = setTimeout("hitPOIs();", 2000);
    	status_div.innerHTML = "not paused";
    	status_div.style.display = "none";
    	play_button_div.style.display = "none";
    	pause_button_div.style.display = "inline";
    }
        
	function processData(doc) {
        // === Parse the JSON document === 
        var jsonData = eval('(' + doc + ')');
        
        // === Plot the markers ===
        for (var i=0; i<jsonData.markers.length; i++) {
        	POIs.push( new POI(jsonData.markers[i].latitude, jsonData.markers[i].longitude, jsonData.markers[i].source, jsonData.markers[i].logo, jsonData.markers[i].title, jsonData.markers[i].summary, jsonData.markers[i].link, jsonData.markers[i].age) );
          //var point = new GLatLng(, jsonData.markers[i].lng);
          //var marker = createMarker(point, jsonData.markers[i].label, jsonData.markers[i].html);
          //map.addOverlay(marker);
        }
	}

	function slurpPOIs() {
		// === Fetch the JSON data file ====    
		GDownloadUrl("/livemaps/data/mbase/", processData);
	}
	
	function POI(latitude, longitude, source, logo, title, summary, link, age) {
		this.title = title;
		this.latitude = latitude;
		this.longitude = longitude;
		this.summary = summary;
		this.source = source;
		this.logo = logo;
		this.link = link;
		this.age = age;
	}
	
	function show_credits() {
		st_pause();
		splash_div.style.display = "block";
		splash_headline = document.getElementById("st_splash_headline");
		splash_headline.style.display = "none";
		splash_text = document.getElementById("st_splash_text");
		splash_text.style.display = "none";
		credits_text = document.getElementById("st_credits_text");
		credits_text.style.display = "block";
	}
