function pic_handleRating(id,star_rating) {
	var dd = document.getElementById('rating');
	
	new Ajax.Request('/pics/process_ajax_rating?id=' + id + '&r=' + star_rating,{asynchronous:true, 
		onSuccess: function(t) {
			dd.innerHTML = t.responseText;
		},
		onFailure: function(t) {
			dd.innerHTML = "Error occured";
		}
	});
}

function getRating(d,id) {
	if(d.length < 1) { return; }
	ajaxUpdater(d,'/pics/get_ajax_rating?id=' + id);
}

function ShowFlagForm(d,id) {
	if(d.length < 1) { return; }
	
	//requires scriptaculous builder
	Effect.Appear('popup_overlay', { duration: 0.2, from: 0.0, to: 0.8 });
	
	ajaxUpdater(d,'/pics/get_ajax_flag_form?id=' + id);
	var dd = document.getElementById(d);
	AssignPositionCenter(dd,400,400);
	dd.style.display = "block";
	
	//requires scriptaculous builder
	$('popup_overlay').observe('click', (function() { 
		 hideFlagForm(d);
	}).bind(this));
}

function handleFlagSubmit(id) {
	
	var reason = document.getElementById('flag_reason_text').value;
	var post_body = 'picture_id=' + id + '&reason=' + reason;
	
	if (reason.length > 0) {
		divLinkName = 'flag_div';
		divFlagFormName = 'flag_form'; 
		
		var dd = document.getElementById(divLinkName);
		
		new Ajax.Request('/pics/process_ajax_flag_submit',{
			
			method:'post', 
			postBody:post_body,
			asynchronous:true, 
			
			onSuccess: function(t) {
				dd.innerHTML = "Flag Submitted";
				hideFlagForm(divFlagFormName);
			},
			
			onFailure: function(t) {
				dd.innerHTML = "Error!";
				hideFlagForm(divFlagFormName);
			}
		});
	} else {
		alert('You must submit a reason for flagging this post');
	}
}

function hideFlagForm(d) {
	Effect.Fade(d, { duration: 0 }); 
	Effect.Fade('popup_overlay', { duration: 0.2 });
}

function place_images(json_array,content_id) {
	image_list = eval("(" + json_array + ")");
	
	var prev_link = document.getElementById('prev_link');
	var next_link = document.getElementById('next_link');
	
	if (image_list.current_page > 1) {
		prev_link.innerHTML = '<a href="#" onclick="load_gallery_images(' + (eval(image_list.current_page)-1) + ',\'\')">&laquo;</a>'
	} else {
		prev_link.innerHTML = '';
	}
	
	if (image_list.current_page < image_list.pages) {
		next_link.innerHTML = '<a href="#" onclick="load_gallery_images(' + (eval(image_list.current_page)+1) + ',\'\')">&raquo;</a>'
	} else {
		next_link.innerHTML = '';
	}
	
	var div_cnt = 0;
	var image_num = 0;
	
	for (i=0;i<6;i++) {
		var img_div = document.getElementById('img' + div_cnt);
		if (typeof(image_list.pictures[i]) != "undefined") {
			
			img_div.innerHTML = '<a href="#pics" onclick="showImage(' + i + ')"><img src="/site_images/' + image_list.pictures[i].thumbfile + '" alt="" class="media2" /></a>';

			if (image_list.pictures[i].content_id == content_id) {
				image_num = i;
			}
		
		} else {
			img_div.innerHTML = '';
		}
		div_cnt++;
	}
	
	if (div_cnt < 6) {
		for (i=div_cnt;i<6;i++) {
			var img_div = document.getElementById('img' + i);
			img_div.innerHTML = '';
		}
	}
	
	showImage(image_num);
}

function showImage(image_num) {	
	var large_image_holder = document.getElementById('large_image_holder');
	var image_title = document.getElementById('image_title');
	var image_desc = document.getElementById('image_desc');
	var flag_div = document.getElementById('flag_div');

	large_image_holder.src = '/site_images/' + image_list.pictures[image_num].filename;
	image_title.innerHTML = image_list.pictures[image_num].title;
	image_desc.innerHTML = image_list.pictures[image_num].content;
	
	flag_div.innerHTML = '<a href="#" onclick="ShowFlagForm(\'flag_form\',' + image_list.pictures[image_num].picture_id + ')">Flag this picture</a>';
	
	getRating('rating',image_list.pictures[image_num].picture_id);
}

function load_gallery_images(page_num,content_id) {	
	new Ajax.Request('/pics/get_ajax_pictures?p=' + page_num + '&cid=' + content_id, {
		onSuccess: function (response) {
			place_images(response.responseText,content_id);
		}
	});
}

image_list = {};