// ghazalreader.js

// setting parameter variables

font_ur = null; // urdu font-family
fontsize_ur = null; // urdu font-size
font_trans = null; // transliteration font-family
fontsize_trans = null; // transliteration font-size
loadanyways = null; // load bad browser anyways
nocookies=null; // don't use cookies
/*screenwidth = null;*/ // no longer necessary (used to just be a 780px window)


// code to set fonts
function setDocFont(fonttype,newfontfamily,newfontsize){

	if (!document.styleSheets) return;

	var theRules = new Array();
	if (document.styleSheets[0].cssRules){
		theRules = document.styleSheets[0].cssRules}
	else if (document.styleSheets[0].rules){
		theRules = document.styleSheets[0].rules}
	else return;
	for (i=0;i<theRules.length;i++){

	if (theRules[i].selectorText!=null) {
		if (theRules[i].selectorText.indexOf(fonttype)!=-1)

		{
			theRules[i].style.fontFamily=newfontfamily;
			theRules[i].style.fontSize=newfontsize;
	      break;
		}
	}	
}

}

function setFonts() {
// this function adjusts the stylesheet.
 setDocFont("urdufont",font_ur,fontsize_ur);
 setDocFont("transliterationfont",font_trans,fontsize_trans);
}

// code to set/read cookies

function setCookie(cookieName,value,days) {
 today = new Date();
 expire = new Date();
 if (days==null || days==0) days=1;
 expire.setTime(today.getTime() + 3600000*24*days);
 
 document.cookie = cookieName+"="+escape(value)
                 + ";expires="+expire.toGMTString();
 
}

function readCookie(name) {
 theCookie=""+document.cookie;
 i_start=theCookie.indexOf(name);
 if (i_start>-1) {
    var i_end=theCookie.indexOf(';',i_start);
 }
 if (i_end==-1) i_end=theCookie.length; 
 
 s = unescape(theCookie.substring(i_start+name.length+1,i_end));
 
 return s;  // will be null if no cookie
}

function setParameterVariable(target,value) {
// adjust parameter variables.  Needed to deal with variable scope issues.
// Would not be necessary with better encapsulation.

  if (value=="true") value = true;
  if (value=="false") value = false;
  if (target=="font_ur") {font_ur = value}
  if (target=="fontsize_ur") {fontsize_ur = value}
  if (target=="font_trans") {font_trans = value}
  if (target=="fontsize_trans"){ fontsize_trans = value}
  if (target=="loadanyways"){loadanyways = value}
  if (target=="nocookies") {nocookies = value}
}

function grabParameterAs(source,target,equalsmarker) {
// grabs query (search) parameter 'variable' from 'source' and sets 'target' to its value.  Calls setParameterVariable().
  if (equalsmarker==null) {equalsmarker="=";}
  var vars = source.split("&");
  for (var i=0;i<vars.length;i++) {
    var pair = vars[i].split(equalsmarker);  // this allows for ":" in cookie and "=" in query string.
    if (pair[0] == target) {
  //    alert("found "+pair[0]+" as "+pair[1]);
      setParameterVariable(target,pair[1]);  // variable scope forces this work around.
    }
  } 
  // otherwise, don't set target
}
// ----

function readDigitalUrduGhazalReaderCookie() {
  var cookieValue;  // holds value of cookie
  cookieValue = readCookie("digitalUrduGhazalReader");
  //alert("cookieValue = "+cookieValue);
  
  if (cookieValue!=null){ // we have cookie.
    cookieValue = unescape(cookieValue); // unescape it.
    grabParameterAs(cookieValue,"font_ur",":"); // urdu font-family
    grabParameterAs(cookieValue,"fontsize_ur",":");// urdu font-size
    grabParameterAs(cookieValue,"font_trans",":");// transliteration font-family
    grabParameterAs(cookieValue,"fontsize_trans",":"); // transliteration font-size
    grabParameterAs(cookieValue,"loadanyways",":"); // load stinky browser
  }

}

function readQueryString() {
// reads parameters set in the query (search) string
  var queryString = unescape(window.location.search.substring(1));
  if (queryString!=null||queryString.length>0) { // we have a query string.
    grabParameterAs(queryString,"font_ur");
    grabParameterAs(queryString,"fontsize_ur");
    grabParameterAs(queryString,"font_trans");
    grabParameterAs(queryString,"fontsize_trans");
    grabParameterAs(queryString,"loadanyways");
    grabParameterAs(queryString,"nocookies");
    
  }
}


function passParametersToInternalAnchors() {
// This function will pass the settings to internal anchors.  By internal anchors
// I mean those that are not marked rel=external and that have an href.
// This is used to pass settings parameters if cookies cannot be set.
// Parameters override cookies.  If you are interested in cookies, by the way,
// I recommend the following site:  http://www.nicecupofteaandasitdown.com/

}
function setAllExternalAnchorsToTargetBlank() {
// This function marks all anchors's marked with rel=external to target="_blank"
// This is not a valid setting in XHTML strict but is part of DOM.
// Perhaps the external window is annoying, but maybe useful for some people 
// who might otherwise get lost.

  if (!document.getElementsByTagName) return; 
  var anchors = document.getElementsByTagName("a"); 
  for (var i=0; i<anchors.length; i++) { 
    var anchor = anchors[i];
    if (anchor.getAttribute("href") &&
    anchor.getAttribute("rel") == "external") {
      anchor.target = "_blank"; 
    } 
  }
}

