//////////////////////////////
// INIT SCREEN DETERMINATION

function getCSSRule(ruleName, deleteFlag) {               // Return requested style obejct
   ruleName=ruleName.toLowerCase();                       // Convert test string to lower case.
   if (document.styleSheets) {                            // If browser can play with stylesheets
      for (var i=0; i<document.styleSheets.length; i++) { // For each stylesheet
         var styleSheet=document.styleSheets[i];          // Get the current Stylesheet
         var ii=0;                                        // Initialize subCounter.
         var cssRule=false;                               // Initialize cssRule. 
         do {                                             // For each rule in stylesheet
            if (styleSheet.cssRules) {                    // Browser uses cssRules?
               cssRule = styleSheet.cssRules[ii];         // Yes --Mozilla Style
            } else {                                      // Browser usses rules?
               cssRule = styleSheet.rules[ii];            // Yes IE style. 
            }                                             // End IE check.
            if (cssRule)  {                               // If we found a rule...
               if (cssRule.selectorText.toLowerCase()==ruleName) { //  match ruleName?
                  if (deleteFlag=='delete') {             // Yes.  Are we deleteing?
                     if (styleSheet.cssRules) {           // Yes, deleting...
                        styleSheet.deleteRule(ii);        // Delete rule, Moz Style
                     } else {                             // Still deleting.
                        styleSheet.removeRule(ii);        // Delete rule IE style.
                     }                                    // End IE check.
                     return true;                         // return true, class deleted.
                  } else {                                // found and not deleting.
                     return cssRule;                      // return the style object.
                  }                                       // End delete Check
               }                                          // End found rule name
            }                                             // end found cssRule
            ii++;                                         // Increment sub-counter
         } while (cssRule)                                // end While loop
      }                                                   // end For loop
   }                                                      // end styleSheet ability check
   return false;                                          // we found NOTHING!
}                                                         // end getCSSRule 

function killCSSRule(ruleName) {                          // Delete a CSS rule   
   return getCSSRule(ruleName,'delete');                  // just call getCSSRule w/delete flag.
}                                                         // end killCSSRule

function addCSSRule(ruleName) {                           // Create a new css rule
   if (document.styleSheets) {                            // Can browser do styleSheets?
      if (!getCSSRule(ruleName)) {                        // if rule doesn't exist...
         if (document.styleSheets[0].addRule) {           // Browser is IE?
            document.styleSheets[0].addRule(ruleName, null,0);      // Yes, add IE style
         } else {                                         // Browser is IE?
            document.styleSheets[0].insertRule(ruleName+' { }', 0); // Yes, add Moz style.
         }                                                // End browser check
      }                                                   // End already exist check.
   }                                                      // End browser ability check.
   return getCSSRule(ruleName);                           // return rule we just created.
} 

var browserHeight;
var browserHeightFlip = 640;
var global;
function adjustFixed() {

	if (self.innerHeight) {
		browserHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) {
		browserHeight = document.documentElement.clientHeight;
	} else if (document.body) {
		browserHeight = document.body.clientHeight;
	}

	if (browserHeight < browserHeightFlip) {
		global = getCSSRule('.dropShadow');
		global.style.position = "absolute";
		global = getCSSRule('#topContentWrapper');
		global.style.position = "relative";
		global = getCSSRule('#mainContent');
		global.style.paddingTop = "0";
		global = getCSSRule('#widgetsDiv');
		global.style.padding = "0 0 55px 0";
		global = getCSSRule('#subscribeDiv');
		global.style.position = "relative";
		global.style.height = "1px";
	} else {
		global = getCSSRule('.dropShadow');
		global.style.position = "fixed";
		global = getCSSRule('#topContentWrapper');
		global.style.position = "fixed";
		global = getCSSRule('#mainContent');
		global.style.paddingTop = "124px";
		global = getCSSRule('#widgetsDiv');
		global.style.padding = "1px 0 55px 0";
		global = getCSSRule('#subscribeDiv');
		global.style.position = "fixed";
	}
	
}
adjustFixed();


