
// holds an instance of XMLHttpRequest
var xmlHttp = createXmlHttpRequestObject();


// creates an XMLHttpRequest instance
function createXmlHttpRequestObject() 
{
  // will store the reference to the XMLHttpRequest object
  var xmlHttp;
  // this should work for all browsers except IE6 and older
  try
  {
    // try to create XMLHttpRequest object
    xmlHttp = new XMLHttpRequest();
  }
  catch(e)
  {
    // assume IE6 or older
    var XmlHttpVersions = new Array("MSXML2.XMLHTTP.6.0",
                                    "MSXML2.XMLHTTP.5.0",
                                    "MSXML2.XMLHTTP.4.0",
                                    "MSXML2.XMLHTTP.3.0",
                                    "MSXML2.XMLHTTP",
                                    "Microsoft.XMLHTTP");
    // try every prog id until one works
    for (var i=0; i<XmlHttpVersions.length && !xmlHttp; i++) 
    {
      try 
      { 
        // try to create XMLHttpRequest object
        xmlHttp = new ActiveXObject(XmlHttpVersions[i]);
      } 
      catch (e) {}
    }
  }
  // return the created object or display an error message
  if (!xmlHttp)
    {
     //alert("Error creating the XMLHttpRequest object.");
     return;
    }
  else 
    {//alert("successfully created XMLHttp object");
    return xmlHttp;
    }
}


/* VF MAIN PAGE SCRIPTS FILE */


//on window load initialize blurlinks function:
//window.onload = blurLinks;
blurLinks();



/******************************************************
* function to blur clicked links and remove the 
* annoying little dots around them
*******************************************************/
function blurLinks() {
	var links = document.getElementsByTagName('a');
	for (var i = 0; i < links.length; i++) { 
		currentLink = links[i];
		currentLink.onfocus = function() { this.blur(); };
	}
}


/******************************************************
* function to toggle righthand menus on and off 
* lights up background and displays / hides submenus
*******************************************************/
function activateRightMenu(thelink, menuid) {

	// switch on / off the associated submenu
	showHide(menuid);
	
	// change the classname of the clicked link 
	if (thelink.className == "rightbtn") { thelink.className = "rightbtn_on"; }
	else { thelink.className = "rightbtn"; }
		
}


/******************************************************
* ElectricStorm's function to set VF as your homepage 
* (nb this uses browserdetector.js) 
*******************************************************/
function setVFHome() {
	
	var theUrl = 'http://vampirefreaks.com';
	var msgStart = 'To set VF as your homepage go to'
	var msgEnd = 'and type ' + theUrl + ' into the Home page box.';
	
	//Internet Explorer:
	if (BrowserDetect.browser == 'Explorer') { 
		//if IE 5/6: 
 		if (BrowserDetect.version >= '5' && BrowserDetect.version <= '6') { 
			//set automatically (only these versions support):
			this.style.behavior='url(#default#homepage)';
			this.setHomePage(theUrl);
		} 
		else { 
			//for other versions of IE just provide instructions: 
			alert(msgStart + ' Tools > Internet Options ' + msgEnd);
		}
	} 
	//FireFox: 
	else if (BrowserDetect.browser == 'Firefox') { alert(msgStart + ' Tools > Options ' + msgEnd); }
	//Opera: 
	else if (BrowserDetect.browser == 'Opera') { alert(msgStart + ' Tools > Preferences ' + msgEnd); }
	//Netscape: 
	else if (BrowserDetect.browser == 'Netscape') { alert(msgStart + ' Tools > Internet Options ' + msgEnd); }
	//Safari: 
	else if (BrowserDetect.browser == 'Safari') { alert(msgStart + ' Edit > Preferences ' + msgEnd); }
	//Browser Unknown:
	else { alert(msgStart + ' your internet preferences ' + msgEnd); }	
	
}

var current_content = "newestmembers";
swapper_interval = setInterval( "swapper()", 10000);
setTimeout( "clearInterval("+swapper_interval+");", 800000 );