function sniffForStinkyBrowser() {
// this will look for stinky browsers mostly using object detection.
// It is not perfect.  It will rule out non-Dom browser (ie nn4)
// It will check ie browser more specifically, as ie 6 is non compliant and still popular.
// not sure about netscape versions.
// not sure about opera.
// Need to check safari.

  stinkyBrowser = false;
  stinkyIEBrowser = false;
  I   = (document.all) ? true:false;                    // ie4+ (haven't check ie 7)
  DOM = (document.getElementById) ? true:false;		// will work even for ie5.5
							// will not work for netscape 4
  if (DOM==false) {StinkyBrowser = true;}
  
// now sniff IE... (should I check for WIN?  No.. It's no longer available for Apple.
  if (document.all&&DOM==true) { // check if old ie
    version=0
    if (navigator.appVersion.indexOf("MSIE")!=-1){
      temp=navigator.appVersion.split("MSIE")
      version=parseFloat(temp[1])
    }

    if (version>0&&version<7) { // check if not ie 7 or greater
      stinkyBrowser = true;
      stinkyIEBrowser = true;
    }

  }
  if (stinkyBrowser == true) {
     confirmString = "Your browser [name: "+navigator.appName+"; version: "+navigator.appVersion+"] will most likely not display the Digital Urdu Ghazal Reader properly as it does not conform to World Wide Web Consortium (W3C) standards.";
     if (stinkyIEBrowser==true) {
       confirmString = confirmString + " The latest version of Internet Explorer (version 7) will work properly, but not versions 6 or lower.  (Version 6 shipped with Windows XP). You can download the latest version of IE from www.microsoft.com/windows/ie/ or try the Firefox browser from mozilla.com";
     }
     confirmString = confirmString + " Press OK to proceed to learn more about the system requirements.";
     
     confirmString = confirmString + " Press Cancel to load anyways.";
     
  
     b = confirm(confirmString);
     if (b==true) { // they pressed ok
        loadanyways=false;
        window.location.href = "info.html#_systemrequirements";//don't bother passing parameters
     }
     if (b==true) {
       loadanyways = true;
     }
   } // voila.
}

function setParametersToDefault(forceDefault) {

  default_font_ur = "Nafees Web Naskh"; // this could work on all platforms...
  default_fontsize_ur = "large";
  default_font_trans = "Gentium"; // GentiumAlt is better but less compatible.
  default_fontsize_trans = "medium";
    
  if (font_ur==null||forceDefault==true) { font_ur = default_font_ur;}
  if (fontsize_ur==null||forceDefault==true) {fontsize_ur = default_fontsize_ur;}
  if (font_trans==null||forceDefault==true) {font_trans = default_font_trans;}
  if (fontsize_trans==null||forceDefault==true) {fontsize_trans = default_fontsize_trans;}

}
function setAllParametersToDefault() {
  setParametersToDefault(true); // force all parameters to defaults.
}


function setParametersToDefaultIfNecessary() {
// here we check if something is not yet set, and set to default if necessary.
// early, I was thinking of sniffing the operating system and then selecting nice defaults.
// These fonts should work on all platforms, though, and they have nice licenses.
// If you have anything better, please let me know.

  setParametersToDefault(false); // do not force parameters...

}


function setDigitalUrduGhazalReaderCookie() {
  //cookieString = //
  
  //cookieString = "digitalurdughazalreader="?"+
  cookieString = ""+"font_ur:"+font_ur+"&" +
           "fontsize_ur:"+fontsize_ur+"&" +
           "font_trans:"+font_trans+"&" +
           "fontsize_trans:"+fontsize_trans;
           
  if (loadanyways==true) out = out+"&loadanyways:true";

  currentCookie = readCookie("digitalUrduGhazalReader");
  
  if (cookieString!=currentCookie) {
    setCookie("digitalUrduGhazalReader",cookieString,365);//cookie= "urdufcookie="+escape(newf)+"; expires=Sat, 08-Apr-2006 20:03:00 GMT";
  
}
}
function loadParameters(noalert) {
// this function does the following
// 1. reads cookie { [Digital Urdu Ghazal Reader]:font_ur; fontsize_ur; font_trans; fontsize_trans; loadanyways (set only if specified), nocookies (set only if specified), /*screenwidth*/}
// 2. read parameters; if they exist, set those.
// 3. sniff browser
// if not /loadanyway/, check if the browser is supported
// 4. Check Parameters
// 4. if nocookies= false, set a cookie.  Otherwise, adjust parameters of all internal links (those with href, and not marked rel=external).
// 5. set all external anchors to target=blank. (thank you, xhtml).




  readDigitalUrduGhazalReaderCookie();
  readQueryString();
  if (loadanyways!=true||noalert!=true) {sniffForStinkyBrowser();}
  setParametersToDefaultIfNecessary();
  if (nocookies!=true) {setDigitalUrduGhazalReaderCookie();}
  
}
function adjustAnchors() {
  if (!document.getElementsByTagName) return;
  var anchors = document.getElementsByTagName("a");
  for (var i=0; i<anchors.length; i++) {
    var anchor = anchors[i];
    if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external") {
     anchor.target = "_blank";
    }
  }
}

function standardInit() {
  loadParameters();
  setFonts();
  adjustAnchors();
}
function gentleInit() {
  loadParameters(true);
  setFonts();
  adjustAnchors();
}

// ---- end ghazalreader.js

