// JavaScript Document 
var isIE;

isIE = document.all ? true : false;
function findPosX(obj)
  {
    var curleft = 0;
    if(obj.offsetParent)
        while(1) 
        {
          curleft += obj.offsetLeft;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.x)
        curleft += obj.x;
    return curleft;
  }




function cloneEl(el, newID) {
	
	var newEl = el.cloneNode(true);
	newEl.id = newID;
	newEl.name = newID;
	el.parentNode.insertBefore(newEl, el);
	clear_node(newEl);

	set_node_index(newID,newEl);
	nodes = el.parentNode.childNodes;
	document.getElementById("order_"+el.parentNode.id).value = "";
	for(var x = 0; x < nodes.length;x++)
	if(nodes[x].id)
	document.getElementById("order_"+el.parentNode.id).value += nodes[x].id+", ";
}

function set_node_index(new_index,el)
{

	
	var testering = new_index.substr(4);
	var thenodes;
	var test_val = parseInt(testering);

		thenodes = el.getElementsByTagName('*');

	for(var x = 0; x < thenodes.length;x++)
	{
		if(thenodes[x].tagName == "INPUT" || thenodes[x].tagName == "SELECT")
		{

			
			thenodes[x].id = thenodes[x].id.replace(/\d+/,test_val);
			thenodes[x].title = thenodes[x].id;
			thenodes[x].name = thenodes[x].name.replace(/\d+/,test_val);
			

			
		}
	}
}

function clear_node(el)
{ //this will clear the inputs, selects, and checkboxes in a node.
	var thenodes;
	//if(isIE)
		thenodes = el.getElementsByTagName('*');

	//else
	//	thenodes = el.childNodes[1].childNodes[0].childNodes[0].childNodes[1].childNodes;
	for(var x = 0; x < thenodes.length;x++)
	{
		
		if(thenodes[x].tagName == "INPUT")
		{
			
			if(("  "+thenodes[x].id).indexOf('url')>0)
			{
				thenodes[x].value = "http://";
			}
			else
			{
				thenodes[x].value = "";
				if(thenodes[x].checked)
				thenodes[x].checked = false;
			}
		}
		if(thenodes[x].tagName == "SELECT")
			thenodes[x].selectedIndex = -1;
		
		
	}
	
}


function clone(parentID)
{
	var myclonednode = document.getElementById(parentID).childNodes;
	var info = "";
	var num_els = 0;
	for(var x = 0; x < myclonednode.length;x++)
	{
		if(myclonednode[x].id)
		{
			num_els++;
		}
	}
	
	for(var x = 0; x < myclonednode.length;x++)
	{
		if(myclonednode[x].id)
		{
			cloneEl(myclonednode[x],'ele['+(num_els)+']');
			x = myclonednode.length;
		}
	}	
}


  function findPosY(obj)
  {
    var curtop = 0;
    if(obj.offsetParent)
        while(1)
        {
          curtop += obj.offsetTop;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.y)
        curtop += obj.y;
    return curtop;
  }


function getScrollXY() {
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  return [ scrOfX, scrOfY ];
}

function disp_img(image_name,div_name)
{
	//alert(document.getElementById(div_name).offsetTop);
	var elem = document.getElementById('disp_img');
	var vscroll = getScrollXY()[1];
	elem.style.display = 'block';
	elem.innerHTML = image_name+'<br><img onclick="this.parentNode.style.display = \'none\';" onmouseout="this.parentNode.style.display = \'none\';" style="border:thin solid #FFF;" src="http://www.law2.byu.edu/novell_pics/law2pics2/thumbnails/'+image_name+'" />';
	var top = document.getElementById(div_name).offsetTop;
	var offset = 211;
	if(isIE)
		offset = 269;
	var scroll_off = document.getElementById("holder_div").scrollTop;
	elem.style.top = (top + offset - scroll_off)+'px';
	
}

function open_edit_div(id_img)
{
	var edit_img = document.getElementById(id_img);
	edit_img.src='/image_db/icons/backgroundleftcovblank.png'; 
	var edit_div = document.getElementById('image_editor');
	edit_div.style.display = 'block';	


}

function close_edit_div(id_img)
{
	var edit_img = document.getElementById(id_img);
	edit_img.src='/image_db/icons/backgroundleftcovwh.png'; 
	var edit_div = document.getElementById('image_editor');
	edit_div.style.display = 'none';	


}

