function getElement(aID) // automatically set iframe to same height as iframe content
{
	return (document.getElementById) ?
		document.getElementById(aID) : document.all[aID];
}

function getIFrameDocument(aID){  // automatically set iframe to same height as iframe content
	var rv = null; 
	var frame=getElement(aID);
	// if contentDocument exists, W3C compliant (e.g. Mozilla) 

	if (frame.contentDocument)
		rv = frame.contentDocument;
	else // bad IE  ;)

		rv = document.frames[aID].document;
	return rv;
}

function adjustMyFrameHeight() // automatically set iframe to same height as iframe content
{
	var frame = getElement("myFrame");
	var frameDoc = getIFrameDocument("myFrame");
	frame.height = frameDoc.body.offsetHeight;
}

function AddAlbum() {
	if($('postnewalbum').style.display == 'none') {
		$('settings').hide();
		$('pagecontent').show();
		$('postnewalbum').show();
	} else {
		$('postnewalbum').hide();
	}
}

function editAlbum() {
	if($('passwordbox').style.display == 'none') {
		$('passwordbox').show();
	} else {
		$('passwordbox').hide();
	}
}

function clickalbum(id)
{
	{
		var clicks = $('detail_clicks'+id).innerHTML;
		var totalclicks = (parseFloat(clicks)+1);  // parsefloat converts string to number
		$('detail_clicks'+id).innerHTML = totalclicks + ' clicks';

		var ajax = new Ajax.Request('gallery/public_controller.php',
		{	method: 'get',
			parameters: 'request_name=increase_clicks_public'+
									'&album_id='+id
		});
		// pausecomp(200); // scotty pause a little so it can write # of clicks to db
		// var url = $F('album_url'+obj);
		// window.location = url;
	}
}

function admin(menu_case) 
{
	$('settings').style.marginTop='33px';
	$('settings').innerHTML = '<img src="images/ajax-loader.gif">';
	if(menu_case == 'add_album') {
		$('settings').show();
	} else if(menu_case == 'add_mass_albums') {
		$('pagecontent').hide();
		$('settings').show();
	} else if (menu_case == 'settings')
	{
		$('settings').show();
		$('pagecontent').hide();
	} else if (menu_case == 'delete_all')
	{
		$('settings').show();
		$('pagecontent').hide();
	} else if (menu_case == 'auto_generate_thumbs')
	{
		$('settings').show();
		$('pagecontent').hide();
	};

	var params = 'request_name='+menu_case;
	var ajax   = new Ajax.Updater(
	 {success: 'settings'},
		'gallery/public_controller.php',
	 {method: 'get', parameters: params}
	 );
}


function Canceladmin(menu_case) 
{
	$('pagecontent').show();
	$('settings').hide();
}

function save_public_album (album_id) {
	var params = Form.serialize('form_public_album');

	var params ='request_name='+'update_publicalbum&'+params;

	var ajax   = new Ajax.Updater(
	 {success:refreshPage()},
		'gallery/public_ajax_controller.php',
	 {method: 'get', parameters: params}
	 );
}

function add_mass_albums(id)
{
	var params = Form.serialize('form_mass_add');
	var ajax = new Ajax.Request('gallery/public_ajax_controller.php',
	{	method: 'get',
		parameters: 'request_name=add_mass_albums&id='+id+'&'+params,
		onComplete: function(originalRequest) {
			refreshPage();
	}});
}

function create_thumbnails_from_google(id)
{
	$('progressmeter').innerHTML='<img src="images/ajax-loader.gif">';
	var ajax = new Ajax.Request('gallery/public_ajax_controller.php',
	{	method: 'get',
		parameters: 'request_name=create_thumbnails_from_google&id='+id,
		onComplete: function(originalRequest) {
		$('progressmeter').innerHTML='Thumbnails DONE!';

	}});
}


function get_thumbnail(id)
{
	$('maindiv'+id).innerHTML = '<img src="images/ajax-loader.gif">';
	var ajax = new Ajax.Request('gallery/public_ajax_controller.php',
	{	method: 'get',
		parameters: 'request_name=get_thumbnail&id='+id,
		onComplete: function(originalRequest) {
			$('maindiv'+id).innerHTML = originalRequest.responseText;;
	}});
}


function add_external_style () 
{
	if ($F('externalstylesheet')==0) {
	Form.Element.Methods.disable('style_id');
	$('ext_stylesheet').show();
	} 
	else {
	Form.Element.Methods.enable('style_id');
	$('ext_stylesheet').hide();
	} 
}

