function update_photo(img_url, piccount, tags, img_id) {
 img = document.getElementById(img_id);
 dest_img = document.getElementById('bimage');
 dest_img.src = img_url;
 if( img.width > 0 || img.height > 0 ) {
   dest_img.width = img.width;
   dest_img.height = img.height;
 }
 else {
	img_resize('bimage');
 }
 document.getElementById('img_url').value = img_url;
 document.getElementById('piccount').value = piccount;
 document.getElementById('tag_tags').value = tags;
}

function rotate_photo(incr, tot_count, save) {
	cur_photo_id = parseInt(document.getElementById('piccount').value);
	new_photo_id = cur_photo_id + incr;
	if( !cur_photo_id ) {
		return;
	}
	if( tot_count == 1 )
		new_photo_id = cur_photo_id
	while( new_photo_id != cur_photo_id ) {
		if( new_photo_id < 1 ) {
			new_photo_id = tot_count;
		}
		if( new_photo_id > tot_count ) {
			new_photo_id = 1;
		}
		if( document.getElementById('img' + new_photo_id) ) {
			tag_tags = document.getElementById('tag_tags');
			if(save)
				prev_tags = tag_tags.value;
			document.getElementById('picr' + new_photo_id).click();
			if( save && tag_tags.value == "" )
				tag_tags.value = prev_tags
			break;
		}
		new_photo_id += incr;
	}
	if (new_photo_id == cur_photo_id && !document.getElementById('img' + new_photo_id) ) {
		$("bimage").src = '/images/noimage.jpg';
	  $("img_url").value = '';
	  $("piccount").value = '';
		$("bimagebrowse").innerHTML = '';
		$("taglayout").innerHTML = "Nothing to Bookmark";
	}

}

function search(frm) {
	$("search").action = "/picture/" + $("opt").value;
	return true;
}

document.getElementsById = function (tagName) {
	var unFiltered = document.getElementsByTagName('*');
	var filtered = [];
	for (i = 0; i < unFiltered.length; i++) if (unFiltered[i].getAttribute('id') == tagName) filtered.push(unFiltered[i]);
	return filtered;
}

function img_resize(img_id, id, tot_count) {
	var img = document.getElementById(img_id);
	var old_width = img.width;
	var old_height = img.height;
	if(navigator.appName == "Microsoft Internet Explorer") {
		var tmpimg = document.createElement("IMG");
		tmpimg.src = img.src;
		old_width = tmpimg.width;
		old_height = tmpimg.height;		
	}
	if( old_width == 0 && old_height == 0 ) {
		img.height = "120";
		img.width = "160";
		return;
	}
	var new_width = old_width;
	var new_height = old_height;

	if( old_width > old_height ) {
	  if( old_width > 160 ) {
		new_width = 160; 
	 	new_height = new_width * old_height / old_width; 
   	  }
	}
	
	old_width = new_width;
	old_height = new_height;

	if( old_height > 120 ) {
		new_height = 120;
		new_width = new_height * old_width / old_height;
	}
	img.width = parseInt(new_width);
	img.height = parseInt(new_height);
	if( img.width < 160 && img.height < 120 && id ) {
		piccount = parseInt(document.getElementById('piccount').value);
		if( piccount == id ) {
			rotate_photo(1, tot_count);
		}
		Element.remove('pic' + id);
	}
}
