//	Copyright � 2010 purpleturtlearts.ca, All Rights Reserved.

$j(function() {
	$j(".tabs").tabs();
 
	$j(".drop-down").hoverIntent(function(){
	  $j(".expand").slideDown("fast");
	}, function() {
	  $j(".expand").slideUp("fast");
	});
 
	jQuery.fn.fadeToggle = function(speed, easing, callback) {
	  return this.animate({opacity: 'toggle'}, speed, easing, callback);
	}; 
 
	$j(".expand-link").click(function(){
	  $j(".popup").fadeToggle("fast");
	});
	
	$j('body').click(function() {
 	  $j("#login-panel").fadeOut("fast");
 	});

 	$j('#n-login').click(function(event){
	  event.stopPropagation();
	});
	
	$j('input.suggested_text, textarea.suggested_text').addClass("field-inactive");
	$j('input.suggested_text, textarea.suggested_text').focus(function() {
		$j(this).removeClass("field-inactive").addClass("field-active");
	    if (this.value == this.defaultValue){ 
	    	this.value = '';
		}
		if(this.value != this.defaultValue){
			this.select();
		}
	});
	$j('input.suggested_text, textarea.suggested_text').blur(function() {
	    if ($j.trim(this.value) == ''){
	    	this.value = (this.defaultValue ? this.defaultValue : '');
			$j(this).removeClass("field-active").addClass("field-inactive");
		}
	});
});

function showUpdatingPopup() {
	if(!document.getElementById("search_gif")) {return false;}
	var ScrollTop = document.body.scrollTop;
	if (ScrollTop == 0) {
		if (window.pageYOffset) {ScrollTop = window.pageYOffset;}
		else {ScrollTop = (document.body.parentElement) ? document.body.parentElement.scrollTop : 0;}
	
	}
	var width = (screen.width - 150) / 2;
	var height = screen.height / 2 - 200;
	popup = document.getElementById("search_gif");
	popup.style.left = width + 'px';
	popup.style.top = height + 'px';
	popup.style.display = "block";
}
function hideUpdatingPopup() {
	if(!document.getElementById("search_gif")) {return false;}
	document.getElementById("search_gif").style.display = "none";
}
function showReportPopup(obj) {
	var popup = document.getElementById(obj.id+"p");
	popup.style.left = findPosX(obj) + 'px';
	popup.style.top = findPosY(obj) + 16 + 'px';
	document.body.appendChild(popup);
	popup.style.display = "block";
}
function closeReportPopup(obj) {
	//document.getElementById("bg").style.display = "none";
	obj.parentNode.style.display = "none";
}
function showHelp(obj) {
	var popup = document.getElementById(obj.id+"p");

	xPos = findPosX(obj);
	var offset = Math.ceil(306 - (windowWidth()/2 + 480 - xPos));
	offset = Math.max(offset, 20);
	xPos = xPos - offset;
	
	popup.style.left = xPos + 'px';
	popup.style.top = (findPosY(obj)+20) + 'px';
	popup.style.display = "block";
	popup.onclick = function() {this.style.display = "none";}
}
function showCommonHelp(obj, popupId) {
	var popup = document.getElementById(popupId);
	popup.style.left = findPosX(obj) - 20 + 'px';
	popup.style.top = findPosY(obj) + 'px';
	popup.style.display = "block";
	popup.onclick = function() {this.style.display = "none";}
}

function showPopup(id, scrolling) {
	lightboxPopup("pop-up-" + id, scrolling);
}
function lightboxPopup(id, scrolling) {
	if(!document.getElementById(id)) {return false;}

	if($("black") == null) {
		var black = document.createElement('div');
		black.id = "black";
		black.style.height = (document.body.offsetHeight+50) + 'px';
		document.getElementsByTagName("body")[0].appendChild(black);
	}

	if($('vid-embed')) { $('vid-embed').style.visibility = 'hidden'; }
	var popup = document.getElementById(id);

	if(oldBrowser()) {scrolling = 'absolute';}

	if(scrolling == undefined || scrolling == null || scrolling == 'fixed') {
		popup.style.position = 'fixed';
		popup.style.display = "block";
		offsetHeight = Math.floor((document.documentElement.clientHeight - popup.offsetHeight) / 2);
		popup.style.top = offsetHeight + 'px';
	} else {
		popup.style.top = windowYOffset() + 150 + 'px';
		popup.style.position = 'absolute';
		popup.style.display = "block";
	}	
}
function hidePopup(id) {
	hideLightboxPopup("pop-up-" + id);
}
function hideLightboxPopup(id) {
	if(!document.getElementById(id)) {return false;}

	var black = document.getElementById("black");
	document.getElementsByTagName("body")[0].removeChild(black);
	black.style.display = "none";

	if($('vid-embed')) { $('vid-embed').style.visibility = 'visible'; }
	document.getElementById(id).style.display = "none";
}
function showLockedPopup(obj, user_id) {
	if($('private_popup_link'))
		$('private_popup_link').href = '/people/add_friend?friend_id=' + user_id;
	var popup = document.getElementById("lockedUserp");
      popup.style.left = findPosX(obj) + 'px';
	popup.style.top = findPosY(obj) + 16 + 'px';
	document.body.appendChild(popup);
	popup.style.display = "block";
}

