var elements_on=new Array();
var elements_off=new Array();

/* * * * * * * * * * * * * * * * * * * * */

function show(obj, type) {	
	if (type==null) {type = 'inline';}
	obj.css('display',type);
	elements_on.push(obj);
}

/* * * * * * * * * * * * * * * * * * * * */

function hide(obj) {
	obj.css('display','none');
	elements_off.push(obj);
}

/* * * * * * * * * * * * * * * * * * * * */

function hide_all() {
	for(var i=elements_on.length-1;i>=0;i--) {elements_on[i].css('display','none');elements_on.pop();}
}

/* * * * * * * * * * * * * * * * * * * * */

function toggle(obj, type) {
	if (type==null)	{type = 'inline';}
		
	obj.each(function(){
		if ($(this).css('display') == 'none') {
			show($(this), type);
			elements_on.push($(this));
		}	
		else {
			hide($(this));
			elements_off.push($(this));
		}		
	});		
	
}

/* * * * * * * * * * * * * * * * * * * * */

function feedback(obj,text,speed) {
	if (speed==null) {speed = 2000;}
	obj.html(text);
	obj.css('display','inline');
	setTimeout('$(".feedback").fadeOut("slow");',speed);
}

/* * * * * * * * * * * * * * * * * * * * */

function setup_ticker() {
	$("#news").newsTicker(4000);
	$("#ticker").prependTo("body");
}

/* * * * * * * * * * * * * * * * * * * * */

function show_register_box(must_be_logged_in, taxon_id) {
	//window.location.href='register.php?' + ((must_be_logged_in != null) ? '&must_be_logged_in=1' : '');
	window.location.href='/login.php';	
	return false;
}

/* * * * * * * * * * * * * * * * * * * * */

function show_info_box(message) {
	$("td.info_box_message").html(message);
	showBox($("div#info_box"));
}

/* * * * * * * * * * * * * * * * * * * * */

function show_error_box(message) {
	$("td.error_box_message").html(message);
	showBox($("div#error_box"));
}

/* * * * * * * * * * * * * * * * * * * * */

function showBox(element){
	self.scrollTo(0,0);
	$('div#overlay').show();
	center(element);
	page_size_array = getPageSize();
	$('div#overlay').css("height",page_size_array[1]);    
	return false;
}

/* * * * * * * * * * * * * * * * * * * * */

function hideBox(element){
	$('div#overlay').hide();
	element.hide();
	return false;
}

/* * * * * * * * * * * * * * * * * * * * */

function center(element){

	var my_width  = 0;
	var my_height = 0;

	if ( typeof( window.innerWidth ) == 'number' ){
		my_width  = window.innerWidth;
		my_height = window.innerHeight;
	}else if ( document.documentElement && 
	( document.documentElement.clientWidth ||
	document.documentElement.clientHeight ) ){
		my_width  = document.documentElement.clientWidth;
		my_height = document.documentElement.clientHeight;
	}
	else if ( document.body && 
	( document.body.clientWidth || document.body.clientHeight ) ){
		my_width  = document.body.clientWidth;
		my_height = document.body.clientHeight;
	}

	element.css("position","absolute");
	element.css("zIndex","99");

	var scrollY = 0;

	if ( document.documentElement && document.documentElement.scrollTop ){
		scrollY = document.documentElement.scrollTop;
	}else if ( document.body && document.body.scrollTop ){
		scrollY = document.body.scrollTop;
	}else if ( window.pageYOffset ){
		scrollY = window.pageYOffset;
	}else if ( window.scrollY ){
		scrollY = window.scrollY;
	}

	var setX = ( my_width  - element.width()  ) / 2;
	var setY = 50;   

	setX = ( setX < 0 ) ? 0 : setX;
	setY = ( setY < 0 ) ? 0 : setY;

	element.css("left",setX + "px");
	element.css("top",setY + "px");

	element.css("display","block");	    
}    

/* * * * * * * * * * * * * * * * * * * * */

function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}


	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight); 
	return arrayPageSize;
}

/* * * * * * * * * * * * * * * * * * * * */

function check_email(email){	
	var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
	if (filter.test(email)) {
		return true;
	}	
	else{
		return false;
	}	
}

/* * * * * * * * * * * * * * * * * * * * */

function convert_br_to_linefeed(text){	
	text = text.replace(/<br>/g, "\n");
	text = text.replace(/<BR>/g, "\n");	
	return text;
}

/* * * * * * * * * * * * * * * * * * * * */

function convert_linefeed_to_br(text){	
	text = text.replace(/\n/g, "<br>");
	return text;
}

/* * * * * * * * * * * * * * * * * * * * */

var anim_button;
var anim_button_text;
var anim_arrows = '';
var anim_interval;

function anim_busy() {
	if (anim_arrows.length == 2) {
		anim_arrows='';
	}
	else {
		anim_arrows+='>';
	}	
	anim_button.val(anim_button_text + anim_arrows);
}

function set_busy(obj, text) {
	anim_button=obj;anim_button_text=text;
	$(".app_button").attr("disabled","disabled");
	$(".edit_link").css("display","none");
	anim_button.val(anim_button_text);
	anim_interval = setInterval("anim_busy()", 800);
}