function swapper(){
	//alert("in swapper");
	if (current_content=="newestmembers"){
		swapLatestStuff("models");
	} else if (current_content=="models"){
	//	swapLatestStuff("toppics")
	//} else if (current_content=="toppics"){
		swapLatestStuff("newestpics");
	} else if (current_content=="newestpics"){
		swapLatestStuff("newestvideos");
	} else if (current_content=="newestvideos"){
		swapLatestStuff("latestforumposts");
	} else if (current_content=="latestforumposts"){
		swapLatestStuff("newestmembers");
	}

}

/******************************************************
* function to swap lateststuff box content 
* swap between forum posts / members / pics / videos
*******************************************************/
function swapLatestStuff(swapto) {
	
	clearInterval(swapper_interval);
	swapper_interval = setInterval( "swapper()", 30000);	
	
	//global current_content;
	current_content = swapto;
		
                  if (xmlHttp)
                  {        
                                var get_page =  "http://vampirefreaks.com/ajax/mainpage_content.php?cache_page="+swapto;
                                xmlHttp.open("GET", get_page, true);
                                if(swapto == "newestpics"){
                                    xmlHttp.onreadystatechange = handleNewestPics; 
                                }
                                else if (swapto == "newestvideos"){
                                    xmlHttp.onreadystatechange = handleNewestVideos;
                                }   
                                else if (swapto == "newestmembers"){
                                    xmlHttp.onreadystatechange = handleRandomMembers;
                                }       
                                else if (swapto == "latestforumposts"){
                                    xmlHttp.onreadystatechange = handleForumPosts;
                                }              
                                else if (swapto == "toppics"){
                                    xmlHttp.onreadystatechange = handleTopPics;
                                }                                                                                                                      
                                else if (swapto == "models"){
                                    xmlHttp.onreadystatechange = handleModels;
                                }  
                                xmlHttp.send(null);
                  }   
	// make array of the 4 possible content boxes to show / hide:
	var contentBoxes = new Array('latestforumposts', 'newestmembers', 'newestpics', 'newestvideos', 'models'); 

	// if swapto param is in the array...
	if (arraySearch(contentBoxes, swapto) !== false) { 
	
		// activate swapStuff function to swap the content box, and determine which link shows as plain text:
		swapStuff(contentBoxes, swapto); 		
		
		// fetch box title (top) and more link (bottom) so we can change them:
		var latestWhat = document.getElementById('latestwhat');
		var moreLatestStuff = document.getElementById('morelateststuff');				

		//change box title and more link:	
		if (swapto == 'newestmembers') {
			latestWhat.innerHTML = 'NOW ONLINE';
			moreLatestStuff.innerHTML = '<a href="http://vampirefreaks.com/gothic_users.php" title="view more users online">MORE USERS ONLINE <span class="ninept">&raquo;</span></a>';
		} 
		else if (swapto == 'newestpics') {
			latestWhat.innerHTML = 'NEWEST PICTURES';
			moreLatestStuff.innerHTML = '<a href="http://vampirefreaks.com/newest_pics.php" title="view more newest pictures">MORE NEWEST PICS <span class="ninept">&raquo;</span></a>';
		} 
		else if (swapto == 'newestvideos') {
			latestWhat.innerHTML = 'NEWEST VIDEOS';
			moreLatestStuff.innerHTML = '<a href="http://vampirefreaks.com/video/lists.php&#63;pg=1&amp;sortby=date_added&amp;genre=0" title="view more newest videos">MORE NEWEST VIDEOS <span class="ninept">&raquo;</span></a>';
		}
        else if (swapto == 'toppics') {
            latestWhat.innerHTML = 'TOP PICS';
            moreLatestStuff.innerHTML = '<a href="http://vampirefreaks.com/pictures/props.php" title="top pictures this week">MORE TOP PICTURES <span class="ninept">&raquo;</span></a>';
        }   				
        else if (swapto == 'models') {
            latestWhat.innerHTML = 'MODELS';
            moreLatestStuff.innerHTML = '<a href="http://vampirefreaks.com/models.php" title="vampirefreaks models">MORE MODELS <span class="ninept">&raquo;</span></a>';
        }   
		else {
			latestWhat.innerHTML = 'HOT FORUM POSTS';
			moreLatestStuff.innerHTML = '<a href="http://vampirefreaks.com/forum/" title="view more forum posts">MORE FORUM POSTS <span class="ninept">&raquo;</span></a>';
		}
		
	}
	
}


