var currentTab = new Array();
var playing = new Array();
var delayTime = 5000; // seconds*1000=delayTime
var timeOutFunction = new Array();
var maxTabs = new Array();
var galleryMax;

//example: 'popular', 'tab01', 'popular-tab01.jsp'
function tabPress(group, tabId, file) {

	
	var skipDate = true;
	if(!currentTab[group]) {
		currentTab[group] = group + "-tab01";
	}
	
	document.getElementById(currentTab[group]).className = "";
	document.getElementById(group + "-" + tabId).className = "current";
	currentTab[group] = group + "-" + tabId;
	var div = document.getElementById(group + "-tabs");
	if(file=="main-tab01.front" || file=="main-tab02.front" || file=="main-tab03.front"|| file=="main-tab04.front"  || file=="main-tab05.front") {
		skipDate = false;
	}
	
	// Begin AJAX request
	var http_request = false;
    if (window.XMLHttpRequest) { // Mozilla, Safari, ...
       http_request = new XMLHttpRequest();
       if (http_request.overrideMimeType) {
       	  http_request.overrideMimeType('text/xml');
       }
    } 
	else if (window.ActiveXObject) { // IE
      try {
           http_request = new ActiveXObject("Msxml2.XMLHTTP");
      } 
	  catch (e) {
          try {
               http_request = new ActiveXObject("Microsoft.XMLHTTP");
          } 
		  catch (e) {
		  }
      }
    }
    
    if (!http_request) {
       return false;
    }
    http_request.onreadystatechange = function() { printContent(http_request, div, skipDate); };
    
	var now = new Date();
	var timestamp = now.getTime();
	if(file.indexOf("?") == -1 ) {
    	var filenamed = "/modules/mostpopular/" + file + "?time=" + timestamp;
	} else {
		var filenamed = "/modules/mostpopular/" + file + "&time=" + timestamp;
	}
		    	
    http_request.open('GET', filenamed, true);
    http_request.send(null);

}

function printContent(http_request, div, skipDate) {
	if (http_request.readyState == 4) {
        if (http_request.status == 200) {
            div.innerHTML = http_request.responseText;
			if(!skipDate){
				getTheDate("last-updated");
				getTheDate("last-updated-related");
			}
        } else {
            alert('There was a problem with the request.');
        }
    }
}


function playTab(div) {
    if(!playing[div]){
        playing[div] = true;
        slideShow(div);
    }
}

function pauseTab(div) {
    if(playing[div]){
        playing[div] = false;
        clearTimeout(timeOutFunction[div]);
    }
}

function slideShow(div) {
    var finalNextTab;
    var finalNextFile;
	    
    for(var i=1; i<=maxTabs[div]; i++) {
        var thisTabNum = "tab0" + i;
        var thisTab = div + "-" + thisTabNum;
        
        var nextI = i + 1;
        if(nextI > maxTabs[div]) {
            nextI = 1;
        }
        var nextTab = "tab0" + nextI;
        var nextFile = div + "-tab0" + nextI + ".front";
        
        if(currentTab[div] == thisTab){
            finalNextTab = nextTab;
            finalNextFile = nextFile;
        }
    }
    
    tabPress(div, finalNextTab, finalNextFile);
    
    if(playing[div]){
        timeOutFunction[div] = setTimeout('slideShow(\''+div+'\')', delayTime);
    }
}

function setTab(tabId, group, tabs) {
    currentTab[group] = group + "-" + tabId;
	maxTabs[group] = tabs;
}

// divNameRoot = "last-updated" or "last-updated-related"
function getTheDate(divNameRoot) {
    var i=1;
    for (i=1; i<16; i++) {
        var divString = divNameRoot + "-" + i;
        var dateContent =  document.getElementById(divString);
        if(dateContent == null) {
                        break;
        }
        var lastModTime = (new Date(dateContent.innerHTML)).getTime();
        var nowTime = (new Date()).getTime();
        var minutesAgo = Math.round(((nowTime - lastModTime)/1000)/60);
        if (minutesAgo < 60 && minutesAgo > 0) {
            if (minutesAgo <= 1) {
                document.getElementById(divString).innerHTML = '<p class="last-updated">Updated: less than a minute ago<p>';
				document.getElementById(divString).className = "";
            } else {
                document.getElementById(divString).innerHTML = '<p class="last-updated">Updated: ' + minutesAgo + ' minutes ago</p>';
				document.getElementById(divString).className = "";
            }
        }
        else if (minutesAgo <= 180 && minutesAgo > 0){
            var lastModTime = (new Date(dateContent.innerHTML));
            var hours = lastModTime.getHours();
            var minutes = lastModTime.getMinutes();
            var dateString = "";
            if(minutes < 10) {
                minutes = '0' + minutes;
            }
            if(hours == 0) {
                dateString = dateString + '12:' + minutes + ' a.m.';
            }
            else if(hours == 12) {
                dateString = dateString + '12:' + minutes + ' p.m.';
            }
            else if(hours > 12) {
                dateString = dateString + hours-12 + ':' + minutes + ' p.m.';
            }            
            else {
                dateString = dateString + hours + ':' + minutes + ' a.m.';
            }
            document.getElementById(divString).innerHTML = '<p class="last-updated">Updated: ' + dateString + ' </p>';
			document.getElementById(divString).className = "";
        }
        
    }
}