var fibernocciOne = 2;
var fibernocciTwo = 3;
var emailToSend;

////////////////////////////////////////////////////////////////////////////////////
// ARTICLE

var articleStatus = "stopped";
var articleHeight = 0;

function showArticle(id) {
	if (articleStatus != "in transit") {
		fibernocciOne = 2;
		fibernocciTwo = 3;
		if (document.getElementById("article_"+id).style.height == "1px") {
			articleStatus = "in transit";
			articleHeight = 1;
			openArticleLoop=setInterval("openArticle("+id+")",100);
		} else {
			articleStatus = "in transit";
			articleHeight = 1;
			closeArticleLoop=setInterval("closeArticle("+id+")",100);
		}
	}
}

function openArticle(id) {
	articleHeight = fibernocciOne + fibernocciTwo;
	fibernocciOne = fibernocciTwo;
	fibernocciTwo = articleHeight;

	if (articleHeight >= 150) {
		document.getElementById("article_"+id).style.height = "auto";
		clearInterval(openArticleLoop);
		articleStatus = "stopped";
	} else {
		document.getElementById("article_"+id).style.height = articleHeight + "px";
	}
}

function closeArticle(id) {
	articleHeight = fibernocciOne + fibernocciTwo;
	fibernocciOne = fibernocciTwo;
	fibernocciTwo = articleHeight;
	
	if (articleHeight >= 150) {
		document.getElementById("article_"+id).style.height = "1px";
		clearInterval(closeArticleLoop);
		articleStatus = "stopped";
	} else {
		document.getElementById("article_"+id).style.height = (150 - articleHeight) + "px";
	}
}

////////////////////////////////////////////////////////////////////////////////////
// EMAIL

var emailStatus = "stopped";
var emailHeight = 0;

function showEmail(id) {
	if (emailStatus != "in transit") {
		fibernocciOne = 1;
		fibernocciTwo = 2;
		if (document.getElementById("email_"+id).style.height == "1px") {
			emailStatus = "in transit";
			emailHeight = 1;
			openEmailLoop=setInterval("openEmail("+id+")",100);
		} else {
			emailStatus = "in transit";
			emailHeight = 1;
			closeEmailLoop=setInterval("closeEmail("+id+")",100);
		}
	}
}

function openEmail(id) {
	emailHeight = fibernocciOne + fibernocciTwo;
	fibernocciOne = fibernocciTwo;
	fibernocciTwo = emailHeight;

	if (emailHeight >= 50) {
		document.getElementById("email_"+id).style.height = "auto";
		clearInterval(openEmailLoop);
		emailStatus = "stopped";
	} else {
		document.getElementById("email_"+id).style.height = emailHeight + "px";
	}
}

function closeEmail(id) {
	emailHeight = fibernocciOne + fibernocciTwo;
	fibernocciOne = fibernocciTwo;
	fibernocciTwo = emailHeight;
	
	if (emailHeight >= 50) {
		document.getElementById("email_"+id).style.height = "1px";
		clearInterval(closeEmailLoop);
		emailStatus = "stopped";
	} else {
		document.getElementById("email_"+id).style.height = (50 - emailHeight) + "px";
	}
}

function sendEmail(id) {
	if (!isWorking && http) {
		emailToSend = id;
		http.open("GET", "ajax/send_email.html?id=" + id + "&name=" + document.forms['emailForm'+id]['name'].value + "&email=" + document.forms['emailForm'+id]['email'].value, true);
		http.onreadystatechange = handleHttpResponse;
		isWorking = true;
		http.send(null);
	}
}

function handleHttpResponse() {
	if (http.readyState == 4) {
		if (http.responseText.indexOf('invalid') == -1) {
			results = http.responseText;
			document.getElementById("emailInner_"+emailToSend).innerHTML = "<DIV STYLE='font-size:9pt;padding:9px;color:#786b05;'><CENTER>This article has been mailed</CENTER></DIV>";
			isWorking = false;
		}
	}
}

