
	var imageArguments;
	var currentImage=0;

	var images = new Array();
	var delImages = new Array();

	var pubimgdir = "/images";
        var imgdir = "";

	var captions = new Array();

	var iphotographer = new Array();
	
	var iowner = new Array();
	
	var iwebref = new Array();
	

	function addImages(img){

		images.push(img);
		captions.push('');
		buildImages();

	}

	function buildImages(){
		
		var imgHtml="";
		var imgdesc;
		var critinfo;
				
		for(x=0;x<images.length;x++){
			
			imgSrc = images[x];
            orgSrc = imgSrc.replace("showcase/", "showcase/original/orig_");
            
            if (x == 0) imgHtml+='<a href="uploader.php" class="uploadImage"><img src="/images/addpic_button.png" border="0" onMouseOver="window.status=\'Add an Additional Image\'; this.style.cursor=\'hand\'" align="right" title="Add an Image" /></a><br /><br />';
			imgHtml+='<table border="0"><tr><td><img class="imagelink" id="theimage'+x+'" img border="1" src="'+imgSrc+'" hspace="8" title="'+images[x]+'" onmouseover="this.style.cursor=\'hand\'" onmouseout="this.style.cursor=\'text\'" width="400"></td></tr><tr><td align="left"><img title="remove '+images[x]+'" align="left" src="'+ pubimgdir +'/delete_button.png" onmouseover="this.style.cursor=\'hand\'" onmouseout="this.style.cursor=\'text\'" onclick="removeImage('+x+')" hspace="5">&nbsp;&nbsp;<a title="Move Up" href="javascript:moveImage('+x+',-1)">[UP]</a>&nbsp;&nbsp;<a title="Move Down" href="javascript:moveImage('+x+',1)">[DOWN]</a>&nbsp;&nbsp;<a title="Download Original Image" href="'+orgSrc+'" target="_blank">[ORIGINAL]</a>&nbsp;&nbsp;<a title="Edit Caption" href="#" onclick="addImageCaption('+x+'); return false;">[CAPTION]</a></td></tr><tr><td><div id="img' + x + 'caption" style="font-style:italic;">'+captions[x]+'</div></td></tr></table>';
			//imgHtml+='<table border="0"><tr><td><img class="imagelink" id="theimage'+x+'" img border="1" src="'+imgSrc+'" hspace="8" title="'+images[x]+'" onmouseover="this.style.cursor=\'hand\'" onmouseout="this.style.cursor=\'text\'" width="400"></td></tr><tr><td align="left"><img title="remove '+images[x]+'" align="left" src="'+ pubimgdir +'/delete_button.png" onmouseover="this.style.cursor=\'hand\'" onmouseout="this.style.cursor=\'text\'" onclick="removeImage('+x+')" hspace="5">&nbsp;&nbsp;<a href="javascript:moveImage('+x+',-1)">[UP]</a>&nbsp;&nbsp;<a href="javascript:moveImage('+x+',1)">[DOWN]</a>&nbsp;&nbsp;<a href="'+orgSrc+'" target="_blank">[DOWNLOAD ORIGINAL]</a></td></tr></table>';
			
			delete imageinfo;	
		}
		
		document.getElementById('imageDiv').innerHTML = imgHtml;
		if(typeof initLinks == 'function') { initLinks(); }
	}
	
	function moveImage(mid, mdir){
		if(mid+mdir >= 0 && mid+mdir < images.length){
			mtemp = images[mid];
			images[mid] = images[mid+mdir];
			images[mid+mdir] = mtemp;
		}
		buildImages();
	}
	
	function buildImages4Editor(){
		
		var imgHtml="";
		var imgdesc;
		var critinfo;
				
		for(x=0;x<images.length;x++){
			if (x == 0) imgdesc = images[x];
			if (x > 0) imgdesc += "," + images[x];
		}
		
		$.get("query.php?credits=" + imgdesc, function(critinfo){
			
		    var imgpart = critinfo.split("::");
			
			for(x=0;x<images.length;x++){
				
				var imageinfo;
				if (imgpart[x]) imageinfo = imgpart[x].split(",");
				
				//imgHtml +="Debug: "+ imgdesc + "<br>";
				
				imgSrc=imgdir+ "/" +images[x];
		
				imgHtml+='<table border="0"><tr><td><img class="imagelink" id="theimage'+x+'" img border="1" src="'+imgSrc+'" hspace="8" onclick="changeImage('+x+')" title="click to change image" onmouseover="this.style.cursor=\'hand\'" onmouseout="this.style.cursor=\'text\'" width="280"></td></tr><tr><td align="left"><img title="remove '+images[x]+'" align="left" src="'+ pubimgdir +'/delete_button.png" onmouseover="this.style.cursor=\'hand\'" onmouseout="this.style.cursor=\'text\'" onclick="removeImage('+x+')" hspace="5"><img title="Add imagecaption" align="left" src="'+ pubimgdir +'/caption.gif" onmouseover="this.style.cursor=\'hand\'" onmouseout="this.style.cursor=\'text\'" onclick="addImageCaption('+x+')" hspace="5">&nbsp;<a href="mediagallery.php?id='+imageinfo[3]+'" target="_blank"><span class="imagelink">'+images[x]+'</span></a></td></tr><tr><td><div id="img' + x + 'caption">'+captions[x]+'</div></td></tr>';
				
				if (imageinfo.length == 4){
				if (imageinfo[0].length > 0)
				if (imageinfo[0] != 0)
				imgHtml += '<tr><td><input type="checkbox" name="photographer'+ x +'" value="ON" id="photographer'+ x +'" checked />&nbsp;&nbsp;'+ imageinfo[0] +'</td></tr>';
				
				if (imageinfo[1].length > 0) 
				if (imageinfo[1] != 0)
				imgHtml += '<tr><td><input type="checkbox" name="owner'+ x +'" value="ON" id="owner'+ x +'" checked />&nbsp;&nbsp;'+ imageinfo[1] +'</td></tr>';
				
				if (imageinfo[2].length > 0) 
				if (imageinfo[2] != 0)
				imgHtml += '<tr><td><input type="checkbox" name="webref'+ x +'" value="ON" id="webref'+ x +'" checked />&nbsp;&nbsp;'+ imageinfo[2] +'</td></tr>';
				}
				imgHtml += '</table>';
				
				delete imageinfo;	
			}
			
			document.getElementById('imageDiv').innerHTML = imgHtml;
		
		});
	}

	function query(criteria){
			
			
			$.get("query.php?credits=" + criteria, function(critinfo){
				return critinfo;
			});
	}
	
	function buildImages4Editor2(){
		
		var imgHtml="";
		var imgdesc;
		var critinfo;
				
		for(x=0;x<images.length;x++){
			if (x == 0) imgdesc = images[x];
			if (x > 0) imgdesc += "," + images[x];
		}
		
		$.get("query.php?id=" + newsid + "&credopt=" + imgdesc, function(critaf){
			
			var imgaf = critaf.split("::");
			
			$.get("query.php?credits=" + imgdesc, function(critinfo){
				
				var imgpart = critinfo.split("::");
				
				for(x=0;x<images.length;x++){
					
					var imageinfo;
					var imageaf;
					if (imgpart[x]) imageinfo = imgpart[x].split(",");
					if (imgaf[x]) imageaf = imgaf[x].split(",");
					
					//imgHtml +="Debug: "+ critinfo + "<br>";
					//imgHtml +="Debug: "+ imgpart[x] + "<br>";
					
					imgSrc=imgdir+ "/" +images[x];
			
					imgHtml+='<table border="0"><tr><td><img class="imagelink" id="theimage'+x+'" img border="1" src="'+imgSrc+'" hspace="8" onclick="changeImage('+x+')" title="click to change image" onmouseover="this.style.cursor=\'hand\'" onmouseout="this.style.cursor=\'text\'" width="280"></td></tr><tr><td align="left"><img title="remove '+images[x]+'" align="left" src="'+ pubimgdir +'/delete_button.png" onmouseover="this.style.cursor=\'hand\'" onmouseout="this.style.cursor=\'text\'" onclick="removeImage('+x+')" hspace="5"><img title="Add imagecaption" align="left" src="'+ pubimgdir +'/caption.gif" onmouseover="this.style.cursor=\'hand\'" onmouseout="this.style.cursor=\'text\'" onclick="addImageCaption('+x+')" hspace="5">&nbsp;<a href="mediagallery.php?id='+imageinfo[3]+'" target="_blank"><span class="imagelink">'+images[x]+'</span></a></td></tr><tr><td><div id="img' + x + 'caption">'+captions[x]+'</div></td></tr>';
					
					if (imageinfo.length == 4){
					if (imageinfo[0].length > 0)
					if (imageinfo[0] != 0){
						imgHtml += '<tr><td><input type="checkbox" name="photographer'+ x +'" value="ON" id="photographer'+ x +'"';
						if (imageaf[0] == 1) imgHtml += ' checked';
						imgHtml += ' />&nbsp;&nbsp;'+ imageinfo[0] +'</td></tr>';
					}
					
					if (imageinfo[1].length > 0) 
					if (imageinfo[1] != 0){
						imgHtml += '<tr><td><input type="checkbox" name="owner'+ x +'" value="ON" id="owner'+ x +'"';
						if (imageaf[1] == 1) imgHtml += ' checked';
						imgHtml += ' />&nbsp;&nbsp;'+ imageinfo[1] +'</td></tr>';
					}
					
					if (imageinfo[2].length > 0) 
					if (imageinfo[2] != 0){
						imgHtml += '<tr><td><input type="checkbox" name="webref'+ x +'" value="ON" id="webref'+ x +'"';
						if (imageaf[2] == 1) imgHtml += ' checked';
						imgHtml += ' />&nbsp;&nbsp;'+ imageinfo[2] +'</td></tr>';
					}
					}
					imgHtml += '</table>';
					
					delete imageinfo;	
					delete imageaf;
				}
				
				document.getElementById('imageDiv').innerHTML = imgHtml;
			
			});
		});
	}


	
	function addImageCaption(no){

		//imgCaptionHtml='<textarea name="img'+no+'capArea" id="img'+no+'capArea" class="imgcaptionarea">' + captions[no] + '</textarea><br><img title="add caption" align="left" src="'+ pubimgdir +'/insertc.gif" onmouseover="this.style.cursor=\'hand\'" onmouseout="this.style.cursor=\'text\'" onclick="addCaption('+no+')" hspace="5">';
		imgCaptionHtml='<textarea name="img'+no+'capArea" id="img'+no+'capArea" class="imgcaptionarea" style="width: 415px;">' + captions[no] + '</textarea><br><a title="Save Caption" align="left" href="#" onclick="addCaption('+no+'); return false;">[SAVE]</a>';
		
		capfieldname='img'+no+'caption';
		document.getElementById(capfieldname).innerHTML = imgCaptionHtml;
	}


	function addCaption(no){

		areaname='img'+no+'capArea';
		caption=document.getElementById(areaname).value;

		captions[no]=caption;

		capfieldname='img'+no+'caption';
		document.getElementById(capfieldname).innerHTML = "";
		document.getElementById(capfieldname).innerHTML = caption;

	}


	function changeImage(id) {

		currentImage=id;
		//open('imagegallery.php', 'imagepopup', 'width=640,height=400,scrollbars=1,resizable=1'); 
		$.fn.colorbox({open:true, href:'imagegallery.php', width:"640", height:"500", iframe:true});
	}


	function updateImage(img){

		images[currentImage]=img;
		if (typeof newsid == "undefined")
			buildImages();
		else
			buildImages4Editor();
	}


	function addImage(){

		images.push("topstorypic2.jpg");
		captions.push("");
		if (typeof newsid == "undefined")
			buildImages();
		else
			buildImages4Editor();
	}


	function removeImage(id){

		tempArr = new Array(0);
		tempCapArr = new Array(0);
		delImages.push(images[id]);
		for(x=0;x<images.length;x++){

			if(x!=id){
				imgStr=images[x];
				tempArr.push(imgStr);
				tempCapArr.push(captions[x]);
			} 

		}


		while(images.length > 0){

			images.pop();
			captions.pop();

		}

		for(x=0;x<tempArr.length;x++){

			imageStr=tempArr[x];
			images.push(imageStr);
			captions.push(tempCapArr[x]);
			
		}

		if (typeof newsid == "undefined")
			buildImages();
		else
			buildImages4Editor();

	}