function generateDate(fullDate, shortDate) {
	var lastModTime = (new Date(fullDate)).getTime();
    var nowTime = (new Date()).getTime();
    var minutesAgo = Math.round(((nowTime - lastModTime)/1000)/60);
    if (minutesAgo < 60 && minutesAgo > 0) {
        if (minutesAgo <= 1) {
            document.write('<span style="display:block">Updated: less than a minute ago</span>');
        } else {
            document.write('<span style="display:block">Updated: ' + minutesAgo + ' minutes ago</span>');
        }
    }
	else if (minutesAgo <= 180 && minutesAgo > 0) {
		if(shortDate != "") {	
    		var dateLength = shortDate.length;
    		var shortDateSub = shortDate.substring(0, dateLength-2);
    		    		
    		if (shortDate.substring(dateLength-2) == 'AM')
    		    document.write('<span style="display:block">Updated: ' + shortDateSub + 'a.m.' + '</span>');
    		else
    		    document.write('<span style="display:block">Updated: ' + shortDateSub + 'p.m.' + '</span>');
	    }
	}
}
function generateDateShort(fullDate, shortDate) {
	var lastModTime = (new Date(fullDate)).getTime();
    var nowTime = (new Date()).getTime();
    var minutesAgo = Math.round(((nowTime - lastModTime)/1000)/60);
    if (minutesAgo < 60 && minutesAgo > 0) {
        if (minutesAgo <= 1) {
            document.write('<span class="short-date">less than a min</span>');
        } else {
            document.write('<span class="short-date">' + minutesAgo + ' mins ago</span>');
        }
    }
	else if (minutesAgo <= 180 && minutesAgo > 0) {
		if(shortDate != "") {	
    		var dateLength = shortDate.length;
    		var shortDateSub = shortDate.substring(0, dateLength-2);
    		    		
    		if (shortDate.substring(dateLength-2) == 'AM')
    		    document.write('<span class="short-date">' + shortDateSub + 'a.m.' + '</span>');
    		else
    		    document.write('<span class="short-date">' + shortDateSub + 'p.m.' + '</span>');
	    }
	}
}
//Lead Content Rotating
var currentStory = 0;
var playingRotation = false;
var totalItems = 0;
var rotations = 0;

function storyRotationLoad(itemIndex) {
	
	// Begin AJAX request
	var http_request = false;
    if (window.XMLHttpRequest) { // Mozilla, Safari, ...
       http_request = new XMLHttpRequest();
       if (http_request.overrideMimeType) {
       	  http_request.overrideMimeType('text/xml');
       }
    } 
	else if (window.ActiveXObject) { // IE
      try {
           http_request = new ActiveXObject("Msxml2.XMLHTTP");
      } 
	  catch (e) {
          try {
               http_request = new ActiveXObject("Microsoft.XMLHTTP");
          } 
		  catch (e) {
		  }
      }
    }    
    if (!http_request) {
       return false;
    }	
    http_request.onreadystatechange = function() { printContent(http_request, document.getElementById('module-lead-content'), false); };
    
	var now = new Date();
	var timestamp = now.getTime();
	var filenamed = "/story-rotation.front?index=" + itemIndex + "&time=" + timestamp + "&coll=" + itemColl;
			
    http_request.open('GET', filenamed, true);
    http_request.send(null);
}

function nextStoryRotation() {
	currentStory++;
	if(currentStory > totalItems) {
		currentStory = 1
		rotations+=1;
	}
	storyRotationLoad(currentStory);
}
function previousStoryRotation() {
	currentStory--;
	if(currentStory <= 0) {
		currentStory = totalItems
	}
	storyRotationLoad(currentStory);
}
function rotationPrev() {
	clearTimeout(rotationTimeout);
	previousStoryRotation();
}
function rotationStart(items, collection) {
	totalItems = items;
	itemColl = collection;
	if(currentStory == 1 || currentStory == 0){
		rotations = 0;
	} else {
		rotations = -1;
	}
	if(playingRotation) {
		clearTimeout(rotationTimeout);
	}
	rotateStory();
}
function rotateStory() {
	if(rotations<3){
		nextStoryRotation()
		rotationTimeout = setTimeout('rotateStory()', 10000);
		playingRotation = true;
	}
}
function rotationStop() {
	clearTimeout(rotationTimeout);
}

