// Font Size =====================================
var defaultFontSize =  75;
var minimumFontSize =  60;
var maximumFontSize = 250;

var defaultStyle = "Fixed";

$debug_this_script = false;

var prefsLoaded = false;
var currentFontSize = defaultFontSize;

var pdfIcon ="&nbsp;<img src='/i/icon/icon_pdf.gif' width='17' height='19' alt='pdf' />";
var xlsIcon ="&nbsp;<img src='/i/icon/icon_xsl.gif' width='17' height='19' alt='xsl' />";
var docIcon ="&nbsp;<img src='/i/icon/icon_doc.gif' width='17' height='19' alt='doc' />";
var pptIcon ="&nbsp;<img src='/i/icon/icon_ppt.gif' width='17' height='19' alt='ppt' />";
var zipIcon ="&nbsp;<img src='/i/icon/icon_zip.gif' width='17' height='14' alt='zip' />";
var csvIcon ="&nbsp;<img src='/i/icon/icon_csv.gif' width='17' height='19' alt='csv' />";
var popIcon ="&nbsp;<img src='/i/icon/icon_popup.gif' width='16' height='16' alt='popup' />";


function do_template_specific_stuff(){
}


function toggleFluid(){
	var switchTo = (currentStyle == "Fixed") ? "Fluid" : "Fixed";

	document.body.className = switchTo;

}


function setWidth(width){
	
	if(width != "Fluid"){
		document.body.className = '';
		currentStyle = "Fixed";
	}else{
		document.body.className = 'bodyfluid';
		currentStyle = "Fluid";
	}
}



	function revertStyles(){
		currentFontSize = defaultFontSize;
		changeFontSize(0);
	}

	
	function changeFontSize(sizeDifference){
		currentFontSize = parseInt(currentFontSize) + parseInt(sizeDifference * 20);
	
		if(currentFontSize > maximumFontSize){
			currentFontSize = maximumFontSize;
		}else if(currentFontSize < minimumFontSize){
			currentFontSize = minimumFontSize;
		}
		setFontSize(currentFontSize);

		var title = getActiveStyleSheet();
		createCookie("style", title, 365);

		createCookie("fontSize", currentFontSize, 365);

	}

	function setFontSize(fontSize){
		if($debug_this_script){alert ('fontsize is being set: ' + fontSize);}
		var stObj = (document.getElementById) ? document.getElementById('content_area') : document.all('content_area');
		document.body.style.fontSize = fontSize + '%';
	}




	function setActiveStyleSheet(title) {
	  var i, a, main;
	  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
	    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
	      a.disabled = true;
	      if(a.getAttribute("title") == title) a.disabled = false;
	    }
	  }
	}

	function getActiveStyleSheet() {
	  var i, a;
	  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
	    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && !a.disabled) return a.getAttribute("title");
	  }
	  return null;
	}

	function getPreferredStyleSheet() {
	  var i, a;
	  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
	    if(a.getAttribute("rel").indexOf("style") != -1
	       && a.getAttribute("rel").indexOf("alt") == -1
	       && a.getAttribute("title")
	       ) return a.getAttribute("title");
	  }
	  return null;
	}




	function createCookie(name,value,days) {
	  if (days) {
	    var date = new Date();
	    date.setTime(date.getTime()+(days*24*60*60*1000));
	    var expires = "; expires="+date.toGMTString();
	  }
	  else expires = "";
	  document.cookie = name+"="+value+expires+"; path=/";
	}

	function readCookie(name) {
	  var nameEQ = name + "=";
	  var ca = document.cookie.split(';');
	  for(var i=0;i < ca.length;i++) {
	    var c = ca[i];
	    while (c.charAt(0)==' ') c = c.substring(1,c.length);
	    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	  }
	  return null;
	}



	function js_init() {
		if($debug_this_script){  alert('window.onload firing');}

			cookie = readCookie("pageWidth");
			currentStyle = cookie ? cookie : "Fixed";
			setWidth(currentStyle);


			var cookie = readCookie("style");
			var title = (cookie) ? cookie : getPreferredStyleSheet();
			setActiveStyleSheet(title);

			cookie = readCookie("fontSize");
                        currentFontSize = (cookie) ? cookie : defaultFontSize;
			setFontSize(currentFontSize);

			do_template_specific_stuff();

		if($debug_this_script){alert('window.onload is exiting');}
	}