/* * * * * * * * * * * * * * * * * * * * */

function image_like(obj) {

	var this_id = $(".image_like").attr("rel");
	var this_parent = $(".image_like").parent();
	var old_html = this_parent.html();

	this_parent.html('<span class="processing">Saving...</span>');

	$.ajax({
		type: "POST",cache: false,url: "gears/gear_save_image_like.php",
		data: "image_id=" + this_id,
		dataType: "json",
		success: function(json){
			if (json.succeeded) {
				this_parent.html('<b>You like this image</b><br/><a href="' + image_likes_script + this_id + '">' + json.image_likes + '</a> ' + (json.image_likes != 1 ? 'people like' : 'person likes') + ' this image');
				liked_array.push(this_id);
				liked_array_image_liked_count.push(json.image_likes);
			}
			else {
				hideBox($("div.box_div"));
				show_register_box(true);
				this_parent.html(old_html);
			}
		}
	});

	return false;
}

/* * * * * * * * * * * * * * * * * * * * */

function image_like_no_popup(obj) {

	var this_id = obj.attr("rel");
	var this_parent = obj.parent();
	var old_html = this_parent.html();

	this_parent.html('<span class="processing">Saving...</span>');

	$.ajax({
		type: "POST",cache: false,url: "gears/gear_save_image_like.php",
		data: "image_id=" + this_id,
		dataType: "json",
		success: function(json){
			if (json.succeeded) {
				this_parent.html('<b>You like this image</b><br/><a href="' + image_likes_script + this_id + '">' + json.image_likes + '</a> ' + (json.image_likes != 1 ? 'people like' : 'person likes') + ' this image');
			}
			else {
				show_register_box(true);
				this_parent.html(old_html);
			}
		}
	});

	return false;
}

/* * * * * * * * * * * * * * * * * * * * */

function show_image_lb() {

	var item_arr = $(this).attr("rel").split("-");
	$("img[@id='taxon-image-large']").attr("src", (bucket_taxon_images_path_large + item_arr[0] + '.jpg'));
	$(".taxon_image_info_copy").html($(this).parent().find(".taxon_image_info").html() + '<br/>');
	$(".taxon_image_copyright_member").html($(this).parent().find(".taxon_image_member").html());
	$(".taxon_image_member_contact").html('<a href="/privmsg.php?mode=post&u=' + item_arr[1] + '">contact</a>');
	$(".image_like").attr("rel",item_arr[2]);

	if (item_arr[3] == 1) {

		if (liked_array.toString().indexOf(item_arr[2]) !== -1) {
			$(".image_like_cell").html('<b>You like this image</b><br/><a href="' + image_likes_script + item_arr[2] + '">' + liked_array_image_liked_count[liked_array.toString().indexOf(item_arr[2])] + '</a> ' + (liked_array_image_liked_count.toString().indexOf(item_arr[2]) != 1 ? 'people like' : 'person likes') + ' this image');
		}
		else if (item_arr[4] == 1) {
			$(".image_like_cell").html('<b>You like this image</b><br/><a href="' + image_likes_script + item_arr[2] + '">' + item_arr[5] + '</a> ' + (item_arr[5] != 1 ? 'people like' : 'person likes') + ' this image');
		}
		else {
			$(".image_like_cell").html('<a href="#" class="image_like" rel="' + item_arr[2] + '">I like this image</a><br/><a href="' + image_likes_script + item_arr[2] + '">' + item_arr[5] + '</a> ' + (item_arr[5] != 1 ? 'people like' : 'person likes') + ' this image');
			$(".image_like").click(image_like);
		}

		$(".image_like_cell").css('padding-right','10px');
	}
	else if (item_arr[5] > 0) {
		$(".image_like_cell").html('<a href="' + image_likes_script + item_arr[2] + '">' + item_arr[5] + '</a> ' + (item_arr[5] != 1 ? 'people like' : 'person likes') + ' your image');
		$(".image_like_cell").css('padding-right','10px');
	}
	else {
		$(".image_like_cell").html('');
		$(".image_like_cell").css('padding-right','0px');
	}

	showBox($("div#lightbox"));
	return false;
}

/* * * * * * * * * * * * * * * * * * * * */

function show_help_box() {	

	$.ajax({type: "POST",cache: false,url: "/plantstew/gears/gear_get_help.php",
		data: "help_index=" + $(this).attr("rel"),
		success: function(msg){			
			$("td.help_box_message").html(msg);
			showBox($("div#help_box"));
		}
	});
}

/* * * * * * * * * * * * * * * * * * * * */

function print_json(json) {
	for (var key in json) {
		alert(json[key]);
	}
}	

/* * * * * * * * * * * * * * * * * * * * */

/* This script and many more are available free online at
The JavaScript Source :: http://javascript.internet.com
Created by: kojak :: http://commoncoder.com */

// fieldname, warningname, remainingname, maxchars
function CheckFieldLength(fn,rn,mc) {
  var len = fn.value.length;
  if (len > mc) {
    fn.value = fn.value.substring(0,mc);
    len = mc;
  }
  document.getElementById(rn).innerHTML = mc - len;
}

/* * * * * * * * * * * * * * * * * * * * */

function cancel_registration() {
	history.go(-1);
}