//functions for promo rotation

var promoMax;

function promoRotationNext(promoIndex, promoColl) {
	promoIndex++;
	if(promoIndex > promoMax) {
		promoIndex = 1;
	}
	promoRotationLoad(promoIndex, promoColl);
}

function promoRotationPrev(promoIndex, promoColl) {
	promoIndex--;
	if(promoIndex <= 0) {
		promoIndex = promoMax;
	}
	promoRotationLoad(promoIndex, promoColl);
}

function promoRotationLoad(promoItemIndex, promoItemColl) {
	
	// Begin AJAX request
	var http_request = false;
    if (window.XMLHttpRequest) { // Mozilla, Safari, ...
       http_request = new XMLHttpRequest();
       if (http_request.overrideMimeType) {
       	  http_request.overrideMimeType('text/xml');
       }
    } 
	else if (window.ActiveXObject) { // IE
      try {
           http_request = new ActiveXObject("Msxml2.XMLHTTP");
      } 
	  catch (e) {
          try {
               http_request = new ActiveXObject("Microsoft.XMLHTTP");
          } 
		  catch (e) {
		  }
      }
    }    
    if (!http_request) {
       return false;
    }	
    http_request.onreadystatechange = function() { printContent(http_request, document.getElementById('module-promo'), false); };
    
	var now = new Date();
	var timestamp = now.getTime();
	var filenamed = "/promo-rotation.front?index=" + promoItemIndex + "&time=" + timestamp + "&coll=" + promoItemColl;
			
    http_request.open('GET', filenamed, true);
    http_request.send(null);
}

//Functions for Photo Gallery Display 

function galleryRotationNext(promoIndex, promoColl) {
	promoIndex++;
	if(promoIndex > galleryMax) {
		promoIndex = 1;
	}
	galleryRotationLoad(promoIndex, promoColl);
}

function galleryRotationPrev(promoIndex, promoColl) {
	promoIndex--;
	if(promoIndex <= 0) {
		promoIndex = galleryMax;
	}
	galleryRotationLoad(promoIndex, promoColl);
}

function galleryRotationLoad(promoItemIndex, promoItemColl) {
	
	// Begin AJAX request
	var http_request = false;
    if (window.XMLHttpRequest) { // Mozilla, Safari, ...
       http_request = new XMLHttpRequest();
       if (http_request.overrideMimeType) {
       	  http_request.overrideMimeType('text/xml');
       }
    } 
	else if (window.ActiveXObject) { // IE
      try {
           http_request = new ActiveXObject("Msxml2.XMLHTTP");
      } 
	  catch (e) {
          try {
               http_request = new ActiveXObject("Microsoft.XMLHTTP");
          } 
		  catch (e) {
		  }
      }
    }    
    if (!http_request) {
       return false;
    }	
    http_request.onreadystatechange = function() { printContent(http_request, document.getElementById('gallery-module'), false); };
    
	var now = new Date();
	var timestamp = now.getTime();
	var filenamed = "/gallery-rotation.front?index=" + promoItemIndex + "&max=" + galleryMax + "&item=" + promoItemColl;
			
    http_request.open('GET', filenamed, true);
    http_request.send(null);
}

//Health Lead Content

function itemRotationLoad(itemIndex) {
	
	// Begin AJAX request
	var http_request = false;
    if (window.XMLHttpRequest) { // Mozilla, Safari, ...
       http_request = new XMLHttpRequest();
       if (http_request.overrideMimeType) {
       	  http_request.overrideMimeType('text/xml');
       }
    } 
	else if (window.ActiveXObject) { // IE
      try {
           http_request = new ActiveXObject("Msxml2.XMLHTTP");
      } 
	  catch (e) {
          try {
               http_request = new ActiveXObject("Microsoft.XMLHTTP");
          } 
		  catch (e) {
		  }
      }
    }    
    if (!http_request) {
       return false;
    }	
    http_request.onreadystatechange = function() { printContent(http_request, document.getElementById('lead-content-single-item'), false); applyImagesToLeadContent(itemIndex); };
    
	var filenamed = "/health-lead-content-1.front?index=" + itemIndex + "&coll=" + itemColl + "&limit=" + healthLeadCount;
			
    http_request.open('GET', filenamed, true);
    http_request.send(null);
	
	
}

