


function openWin(theURL,winName,features) {
	window.open(theURL,winName,features);
}

function openPhotoBrowser(t,d)
{
  if(t == undefined || t.length == 0) t = 'editors';
  if(d == undefined || d.length == 0) d = 'photo_browser';
	openWin('/admin/'+t+'/'+d,'photoBrowser','scrollbars=yes,width=750px,height=600px');
}
function openPhotoBrowserTags(t,d)
{
  if(t == undefined || t.length == 0) t = 'editors';
  if(d == undefined || d.length == 0) d = 'photo_browser';
	openWin('/admin/'+t+'/'+d+'/minWidth.640/page.1/language.en','photoBrowser','scrollbars=yes,width=900px,height=800px');
}

function openPhotoBrowserExperts(t,d)
{
  if(t == undefined || t.length == 0) t = 'experts';
  if(d == undefined || d.length == 0) d = 'photo_browser';
	openWin('/admin/'+t+'/'+d,'photoBrowser','scrollbars=yes,width=750px,height=600px');
}

function openVideoBrowser(t,d)
{
  if(t == undefined || t.length == 0) t = 'editors';
  if(d == undefined || d.length == 0) d = 'video_browser';
	openWin('/admin/'+t+'/'+d,'videoBrowser','scrollbars=yes,width=750px,height=500px');
}

function openVideoBrowserExperts(t,d)
{
  if(t == undefined || t.length == 0) t = 'experts';
  if(d == undefined || d.length == 0) d = 'video_browser';
	openWin('/admin/'+t+'/'+d,'videoBrowser','scrollbars=yes,width=750px,height=500px');
}

function openAddPhoto(t,d)
{
  if(t == undefined || t.length == 0) t = 'editors';
  if(d == undefined || d.length == 0) d = 'photo_editor';
	openWin('/admin/'+t+'/'+d+'/action.add','photoBrowser','scrollbars=yes,width=900px,height=800px');
}

function openArticleBrowser(t,d)
{
  if(t == undefined || t.length == 0) t = 'editors';
  if(d == undefined || d.length == 0) d = 'editor_picks';
  openWin('/admin/'+t+'/'+d+'/action.articlepop','articleBrowser','scrollbars=yes,width=850px,height=600px');
}

function openArticleBrowserExperts()
{
  if(t == undefined || t.length == 0) t = 'experts';
  if(d == undefined || d.length == 0) d = 'expert_picks';
  openWin('/admin/'+t+'/'+d+'/action.articlepop','articleBrowser','scrollbars=yes,width=850px,height=500px');
}

function openListingsBrowser(t,d)
{
  if(t == undefined || t.length == 0) t = 'editors';
  if(d == undefined || d.length == 0) d = 'article_editor';
  openWin('/admin/'+t+'/'+d+'/action.addListing','listingBrowser','scrollbars=yes,width=850px,height=500px');
}

function openNewAuthor()
{
	openWin('/admin/editors/author_editor/action.add','authorBrowser','scrollbars=yes,width=850px,height=200px');
}


function openTagEditor()
{
	openWin('/admin/editors/tag_editor/action.add','tagEditor','scrollbars=yes,width=850px,height=450px');
}


function videoRemove()
{
	var videoObjLength = document.getElementById('video_files').options.length;
	for(i=0;i<videoObjLength;i++)
	{
		if(document.getElementById('video_files').options[i].selected)
		{
			var selOption = document.getElementById('video_files').options[i];
			selOption.parentNode.removeChild(selOption);
		}
	}
}

function addVideo(videoID,videoName)
{
	//check existing option
	var bFalseVideo = 0;

	var existingOpts = document.getElementById('video_files').options.length;

	if(existingOpts > 0 )
	{
		for(i=0;i<existingOpts;i++)
		{
			var optID = document.getElementById('video_files').options[i].value.split("_");
			if(parseInt(optID[1]) == parseInt(videoID))
			{
				bFalseVideo = 0;
				break;
			}
			else
			{
				bFalseVideo = 1;
			}
		}

		if(bFalseVideo == 1)
		{
			var videoOption = new Option(videoName,'video_'+videoID);
			document.getElementById('video_files').options[existingOpts++] = videoOption;
		}
	}
	else
	{
		var videoOption = new Option(videoName,'video_'+videoID);
		document.getElementById('video_files').options[0] = videoOption;
	}
}



function articleRemove()
{
	var articleObjLength = document.getElementById('article_files').options.length;
	for(i=0;i<articleObjLength;i++)
	{
		if(document.getElementById('article_files').options[i].selected)
		{
			var selOption = document.getElementById('article_files').options[i];
			selOption.parentNode.removeChild(selOption);
		}
	}
}


