// Shows or Hides a DIV
function showHideDiv(name,state){

	// get DIV object
	var divTarget = document.getElementById(name);

	// Set State
	if (state == 'hide'){
	   divTarget.style.visibility = 'hidden';
	   divTarget.style.display = 'none';
	} else {
	   divTarget.style.visibility = 'visible';
	   divTarget.style.display = 'block';
	}
}


// Check if Querystring attributes are present
// During Page Navigation
function checkPageNav(variable, valAry) {
	
	// Return Value
	var ret = false;
	
	// Evaluate Array
	var i = 0;
	for (i==0;i<=valAry.length - 1;i++){
			
		// Get value
		var qs = getQueryVariable(variable);
		
	
		// Compare Value
		if (valAry[i] == qs){
			ret = true;
			break;
		}
	}

	
	// Return
	return ret;
}


//Request.QueryString for JavaScript
function getQueryVariable(variable) {
	var query = window.location.search.substring(1);
   	var vars = query.split("&");
   	for (var i=0;i<vars.length;i++) {
    	var pair = vars[i].split("=");
     	if (pair[0] == variable) {
       		return pair[1];
     	}
   	} 
}


// Adds Javascript or CSS to HEAD
function loadjscssfile(filename, filetype){
	if (filetype=="js"){ //if filename is a external JavaScript file
	var fileref=document.createElement('script')
	fileref.setAttribute("type","text/javascript")
	fileref.setAttribute("src", filename)
}
else if (filetype=="css"){ //if filename is an external CSS file
	var fileref=document.createElement("link")
	fileref.setAttribute("rel", "stylesheet")
	fileref.setAttribute("type", "text/css")
	fileref.setAttribute("href", filename)
}
if (typeof fileref!="undefined")
	document.getElementsByTagName("head")[0].appendChild(fileref)
}


// Load CSS at runtime for Chat Module
function loadChatCSS(){
	var qs = getQueryVariable("style");
	
	// Evaluate
	if (qs == "1"){
		loadjscssfile("css/vBulletin.css","css");
	}
	else {
		loadjscssfile("css/whosonline.css","css");
	}
}


// Replaces a div with iFrame src content
function iFrameInjection(divtarget,iFrameSrc) { 
	var _y = document.getElementById(divtarget); 
	var _x = document.getElementById(iFrameSrc).contentWindow.document.body.innerHTML;
	//var _x = window.frames[0].document.body.innerHTML; 
	_y.innerHTML = _x; 
} 


// Switches Daily Heroes Image
function switchRandomDisplay(type, valAry){
	
	// Return value
	var ret;
	
	// Get value
	var newDate = new Date();
	var itemNum = newDate.getMilliseconds() % (valAry.length);
	
	// Leading URl
	if (type == "image"){
		var url = "http://www.wmdgaming.com/forums/projectferret/images/";
		ret = url + valAry[itemNum];
	} 
	else {
		ret = valAry[itemNum];
	}
	
	// Output
	return ret;
}


// Get All Images
// Preload Upload Images
function getAllImages(){
	if (document.images) {

		var iMaxWidth = 780;
		var i = 0;
		for (i = 0; i < document.images.length; i++) {
			var temp_val = document.images[i].src;
			
			// Process images that are:
			// Not from WMD Gaming
			// From the WMD Gaming Image Hosting
			if ((temp_val.indexOf("wmdgaming.com/forums/") == -1) ||
				(temp_val.indexOf("wmdgaming.com/forums/imagehosting/") != -1)){
				
				// Scale Image
				scaleImages(document.images[i], iMaxWidth);
			}
		}
	}
}


// Scale Images
function scaleImages(imageobj, iMaxWidth){
	var iWidth = imageobj.width;
	var iHeight = imageobj.height;
				
	// Evaluate
	if (iWidth > iMaxWidth){
		var iRatio = iMaxWidth / iWidth;
						
		// Set Width / Height
		iWidth = iMaxWidth;
		iHeight = (iHeight * iRatio);
		imageobj.width = iWidth;
		imageobj.height = iHeight;
		imageobj.alt = "Your image may not be displayed properly because we only support a max width of 720px!";
	}
}


// Checks date against 3 days before 
// and after today
function dateCheck(testDate, iUnder, iOver){
	
	// Today
	var curDate = new Date();
	var year = curDate.getFullYear();
	var month = curDate.getMonth();
	var day = curDate.getDate();
	var curDateUTC = Date.UTC(year, (month + 1), day);

	// One Day
	var iDay = 86400000;
			
	// Test Date
	var testSplit = testDate.split(".");
	var testMonth = testSplit[0];
	var testDay = testSplit[1]; 		
	var checkDate = Date.UTC(year, testMonth, testDay);

	// Early Date (-X Days) / Late Date (+Y Days)
	var earlyDate = checkDate - (iUnder * iDay);
	var lateDate = checkDate + (iOver * iDay);	
	
	// Check Range
	if ((curDateUTC >= earlyDate) && (curDateUTC < lateDate)){
		return true
	}
	// Next Year (January Only)
	else if (testMonth == 1){
		var newCheckDate = Date.UTC(year + 1, (month + 1),  day);
		if ((newCheckDate >= earlyDate) && (newCheckDate <= lateDate)){
			return true
		}
		else {
			return false;
		}
	}
	else {
		return false;
	}
}


// Retrieves header image based on date
function getHeaderImage(){

	// Default header image
	var headerImage = "bg_front";
	
	// Array of dates
	// each value is an array containing:
	// Date (mm:dd) and corresponding header CSS path
	var newArray = [
						[10.31,"bg_front_halloween",5,1],
						[11.26,"bg_front_thankgiving_us",5,1],
						[12.25,"bg_front_christmas",5,7],
						[1.1,"bg_front_newyears",2,1]
					];
	
	// Loop through Events			
	for(i=0; i < newArray.length; i++){
		var newDate = String(newArray[i]);
		var newDateAry = newDate.split(",");
		
		var testDate = newDateAry[0];
		var iUnder = parseInt(newDateAry[2]);
		var iOver = parseInt(newDateAry[3]);
		var results = dateCheck(testDate,iUnder,iOver);
		
		if (results == true){
			headerImage = newDateAry[1];
			break;
		}
	}
	
	// Return image path
	return headerImage;
}