// Array Remove - By John Resig (MIT Licensed)
Array.remove = function(array, from, to) {
  var rest = array.slice((to || from) + 1 || array.length);
  array.length = from < 0 ? array.length + from : from;
  return array.push.apply(array, rest);
};


/*
// Remove the second item from the array
array.remove(1);
// Remove the second-to-last item from the array
array.remove(-2);
// Remove the second and third items from the array
array.remove(1,2);
// Remove the last and second-to-last items from the array
array.remove(-2,-1);*/
// Array Remove - By John Resig (MIT Licensed)
Array.prototype.remove = function(from, to) {
  var rest = this.slice((to || from) + 1 || this.length);
  this.length = from < 0 ? this.length + from : from;
  return this.push.apply(this, rest);
};


function select_all()
{
	var info = new Array();
	info  = $("img[id*='img']");
	for(var x = 0; x < info.length;x++)
	{
		//select all images that need to be added to the list.
		if(info[x].src.indexOf('add')!=-1)
		{
			//alert(info[x]);
			//alert(document.getElementById(info[x].id).click());
			info[x].onclick();
			
		}
	}
	
	//alert(info.length);
}

function remove_leading_zeros(value)
{
	while(value.substring(0,1)=='0')
	{
		value = value.substring(1);
	}
	return value;	
}

function clear_list()
{
	var data = document.getElementById('pic_ids');
	var clear_list_button = document.getElementById('clear_list_button');
	//alert('coming soon! clearing your list!');
	var things = data.value.split(',');
	
	
	for(var x = 0; x < things.length;x++)
	{
		//alert(things[x].substr(1,things[x].length-5));
		var img_num = 'img'+remove_leading_zeros(things[x].substr(1,things[x].length-5));
		if(document.getElementById(img_num)!=null)
		{
			document.getElementById(img_num).src="http://www.law2.byu.edu/image_db/icons/add.png";
		}
	
	}
	
	setContent('backwork', 'includes/image_pick.php?r_all_images=yes', 'GET', '');
	
	data.value='';
	
	var holder_div = document.getElementById('holder_div');
	
	
	holder_div.innerHTML = '<div id="disp_img" style="display:none;position:fixed; background-color:#435462"></div>';
	
	clear_list_button.style.display='none';
	
	
}


function change_pic(pic_url,img_id)
{
	var arrow = document.getElementById(img_id);
	if(typeof(arrow) == 'undefined')
	{
		arrow = document.createElement('img');
		arrow.src = 'http://www.law2.byu.edu/image_db/icons/add.png';
	}
	var data = document.getElementById('pic_ids');
	var clear_list_button = document.getElementById('clear_list_button');
	var things = data.value.split(',');
	var img_name = pic_url.substr(1+pic_url.lastIndexOf('/'));
	var img_num = img_name.substr(0,img_name.length-4);
	

	
	//alert(arrow.src);
	if(arrow.src=="http://www.law2.byu.edu/image_db/icons/add.png")
	{
		if(data.value!="")
		{
			data.value+=','+img_name;
		}
		else
		{
			clear_list_button.style.display='inline';
			data.value=img_name;
		}
		things.push(img_name);	
		arrow.src="http://www.law2.byu.edu/image_db/icons/remove.png";
		setContent('backwork', 'includes/image_pick.php?images='+img_name, 'GET', ''); 
	}
	else
	{
		
			for(var x = 0; x < things.length;x++)
			{
				if(things[x]==img_name)
				{
					things.remove(x);
					x = things.length;
				}
			}
		

		data.value="";
		for(var x = 0; x < things.length;x++)
		{
			if(x!=0)
				data.value+=','+things[x];
			else
				data.value=things[x];
			
		}
		if(data.value=='')
			clear_list_button.style.display='none';
		arrow.src="http://www.law2.byu.edu/image_db/icons/add.png";
		setContent('backwork', 'includes/image_pick.php?r_images='+img_name, 'GET', '');
	}
	
	var holder_div = document.getElementById('holder_div');
	holder_div.innerHTML = '<div id="disp_img" style="display:none;position:fixed; background-color:#435462"></div>';
	
	for(var x = 0; x < things.length; x++)
	{
		if(things[x].length>2)
		holder_div.innerHTML+='<div style="width:55px" id="div'+x+'" onmouseover="disp_img(\''+things[x]+'\',\'div'+x+'\')">'+things[x].substr(0,things[x].length-4);+'</div>';
		
	}

}