////////////////////////////////////////////////////////////////////////////////////
// SUBSCRIBE

var subscribeStatus = "stopped";
var subscribeHeight = 0;

function showSubscribe() {
	if (subscribeStatus != "in transit") {
		fibernocciOne = 1;
		fibernocciTwo = 2;
		if (document.getElementById("subscribe").style.height == "1px") {
			subscribeStatus = "in transit";
			subscribeHeight = 1;
			openSubscribeLoop=setInterval("openSubscribe()",100);
		} else {
			subscribeStatus = "in transit";
			subscribeHeight = 1;
			closeSubscribeLoop=setInterval("closeSubscribe()",100);
		}
	}
}

function openSubscribe() {
	subscribeHeight = fibernocciOne + fibernocciTwo;
	fibernocciOne = fibernocciTwo;
	fibernocciTwo = subscribeHeight;

	if (subscribeHeight >= 70) {
		document.getElementById("subscribe").style.height = "70px";
		clearInterval(openSubscribeLoop);
		subscribeStatus = "stopped";
	} else {
		document.getElementById("subscribe").style.height =subscribeHeight + "px";
	}
}

function closeSubscribe() {
	subscribeHeight = fibernocciOne + fibernocciTwo;
	fibernocciOne = fibernocciTwo;
	fibernocciTwo = subscribeHeight;
	
	if (subscribeHeight >= 70) {
		document.getElementById("subscribe").style.height = "1px";
		clearInterval(closeSubscribeLoop);
		subscribeStatus = "stopped";
	} else {
		document.getElementById("subscribe").style.height = (70 - subscribeHeight) + "px";
	}
}

function sendSubscribe() {
	if (!isWorking && http) {
		if (document.forms['subscribeForm']['email'].value) {
			http.open("GET", "ajax/subscribe.html?email=" + document.forms['subscribeForm']['email'].value, true);
			http.onreadystatechange = handleHttpResponseTwo;
			isWorking = true;
			http.send(null);
		}
	}
}

function handleHttpResponseTwo() {
	if (http.readyState == 4) {
		if (http.responseText.indexOf('invalid') == -1) {
			results = http.responseText;
			
			rexp = /success/;
			if (rexp.test(results)) {
				document.getElementById("subscribe").innerHTML = "<DIV STYLE='font-size:9pt;padding-top:17px;color:#786b05;'><CENTER>Thank you for signing up</CENTER></DIV>";
			}
			
			rexp = /duplicate/;
			if (rexp.test(results)) {
				document.getElementById("subscribe").innerHTML = "<DIV STYLE='font-size:9pt;padding-top:17px;color:#786b05;'><CENTER>You are already signed up</CENTER></DIV>";
			}
			
			isWorking = false;
		}
	}
}


////////////////////////////////////////////////////////////////////////////////////
// SLIDES

if (navigator.appVersion.indexOf('Mac') != -1) mac = 1;
else mac = 0;

var quotes=new Array();
//var size=new Array();

startImage = 1;


function switchImage(direction) {
	endImage = quotes.length-1;
	
	if (direction == "next"){startImage++}
	else {startImage--}
	if (startImage == endImage+1){startImage = 1};
	if (startImage == 0){startImage = 1};
	
	if (!mac){document.getElementById('main').filters.blendTrans.Apply()}
	
	document.getElementById('main').src = "/images/slides/"+slideCategory+"_"+startImage+".jpg";

	document.getElementById('quote').innerHTML = quotes[startImage];
//	document.getElementById('quote').style.height = (size[startImage] * 16);
//	document.getElementById('quote').style.top = -21 - (16*size[startImage]);

	if (!mac){document.getElementById('main').filters.blendTrans.Play()}
	
	document.getElementById('progressMarker').style.marginLeft = ((306 / endImage) * (startImage-1)) + "px";
	
	// PRELOAD NEXT IMAGE
	ImageNext = new Image();
	ImageNext.src = "/images/slides/"+slideCategory+"_"+(startImage+1)+".jpg";
}