// JavaScript Document

function getElementsByClassName(oElm, strTagName, oClassNames){
	var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);
	var arrReturnElements = new Array();
	var arrRegExpClassNames = new Array();
	if(typeof oClassNames == "object"){
		for(var i=0; i<oClassNames.length; i++){
			arrRegExpClassNames.push(new RegExp("(^|\\s)" + oClassNames[i].replace(/\-/g, "\\-") + "(\\s|$)"));
		}
	}
	else{
		arrRegExpClassNames.push(new RegExp("(^|\\s)" + oClassNames.replace(/\-/g, "\\-") + "(\\s|$)"));
	}
	var oElement;
	var bMatchesAll;
	for(var j=0; j<arrElements.length; j++){
		oElement = arrElements[j];
		bMatchesAll = true;
		for(var k=0; k<arrRegExpClassNames.length; k++){
			if(!arrRegExpClassNames[k].test(oElement.className)){
				bMatchesAll = false;
				break;
			}
		}
		if(bMatchesAll){
			arrReturnElements.push(oElement);
		}
	}
	return (arrReturnElements)
}

function addEventToObject(obj,evt,func) {
	var oldhandler = obj[evt];
	obj[evt] = (typeof obj[evt] != 'function') ? func : function(ev){oldhandler(ev);func(ev);};
}

// hide/show object
function hideObj(obj) {
	obj = document.getElementById(obj);
	obj.style.display = "none";
}

function showObj(obj) {
	obj = document.getElementById(obj);
	obj.style.display = "block";
}

function showInlineObj(obj) {
	obj = document.getElementById(obj);
	obj.style.display = "inline";
}

function showTableRowObj(obj) {
	obj = document.getElementById(obj);
	obj.style.display = "";
}

// initiate dynamic topnav
var arrTabStyle = new Array("sage", "yellow", "orange", "red", "maroon", "tan", "brown");
function initTopnav() {
	var obj = document.getElementById("topnavList")
	var objTabs = obj.getElementsByTagName("a");
	obj.style.width = (150 * objTabs.length) + "px"; // set correct width for ul list
	obj.style.marginLeft = (-18 * (objTabs.length - 2)) + "px"; // set correct left margin for ul list
	for (i = 0; i < objTabs.length; i++) {	
		objTabs[i].className = arrTabStyle[i]; // set correct tab colors
	}
}

// create drop shadow for text
function doShadow(obj) {
	var objOrig = document.getElementById(obj);
	var objShadow = document.getElementById(obj + "Shadow");
	objShadow.innerHTML = objOrig.innerHTML;
}

// set new star rating
function setRating(num) {
	var stars = document.getElementById("reviewStars");
	var path = document.getElementById("ratingholder").innerHTML;
	stars.src = path + "/images/common/rating-" + num + "-stars.jpg";
	var field = document.getElementById("reviewrating");
	field.value = num;
}

// display new star rating on hover
function displayRating(num) {
	var stars = document.getElementById("reviewStars");
	var path = document.getElementById("ratingholder").innerHTML;
	stars.src = path+"/images/common/rating-" + num + "-stars.jpg";
}

// return to current new star rating
function returnRating(num) {
	var stars = document.getElementById("reviewStars");
	var path = document.getElementById("ratingholder").innerHTML;
	stars.src = path + "/images/common/rating-" + num + "-stars.jpg";
}

// create dyanmic buttons (visible text, link URL, additional classes)
function createButton(label, url, classes) {
	document.write('<div class="button ' + classes + '">');
	document.write('<span>' + label + '</span>');
	document.write('<a href="' + url + '" title="' + label + '">' + label + '</a>');
	document.write('</div>');
}

// change form action
function setAction (formName, url) {
	var obj = document.getElementById(formName);
	obj.action = url;
}

// click all text from textarea
var boolClearTA = true;
function clearTextarea (obj) {
	if (boolClearTA) {
		obj.innerHTML = "";
		boolClearTA = false;
	}
}

