//////////////////////////////
// INIT SCREEN DETERMINATION

function getCSSRule(ruleName, deleteFlag) {               
   ruleName=ruleName.toLowerCase();                       
   if (document.styleSheets) {                            
      for (var i=0; i<document.styleSheets.length; i++) { 
         var styleSheet=document.styleSheets[i];          
         var ii=0;                                        
         var cssRule=false;                               
         do {                                             
            if (styleSheet.cssRules) {                    
               cssRule = styleSheet.cssRules[ii];         
            } else {                                      
               cssRule = styleSheet.rules[ii];            
            }                                             
            if (cssRule)  {                               
               if (cssRule.selectorText.toLowerCase()==ruleName) { 
                  if (deleteFlag=='delete') {            
                     if (styleSheet.cssRules) {           
                        styleSheet.deleteRule(ii);        
                     } else {                             
                        styleSheet.removeRule(ii);       
                     }                                    
                     return true;                         
                  } else {                                
                     return cssRule;                      
                  }                                       
               }                                          
            }                                             
            ii++;                                         
         } while (cssRule)                                
      }                                                   
   }                                                      
   return false;                                         
}                                                         

function killCSSRule(ruleName) {                          
   return getCSSRule(ruleName,'delete');                  
}                                                         

function addCSSRule(ruleName) {                           
   if (document.styleSheets) {                            
      if (!getCSSRule(ruleName)) {                        
         if (document.styleSheets[0].addRule) {           
            document.styleSheets[0].addRule(ruleName, null,0);      
         } else {                                         
            document.styleSheets[0].insertRule(ruleName+' { }', 0); 
         }                                                
      }                                                   
   }                                                      
   return getCSSRule(ruleName);                           
} 

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 = "0";
	} 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();
window.onresize = 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;
		}
	}
}



////////////////////////////////////////////////////////////////////////////////////
// SOCIAL BAR DESCRIPTION

function socialBarDescOn(bgPosition, copyPosition, copy) {
	document.getElementById('socialBarDesc').style.display = "block";
	document.getElementById('socialBarDescBg').style.backgroundPosition = bgPosition + "px 0";
	document.getElementById('socialBarDescCopy').style.paddingLeft = copyPosition + "px";
	document.getElementById('socialBarDescCopy').innerHTML = copy;
}

function socialBarDescOff() {
	document.getElementById('socialBarDesc').style.display = "none";
	document.getElementById('socialBarDescBg').style.backgroundPosition = "0 0";
	document.getElementById('socialBarDescCopy').style.paddingLeft = 0;
	document.getElementById('socialBarDescCopy').innerHTML = "";
}