function clear_selection()
{
		alert('to do, remove images, set back the arrows to appropriate types.');
		var data = document.getElementById('pic_ids');
		setContent('backwork', 'includes/image_pick.php?r_images='+data.value, 'GET', '');
	
	
}

function jumper(ipp, cur, restrictions, query, get_content)
{
	if(restrictions == "")
	{
		setContent('disp_images_ajax', 'includes/content.php?page='+document.getElementById('jump_to').value+'&ipp='+ipp+'&cur='+cur+get_content, 'GET', ''); 
	}
	else
	{
		setContent('disp_images_ajax', 'includes/content.php?page='+document.getElementById('jump_to').value+'&ipp='+ipp+'&cur='+cur+'&query='+query+'&restrictions='+restrictions+get_content, 'GET', '');
	}
}

function submitenter(e)
{
var keycode;
if (window.event) keycode = window.event.keyCode;
else if (e) keycode = e.which;
else return true;

if (keycode == 13)
   {
  		document.getElementById('jumper').click();

   }

}



function update_colorbox_pages(width2)
{
	
		$(".display1").colorbox({iframe:true,width:width2,height:"90%"});	
		$(".display2").colorbox({iframe:true,width:width2,height:"90%"});	
	
}



function doBidding(page,id,query)
{
	setContent('disp_images_ajax', 'includes/content.php?page='+page+'&cur='+id+'&query='+query, 'GET', '');
	tb_remove();
}

function change_boolean(elementID)
{
	if(document.getElementById(elementID).innerHTML=='&lt;')
	{
		document.getElementById(elementID).innerHTML = '&gt;';
		document.getElementById(elementID).value = '&gt;';
		document.getElementById(elementID+'_holder').value = '&gt;';
		
	}
	else if(document.getElementById(elementID).innerHTML=='&gt;')
	{
		document.getElementById(elementID).innerHTML = '=';
		document.getElementById(elementID).value = '=';
		document.getElementById(elementID+'_holder').value = '=';
	}
	else if(document.getElementById(elementID).innerHTML=='=')
	{
		document.getElementById(elementID).innerHTML = '&lt;';
		document.getElementById(elementID).value = '&lt;';
		document.getElementById(elementID+'_holder').value = '&lt;';
	}
}

function show_hide(elementID)
{
	if(document.getElementById(elementID).style.display!='none')
	{
		document.getElementById(elementID).style.display = 'none';
	}
	else
		document.getElementById(elementID).style.display = 'inline';
}

function show_hide_block(elementID)
{
	if(document.getElementById(elementID).style.display!='none')
	{
		document.getElementById(elementID).style.display = 'none';
	}
	else
		document.getElementById(elementID).style.display = 'block';
}


//my automagic stuff! prettiness on displaying dynamically resizing images and their properties.
var y = 0;
var orig_x = 0;
var orig_y = 0;
var activated = false;
var activated_id = "";
var orig_width = 0;
var orig_height = 0;
var clock_id;
var max_width = 0;

function stop_grow(elementID)
{
	if(activated&&elementID==activated_id)
	{
		document.getElementById(activated_id).style.left = orig_x+"px";
		document.getElementById(activated_id).style.top = orig_y+"px";
		document.getElementById(activated_id).style.width = orig_width+"px";
		document.getElementById(activated_id).style.height =  orig_height+"px";
		document.getElementById(activated_id).style.zIndex = 10;
		document.getElementById('info_'+activated_id).style.display = 'none';
		activated = false;
		activated_id = "";
		clearInterval(clock_id);
		y = 0;
	}
}