function setIcon(icon) {
	document.getElementById('avatar').value = icon;
	var arrTmp = document.getElementById("iconList").getElementsByTagName("a");
	for (i = 0; i < arrTmp.length; i++) {
		arrTmp[i].className = '';
	}
	if(document.getElementById("uploadedAvatars")) {
		var Tmp = document.getElementById("uploadedAvatars").getElementsByTagName("a");
		for (i = 0; i < Tmp.length; i++) {
			Tmp[i].className = '';
		}
	}

	document.getElementById(icon).className = "default";
}

// enlarge/reduce textarea height
var textareaSize = "small";
function enlargeBox(boxObj) {
	textObj = document.getElementById('textareaSize');
	boxObj = document.getElementById(boxObj);
	if (textareaSize == "small") { // make larger
		textObj.innerHTML = "Smaller";
		boxObj.className = "textareaLarge";
		textareaSize = "large";
	} else { // make smaller
		textObj.innerHTML = "Larger";
		boxObj.className = "";
		textareaSize = "small";
	}
}

// to open a pop-up window
function openW(url, name, w, h, o) {
// options - menubar,location,resizable,scrollbars,status,toolbar (there are more)
// if you want them just list them seperated by commas
	var wlocation = "left=25,screenX=25,top=25,screenY=25";
	var windowprops = "width=" + w + ",height=" + h;
	if (o != '')
	    windowprops = windowprops + "," + o + "," + wlocation;
	popup = window.open(url, name, windowprops);
	popup.focus();
}

// open popup for importing email addresses
function openImport(form, url) {
	provider = form.provider.value;
	username = form.username.value;
	password = form.password.value;
	openW(url+"/friends-invite-import.php?provider=" + provider + "&username=" + username + "&password=" + password , "import", 600, 480, "menubar=0,toolbar=0")
}

// select all checkboxes
function selectAllOptions(checker, list) {
	var list = document.getElementById(list).getElementsByTagName("input");
	// alert(list.length);
	for (i = 0; i < list.length; i++) {
		if (list[i].type == "checkbox") {
			if (checker.checked) {
				list[i].checked = true;
			} else {
				list[i].checked = false;
			}
		}
	}
}

// collect all values from selected checkboxes and turn into string
function getCheckedValues(list) {
	var list = document.getElementById(list).getElementsByTagName("input");
	var strTmp = "";
	for (i = 0; i < list.length; i++) {
		if (list[i].type == "checkbox") {
			if (list[i].checked) {
				strTmp += list[i].value + ", ";	
			}
		}
	}
	// alert(strTmp);
	return strTmp;
}

function showHideObject(objectId) {
	var obj = document.getElementById(objectId);
	if ( obj.className == 'remove' ) {
		obj.className = '';		
	}
	else {
		obj.className = 'remove';
	}
}

/**
 * Unobtrusively hijacks the login link so that instead of forwarding to the login page onclick it displays the login boxes inline on the page.
 *
 * @author: Tom Willmot
 * @version 1.0
 **/
function inlineLoginInit() {

	if ( !document.getElementById || !document.createElement )return;
	var loginLinks = getElementsByClassName(document, "a", "login-link");
	if ( !loginLinks ) return;
	for (i = 0; i < loginLinks.length; i++) {
		loginLinks[i].removeAttribute('href');
		loginLinks[i].setAttribute('href','#inlinelogin');		
		loginLinks[i].onclick = function() {
			showHideObject('inline-login');
			if ( !document.inlineLogin.className == 'remove' ) {
				document.inlineLogin.user_username.focus()
				}
		}
	}
}

function forgotPasswordInline() {
    document.getElementById('forgot-password').style.display = 'block';
   	document.getElementById('user_login2').focus()
}

/**
 * Unobtrusively hijacks the login link so that instead of forwarding to the login page onclick it displays the login boxes inline on the page.
 *
 * @author: Tom Willmot
 * @version 1.0
 **/
