


// Site specific javascript documnent - contains Style swithcer (needs to be above left nav code), Left Nav and Popup code

////////////////////////////////////////////////////////////
//   STYLE SWITCHER
////////////////////////////////////////////////////////////

function setActiveStyleSheet(title) {
  var i, a, main;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
      a.disabled = true;
      if(a.getAttribute("title") == title) a.disabled = false;
    }
  }
}

function getActiveStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && !a.disabled) return a.getAttribute("title");
  }
  return null;
}

function getPreferredStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1
       && a.getAttribute("rel").indexOf("alt") == -1
       && a.getAttribute("title")
       ) return a.getAttribute("title");
  }
  return null;
}

function createCookie(name,value,days) {
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
  }
  else expires = "";
  document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  }
  return null;
}

window.onload = function(e) {
  var cookie = readCookie("style");
  var title = cookie ? cookie : getPreferredStyleSheet();
  setActiveStyleSheet(title);
}

window.onunload = function(e) {
  var title = getActiveStyleSheet();
  createCookie("style", title, 365);
}

var cookie = readCookie("style");
var title = cookie ? cookie : getPreferredStyleSheet();
setActiveStyleSheet(title);





$(document).ready(function(){

	$("body").removeClass("noJs");
	$("body").addClass("jsEnabled");
	
	 if ($('span#noFlashPresent').length) {  
			$('#noflash').show();
	}
	
	$("li.textOnlyLink a").click(function(){
		$( 'html, body' ).animate( { scrollTop: 0 }, 'slow' );
		return false;
	});

});





////////////////////////////////////////////////////////////
//   LEFT NAV JS
////////////////////////////////////////////////////////////
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];
		}
	} 
}

function createLeftNav() {
	
	var nav = getQueryVariable('nav');
	
	if (nav != undefined) {
		var col = document.body.className;
		var navArr = nav.split(',');
		var pathNavContainer = document.getElementById('pathNavContainer');
		var navStart;
		var navMain;


///////////////// GLOBAL CODE BLOCKS////////////
// LEVEL 0
var navHome = '<li id="level0_home" class="level"><a href="../index.html" title="Back to the homepage" accesskey="1">Back to the homepage</a></li>';

//LEVEL 1
var navConcerned = '<li id="level1_concerned_about_someone" class="level"><a href="../index.html#/concerned_about_someone/" title="Concerned about someone?" accesskey="2">Concerned about someone?</a></li>';
var navDoneSomething = '<li id="level1_done_something" class="level"><a href="../index.html#/done_something/" title="Done something you\'re worried about" accesskey="2">Done something you\'re worried about</a></li>';
var navGettingPressure = '<li id="level1_getting_pressure" class="level"><a href="../index.html#/getting_pressure_from_your_mates/" title="Getting pressure from your friends?" accesskey="2">Getting pressure from your friends?</a></li>';
var navBeenVictim = '<li id="level1_been_a_victim" class="level"><a href="../index.html#/been_a_victim_and_need_some_help/" title="Been a victim and need some help?" accesskey="2">Been a victim and need some help?</a></li>';

/////////////////////////////////////////////////////////
// LEVEL 2
var navMate = '<li id="level2_mate" class="level"><a href="../index.html#/concerned_about_someone/a_mate/" title="Concerned for a friend?" accesskey="3">Concerned for a friend?</a></li>';
var navFamily = '<li id="level2_family" class="level"><a href="../index.html#/concerned_about_someone/a_family_member/" title="Concerned for a family member?" accesskey="3">Concerned for a family member?</a></li>';
var navChild = '<li id="level2_child" class="level"><a href="../index.html#/concerned_about_someone/your_child/" title="Concerned for your child?" accesskey="3">Concerned for your child?</a></li>';

/////////////////////////////////////////////////////////
// LEVEL 3 >> MATE
var navMateGetting = '<li id="level3_getting" class="level"><a href="../index.html#/concerned_about_someone/a_mate/think_they_might_be_getting/" title="Think they might be getting into something they shouldn\'t?" accesskey="4">Think they might be getting into something they shouldn\'t?</a></li>';
var navMateVictim = '<li id="level3_victim" class="level"><a href="../index.html#/concerned_about_someone/a_mate/worried_theyre_a_victim/" title="Worried they\'re a victim of something?" accesskey="4">Worried they\'re a victim of something?</a></li>';

// LEVEL 3 >> FAMILY
var navFamilyGetting = '<li id="level3_getting" class="level"><a href="../index.html#/concerned_about_someone/a_family_member/think_they_might_be_getting/" title="Think they might be getting into something they shouldn\'t?" accesskey="4">Think they might be getting into something they shouldn\'t?</a></li>';
var navFamilyVictim = '<li id="level3_victim" class="level"><a href="../index.html#/concerned_about_someone/a_family_member/worried_theyre_a_victim/" title="Worried they\'re a victim of something?" accesskey="4">Worried they\'re a victim of something?</a></li>';

// LEVEL 3 >> CHILD
var navChildGetting = '<li id="level3_getting" class="level"><a href="../index.html#/concerned_about_someone/your_child/think_they_might_be_getting/" title="Think they might be getting into something they shouldn\'t?" accesskey="4">Think they might be getting into something they shouldn\'t?</a></li>';
var navChildVictim = '<li id="level3_victim" class="level"><a href="../index.html#/concerned_about_someone/your_child/worried_theyre_a_victim/" title="Worried they\'re a victim of something?" accesskey="4">Worried they\'re a victim of something?</a></li>';


/////////////  CODE INSERT FUNCTIONS /////////////////////

//Concerned for >> MATE GETTING
		if (navArr[0] == 'a_mate_getting') {
			navStart = navHome+ '\n' + navConcerned;			
			navMain = navMate +'\n' + navMateGetting;
		}
		
//Concerned for >> MATE VICTIM
		if (navArr[0] == 'a_mate_victim') {
			navStart = navHome+ '\n' + navConcerned;			
			navMain = navMate +'\n' + navMateVictim;
		}

//Concerned for >> FAMILY GETTING
		if (navArr[0] == 'family_getting') {
			navStart = navHome+ '\n' + navConcerned;			
			navMain = navFamily +'\n' + navFamilyGetting;
		}
		
//Concerned for >> FAMILY VICTIM
		if (navArr[0] == 'family_victim') {
			navStart = navHome+ '\n' + navConcerned;			
			navMain = navFamily +'\n' + navFamilyVictim;
		}

//Concerned for >> CHILD GETTING
		if (navArr[0] == 'child_getting') {
			navStart = navHome+ '\n' + navConcerned;			
			navMain = navChild +'\n' + navChildGetting;
		}
		
//Concerned for >> CHILD VICTIM
		if (navArr[0] == 'child_victim') {
			navStart = navHome+ '\n' + navConcerned;			
			navMain = navChild +'\n' + navChildVictim;
		}


//DONE SOMETHING
		if (navArr[0] == 'done_something') {
			navStart = navHome;			
			navMain = navDoneSomething;
		}

//GETTING PRESSURE
		if (navArr[0] == 'getting_pressure') {
			navStart = navHome;
			navMain = navGettingPressure;
		}

//BEEN A VICTIM
		if (navArr[0] == 'been_a_victim') {
			navStart = navHome;
			navMain = navBeenVictim;
		}



		pathNavContainer.innerHTML = navStart;
		pathNavContainer.innerHTML += navMain;
	}
}