function grow_element(elementID,element_width)
{
	if(activated&&elementID!=activated_id)
	{
		document.getElementById(activated_id).style.left = orig_x+"px";
		document.getElementById(activated_id).style.top = orig_y+"px";
		document.getElementById(activated_id).style.width = orig_width+"px";
		document.getElementById(activated_id).style.height =  orig_height+"px";
		document.getElementById(activated_id).style.zIndex = 10;
		document.getElementById('info_'+activated_id).style.display = 'none';
		activated = false;
		clearInterval(clock_id);
		y = 0;
	}
	else if(elementID!=activated_id)
	{
		max_width = element_width;
		document.getElementById(elementID).style.zIndex = 100;
		orig_x = parseInt(document.getElementById(elementID).style.left);
		orig_y = parseInt(document.getElementById(elementID).style.top);
		orig_width = parseInt(document.getElementById(elementID).style.width);
		orig_height = parseInt(document.getElementById(elementID).style.height);
		activated = true;
		activated_id = elementID;	
		var num = parseInt(elementID.substr(3));
		var vert;
		var horiz;
		if(num%5==0)
			vert = 2;
		else if(num%5==4)
			vert = 0;
		else
			vert = 1;
		
		if(num<5)
			horiz = 2;
		else if(num>19)
			horiz = 0;
		else
			horiz = 1;
		clock_id = setInterval('go_grow('+vert+','+horiz+')',20);
	}
}
function go_grow(vertical,horizontal)
{
	y+=5;
	
	if(parseInt(document.getElementById(activated_id).style.width)>=max_width)
	{
		clearInterval(clock_id);
		document.getElementById('info_'+activated_id).style.display = 'inline';
		document.getElementById('info_'+activated_id).style.left = (parseInt(document.getElementById(activated_id).style.left)+((parseInt(document.getElementById(activated_id).style.width)-400)/2))+"px";
		document.getElementById('info_'+activated_id).style.top = (parseInt(document.getElementById(activated_id).style.height)+parseInt(document.getElementById(activated_id).style.top))+"px";
		
	}
	else
	{
		document.getElementById(activated_id).style.height = (orig_height*(1+(y/33)))+"px";
		switch(vertical)
		{
			case 0: document.getElementById(activated_id).style.left = (orig_x - (orig_width*(y/33)))+"px";
			break;
			case 1: document.getElementById(activated_id).style.left = (orig_x - (orig_width*(y/66)))+"px";
			break;
			case 2: //do nothing
			break;
		}
	
		document.getElementById(activated_id).style.width = (orig_width*(1+(y/33)))+"px";
		switch(horizontal)
		{
			case 0: document.getElementById(activated_id).style.top =  (orig_y - (orig_height*(y/33)))+"px";
			break;
			case 1: document.getElementById(activated_id).style.top =  (orig_y - (orig_height*(y/66)))+"px";
			break;
			case 2: //do nothing
			break;
		}
	}
	
	if(y>100)
	{
		clearInterval(clock_id);	
		document.getElementById('info_'+activated_id).style.display = 'inline';
		document.getElementById('info_'+activated_id).style.left = (parseInt(document.getElementById(activated_id).style.left)+((parseInt(document.getElementById(activated_id).style.width)-400)/2))+"px";
		document.getElementById('info_'+activated_id).style.top = (parseInt(document.getElementById(activated_id).style.height)+parseInt(document.getElementById(activated_id).style.top))+"px";
	}
}

function add_tag(num_tags)
{

	var tag_data = "?";
	
	var first = true;
   for(i=0; i<document.forms[2].elements.length; i++){
	  if(document.forms[2].elements[i].name.indexOf('tag')!=-1)
	  {
	  if(first)
	   {
		   first = false;
		   tag_data+=document.forms[2].elements[i].name+"="+document.forms[2].elements[i].value;
	   }
	   else
	   {
		   tag_data+="&"+document.forms[2].elements[i].name+"="+document.forms[2].elements[i].value;
	   }
	  }
   }
	/*
	for(var x = 0; x < num_tags; x++)
	{
		if(x==0)
			tag_data=tag_data+"tag["+x+"]="+document.getElementById('tag['+x+']').value.replace(/&/g, "QAMP");
		else
			tag_data=tag_data+"&tag["+x+"]="+document.getElementById('tag['+x+']').value.replace(/&/g, "QAMP");
	
	tag_data=tag_data+"&tag_id["+x+"]="+document.getElementById('tag_id['+x+']').value.replace(/&/g, "QAMP");
	
	}*/
	setContent('ajax_tag_div', 'phototags.php'+tag_data, 'GET', '');
	
}


function validate_search(pressed_key,pull_db)
{
	if(pressed_key==13)
		ajax_cur(-5,document.getElementById('searched_text').value,pull_db);
}

function toggleMenu(objID) {
	if (!document.getElementById) return;
	var ob = document.getElementById(objID).style;
	ob.display = (ob.display == 'block') ? 'none' : 'block';
}

