


/***********************************************

* Switch Content script- © Dynamic Drive (www.dynamicdrive.com)

* This notice must stay intact for legal use. Last updated April 2nd, 2005.

* Visit http://www.dynamicdrive.com/ for full source code

***********************************************/

/*
	class names:  switchcontent|showstate|groupstate
	ids: prefixed by owneer
	
	example tree 
	->  id: foo  class:groupstate onclick: hacks(this)
	  -> id: foo_bar class:groupstate onclick: hacks(this)
	    -> id:  foo_bar_1 class:showstate onclick: hacks(this)
	      -> id:  foo_bar_1_content class:switchcontent
*/

function isdefined( variable)
{
    return (typeof(window[variable]) == "undefined")?  false: true;
}

var enablepersist="off" //Enable saving state of content structure using session cookies? (on/off)

var collapseprevious="no" //Collapse previously open content when opening present? (yes/no)

//find out if browser is IE
var agt=navigator.userAgent.toLowerCase();
var is_ie = (agt.indexOf("msie") != -1);
var is_opera = (agt.indexOf("Opera") == -1);
var is_nav = ((agt.indexOf('mozilla')!=-1) && (agt.indexOf('spoofer')==-1) && (agt.indexOf('compatible') == -1) && (agt.indexOf('opera')==-1) && (agt.indexOf('webtv')==-1) && (agt.indexOf('hotjava')==-1));

// define these vars in the page so they can be different by page
//var groupexpandsymbol = '<img src="images/minus_all_ty.png">'
//var groupcontractsymbol = '<img src="images/plus_all_ty.png">'
if(!window.groupexpandsymbol)
{
	if(is_ie)
		groupexpandsymbol = '<IMG src="http://www.frontalot.com/_index.php/images/minus_all_ty.png">'
	else if (is_nav)
		groupexpandsymbol = '<img src="/images/minus_all_ty.png">'
	else if (is_opera)
		groupexpandsymbol = '<IMG src="/images/minus_all_ty.png"/>'
	else
		groupexpandsymbol = '<img src="/images/minus_all_ty.png">'
}
	
if(!window.groupcontractsymbol)
{
	if(is_ie)
		groupcontractsymbol = '<IMG src="http://www.frontalot.com/_index.php/images/plus_all_ty.png">'
	else if (is_nav)
		groupcontractsymbol = '<img src="/images/plus_all_ty.png">'
	else if (is_opera)
		groupcontractsymbol = '<IMG src="/images/plus_all_ty.png"/>'
	else
		groupcontractsymbol = '<img src="/images/plus_all_ty.png">'
}


//var contractsymbol='-' //HTML for contract symbol. For image, use: <img src="whatever.gif">

//var expandsymbol='+' //HTML for expand symbol.

var contractsymbol='<img src="/images/-.png" width="27" height="11" border="0">' //HTML for contract symbol. For image, use: <img src="whatever.gif">

var expandsymbol='<img src="/images/+.png" width="27" height="11" border="0">' //HTML for expand symbol.

var submissionform;

//if getElementyById is supported, insert the switchcontent css class

if (document.getElementById){

	document.write('<style type="text/css">')

	document.write('.switchcontent{display:none;}')

	document.write('</style>')

}


//search through the rootobj array and return an array containing every element 

//of rootobj that is of class classname

	function getElementbyClass(rootobj, classname){

	var temparray=new Array()

	var inc=0

	var rootlength=rootobj.length

	for (i=0; i<rootlength; i++){

		if (rootobj[i].className==classname)

			temparray[inc++]=rootobj[i]

	}

	return temparray

}
function voteSubmit(form,v)
{
    var originaloldvote = form.vote.value;
    var elements = document.getElementsByName('vote'+form.rating_id.value);
    for(i=0; i<elements.length; i++){
        elements[i].oldvote.value=originaloldvote;
        elements[i].vote.value = v
    }
	var url = 'ajaxy.php';
	var vars = 'rating_id='+form.rating_id.value+'&vote='+v;
	submissionform = form;
	var request = new Ajax.Request(url, {method: 'post', postBody: vars, onSuccess:ajaxCallSuccess, onFailure:ajaxCallFailure});
	return false;
}


function ajaxCallSuccess(t){
	// Change the page around to reflect the submitted vote
	if(t.responseText != 'failure'){
		var id = submissionform.rating_id.value;
		var old = id+"vote"+submissionform.oldvote.value;
		var vote = id+"vote"+submissionform.vote.value;
        if(submissionform.oldvote.value != 4 && submissionform.oldvote.value != 0){
    		var elements = document.getElementsByName(old);
            for(i=0; i< elements.length; i++){
                 elements[i].className = "vote";
            }
        }
        if(submissionform.vote.value != 4 && submissionform.vote.value != 0){
    		var elements = document.getElementsByName(vote);
            for(i=0; i< elements.length; i++){
                 elements[i].className = "votelit";
            }        
        }
        
        var elements = document.getElementsByName('score'+id);
        for(i=0; i<elements.length; i++){
            elements[i].innerHTML = t.responseText;
        }
	}else{
        submissionform.submit();
    }
}

function ajaxCallFailure(t){
	submissionform.submit();
}

function getElementbyPrefix(rootobj, classname){
	var temparray=new Array()
	var inc=0
	var rootlength=rootobj.length
	var strlen = classname.length
	
	for (i=0; i<rootlength; i++){
		
		var sub = rootobj[i].id.substring(0,strlen)
		if (sub==classname)

			temparray[inc++]=rootobj[i]

	}

	return temparray

}



