/*
 Javascript functions to support the 2009 WedShare.com Photo Lab
 on the client (website) side
 
 Copyright 2009 Mediasoft Technologies, Inc.
*/


function showPhotoComments(site_id, image_id, width, unique_id) {
	//jumpCommentPopPage(site_id,image_id,width,unique_id,'0');
	var container = document.getElementById('comments-table' + unique_id);
	var page = 0;
	if ((container.innerHTML == '') || (width == 'pop') || (width == 'popimage')) {
		var status_container = document.getElementById('comment_status_container' + unique_id);
		//startProgress('comment_status_container' + unique_id);
		
		// load the comments
		var url = '/home.html';
		var send_data = 'ajax_router=photolab&do=getPhotoCommentTable&site_id=' + site_id + '&image_id=' + image_id + '&unique_id=' + unique_id + '&width=' + width + '&this_page=' + page + '&is_public=1';
		
		request = createRequest();
		request.onreadystatechange=function() {
			if (request.readyState == 4) {
				if (request.status == 200) {
					clearText(status_container);
					
					var msg_array = request.responseText.split('<SEP>');
					container.innerHTML = msg_array[0];
					var num_comments = parseInt(msg_array[1]);
					
					if ((width == 'pop') || (width == 'popimage')) {
						var oPanel = new YAHOO.widget.Panel("myPanel" + unique_id, {  width:"500px",constraintoviewport:true,underlay:"shadow",close:true,visible:false,draggable:true,effect:{effect:YAHOO.widget.ContainerEffect.FADE,duration:0.5}});
						oPanel.render();
						oPanel.show();
					}

					rc = new DHTMLgoodies_roundedCorners();
						for (var counter = 1; counter <= num_comments; counter++) {
						if (document.getElementById('comment' + unique_id + '_' + counter)) rc.addTarget('comment' + unique_id + '_' + counter,6,6,'#EEF4FB','#FFFFFF',5,false,'top_left,top_right,bottom_left,bottom_right');
					}
					if (document.getElementById('comment' + unique_id + '_new')) rc.addTarget('comment' + unique_id + '_new',6,6,'#EEF4FB','#FFFFFF',5,false,'top_left,top_right,bottom_left,bottom_right');
					rc.init();
				}
			}
		}
		
		request.open('POST',url,true);
		request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		request.send(send_data);
		
	} else {
		// comments already loaded, so remove them
		container.innerHTML = '';
	}
}

function jumpCommentPopPage(site_id, image_id, width, unique_id, page) {
	var container = document.getElementById('comment_page' + unique_id);
	var footercontainer = document.getElementById('footernav' + unique_id);
	var status_container = document.getElementById('comment_status_container' + unique_id);
	
	// load the comments
	var url = '/home.html';
	var send_data = 'ajax_router=photolab&do=getPhotoCommentPage&site_id=' + site_id + '&image_id=' + image_id + '&unique_id=' + unique_id + '&width=' + width + '&this_page=' + page + '&is_public=1';
		request = createRequest();
		request.onreadystatechange=function() {
			if (request.readyState == 4) {
				if (request.status == 200) {
					clearText(status_container);
					
					var msg_array = request.responseText.split('<SEP>');
					container.innerHTML = msg_array[0];
					footercontainer.innerHTML = msg_array[1];
					var num_comments = parseInt(msg_array[2]);

					rc = new DHTMLgoodies_roundedCorners();
					for (var counter = 1; counter <= num_comments; counter++) {
						if (document.getElementById('comment' + unique_id + '_' + counter)) rc.addTarget('comment' + unique_id + '_' + counter,6,6,'#EEF4FB','#FFFFFF',5,false,'top_left,top_right,bottom_left,bottom_right');
					}
					rc.init();
				}
			}
		}
		
		request.open('POST',url,true);
		request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		request.send(send_data);
}

function showPopDetails(site_id, image_id, unique_id) {
	//jumpCommentPopPage(site_id,image_id,'popimage',unique_id,'0');
	showPhotoComments(site_id,image_id,'popimage',unique_id);
}

function savePhotoComment(site_id, image_id, comment_id, div_number, is_public, width, unique_id) {
	var url = '/home.html';
	var workspace_id = 'comment' + unique_id + '_' + div_number;
	var container = document.getElementById(workspace_id);
	var indicator = document.getElementById('comment-indicator' + unique_id);
	var content = document.getElementById('content_' + div_number + unique_id).value;
	var identifier = document.getElementById('identifier_' + div_number + unique_id).value;
	
	if (identifier == '') {
		alert("Please enter your name for this comment");
		return;
	}
	if (content == '') {
		alert("Empty comment!");
		return;
	}
	
	var send_data = 'ajax_router=photolab&do=saveComment&site_id=' + site_id + '&comment_id=' + comment_id + '&image_id=' + image_id + '&width=' + width + '&unique_id=' + unique_id  + '&div_number=' + div_number + '&is_public=' + is_public + '&content=' + escape(content) + '&identifier=' + escape(identifier);
	
	document.getElementById(workspace_id).innerHTML = '';
	var status_container = document.getElementById('comment_status_container' + unique_id);
	startProgress('comment_status_container' + unique_id);
	
	request = createRequest();
	request.onreadystatechange=function() {
		if (request.readyState == 4) {
			if (request.status == 200) {
				clearText(status_container);
				var msg_array = request.responseText.split('<SEP>');
				container.innerHTML = msg_array[0];
				if (msg_array[1]) indicator.innerHTML = msg_array[1];
				
				rc = new DHTMLgoodies_roundedCorners();
				rc.addTarget(workspace_id,6,6,'#EEF4FB','#FFFFFF',5,false,'top_left,top_right,bottom_left,bottom_right');
				rc.init();
			}
		}
	}
	
	request.open('POST',url,true);
	request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	request.send(send_data);
}





// starts the graphical progress indicator
function startProgress(container) {
	// start the graphical progress indicator
	var progressicon = document.createElement("img");
	progressicon.setAttribute("src", "http://www.wedshare.com/images/indicator.gif");
	var container_div = document.getElementById(container);
	clearText(container_div);
	container_div.appendChild(progressicon);
}