function checkurl_admin()
{
	$('albumname_label').style.color="black";$('albumname_label').style.fontWeight="normal";	
	$('urltoalbum_label').style.color="black";$('urltoalbum_label').style.fontWeight="normal";	
	$('urltothumb_label').style.color="black";$('urltothumb_label').style.fontWeight="normal";	

	var checkurl = $F('urltoalbum'); // check the URL to make sure it has "http://" by scotty
	var checkurl2 = $F('urltothumb'); // check the URL to make sure it has "http://" by scotty
    var v = new RegExp();
	var t = new RegExp();
    v.compile("^[A-Za-z]+://[A-Za-z0-9-]+\.[A-Za-z0-9]+");
	t.compile(".*\.(gif|png|jpg|jpeg|JPG|GIF|PNG|JPEG)");

	var error = false;

	if ($F('albumname') == '')
	{
		$('albumname_label').style.color="Red";
		$('albumname_label').style.fontWeight="bold";	
		error = true;
	}
    if (checkurl !='' && !v.test(checkurl)) { // updated by scotty
		$('urltoalbum_label').style.color="Red";
		$('urltoalbum_label').style.fontWeight="bold";		
		error = true;
    } 
	
	if (checkurl2 != '' && !t.test(checkurl2))
    {
		alert("You must have a valid Thumbnail URL that ends in JPG, PNG, or GIF file");
		$('urltothumb_label').style.color="Red";
		$('urltothumb_label').style.fontWeight="bold";
		error = true;
    }
	if(error) return false;
	else {
		return true;
	}

}

function checkurl()
{
	$('albumname_label').style.color="black";$('albumname_label').style.fontWeight="normal";	
	$('urltoalbum_label').style.color="black";$('urltoalbum_label').style.fontWeight="normal";	
	$('urltothumb_label').style.color="black";$('urltothumb_label').style.fontWeight="normal";	
	$('securitycode_label').style.color="black";$('securitycode_label').style.fontWeight="normal";	

	var checkurl = $F('urltoalbum'); // check the URL to make sure it has "http://" by scotty
	var checkurl2 = $F('urltothumb'); // check the URL to make sure it has "http://" by scotty
    var v = new RegExp();
	var t = new RegExp();
    v.compile("^[A-Za-z]+://[A-Za-z0-9-]+\.[A-Za-z0-9]+");
	t.compile(".*\.(gif|png|jpg|jpeg|JPG|GIF|PNG|JPEG)");

	var error = false;

	if ($F('albumname') == '')
	{
		$('albumname_label').style.color="Red";
		$('albumname_label').style.fontWeight="bold";	
		error = true;
	}
    if (checkurl !='' && !v.test(checkurl)) { // updated by scotty
		$('urltoalbum_label').style.color="Red";
		$('urltoalbum_label').style.fontWeight="bold";		
		error = true;
    } 
	
	if (checkurl2 != '' && !t.test(checkurl2))
    {
		alert("You must have a valid Thumbnail URL that ends in JPG, PNG, or GIF file");
		$('urltothumb_label').style.color="Red";
		$('urltothumb_label').style.fontWeight="bold";
		error = true;
    }
	if ($F('security_code') == '')
    {
		$('securitycode_label').style.color="Red";
		$('securitycode_label').style.fontWeight="bold";
		error = true;
    } 
	if(error) return false;
	else {
		return true;
	}

}


function addGalleryCheck()
{
	$('title_label').style.color="black";$('title_label').style.fontWeight="normal";	
	$('securitycode_label').style.color="black";$('securitycode_label').style.fontWeight="normal";	
	$('publicpassword_label').style.color="black";$('publicpassword_label').style.fontWeight="normal";	

	var error = false;

	if ($F('title') == '')
	{
		$('title_label').style.color="Red";
		$('title_label').style.fontWeight="bold";	
		error = true;
	}

	if ($F('publicpassword') == '')
	{
		$('publicpassword_label').style.color="Red";
		$('publicpassword_label').style.fontWeight="bold";	
		error = true;
	}

	if ($F('security_code') == '')
    {
		$('securitycode_label').style.color="Red";
		$('securitycode_label').style.fontWeight="bold";
		error = true;
    } 
	if(error) return false;
	else {
		return true;
	}

}

function save_editedurl(id, i)
{

	/* 
	var checkurl = $('editform'+i).urltoalbum.value; // check the URL to make sure it has "http://" by scotty
	var checkthumb = $('editform'+i).urltothumb.value; // check the URL to make sure it has "http://" by scotty
    var v = new RegExp();
    v.compile("^[A-Za-z]+://[A-Za-z0-9-]+\.[A-Za-z0-9]+");
    if (!v.test(checkurl) || (checkthumb == '')) {
        alert("You must have a valid URL for the album and album thumbnail and include http://");
        return false;
    }
	*/
	var params = Form.serialize('editform'+i);

	var params ='request_name='+'editurl&'+params;
	var ajax = new Ajax.Request('gallery/public_ajax_controller.php',
	{	method: 'get',
		parameters: params,
		onComplete: function(originalRequest) {
			$('maindiv'+i).innerHTML = originalRequest.responseText;;
	}});

}


