// JavaScript Document

// function used to mask email address from robots
var lgHeight = 0;

// fire DOM loaded events here
document.observe("dom:loaded", function() {
});


function loadDisplayContainer(dir, elId){
	responseElement = $(elId);
	url = "source/getImages.php";

	reString = "<div id='top_loading'>\n<br>\n";
	reString += "<img src='images/ajax-loader.gif' align='center'/>\n<br>\n";
	reString += "<p align='center'>Loading...</p>\n</div>\n";
	reString += "<div id='bodyWrap'><div id='protoflow'></div></div>";
	reString += "\n<div id='top_control' style='display:none;'><img src='images/left-arrow20.gif' class='arrow' onClick='javascript:pf_top.previous();'><span class='controlText'> Click arrows to scroll. Click image to enlarge. </span><img src='images/right-arrow20.gif' class='arrow' onClick='javascript:pf_top.next();'></div>";
	reString += "\n<div id='conBarTop' class='manageBar' style='display:none'></div>"
	reString += "\n<div id='groupMessage'><p>Clicking a group will expand it here</p></div>";
	responseElement.update(reString);
	responseElement.style.height='';
	random_num = Math.round( ( Math.random() * 666 ) );
	
	if(dir.length <1){
		responseElement.update("<p align='center'>No available Favorites yet.</p>");
	}
	else if ( typeof(dir) != "string" ) {
		//make ajax call sending $favList as this array
		pics = '';
		dir.each(function(s){
			if(pics != ''){pics+='&';}
			pics += "favList[]='"+s+"'";
		})
		new Ajax.Request(url, {
			method: 'post',
			parameters: pics + "&randomnum=" + random_num,
			onSuccess: function(transport){
				mainImagesObject = eval('(' + transport.responseText + ')');
				if (mainImagesObject.Error) {//process error and exit
					responseElement.update("error");
					FadeShrink(responseElement);
				}
				else 
					if (mainImagesObject.debug) {//process debug and exit
						responseElement.update("debug");
						FadeShrink(responseElement);
					}
					else {
						// files are present in requested directory names are in mainImagesObject
						
						//effectQueueWait();
						var innerString = '';
						if (mainImagesObject.standalone != null) {
							mainImagesObject.standalone.each(function(s,i){
								imageID = /image([0-9]{3})/.exec(s.src)[1];
								directory = "photos/"+ /^(.*?)\//.exec(s.src)[1] + "/";
								img = /^(.*?)\/(.*)/.exec(s.src)[2];
								imageIDNum = parseInt(imageID, 10);
								innerString += "\n<a href='source/getThumb.php?directory=" + directory + "&image=" + img + "&size=1024' rel='lightbox' title=''>";
								innerString += "\n<img src='source/getThumb.php?directory=" + directory + "&image=" +img + "&size=250' alt='Image: " + imageIDNum;
								try {
									imageTitle = /_it_(.*?)_/.exec(s.src)[1];
									innerString += imageTitle != '' ? " (" + imageTitle + " )" : '';
								} 
								catch (e) {
								};
								innerString += "' origX='" + s.sizeX + "' origY='" + s.sizeY + "' thumbX='250'/>";
								innerString += "\n</a>";
							});
						}
						// add script for slideshow
						innerString += "\n<script>";
						innerString += "\n initLightbox();";
						innerString += "pf_top = new ProtoFlow($('protoflow'), {captions: true"
						innerString += ", startIndex: 2";
						innerString += ", enableOnClickScroll: false,useReflection: false,slider: false});";
						innerString += "\n</script>";
						// since this is favs, remove the group message
						$('groupMessage').update('');
						$('top_loading').hide();
						$('protoflow').update(innerString).show();
						$('top_control').show();
					}
			// finished processing
			//responseElement.morph('height:auto', {duration:.3});
			//responseElement.appear({ duration:.3, from:0.0, to:1.0});
			},
			onFailure: function(){
			//alert("AJAX Failure");
			}
		});
		
	}	
	else {
		new Ajax.Request(url, {
			method: 'post',
			parameters: "category=" + dir + "&randomnum=" + random_num,
			onSuccess: function(transport){
				mainImagesObject = eval('(' + transport.responseText + ')');
				if (mainImagesObject.Error) {//process error and exit
					responseElement.update("error");
					FadeShrink(responseElement);
				}
				else 
					if (mainImagesObject.debug) {//process debug and exit
						responseElement.update("debug");
						FadeShrink(responseElement);
					}
					else {
						// files are present in requested directory names are in mainImagesObject
						
						//effectQueueWait();
						var innerString = '';
						var thirdImage = '';
							if (mainImagesObject.groupImages != null) {
							mainImagesObject.groupImages.each(function(s,i){
								groupID = /^group([0-9]{3})/.exec(s[0].src)[1];
								groupNum = parseInt(groupID, 10);
								dir = /\/(.*?)\//.exec(mainImagesObject.directory)[1];
								if(i == 2){ 
									thirdImage = "photos/"+dir+"/"+s[0].src;
								}
								innerString += "\n<a href='javascript:loadGroupContainer(\"" + dir + "\",\"group_protoflow\",\"" + groupID + "\");'>";
								innerString += "\n<img src='source/getThumb.php?directory=" + mainImagesObject.directory + "&image=" + s[0].src + "&size=250&isGroup=true&groupCount=" + s.length + "'";
								innerString += " alt='Group: " + groupNum;
								try {
									groupTitle = /_gt_(.*?)_/.exec(s[0].src)[1];
									innerString += groupTitle != '' ? " ( " + groupTitle + " )" : '';
								} 
								catch (e) {
								};
								innerString += "' origX='" + s[0].sizeX + "' origY='" + s[0].sizeY + "' thumbX='250'/>";
								innerString += "\n</a>";
							});
						}
						if (mainImagesObject.standalone != null) {
							mainImagesObject.standalone.each(function(s){
								imageID = /^image([0-9]{3})/.exec(s.src)[1];
								imageIDNum = parseInt(imageID, 10);
								innerString += "\n<a href='source/getThumb.php?directory=" + mainImagesObject.directory + "&image=" + s.src + "&size=1024' rel='lightbox' title=''>";
								innerString += "\n<img src='source/getThumb.php?directory=" + mainImagesObject.directory + "&image=" + s.src + "&size=250' alt='Image: " + imageIDNum;
								try {
									imageTitle = /_it_(.*?)_/.exec(s.src)[1];
									innerString += imageTitle != '' ? " (" + imageTitle + " )" : '';
								} 
								catch (e) {
								};
								innerString += "' origX='" + s.sizeX + "' origY='" + s.sizeY + "' thumbX='250'/>";
								innerString += "\n</a>";
							});
						}
						// add script for slideshow
						innerString += "\n<script>";
						innerString += "\n initLightbox();";
						innerString += "pf_top = new ProtoFlow($('protoflow'), {captions: true, startIndex: 3, enableOnClickScroll: false,useReflection: false,slider: false});";
						innerString += "\n</script>";
						$('top_loading').hide();
						$('protoflow').update(innerString).show();
						$('top_control').show();
						if (typeof(conTop) == 'undefined') {
							if (typeof(pfOnChangeFunctioList) != 'undefined') {
								conTop = new ControlBar('conTop', 'conBarTop', thirdImage);
							}
						} else {
							if ((typeof(conTop) == 'undefined') || ($('conGroup') === null)) {
								conTop = new ControlBar('conTop', 'conBarTop', thirdImage);
							}
							else {
								conTop.updateBar('conTop', thirdImage);
							}
						}
						if (typeof(pfOnChangeFunctioList) != 'undefined') {
							Effect.SlideDown($('conBarTop').appear());
						}
					}
			// finished processing
			//responseElement.morph('height:auto', {duration:.3});
			//responseElement.appear({ duration:.3, from:0.0, to:1.0});
			},
			onFailure: function(){
			//alert("AJAX Failure");
			}
		});
	}
}
function removeGroup(){
  $("groupContainer").remove();
  $('groupMessage').show();
}
function loadGroupContainer(dir, elId, group){
  if($('groupContainer') != null){$('groupContainer').remove();}
	reString = "<div id='groupContainer'><hr width='1000px'><div id='group_loading'>\n<br>\n";
  reString += "<img src='images/ajax-loader.gif' align='center'/>\n<br>\n";
  reString += "<p align='center'>Loading...</p>\n</div>\n";
  reString += "<div id='group_bodyWrap'><div id='group_protoflow'></div></div>";
  reString += "\n<div id='group_control' style='display:none;'><img src='images/left-arrow20.gif' class='arrow' onClick='javascript:pf_group.previous();'><span class='controlText'> Click arrows to scroll. Click image to enlarge. </span><img src='images/right-arrow20.gif' class='arrow' onClick='javascript:pf_group.next();'>";
  reString += "\n<div id='conBarGroup' class='manageBar' style='display: none'></div>"
  reString += "\n<div><a href='javascript:removeGroup();'>[ CLOSE ]</a></div>";
  reString += "\n</div>";
  $('groupMessage').hide();
  $('mainDisplayContainer').insert(reString);
	groupResponseElement = $(elId);
	url = "source/getImages.php";

  random_num = Math.round( ( Math.random() * 666 ) );
	new Ajax.Request(url, {
			method: 'post',
			parameters: "category="+dir+"&isGroupID="+group+"&randomnum="+random_num,
			onSuccess: function(transport) {					
				groupImagesObject = eval('(' + transport.responseText + ')');
				if(groupImagesObject.Error){//process error and exit
					groupResponseElement.update("error");
					FadeShrink(groupResponseElement);
				}
				else if(groupImagesObject.debug){//process debug and exit
					groupResponseElement.update("debug");
					FadeShrink(groupResponseElement);
				}
				else{
				// files are present in requested directory names are in mainImagesObject
					
					//effectQueueWait();
					var innerString = '';
					var thirdImage = '';
					if(groupImagesObject.groupImages != null){
						groupImagesObject.groupImages.each(function(s,i){
							imageID = /image([0-9]{3})/.exec(s.src)[1];
							imageIDNum = parseInt(imageID, 10);
							groupNum = parseInt(group, 10);
							if(i == 2){ 
								thirdImage = "photos/"+dir+"/"+s.src;
							}
							innerString += "\n<a href='source/getThumb.php?directory="+groupImagesObject.directory+"&image="+s.src+"&size=1024' rel='lightbox' title=''>";
							innerString += "\n<img src='source/getThumb.php?directory="+groupImagesObject.directory+"&image="+s.src+"&size=250' alt='Image: "+imageIDNum;
							try{ imageTitle = /_it_(.*?)_/.exec(s.src)[1]; innerString += imageTitle != '' ? " ( "+imageTitle+" )":'';} catch(e){};							
							innerString += "' origX='"+s.sizeX+"' origY='"+s.sizeY+"' thumbX='250'/>";
							innerString += "\n</a>";
						});
					}
					// add script for slideshow
					innerString += "\n<script>";
					innerString += "\n initLightbox();";
					innerString += "pf_group = new ProtoFlow($('group_protoflow'), {captions: true, startIndex: 2, enableOnClickScroll: false,useReflection: false,slider: false});";
					innerString += "\n</script>";
					$('group_loading').hide();
					$('group_protoflow').update(innerString).show();
					$('group_control').show();
					if (typeof(conGroup) == 'undefined') {
						if (typeof(pfOnChangeFunctioList) != 'undefined') {
							conGroup = new ControlBar('conGroup', 'conBarGroup', thirdImage);
						}
					} else {
						if ((typeof(conGroup) == 'undefined') || ($('conGroup') === null)) {
							conGroup = new ControlBar('conGroup', 'conBarGroup', thirdImage);
						}
						else {
							conGroup.updateBar('conBarGroup', thirdImage);
						}
					}
					if (typeof(pfOnChangeFunctioList) != 'undefined') {
						Effect.SlideDown($('conBarGroup').appear());
					}
				}
				// finished processing
				//responseElement.morph('height:auto', {duration:.3});
				//responseElement.appear({ duration:.3, from:0.0, to:1.0});
			},
			onFailure: function() {
				//alert("AJAX Failure");
			}
		}
	);	
}