function hideMenu(objID) {
	if (!document.getElementById) return;
	var ob = document.getElementById(objID).style;
	ob.display = 'none';
}

var hover = "enabled";
var root = "nav";

function show(objID, objNum) {
	if(hover == "enabled") {
		ob = document.getElementById(objID + '' + objNum);
		ob.style.display = "block";
		//ob.style.visibility = "visible";
	}
}

function hide(objID, objNum) {
	if(hover == "enabled") {
		ob = document.getElementById(objID + '' + objNum);
		ob.style.display = "none";
		//ob.style.visibility = "hidden";
	}
}

function change_all(objID)
{
	$('button').each(function(i)
	{
		if(	this.id.indexOf("main_tab")!=-1)
		{
			this.style.backgroundColor = 'rgb(255, 255, 255)';
			this.style.color = 'rgb(63, 86, 120)';
		}						  
	});	
	swap_style(objID);
}


function swap_style(objID)
{
	var back_color = document.getElementById(objID).style.backgroundColor;
	document.getElementById(objID).style.backgroundColor = document.getElementById(objID).style.color;
	document.getElementById(objID).style.color = back_color;
}

var cur_cat = -1;

function blind_cur(query_use)
{
	var content = query_use.split('&');
	if(content[0][content[0].length-1]=='=' || query_use == '')
	{
		query_use = '?page=1&ipp=10&cur=-1&pull_db=no';
	}
	setContent('disp_images_ajax', 'http://www.law2.byu.edu/image_db/includes/content.php'+query_use, 'GET', '');
}

function ajax_cur(switch_to, search_query,get_query)
{
	var action = new Array();
	action[0] = 'titles_search';
	action[1] = 'captions_search';
	action[2] = 'authors_search';
	action[3] = 'tags_search';
	action[4] = 'sources_search';
	action[5] = 'paths_search';
	action[6] = 'date_created_search';
	action[7] = 'date_submitted_search';
	var total = 0;
	for(var y = 0; y < 8; y++)
	{
		if(document.getElementById(action[y]).checked == 1)
		total+= Math.pow(2,y);
		else
		action[y] = 0;

	}
	
	var ipp = document.getElementById('page_set').options[ document.getElementById('page_set').selectedIndex].value;
	
	//alert(document.getElementById('titles_search').checked);
	if(switch_to!=-5)
	{
		cur_cat = switch_to;
		if(search_query&&search_query.length>2)
			setContent('disp_images_ajax', 'http://www.law2.byu.edu/image_db/includes/content.php?page=1&ipp='+ipp+'&cur='+switch_to+'&query='+search_query+'&restrictions='+total+get_query, 'GET', '');
		else
			setContent('disp_images_ajax', 'http://www.law2.byu.edu/image_db/includes/content.php?page=1&ipp='+ipp+'&cur='+switch_to+get_query, 'GET', '');
	}
	else
	{
		if(search_query&&search_query.length>2)
			setContent('disp_images_ajax', 'http://www.law2.byu.edu/image_db/includes/content.php?page=1&ipp='+ipp+'&cur='+cur_cat+'&query='+search_query+'&restrictions='+total+get_query, 'GET', '');
		else
			setContent('disp_images_ajax', 'http://www.law2.byu.edu/image_db/includes/content.php?page=1&ipp='+ipp+'&cur='+cur_cat+get_query, 'GET', '');

	}
}

function password_delete(pwd)
{
	if (pwd=='jrcbls')
	 {	 
		document.getElementById('delete_pwd').style.display = 'none';
		document.getElementById('deleter_button').style.display = 'inline';	 
	 }
	 
	
	
}


/*** This function handles the resizing of iFrames to 100% of the screen size ***/
// The -140 and -40 take care of space used by the header and footer
// the id "framer" must be included on iframes that are being resized
function resize_iframe() {
	var height = window.innerWidth - 130 - 30; //Firefox
	if (document.body.clientHeight) {height=document.body.clientHeight - 130 - 30;} //IE
	//resize the iframe according to the size of the
	//window (all these should be on the same line)
	document.getElementById("framer").style.height=parseInt(height-
	document.getElementById("framer").offsetTop - 8)+"px";
}

// this will resize the iframe every time you change the size of the window.
// window.onresize = resize_iframe;