//make all switchcontent objects either expand or contract as specified by parameter

function sweeptoggle(ec){

	var thestate=(ec=="expand")? "block" : "none"

	var inc=0

	while (ccollect[inc]){

		ccollect[inc].style.display=thestate

		inc++

	}

	revivestatus()

}



//contract everything that doesn't have a specified id

function contractcontent(omit){

	var inc=0

	while (ccollect[inc]){

		if (ccollect[inc].id!=omit)

			ccollect[inc].style.display="none"

		inc++

	}

}

// Shorthand for getElementById
function ge(elementid) 
{
  return document.getElementById(elementid);
}

function hacks(master_elem)
{
	var group_name = master_elem.id
	var spantags=master_elem.getElementsByTagName("SPAN")
	var showstateobj=getElementbyClass(spantags, "groupstate")
	

	var nestedgroups = getElementbyPrefix(groupcollect,group_name)
	var groupcontent = getElementbyPrefix(ccollect,group_name)
	var groupheaders = getElementbyPrefix(statecollect,group_name)
	
	var doexpand = showstateobj[0].innerHTML== groupexpandsymbol 
	
	var i = 0;
	
	// go through all sub-group headers and activate them
	while(i < nestedgroups.length)
	{
		nestedgroups[i].innerHTML= doexpand ? groupcontractsymbol : groupexpandsymbol		
		i++
	}
	i = 0;
	// activate anyone you own directly
  while(i < groupheaders.length)
  {  	
  	groupcontent[i].style.display = doexpand ? "block" : "none";
  	groupheaders[i].innerHTML = doexpand ? contractsymbol : expandsymbol
		i++;
	}
	
	showstateobj[0].innerHTML= doexpand ? groupcontractsymbol : groupexpandsymbol
	//revivestatus()
}

//expand the specified id, and collapse all others if neccesary

function expandcontent(curobj, cid){
	var spantags=curobj.getElementsByTagName("SPAN")

	var showstateobj=getElementbyClass(spantags, "showstate")

	if (ccollect.length>0){

		//if collapseprevious is yes, collapse all except for this one

		if (collapseprevious=="yes")

			contractcontent(cid)

		

		//switches the current element's expand/contract state

		document.getElementById(cid).style.display=(document.getElementById(cid).style.display!="block")? "block" : "none"

		

		//if "showstate" span exists in header, display the appropriate text/images

		if (showstateobj.length>0){ 

			if (collapseprevious=="no")

				showstateobj[0].innerHTML=(document.getElementById(cid).style.display=="block")? contractsymbol : expandsymbol

			else

				revivestatus()

		}

	}

}

//contracts everything and displays on selected items

function revivecontent(){

	contractcontent("omitnothing")

	selectedItem=getselectedItem()

	selectedComponents=selectedItem.split("|")

	for (i=0; i<selectedComponents.length-1; i++)

		document.getElementById(selectedComponents[i]).style.display="block"

}



//iterate through all showstate elements and show the appropriate state text/image

function revivestatus(){

	var inc=0

	while (statecollect[inc]){

		if (ccollect[inc].style.display=="block")

			statecollect[inc].innerHTML=contractsymbol

		else

			statecollect[inc].innerHTML=expandsymbol

		inc++

	}
	inc = 0
	
	while(groupcollect[inc])
	{
		if(groupcollect[inc])
			groupcollect[inc].innerHTML = groupexpandsymbol
		inc++
	}

}



//returns the value of a given cookie

function get_cookie(Name) {

	var search = Name + "="

	var returnvalue = "";

	if (document.cookie.length > 0) {

		offset = document.cookie.indexOf(search)

		if (offset != -1) {

			offset += search.length

			end = document.cookie.indexOf(";", offset);

			if (end == -1) end = document.cookie.length;

				returnvalue=unescape(document.cookie.substring(offset, end))

		}

	}

	return returnvalue;

}



//return all cookies if there are any

function getselectedItem(){

	if (get_cookie(window.location.pathname) != ""){

		selectedItem=get_cookie(window.location.pathname)

		return selectedItem

	}

	else

		return ""

}



//copy currently openned ids to a cookie and paste them into the url

function saveswitchstate(){

	var inc=0, selectedItem=""

	while (ccollect[inc]){

		if (ccollect[inc].style.display=="block")

			selectedItem+=ccollect[inc].id+"|"

		inc++

	}



	document.cookie=window.location.pathname+"="+selectedItem

}



//load everything

function do_onload(){

	uniqueidn=window.location.pathname+"firsttimeload"

	var alltags=document.all? document.all : document.getElementsByTagName("*")

	ccollect=getElementbyClass(alltags, "switchcontent")

	statecollect=getElementbyClass(alltags, "showstate")
	
	groupcollect = getElementbyClass(alltags, "groupstate")
	//alert("foo" + statecollect.length)

	if (enablepersist=="on" && ccollect.length>0){

		document.cookie=(get_cookie(uniqueidn)=="")? uniqueidn+"=1" : uniqueidn+"=0"

		firsttimeload=(get_cookie(uniqueidn)==1)? 1 : 0 //check if this is 1st page load

		if (!firsttimeload)

			revivecontent()

	}

	if (ccollect.length>0 && statecollect.length>0)
		
		revivestatus()
	
}






if (window.addEventListener)

	window.addEventListener("load", do_onload, false)

else if (window.attachEvent)

	window.attachEvent("onload", do_onload)

else if (document.getElementById)

	window.onload=do_onload



if (enablepersist=="on" && document.getElementById)

window.onunload=saveswitchstate