//function effectQueueWait(){
//	loop = true;
//	while(loop){
//		loop = Effect.Queue.size() > 0 ? true : false;
//	}
//}

function FadeShrink(el){
	el = $(el);
	el.innerHTML = "<div>"+responseElement.innerHTML;
	el.setStyle({height:responseElement.getHeight()+"px"});
	el.firstDescendant().fade({ duration:.3 }); // fade existing content out
	el.morph('height:0px', {duration:.3, queue:'end'});	
}

function openMyFav(loginEl, caller){
	if(user_id != ''){ // user is logged in
		loadDisplayContainer(favs,'mainDisplayContainer');
		// display their favs
	} else {
		//Show login
		//$(caller).firstDescendant().setStyle({background: '#BAD6BE'});
		$(loginEl).appear({duration:1.0});
	}
}
function loginRegister(){
	
}
function login(loginEl, caller){
		params = $H({ username:$('username').value, password:$('password').value}).toQueryString();
		$('username').hide();
		$('password').hide();
		$('loginImg').src="images/login_ajax.gif";
		var thisAjax = new Ajax.Request('source/login.php',{
			method: 'post',
			asynchronous: true,
			contentType: 'application/x-www-form-urlencoded',
			parameters: params,
			onSuccess: function(transport){
			}
		});

}
function include(filename)
{
	var head = document.getElementsByTagName('head')[0];
	
	script = document.createElement('script');
	script.src = filename;
	script.type = 'text/javascript';
	
	head.appendChild(script)
}