function gotoWithNav(page) {
	var nav = getQueryVariable('nav');
	if (nav != undefined) {
		page += '?nav=' + nav;
	}
	document.location = page;
}



////////////////////////////////////////////////////////////
//   POPUP JS
////////////////////////////////////////////////////////////

// one browser sniff to account for a known bug
var sUserAgent = navigator.userAgent.toLowerCase();
var isOp = (sUserAgent.indexOf('opera')!=-1)?true:false;

// make new popup window
function pop(oAnchor,sProps,sWindow){
	var sUrl = '';

	// get URL from calling link oAnchor
	if(oAnchor.getAttribute) sUrl = oAnchor.getAttribute('href');
	if(sUrl=='') sUrl = oAnchor.href;

	// if still no URL, return true and let the regular link take over
	if(sUrl=='') return true;

	// set window name ('_blank' for new window each time)
	var sWindowName = sWindow?sWindow:'reusedWindow';

	// if no window properties are defined in the function call's optional parameter 'sProps'
	if(!sProps) sProps = null;

	// assign the popup to this variable so we can verify it exists
	if(sUrl) var oPopup = window.open(sUrl,sWindowName,sProps);

	// An Opera bug returns too early if you focus the window, so we don't focus it in that browser.
	// Only a noticable defect if a window is already open and hidden.
	if(oPopup && !isOp) oPopup.focus();

	// If popup was created successfully, cancel link in calling window.
	// Acts as regular link in browser that blocks requested popups or has JavaScript turned off.
	return (oPopup)?false:true;

}

// switch an image src without preloading and leave it
function switchImg(sImg,sSrc){
	oImg = document.images[sImg];
	// return true or false based on assignment
	return !(oImg.src = 'img/' + sSrc);
}

// With onkeypress event, this verifies 'Enter' key
function verifyKey(oElement,oEvent){
	if(oEvent.keyCode==13 && oElement.onclick) oElement.onclick();
}