function cancel_edit_url (i) {
	$('details'+i).style.width = '250px'; 
	$('details'+i).show();
	$('editdiv'+i).hide();
	$('section'+i).show();
}

function edit_urltoalbum(id, album_num, mode)
{
	var mode = mode.toLowerCase();

	if(mode == 'hide')
		if($('detail_hidden'+album_num).innerHTML == 'hidden album') mode = 'show'; else mode = 'hide';

	if(confirm("Do you really want to " + mode + " album ?"))
	{
		if(mode == 'hide')
		{
			$('detail_hidden'+album_num).innerHTML = 'hidden album';
			$('hbtn'+album_num).innerHTML = 'Show';
			mode = 'hide';
		}
		if(mode == 'show')
		{
			$('detail_hidden'+album_num).innerHTML = '';
			$('hbtn'+album_num).innerHTML = 'Hide';
			mode = 'hide';
		}
		var params ='request_name='+mode+'url'+
								'&id='+id;
		var ajax = new Ajax.Request('gallery/public_ajax_controller.php',
		{	method: 'get',
			parameters: params,
			onComplete: function(originalRequest) {
				$('result').innerHTML = originalRequest.responseText;
				if(mode == 'delete') Effect.Fade('album'+id, {duration:.5});
				p_disappear = new PeriodicalExecuter(disappear_result, 4);
		}});
	}
}


function refreshPage() {
	location.reload(true);
}



function disappear_result()
{
	new Effect.Opacity('result', { duration: 1.0, from: 1.0, to: 0.0, afterFinish: delete_result});
}

function delete_result()
{
	$('result').innerHTML = "";
	$('result').style.opacity = 1;
	p_disappear.stop();
}


function goHome() {
	document.location = './index.php';
}

function check_searchenginetype() {
	var value = $F('searchenginetype');
	if (value == '7')
	{
	$('amazon_id_form').show();
	} else {
	$('amazon_id_form').hide();
	}
}

function change2File()
{
	$('file').show();
	$('url').hide();
	$('urltothumb_label').innerHTML = 'Upload Thumbnail:';
	// $F('url') = '';
}

function change2Link()
{
	$('url').show();
	$('file').hide();
	$('urltothumb_label').innerHTML = 'Thumbnail image URL:';
	$F('file') = '';
}

function change2Link2(id)
{
	$('url'+id).show();
	$('file'+id).hide();
	$('urltothumb_label'+id).innerHTML = 'Thumbnail URL:';
	$F('file'+id) = '';
}


function change2File2(id)
{
	$('file'+id).show();
	$('url'+id).hide();
	$('urltothumb_label'+id).innerHTML = 'Upload Thumbnail:';
	// $F('url') = '';
}

/* search */
var animcounter = 0;
var animInterval = null;

function goAnim() {
	document.getElementById('instantIndicator').style.display = ''; // hide search animation
	animInterval = setInterval('startAnim()', 50);
}

function startAnim() {
	//document.images[animcounter].style.display = 'none';
	document.getElementById('anim_'+animcounter).style.visibility = 'hidden';
	animcounter = (animcounter+1) % 7;
	//document.images[animcounter].style.display = '';
	document.getElementById('anim_'+animcounter).style.visibility = 'visible';
}
function stopAnim() {
	document.getElementById('instantIndicator').style.display = 'none'; // hide search animation
	clearInterval(animInterval);
	// document.getElementById('instantIndicator').style.visibility = 'hidden'; // hide search animation
}

function enter_pressed(e){
var keycode;
if (window.event) keycode = window.event.keyCode;
else if (e) keycode = e.which;
else return false;
return (keycode == 13);
}

function search_result(start_item) {
	if (!start_item)
	{
		start_item=0;
	};
	goAnim();
	var search_item = $F('searchKeyWord');
	$('maincontent').hide();
	$('search-box-div').show();

	var ajax = new Ajax.Request('gallerysearch.php',
	{	method: 'get',
		parameters: 'searchterm='+search_item+'&start_item='+start_item,
		onComplete: function(originalRequest) {
	$('search-box-div').innerHTML = originalRequest.responseText;
	setTimeout(stopAnim, 100);
	}});


}

function hideResults() {
	$('maincontent').show();
	$('search-box-div').hide();
}