function addArticle(articleID,articleName)
{
	//check existing option
	var bFalseArticle = 0;

	var existingOpts = document.getElementById('article_files').options.length;

	if(existingOpts > 0 )
	{
		for(i=0;i<existingOpts;i++)
		{
			var optID = document.getElementById('article_files').options[i].value.split("_");
			if(parseInt(optID[1]) == parseInt(articleID))
			{
				bFalseArticle = 0;
				break;
			}
			else
			{
				bFalseArticle = 1;
			}
		}

		if(bFalseArticle == 1)
		{
			var articleOption = new Option(articleName,'article_'+articleID);
			document.getElementById('article_files').options[existingOpts++] = articleOption;
		}
	}
	else
	{
		var articleOption = new Option(articleName,'article_'+articleID);
		document.getElementById('article_files').options[0] = articleOption;
	}
}

function addListing(listingID,listingName)
{
	//check existing option
	var bFalseListing = 0;
	var listingOption = '';
	var existingOpts = document.getElementById('listing_box').options.length;

	if(existingOpts > 0 )
	{
		for(i=0;i<existingOpts;i++)
		{
			var optID = document.getElementById('listing_box').options[i].value.split("_");
			if(parseInt(optID[1]) == parseInt(listingID))
			{
				bFalseListing = 0;
				break;
			}
			else
			{
				bFalseListing = 1;
			}
		}

		if(bFalseListing == 1)
		{
			listingOption = new Option(listingName,'listing_'+listingID);
			document.getElementById('listing_box').options[existingOpts++] = listingOption;
		}
	}
	else
	{
		listingOption = new Option(listingName,'listing_'+listingID);
		document.getElementById('listing_box').options[0] = listingOption;
	}
}

function addPhoto(x,photoID)
{
  if(document.getElementById('defaultPhotoInput'))
  {
    return addTagPhoto(x,photoID);
  }

	var currentElements = new Array();
	var showElements = new Array();
	var elementsValues = new Array();
	var bFalse = '';

	var nodes = document.getElementById('photo_row').childNodes.length;


	//alert(nodes.length);

	if(nodes > 0)
	{
		for(i=0;i<nodes;i++)
		{
			currentElements.push(document.getElementById('photo_row').childNodes[i].id);
		}

		for(z=0;z<currentElements.length;z++)
		{

			if(parseInt(currentElements[z]) == parseInt(photoID))
			{
				var bFalse = 0;
				break;
			}
			else
			{
				bFalse = 1;
			}
		}
	}
	else
	{
		bFalse = 1;
	}


	if(bFalse == 1)
	{

		var img = document.createElement('img');
		img.setAttribute("src","/images/photos/"+x);
		img.setAttribute("id",photoID);
		img.setAttribute("border","1");
		img.setAttribute("hspace","5");
		img.setAttribute("vspace","5");
		img.onclick = new Function("removePhoto("+photoID+")");

		var hiddenElement = document.createElement('input');
		hiddenElement.type = "hidden";
		hiddenElement.name = "photos[]";
		hiddenElement.value = photoID;

		document.getElementById('photo_row').appendChild(img);

		document.getElementsByTagName('form')[0].appendChild(hiddenElement);

		if((nodes%4) == 0 )
		{
			if(nodes > 4)
			{
				if(document.getElementById('photo_row').lastChild.nodeName == "BR")
				{
					document.getElementById('photo_row').removeChild(document.getElementById('photo_row').lastChild);
				}
				if(document.getElementById('photo_row').firstChild.nodeName == "BR")
				{
					document.getElementById('photo_row').removeChild(document.getElementById('photo_row').firstChild);
				}
			}

			var BR = document.createElement('br');
			document.getElementById('photo_row').appendChild(BR);
		}
	}
}

function removePhoto(photoID)
{
  if(document.getElementById('defaultPhotoInput'))
  {
    return removeTagPhoto(photoID);
  }
	var element = document.getElementById(photoID);
	element.parentNode.removeChild(element);

	var hiddenLength = document.getElementsByName('photos[]').length;
	

	if(hiddenLength > 0)
	{
		for(x=0;x<hiddenLength;x++)
		{
			if(document.getElementsByName('photos[]')[x].value == photoID)
			{
				document.getElementsByName('photos[]')[x].value = '';
				document.getElementsByName('photos[]')[x].length = hiddenLength-1;
				document.getElementsByName('photos[]')[x].parentNode.removeChild(document.getElementsByName('photos[]')[x]);
				break;
			}
		}
	}
}