/******************************************************
* function to swap musicstore box content 
* swap between top albums / top songs / new releases
*******************************************************/
function swapMusicStore(swapto) {


                  if (xmlHttp && swapto=="newreleases")
                  {        
                                var get_page =  "http://vampirefreaks.com/ajax/mainpage_content.php?cache_page="+swapto;
                                xmlHttp.open("GET", get_page, true);
                                xmlHttp.onreadystatechange = handleNewReleases; 

                                                                                                 
                                xmlHttp.send(null);
                  } 
                  
                  
	// make array of the 3 possible content boxes to show / hide:
	var contentBoxes = new Array('topalbums',  'newreleases'); 

	// if swapto param is in the array...
	if (arraySearch(contentBoxes, swapto) !== false) { 
		// activate swapStuff function to swap the content box, and determine which link shows as plain text:
		swapStuff(contentBoxes, swapto); 			
	}

}


/***********************************************************
* function to swap theme thumbnail when link moused over
************************************************************/
function swapThemeThumb(swapto) {
			
	// make array of the possible themes:	
	var themes = new Array('orange', 'red', 'green', 'blue', 'pink', 'silver', 'yellow', 'purple', 'greyonwhite', 'oldskool', 'clean'); 

	//check given theme is valid:
	if (arraySearch(themes, swapto) !== false) { 
	
		//get the themepreview div and swap it's background:
		var previewDiv = document.getElementById('themepreview');
		previewDiv.style.background = "url('http://e.vampirefreaks.com/grafix/themepreviews/" + swapto + ".gif')";
	}

}


/******************************************************
* miscellaneous functions used by other functions 
*******************************************************/

// array search function to see if a value is in an array:
function arraySearch(thearray, val) {
	for (var i = 0; i < thearray.length; i++) {
		if (thearray[i] == val) { return i; }
	}
	return false;
}


// function to show / hide elements by id:
function showHide(element) {
	
	if (document.getElementById) { 
		var theItem = document.getElementById(element);
        if (theItem.style.display == "") { theItem.style.display = "none"; }
        else {theItem.style.display = ""; }		
	}

}

	
// function to swap around visibility of content boxes, links & their matching plain text spans:
function swapStuff(theArray, swapto) {  
                  	
	// hide all other content boxes and show swapto one 
	// show links to all other content boxes, and plain text for swapto one:
	for (var i = 0; i < theArray.length; i++) {
		
		// get content box: 
		var thisBoxId = theArray[i];
		var thisBox = document.getElementById(thisBoxId);

		// get link: 
		var thisLinkId = theArray[i] + 'link';
		var thisLink = document.getElementById(thisLinkId);
		
		// get plain text span:
		var thisTextId = theArray[i] + 'text';
		var thisText = document.getElementById(thisTextId);
		
		// ...and swap around what shows:
		if (thisBoxId != swapto) { 
			thisBox.style.display = 'none'; 
			thisLink.style.display = ''; 
			thisText.style.display = 'none'; 
		}
		else { 
			thisBox.style.display = ''; 
			thisLink.style.display = 'none'; 
			thisText.style.display = ''; 			
		}
		
	}
}



function handleNewestPics() {

    if (xmlHttp.readyState == 4) {
      document.getElementById("newestpics").innerHTML = xmlHttp.responseText;
    }
}

function handleNewestVideos() {

    if (xmlHttp.readyState == 4) {
      document.getElementById("newestvideos").innerHTML = xmlHttp.responseText;
    }
}

function handleRandomMembers() {

    if (xmlHttp.readyState == 4) {
      document.getElementById("newestmembers").innerHTML = xmlHttp.responseText;
    }
}

function handleForumPosts() {
    if (xmlHttp.readyState == 4) {
      document.getElementById("latestforumposts").innerHTML = xmlHttp.responseText;
    }
}

function handleNewReleases() {
    if (xmlHttp.readyState == 4) {
      document.getElementById("newreleases").innerHTML = xmlHttp.responseText;
    }
}

function handleTopPics() {
    if (xmlHttp.readyState == 4) {
      document.getElementById("toppics").innerHTML = xmlHttp.responseText;
    }
}

function handleModels() {
    if (xmlHttp.readyState == 4) {
      document.getElementById("models").innerHTML = xmlHttp.responseText;
    }
}