function windowWidth() {
	return document.documentElement && document.documentElement.clientWidth || document.body && document.body.clientWidth || document.body && document.body.parentNode && document.body.parentNode.clientWidth || 0;
}
function windowYOffset() {
	if(window.pageYOffset)
		return window.pageYOffset;
	else
		return (document.body.parentElement) ? document.body.parentElement.scrollTop : 0;
}
function findPosY(obj) {
	var posTop = 0;
	while (obj.offsetParent) {posTop += obj.offsetTop; obj = obj.offsetParent;}
	return posTop;
}
function findPosX(obj) {
	var posLeft = 0;
	while (obj.offsetParent) {posLeft += obj.offsetLeft; obj = obj.offsetParent;}
	return posLeft;
}
function checkFieldLength(fieldID, counterID, maxLength) {
	if($(fieldID)) {
		content = $(fieldID).value;
		length = content.length;

		if(length > maxLength) {
			$(fieldID).value = content.substring(0, maxLength);
			length = maxLength;
		} 
		
		if($(counterID)) {
			$(counterID).innerHTML = (maxLength - length);
		}
	}
}
function askCommentFocus(askID) {
	formID = 'askResponse' + askID;
	if($(formID)) {
		comment_field = $(formID).comment_comment;
		comment_field.style.height = '30px';
		comment_field.style.color = '#000';
		if(comment_field.value == 'Write a comment') { comment_field.value = ''; }
		comment_field.focus();

		$('askResponseSubmit' + askID).style.display = 'block';
	}
}
function askCommentFocusLost(askID) {
	formID = 'askResponse' + askID;
	if($(formID)) {
		comment_field = $(formID).comment_comment;
		if(comment_field.value == '') {
			comment_field.style.height = '15px';
			comment_field.style.color = '#666666';
			comment_field.value = 'Write a comment';
			$('askResponseSubmit' + askID).style.display = 'none';
		}
	}
}
function replaceExtChars(text) {
	fromTo = new Array('&amp;', '&', '&gt;','>','&lt;','<','&quot;','\"');

	for(i=0; i < fromTo.length; i=i+2) {
		text = text.replace(eval('/'+fromTo[i+1]+'/g'), fromTo[i]);
	}
	
	return text;
}
function profileNoteFocus(defaultText) {
	if($('profileNote')) {
		comment_field = $('profileNote').comment_comment;
		//comment_field.style.height = '60px';
		comment_field.style.color = '#000';
		if(comment_field.value == defaultText) { comment_field.value = ''; }
		comment_field.focus();
		//$('profileNoteSubmit').style.display = 'inline';
		//$('character-count').style.display = 'block';
	}
}
function profileNoteFocusLost(defaultText) {
	if($('profileNote')) {
		comment_field = $('profileNote').comment_comment;
		if(comment_field.value == '') {
			//comment_field.style.height = '15px';
			comment_field.style.color = '#666666';
			comment_field.value = defaultText;
			//$('profileNoteSubmit').style.display = 'none';
			//$('character-count').style.display = 'none';
		}
	}
}

function loadScript(src, id) {
  var e = document.createElement('script'); 
  e.setAttribute('language','javascript'); 
  e.setAttribute('type', 'text/javascript');
  e.setAttribute('src',src);
  if(id != null) e.setAttribute('id',id);
  document.getElementsByTagName('head')[0].appendChild(e); 
}
function loadCSS(src, id) {
  var e = document.createElement('link'); 
  e.setAttribute('rel', 'stylesheet'); 
  e.setAttribute('type', 'text/css');
  e.setAttribute('href',src);
  if(id != null) e.setAttribute('id',id);
  document.getElementsByTagName('head')[0].appendChild(e); 
}