function nextItemRotation() {
	currentStory++;
	if(currentStory > totalLeadItems) {
		currentStory = 1
		rotations+=1;
	}
	itemRotationLoad(currentStory);
}
function previousItemRotation() {
	currentStory--;
	if(currentStory <= 0) {
		currentStory = totalLeadItems
	}
	itemRotationLoad(currentStory);
}
function itemRotationPrev() {
	clearTimeout(rotationTimeout);
	previousItemRotation();
}
function itemRotationStart(collection, leadItemCount) {
	totalLeadItems = leadItemCount;
	itemColl = collection;
	healthLeadCount = leadItemCount;
	if(currentStory == 1 || currentStory == 0){
		rotations = 0;
	} else {
		rotations = -1;
	}
	if(playingRotation) {
		clearTimeout(rotationTimeout);
	}
	rotateItem();
}
function rotateItem() {
	if(rotations<3){
		nextItemRotation()
		rotationTimeout = setTimeout('rotateItem()', 10000);
		playingRotation = true;
	}
}

function gotoLeadItem(collection) {
	collCode = collection;
	clearTimeout(rotationTimeout);
	itemRotationLoad(collCode);
}

function applyImagesToLeadContent(imageIndex) {
	theClass = "slide-" + imageIndex;
	var controlDiv = document.getElementById('lead-content-controls');
	linkTags = controlDiv.getElementsByTagName('a');
	for (i=0; i<linkTags.length; i++) {
		if (linkTags[i].className==theClass) {
			linkTags[i].className='lead-slide-index-active';
		}
	}
}
		


function popUp(URL) {
	day = new Date();
	id = day.getTime();
	eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=1,scrollbars=1,location=1,statusbar=1,menubar=1,resizable=1,width=1024,height=768,left = 0,top = 0');");
}
function playRelatedVideo(url) {
	var iFrame = document.getElementById('worldnowFrame526x375');
	if(iFrame != null) {
		if(url.indexOf("?") == -1 ) {
   			iFrame.src = url + "?size=526x389";
		} else {
			iFrame.src = url + "&size=526x389";
		}
		return false;
	} else {
		iFrame = document.getElementById('worldnowFrame300x665');
		if(iFrame != null ) {
			if(url.indexOf("?") == -1 ) {
    			iFrame.src = url + "?size=300x665";
			} else {
				iFrame.src = url + "&size=300x665";
			}
			return false;
		} else {
			return true;
		}
	}
}
function playRelatedVideoSmall(url) {
	var iFrame = document.getElementById('worldnowFrame300x265');
	if(iFrame != null) {
		if(url.indexOf("?") == -1 ) {
   			iFrame.src = url + "?size=300x265";
		} else {
			iFrame.src = url + "&size=300x265";
		}
		return false;
	} 
}
function clickOmni(tabId) {

		    var s=s_gi('tribsunsentinel');
		    s.linkTrackVars='eVar23,server';
		    s.eVar23='Sun-Sentinel.com:' + tabId;
		    s.server='sun-sentinel.com';
		    s.tl(true,'o', tabId );
}
function reSort(x){
	if (document.sort_form.jumpmenu.value != "null") {
		document.location.href = x
	}
}
//GET STYLE to retrieve CSS Values
// The regular version
function getStyle(oElm, strCssRule){
	var strValue = "";
	if(document.defaultView && document.defaultView.getComputedStyle){
		strValue = document.defaultView.getComputedStyle(oElm, "").getPropertyValue(strCssRule);
	}
	else if(oElm.currentStyle){
		strCssRule = strCssRule.replace(/\-(\w)/g, function (strMatch, p1){
			return p1.toUpperCase();
		});
		strValue = oElm.currentStyle[strCssRule];
	}
	return strValue;
}

// The version if you expect any IE 5.0 users whatsoever
function getStyle(oElm, strCssRule){
	var strValue = "";
	if(document.defaultView && document.defaultView.getComputedStyle){
		strValue = document.defaultView.getComputedStyle(oElm, "").getPropertyValue(strCssRule);
	}
	else if(oElm.currentStyle){
		try{
			strCssRule = strCssRule.replace(/\-(\w)/g, function (strMatch, p1){
				return p1.toUpperCase();
			});
			strValue = oElm.currentStyle[strCssRule];
		}
		catch(e){
			// Used to prevent an error in IE 5.0
		}
	}
	return strValue;
}