//	window.onunload = function(e) {
//		if($debug_this_script){ alert('onunload is firing'); }
//
////		  createCookie("pageWidth", currentStyle, 365);
//
//		var title = getActiveStyleSheet();
//		createCookie("style", title, 365);
//
//		createCookie("fontSize", currentFontSize, 365);
//
//		if($debug_this_script){ alert('onunload is firing'); }
//	};


// Small Window =====================================
/*function popup(url) {
	sw = window.open(url, 'sw', 'width=650,height=720,resizable=yes,scrollbars=yes,toolbar=yes,ocation=yes,status=yes,menubar=yes');
}*/

// Printout =====================================
function printout() {
	window.print();
}



// Small Window (w650)=====================================
function popup(url) {
	sw = window.open(url, 'sw', 'width=670,resizable=yes,scrollbars=yes,toolbar=yes,location=yes,status=yes,menubar=yes');
		sw.focus();
}

// Wide Window (w900)=====================================
function popup900(url) {
  sw = window.open(url, 'win800', 'width=920,scrollbars=yes,resizable=yes,toolbar=yes,location=yes,status=yes,menubar=yes');
	if (sw != null)  {
		sw.focus();
	}
}


//Parent Window link
function focusP(url) {
	if(window.opener == null) {
		pw = window.open(url, '_blank');
	}
	else if (!window.opener.closed)  {
		window.opener.location.href = url;
		window.opener.focus();
	} else {
		pw = window.open(url, '_blank');
	}
}


// File Size get=====================================
function createXmlHttp() {
  if (window.XMLHttpRequest) {
    return new XMLHttpRequest();
  } else if (window.ActiveXObject) {
    try {
      return new ActiveXObject('Msxml2.XMLHTTP');
    } catch(e) {
      return new ActiveXObject('Microsoft.XMLHTTP');
    }
  } else {
    return null;
  }
};

function checkExt() {
        var res ;

}

function get_file_size(obj) {
  var Extension = obj.href.split(".");
  var icon      = false;
  var length    = false;
  var req       = createXmlHttp();
  var g_Extension = Extension[Extension.length-1];  
        if(g_Extension == "pdf" || g_Extension == "PDF"){
            icon    = pdfIcon;
        }
        else if(g_Extension == "xls" || g_Extension == "xlsx" || g_Extension == "XLS" || g_Extension == "XLSX"){
            icon    = xlsIcon;
        }
        else if(g_Extension == "doc" || g_Extension == "docx" || g_Extension == "DOC" || g_Extension == "DOCX"){
            icon    = docIcon;
        }
        else if(g_Extension == "ppt" || g_Extension == "pptx" || g_Extension == "PPT" || g_Extension == "PPTX"){
            icon    = pptIcon;
        }
        else if(g_Extension == "zip" || g_Extension == "ZIP"){
            icon    = zipIcon;

        }
        else if(g_Extension == "csv" || g_Extension == "CSV"){
            icon    = csvIcon;
        }

  if(obj.href.split(":")[0]=="javascript"){

    Extension = obj.href.split(":");
    if(Extension[1].substring(0,5) == "popup"){
        //var popNW = document.createElement("span");
        obj.innerHTML += popIcon;
        //obj.parentNode.appendChild(popNW);
    }
    return;
  }
  
  if (req == null) {
    return;
  }
  if(icon != false)obj.innerHTML += icon;

/*  req.onreadystatechange = function() {
    var myNW = document.createElement("span");
         myNW.innerHTML = icon;
         obj.parentNode.insertBefore(myNW,obj.nextSibling);
      }*/
  };



onload = function get_size_all() {
  var links = document.getElementsByTagName('a');
  // hhonjyoh@ewmjapan.com updated 2010/10/15
//  var element = document.getElementById("IdGnavi10");
//  var url     = location.href;
//  var matchStr = url.match("policy_others");
//  if(matchStr == null) element.removeChild(element.firstChild);
  for (var i = 0; i < links.length; i++) {
        get_file_size(links[i]);
  }
};

function goSearch(id) {
        var query = document.getElementById(id).value;
        window.location.href = "http://www.kankyo.metro.tokyo.jp/scs/sf1.wn?query=" + encodeURI(query) ;
}