function addTagPhoto(x,photoID)
{

	var currentElements = new Array();
	var showElements = new Array();
	var elementsValues = new Array();
	var bFalse = '';

	var nodes = document.getElementById('photo_row').childNodes.length;


	//alert(nodes.length);

	if(nodes > 0)
	{
		for(i=0;i<nodes;i++)
		{
			currentElements.push(document.getElementById('photo_row').childNodes[i].id);
		}

		for(z=0;z<currentElements.length;z++)
		{

			if(parseInt(currentElements[z]) == parseInt(photoID))
			{
				var bFalse = 0;
				break;
			}
			else
			{
				bFalse = 1;
			}
		}
	}
	else
	{
		bFalse = 1;
	}


	if(bFalse == 1)
	{

		var img = document.createElement('img');
		img.setAttribute("src","/images/photos/"+x);
		img.setAttribute("id",photoID);
		img.setAttribute("hspace","5");
		img.setAttribute("vspace","5");
    img.setAttribute('style','border:4px solid #ffffff;');
    img.setAttribute('onclick','displayPhoto(this)');

		var hiddenElement = document.createElement('input');
		hiddenElement.type = "hidden";
		hiddenElement.name = "photos[]";
		hiddenElement.value = photoID;

		document.getElementById('photo_row').appendChild(img);

		document.getElementsByTagName('form')[0].appendChild(hiddenElement);

		if((nodes%4) == 0 )
		{
			if(nodes > 4)
			{
				if(document.getElementById('photo_row').lastChild.nodeName == "BR")
				{
					document.getElementById('photo_row').removeChild(document.getElementById('photo_row').lastChild);
				}
				if(document.getElementById('photo_row').firstChild.nodeName == "BR")
				{
					document.getElementById('photo_row').removeChild(document.getElementById('photo_row').firstChild);
				}
			}

			var BR = document.createElement('br');
			document.getElementById('photo_row').appendChild(BR);
		}
	}
}

function removeTagPhoto(photo)
{
	photo.parentNode.removeChild(photo);

  if(photo.id == document.getElementById('defaultPhotoInput').value)
  {
    document.getElementById('defaultPhotoInput').value = '';
  }

	var hiddenLength = document.getElementsByName('photos[]').length;


	if(hiddenLength > 0)
	{
		for(x=0;x<hiddenLength;x++)
		{
			if(document.getElementsByName('photos[]')[x].value == photo.id)
			{
				document.getElementsByName('photos[]')[x].value = '';
				document.getElementsByName('photos[]')[x].length = hiddenLength-1;
				document.getElementsByName('photos[]')[x].parentNode.removeChild(document.getElementsByName('photos[]')[x]);
				break;
			}
		}
	}
}


function selectAllLists()
{
	var Taglists = document.getElementById('multibox').options.length;
	for(i=0;i<Taglists;i++)
	{
		document.getElementById('multibox').options[i].selected = "selected" ;
	}

	var VideoLists = document.getElementById('video_files').options.length;
	for(z=0;z<VideoLists;z++)
	{
		document.getElementById('video_files').options[z].selected = "selected";
	}
}

function startRemovePhoto()
{
  $('input#removePhoto').val('Cancel Remove Photo');
  $('input#removePhoto').bind('click',stopRemovePhoto);
  $('#photo_row img').unbind('click');
  $('#photo_row img').bind('click',function(){if(confirm('Do you want to remove this photo?')) { stopRemovePhoto();removeTagPhoto(this); }});

}

function stopRemovePhoto()
{
  $('#photo_row img').unbind('click');
  $('input#removePhoto').val('Remove Photo');
  $('input#removePhoto').bind('click',startRemovePhoto);
  $('#photo_row img').bind('click',displayPhoto);
}

function startDefaultPhoto()
{
  $('input#defaultPhoto').val('Cancel Default Photo');
  $('input#defaultPhoto').bind('click',stopDefaultPhoto);
  $('#photo_row img').unbind('click');
  $('#photo_row img').bind('click',function(){if(confirm('Do you want to make this photo the default?')) { defaultPhoto(this);stopDefaultPhoto(); }});

}

function stopDefaultPhoto()
{
  $('#photo_row img').unbind('click');
  $('input#defaultPhoto').val('Default Photo');
  $('input#defaultPhoto').bind('click',startDefaultPhoto);
  $('#photo_row img').bind('click',displayPhoto);
}

function defaultPhoto(img)
{
  $('#photo_row img').css('border-color','#ffffff');
  $(img).css('border-color','#ed037c');
  $('#defaultPhotoInput').val(img.id);
}

function displayPhoto(img)
{
  var tmb = $(img).attr('src');
  $(img).attr('src',$(img).attr('longdesc'));
  $(img).attr('longdesc',tmb);
}