function userActionConfirm() {

	if ( !document.getElementById || !document.createElement )return;
	var actionLinks = getElementsByClassName(document, "a", "confirmUserAction");
	if ( !actionLinks ) return;
	for (i = 0; i < actionLinks.length; i++) {
		actionLinks[i].removeAttribute('href');
		actionLinks[i].currentText = actionLinks[i].innerHTML;
		actionLinks[i].onclick = function() {
			confirmQuestion = document.getElementById('q-'+this.id);
			if ( confirmQuestion.style.display == 'none' ) {
				confirmQuestion.style.display = '';
				this.innerHTML = '<b>Cancel</b>';
			} else {
				confirmQuestion.style.display = 'none';
				this.innerHTML = this.currentText;
			}
		}
	}
}

/**
 * Unobtrusively adds the mini-account button via javascript and sets it to toggle the display of the mini account popup
 *
 * @author: Tom Willmot
 * @version 1.0
 **/
function miniAccountInit() {

	if ( !document.getElementById || !document.createElement ) return;
	var parent = document.getElementById('alertsBar');
	if ( document.getElementById('account-popup') ) { // We check for this to make sure that the user is logged in.
		var link = document.createElement('a');
		link.className = 'my-account-icon';
		link.onclick = function() {
			showHideObject('account-popup');
			$file = document.getElementById("link-holder").innerHTML;
			xmlhttpRequest($file, 'account-popup', 'ajax-loading');
			
			$file = document.getElementById("link-holder").innerHTML;
			
			if (document.getElementById('account-popup').className == '') {
				intervalId = setInterval ( "xmlhttpRequest($file, 'account-popup', 'ajax-loading')", 1000);
			}
			if(document.getElementById('account-popup').className == 'remove') {
					clearInterval ( intervalId );
			}
			
		}
		link.innerHTML = 'Mini Account Popup';
		parent.appendChild(link);
	}
}

//Fade out a container
function fadeOutBox(box) {
	var speed = 10;
	var timer = 0;
	for(i = 0; i <= 100; i++) {
		oposite = 100 - i;
		
		setTimeout("changeOpac(" + oposite + ",'" + box + "')",(timer * speed));
		timer++;
	}
	
}

function changeOpac(opacity, id) {
	var object = document.getElementById(id).style;
	
	object.opacity = (opacity / 100);
	//alert(opacity);
	object.filter = "alpha(opacity=" + opacity + ")";
	if(opacity < 2) object.display = 'none';	
}

//Home Tabs
function init_home_tabs() {
	tabsnav = getElementsByClassName(document, "ul", "tabs-nav");
	activetabs = getElementsByClassName(document, "a", "tabs-nav-active");
	tabuls = getElementsByClassName(document, "ul", "tabs-content-ul");
	
	for(i=0;i<tabsnav.length;i++) { tabsnav[i].style.display = 'block'; }
	
	for(i=0;i<tabuls.length;i++) { tabuls[i].style.display = 'none'; }
	
	for(i=0;i<activetabs.length;i++) { ulId = 'ul-' + activetabs[i].id.substring(2); document.getElementById(ulId).style.display = 'block'; }
	
}

function show_home_tab(tabID) {
	tab = document.getElementById(tabID);
	activetabs = getElementsByClassName(tab.parentNode.parentNode.parentNode, "a", "tabs-nav-active");	
	tabuls = getElementsByClassName(tab.parentNode.parentNode.parentNode, "ul", "tabs-content-ul");

	for(i=0;i<tabuls.length;i++) { tabuls[i].style.display = 'none'; }
	
	for(i=0;i<activetabs.length;i++) { if(activetabs[i].parentNode.parentNode.id == tab.parentNode.parentNode.id) { activetabs[i].className = ''; ulId = 'ul-' + tab.id.substring(2); document.getElementById(ulId).style.display = 'block'; } }
	
	tab.className = "tabs-nav-active";
}

//Return the current local date
function get_local_date(seperator) {
	var now = new Date();
	year = now.getFullYear();
	month = now.getMonth() + 1;
	day = now.getDate();
	date = month + seperator + day + seperator + year;
	document.write(date);
}

addEventToObject( window, 'onload', init_home_tabs);

addEventToObject( window, 'onload', inlineLoginInit);
addEventToObject( window, 'onload', miniAccountInit);
addEventToObject( window, 'onload', userActionConfirm);
