var current_header_sort_type = null;
var column_width_adjusted = false;
var shelf_array = null;
var owner_shelf_array = null;
var window_confirm_args = null;
var calculate_text_bottom_obj = null;
var current_cover_flow_target = 0;
var detail_item_objs = null;
var resized_column_x_end = 0;
var resized_column_obj = null;
var resized_column_x = 0;
var resized_column_value_index = -1;
var resized_column_index = -1;
var prev_disp_y = 0;
var init_detail_view_text_display_length = null;
var detail_view_text_display_length = null;
var context_menu_active = false;
var main_ajax_id = 0;
var export_wndw = null;
var current_interval_timer = null;
var current_disp_y = 0;
var image_buffer = new Array();
var cover_flow_height = 0;
var cloud_area_height = 300;
var vertical_slider_width = 0;
//var full_query_result_count = 0;
//var full_query_item_count = 0;
var cloud_area_slide = null;
var cover_flow_slide = null;
var media_str = '';
var search_result = false;
var shelf_color_id = 1;
var close_shelf_list_menu = true;
var column_insert_mode = false;
var show_header_right_click_menu = true;
var current_column = 0;
var show_header_config_menu = true;
var bar_drag_start_x = 0;
var bar_drag_start_y = 0;
var sort_up_img = new Image(); sort_up_img.src = base_url+"/_images/sort_up.gif";
var press_esc_to_exit_img = new Image(); press_esc_to_exit_img.src=base_url + "/_images/press_esc_to_exit.png";
var press_left_right_arrow_keys_img = new Image(); press_left_right_arrow_keys_img.src = base_url + "/_images/press_left_right_arrow_keys.png";
var sort_down_img = new Image(); sort_down_img.src = base_url+"/_images/sort_down.gif";
var ajax_request_url = base_url + "/_rpc.php";
var GENRE_CLOUD = 0;
var AUTHOR_CLOUD = 1;
var DIRECTOR_CLOUD = 2;

var cloud_str = new Array("genre", "author", "director");
var current_cloud_type = -1;
var DEFAULT_VIEW					= 0;
var LIST_VIEW							= 1;
var DETAIL_VIEW						= 2;
var THUMBNAIL_VIEW_LARGE	= 3;
var THUMBNAIL_VIEW_SMALL	= 4;

var borrow_status_images  = new Array();
var	MEDIUM_INDEX								=	0;
var MEDIUM_IMG_INDEX						= 1;
var ITEM_OWNER_ID_INDEX					= 2;
var TITLE_INDEX									= 3;
var PUBLISHER_INDEX							= 4;
var RATING_INDEX								= 5;
var REL_DATE_INDEX							= 6;
var ADD_DATE_INDEX							= 7;
var MPAA_INDEX									= 8;
var AUTHOR_INDEX								= 9;
var DIRECTOR_INDEX							= 10;
var GENRE_INDEX									= 11;
var USER_RATING_INDEX						= 12;
var BEST_PRICE_INDEX						= 13;
var NEW_PRICE_INDEX							= 14;
var BORROW_STATUS_INDEX					= 15;
var PLATFORM_INDEX							= 16;
var SHELF_INDEX									= 17;
var BORROWER_ID_INDEX						= 18;
var BORROWER_NAME_INDEX 				= 19;
var BORROW_RET_DATE_INDEX				= 20;
var ITEM_CODE_INDEX							= 21;
var SMALL_IMG_INDEX 						= 22;
var REVIEW_DATE_INDEX						= 23;
var BORROWER_LINK_INDEX					= 24;
var DETAIL_URL_INDEX						= 25;
var LENDER_NAME_INDEX						= 26;
var BORROW_ITEM_OWNER_ID_INDEX	= 27;
var LARGE_IMG_INDEX							= 28;
var SYNOPSIS_INDEX							= 29;
var MY_REVIEW_INDEX							= 30;
var row_item_count 							= 31; //number of data items in a row of data fetch

var TEXT_VALUE 					= 1;
var RATING_VALUE 				= 2;
var DATE_VALUE 					= 3;
var IMG_VALUE 					= 4;
var STATIC_TEXT_VALUE 	= 5;
var STATIC_DATE_VALUE		= 6;
var HTML_VALUE					= 7;
var SMALL_IMG_VALUE			= 8;
var MEDIUM_IMG_VALUE		= 9;
var LARGE_IMG_VALUE			= 10;
var SHELF_VALUE					= 11;

var SHELF_NAME_SORT			= 0;
var SHELF_ADD_DATE_SORT = 1;

var shelf_sort_type = SHELF_NAME_SORT;
var shelf_sort_direction = 1;
var prev_shelf_sort_type = -1;

var detail_view_format = new Array();
var ALL_MEDIA = 0;
var BOOKS = 1;
var MOVIES = 2;
var MUSIC = 3;
var GAMES = 4;
var SOFTWARE = 5;
var close_right_click_menu = true;
var left_arrow_img = new Image(); 		left_arrow_img.src = base_url+'/_images/left_arrow.gif';
var right_arrow_img = new Image(); 		right_arrow_img.src = base_url+'/_images/right_arrow.gif';
var gen_book_img 			= new Image(); 	gen_book_img.src = base_url+'/_images/gen_book.gif';
var gen_movie_img 		= new Image(); 	gen_movie_img.src = base_url+'/_images/gen_movie.gif';
var gen_music_img 		= new Image(); 	gen_music_img.src = base_url+'/_images/gen_music.gif';
var gen_game_img 			= new Image(); 	gen_game_img.src = base_url+'/_images/gen_game.gif';
var gen_software_img 	= new Image(); 	gen_software_img.src = base_url+'/_images/gen_software.gif';

var pdf_icon = new Image(); pdf_icon.src = base_url + "/_images/new/pdf.gif";
var ppt_icon = new Image(); ppt_icon.src = base_url + "/_images/new/ppt.gif";
var ps_icon  = new Image(); ps_icon.src  = base_url + "/_images/new/ps.gif";
var doc_icon = new Image(); doc_icon.src = base_url + "/_images/new/doc.gif";
var xls_icon = new Image(); xls_icon.src = base_url + "/_images/new/xls.gif";


var medium_imgs = new Array(null, gen_book_img, gen_movie_img, gen_music_img, gen_game_img, gen_software_img);

var prev_save_func = null;
var prev_id_str = null;
var prev_item_count = -1;
var prev_column_index = -1;


var print_view = '';
var media_options = null;

var media_names = new Array("All","Books","Movies","Music","Games","Software");
var menu_names = new Array("all", "books", "movies", "music", "games", "software");
var media_str_names = new Array("all", "book", "movie", "music", "game", "software");
var onclick_array = new Array();
var item_result_array = new Array();

var item_clicked = false;
var details_info_showing = false;
var details_info_close = false;
var avail_width = 0;
var avail_height = 0;
var prev_item_count = -1;

var shelf_area_wide;
var banner_header_size;	//This is the size of our top ad banner plus some extra space
var folder_area_offset; //this is the offset from the start of the media_shelf_table till the start of the item display area
var border_x_width;		//This accounts for the borders on various tables
var page_x_offset;		//this is from the left end of the browser to the start of our table border
var page_y_offset;		//this is from the top of the browser window till the start of the logo picture
var horizontal_scroll_bar_height;
var vertical_scroll_bar_width;
var footer_size; //this is the portion that is at the bottom where the privacy policy is shown
var image_display_width;									//This is the width of the thumbnail image displayed
var image_display_height;	//This is the height of the thumbnail image displayed
var gen_height;
var gen_width;
var image_display_hgap;			//This is the gap between the thumbnail images
var image_display_vgap;			//This is the gap between the thumbnail images
var display_item_per_row;
var query_item_count;  //This is the number of items that will be shown in a screen
var cloud_width = vertical_slider_width;


//var close_details_info_timer = null;
//var details_info_timer = null;
var hover_item_count = -1;
var hover_x = 0;
var prev_hover_x = 0;
var hover_y = 0;
var prev_hover_y = 0;
var hover_timer = null;
var page_width = 0;
var page_height = 0;
var folder_area_high = 0;
var media_shelf_area_high = 0;
var media_area_wide = 0;
var menu_timer = null;
var http_request = null;//createRequestObject();
var query_result_count = 0;
var query_total_result_count = 0;
var menu_open = false;
var current_operation = 0;

var genre_list_start_index = 0;
var genre_list_item_count = 8;
var genre_list_result_count = 0;
var genre_list_total_result_count = 0;
var browser_is_ie = false;
var prev_view_type = -1;
var refresh_webpage = false;
var prev_clip_region = 'rect(0,0,0,0)';
var full_screen_cover_flow_mode = false;

var sort_all_menu_array = new Array(
													new Array('Title', 'title', 'Sort by title'),
													new Array('Rating', 'rating', 'Sort by rating'),
													new Array('Media', 'medium', 'Sort by media (e.g. books, movies, music, games, software)'),
													new Array('Released&nbsp;Date', 'release_date', "Sort by the released date"),
													new Array('Added&nbsp;Date', 'added_date', 'Sort by the added date'),
													new Array('Rating&nbsp;Date', 'user_rating_update_date', "Sort by the date on which the owner set the item ratings"),
													new Array('Review&nbsp;Date', 'user_review_update_date', 'Sort by the date on which owner added reviews to his items'),
													new Array('Used&nbsp;Price', 'best_price', 'Sort by the used price'),
													new Array('New&nbsp;Price', 'new_price', 'Sort by the new price'),
													new Array('Borrow&nbsp;Status', 'borrow_status', 'Sort by the borrowed status'),
													new Array('Note&nbsp;Update&nbsp;Date', 'note_added_date', "Sort by the note added date")
													);


var sort_books_menu_array = new Array(
														new Array('Title', 'title', 'Sort books by title'),
														new Array('Author', 'author', 'Sort books by author'),
														//new Array('Genre...', 'genre', 'Select the genre of the books to list'),
														new Array('Rating', 'rating', 'Sort books by rating'),
														new Array('Published&nbsp;Date', 'release_date', "Sort books by the published date"),
														new Array('Added&nbsp;Date', 'added_date', 'Sort books by the added date'),
														new Array('Rating&nbsp;Date', 'user_rating_update_date', "Sort books by the date on which the owner set the ratings"),
														new Array('Review&nbsp;Date', 'user_review_update_date', 'Sort books by the date on which owner added reviews'),
														new Array('Used&nbsp;Price', 'best_price', 'Sort books by used price'),
														new Array('New&nbsp;Price', 'new_price', 'Sort books by new price'),
														new Array('Borrow&nbsp;Status', 'borrow_status', 'Sort books by borrowed status'),
														new Array('Note&nbsp;Update&nbsp;Date', 'note_added_date', "Sort books by note added date")
													);

var sort_movies_menu_array = 	new Array(
															new Array('Title', 'title', 'Sort movies by title'),
															new Array('Director', 'director', 'Sort movies by director'),
															new Array('Actor', 'author', 'Sort movies by actor'),
															//new Array('Genre...', 'genre', 'Select the genre of the movies to list'),
															new Array('Rating', 'rating', 'Sort movies by rating'),
															new Array('Released&nbsp;Date', 'release_date', "Sort movies by the released date"),
															new Array('Added&nbsp;Date', 'added_date', 'Sort movies by the added date'),
															new Array('Rating&nbsp;Date', 'user_rating_update_date', "Sort movies by the date on which the owner set the ratings"),
															new Array('Review&nbsp;Date', 'user_review_update_date', 'Sort movies by the date on which owner added reviews'),
															new Array('Used&nbsp;Price', 'best_price', 'Sort movies by used price'),
															new Array('New&nbsp;Price', 'new_price', 'Sort movies by new price'),
															new Array('Borrow&nbsp;Status', 'borrow_status', 'Sort movies by borrowed status'),
															new Array('Note&nbsp;Update&nbsp;Date', 'note_added_date', "Sort movies by note added date")
													);

var sort_music_menu_array = 	new Array(
															new Array('Title', 'title', 'Sort music by title'),
															new Array('Artist', 'author', 'Sort music by artist'),
															//new Array('Genre...', 'genre', 'Select the genre of the books to list'),
															new Array('Rating', 'rating', 'Sort music by rating'),
															new Array('Released&nbsp;Date', 'release_date', "Sort music by the released date"),
															new Array('Added&nbsp;Date', 'added_date', 'Sort music by the added date'),
															new Array('Rating&nbsp;Date', 'user_rating_update_date', "Sort music by the date on which the owner set the ratings"),
															new Array('Review&nbsp;Date', 'user_review_update_date', 'Sort music by the date on which owner added reviews'),
															new Array('Used&nbsp;Price', 'best_price', 'Sort music by the used price'),
															new Array('New&nbsp;Price', 'new_price', 'Sort music by the new price'),
															new Array('Borrow&nbsp;Status', 'borrow_status', 'Sort music by the borrowed status'),
															new Array('Note&nbsp;Update&nbsp;Date', 'note_added_date', "Sort music by the note added date")
													);

var sort_games_menu_array = 	new Array(
															new Array('Title', 'title', 'Sort games by title'),
															new Array('Platform', 'platform', 'Sort games by supported platform'),
															//new Array('Genre...', 'genre', 'Select the genre of the genre to list'),
															new Array('Rating', 'rating', 'rating'),
															new Array('Released&nbsp;Date', 'release_date', "Sort music by the released date"),
															new Array('Added&nbsp;Date', 'added_date', 'Sort music by the added date'),
															new Array('Rating&nbsp;Date', 'user_rating_update_date', "Sort games by the date on which the owner set the ratings"),
															new Array('Review&nbsp;Date', 'user_review_update_date', 'Sort games by the date on which owner added reviews'),
															new Array('Used&nbsp;Price', 'best_price', 'Sort games by the used price'),
															new Array('New&nbsp;Price', 'new_price', 'Sort games by the new price'),
															new Array('Borrow&nbsp;Status', 'borrow_status', 'Sort games by the borrowed status'),
															new Array('Note&nbsp;Update&nbsp;Date', 'note_added_date', "Sort games by the note added date")
													);

var sort_software_menu_array = 	new Array(
															new Array('Title', 'title', 'Sort software by title'),
															new Array('Operating System', 'platform', 'Sort software by the supported operating system'),
															//new Array('Genre...', 'genre', 'Select the genre of the software to list'),
															new Array('Rating', 'rating', 'Sort software by rating'),
															new Array('Released&nbsp;Date', 'release_date', "Sort software by the released date"),
															new Array('Added&nbsp;Date', 'added_date', 'Sort software by the added date'),
															new Array('Rating&nbsp;Date', 'user_rating_update_date', "Sort software by the date on which the owner set the ratings"),
															new Array('Review&nbsp;Date', 'user_review_update_date', 'Sort software by the date on which owner added reviews'),
															new Array('Used&nbsp;Price', 'best_price', 'Sort software by the used price'),
															new Array('New&nbsp;Price', 'new_price', 'Sort software by the new price'),
															new Array('Borrow&nbsp;Status', 'borrow_status', 'Sort software by the borrowed status'),
															new Array('Note&nbsp;Update&nbsp;Date', 'note_added_date', "Sort software by the note added date")
													);

var sort_menu_array = new Array(
													sort_all_menu_array, 
													sort_books_menu_array, 
													sort_movies_menu_array, 
													sort_music_menu_array, 
													sort_games_menu_array, 
													sort_software_menu_array
													);

var item_count_images_to_be_fetched = new Array();
var image_refetching = false;

function check_photo_width(obj)
{
var wide = parseInt(obj.width);
//alert(obj.width);
if(wide > 185)
	obj.width = 185;
}

function request_new_image(item_count)
{
var item_owner_id 	= item_result_array[item_count*row_item_count + ITEM_OWNER_ID_INDEX]*1;
var i;
//var title_str				= (item_result_array[item_count*row_item_count + TITLE_INDEX]);
//alert(item_owner_id + "," + title_str);
for(i = 0; i < item_count_images_to_be_fetched.length; ++i)
	{
	if(item_count_images_to_be_fetched[i][2] == item_owner_id)
		{
		if(item_count_images_to_be_fetched[i][1] == 1)
			break
		else
			return;
		}
	}
if((image_refetching==false))
	{
	image_refetching = true;
	item_count_images_to_be_fetched[i] = new Array(item_count, 0, item_owner_id);
	var args = 						{	
												'action'					: 'image_refetch', 
												'item_owner_id'		:	item_owner_id,
												'item_count'			: item_count,
												'random'					:	Math.random()
												};
	ajax_get_request(ajax_request_url, args, image_refetch_response, false);	
	}
else
	{
	item_count_images_to_be_fetched[i] = new Array(item_count, 1, item_owner_id);
	}	
}

function image_refetch_response(response)
{

//alert(response.join("|"));
if(response[0] == 'error')
	{
	alert(response[1]);
	return;
	}
var success = response.shift()*1;
if(success == 1)
	{
	var item_count = response.shift();
	var item_owner_id = item_result_array[item_count*row_item_count + 2];
	if(item_owner_id == response.shift()*1)
		{
		item_result_array[item_count*row_item_count + SMALL_IMG_INDEX] = response.shift();
		item_result_array[item_count*row_item_count + MEDIUM_IMG_INDEX] = response.shift();
		item_result_array[item_count*row_item_count + LARGE_IMG_INDEX] = response.shift();
		
		var item_row_id 	= get_item_row_id(item_owner_id);
		var elem  = document.getElementById(item_row_id);
		if(elem != null && (user_view_type == THUMBNAIL_VIEW_SMALL || user_view_type == THUMBNAIL_VIEW_LARGE))
			{
			elem = elem.offsetParent;
			elem.innerHTML = generate_image_thumbnail(item_count, 0);	
			}
		}	
	}
	
image_refetching = false;	
var i;
for(i = 0; i < item_count_images_to_be_fetched.length; ++i)
	{
	var item_count = item_count_images_to_be_fetched[i][0];
	if(item_count_images_to_be_fetched[i][1] == 1)
		{
		//alert(item_count + "," + item_count_images_to_be_fetched[i][1]);
		request_new_image(item_count);
		image_refetching = true;
		break;
		}
	}
}

function create_sort_menu_item(sort_display_name, sort_type, sort_type_help)
{
var str = '';
str += '<tr>\n';
str += '	<td height="17" onmousedown="sort_list(\''+sort_type+'\')" align="left" valign="top">\n';
str += '		<table ';
str += 'title="'+sort_type_help+'" ';
str += 'style="font-family: arial; font-size: 10pt; border: 1px solid '+theme_color+'; cursor: pointer; cursor: hand;" onmouseover="mouse_over_menulink(this)" onmouseout="mouse_out_menulink(this)" border="0" cellspacing="0" cellpadding="0" width="100%" height="100%">\n';
str += '			<tr>\n';
str += '				<td align="left" valign="middle">\n';
str += '					<span style="vertical-align: middle">';
if(sort_type == user_sort_type)
	{
	str += '<img src="'+base_url+'/_images/tick.gif">';
	}
else
	{
	str += '&nbsp;';
	}
str += '</span>\n';
str += '				</td>\n';
str += '				<td width="100%" height="17" align="left" valign="middle">';
str += sort_display_name;
str += '				</td>\n';
str += '				<td valign="middle">\n';
str += '				<span style="vertical-align: middle">';
if(sort_type == user_sort_type)	
	{
	var img = (user_sort_direction == 1)?(sort_up_img.src):(sort_down_img.src);
	str += "<img src=\""+img+"\">";
	}
else	
	{	
	str +='&nbsp;&nbsp;';
	}
str += '</span>\n';
str += '				</td>\n';
str += '			</tr>\n';
str += '		</table>\n';
str += '	</td>\n';
str += '</tr>\n';

return str;
}

function create_sort_menu(media_id, sort_menu_obj)
{
var sort_array = sort_menu_array[media_id];
var str = ''
str += 	'<table style="border: 1px outset white;" border="0" bgcolor="'+theme_color+'" cellspacing="0" cellpadding="0" width="150" height="100%">';
str += 		'<tr>';
str += 			'<td>';			
str += '<table border="0" cellspacing="4" cellpadding="0" width="100%" height="100%">';
for(i = 0; i < sort_array.length; ++i)
	{
	str += create_sort_menu_item(sort_array[i][0], sort_array[i][1], sort_array[i][2]);
	}
str += '</table>';
str += '			</td>';
str += '		</tr>';
str += '	</table>';

//var obj = document.getElementById('sort_menu');
//if(obj == null)
//	return;
sort_menu_obj.innerHTML = str;
}

function find_browser_type()
{
var browser = navigator.appName;
if(browser == "Microsoft Internet Explorer")
	{
	browser_is_ie = true;
	}
else
	{
	browser_is_ie = false;
	}
}

function createRequestObject()
{
	var ro;
	if(browser_is_ie == true)
		{
	  ro = new ActiveXObject("Microsoft.XMLHTTP");
	  }
	else
		{
	  ro = new XMLHttpRequest();
	  }

	/*}else
	{
	    ro = new XMLHttpRequest();
	}*/
	return ro;
}

function show_sort_menu()
{

var elem1 = document.getElementById("shelf_sort_menu");
//alert(elem1.style.visibility.toLowerCase());
if(elem1.style.visibility.toLowerCase() == 'visible')
	{
	close_shelf_sort_menu();
	return;
	}
hide_all_popup_menu();
hide_all_menu();
var elem1 = document.getElementById("sort_shelf_button");
if(elem1.style.visibility.toLowerCase() == 'visible')
	{
	close_sort_menu();
	return;
	}
var x = getImagePageLeft(elem1);
var y = getImagePageTop(elem1);
var menuTop = y + 15; // LAYER TOP POSITION
var menuLeft = x - 60;

elem1 = document.getElementById("shelf_sort_menu");
elem1.style.top = menuTop;
elem1.style.left = menuLeft;
elem1.style.visibility="visible";

menu_open = true;
}

function close_shelf_sort_menu()
	{
	var elem1 = document.getElementById("shelf_sort_menu");
	if(elem1 != null)
		{
		elem1.style.visibility="hidden";
		}
	}
	
function generate_shelf_info(shelf_name, shelf_id, private_shelf)
{
var str = '';
var title_str = "Click here to list items under shelf "+shelf_name;
var shelf_type_img = base_url + "/_images/shelf.gif";
var shelf_name1 = shelf_name;//.split(' ').join('&nbsp;');

str += ("	<tr>\n");
str += ("		<td height=\"32\" width=\"100%\" valign=\"top\">\n");
str += ("			<table style=\"margin: 0 0 0 3px; \" border=\"0\" cellspacing=\"0\" cellpadding=\"0\" width=\"100%\">\n");
str += ("				<tr>\n");
str += ("					<td style=\"display: block; background: transparent;\" width=\"100%\">\n");

str += ("							<input type=\"hidden\" value=\"\" id=\"shelf_table_check_")+shelf_name+("\">\n"); //This will help to keep track if there is already a shelf with a name exists when adding a new shelf
str += '							<b id="shelf_curve_1_1_'+shelf_id+'" style="font-size: 1px; display:block;  overflow:hidden;  height:1px;  margin:0 0 0 5px;"></b>';
str += '							<b id="shelf_curve_1_2_'+shelf_id+'" style="font-size: 1px; display:block;  overflow:hidden;  height:1px;  margin:0 0 0 3px; border-right:2px;"></b>';
str += '							<b id="shelf_curve_1_3_'+shelf_id+'" style="font-size: 1px; display:block;  overflow:hidden;  height:1px;  margin:0 0 0 2px; border-right:1px;"></b>';
str += '							<b id="shelf_curve_1_4_'+shelf_id+'" style="font-size: 1px; display:block;  overflow:hidden;  height:2px;  margin:0 0 0 1px; border-right:1px;"></b>';
str += ("						<table  onmousedown=\"select_shelf(")+shelf_id+(")\" style=\"cursor: pointer; cursor: hand; font-family: arial; font-size: 9pt;\" id=\"shelf_table_")+shelf_id+("\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\" width=\"100%\" height=\"100%\">\n");
str += ("							<tr >\n");

str += ("								<td valign=\"middle\" align=\"left\" height=\"30\" width=\"30\">\n");
str += "<div style=\"margin: 0px 2px 0px 2px; position: relative\">\n";
str += ("									<img id=\"shelf_img_")+shelf_id+("\" src=\"")+shelf_type_img+("\" vspace=\"0\" hspace=\"0\" height=\"30\">\n");
if(private_shelf == 1)
			{
			str += "<img style=\"vertical-align: top; text-align: right; position: absolute; z-index: 25; left:10; top:15;\" src=\""+base_url+"/_images/lock.gif\">\n";
			}
str += "</div>\n";
str += ("								</td>\n");

str += ("								<td valign=\"middle\" align=\"left\">\n");
str += ("									<span id=\"shelf_link_")+shelf_id+("\" style=\"font-family:arial; font-size:9pt; font-weight:normal; cursor: pointer; cursor: hand;\" title=\"")+title_str+("\" href1=\"javascript:select_shelf(")+shelf_id+(")\">")+shelf_name1+("</span>\n");
str += ("								</td>\n");
str += ("							</tr>\n");
str += ("						</table>\n");

str += '							<b id="shelf_curve_2_1_'+shelf_id+'" style="font-size: 1px; display:block;  overflow:hidden;  height:2px;  margin:0 0 0 1px; border-right:1px;"></b>';
str += '							<b id="shelf_curve_2_2_'+shelf_id+'" style="font-size: 1px; display:block;  overflow:hidden;  height:1px;  margin:0 0 0 2px; border-right:1px;"></b>';
str += '							<b id="shelf_curve_2_3_'+shelf_id+'" style="font-size: 1px; display:block;  overflow:hidden;  height:1px;  margin:0 0 0 3px; border-right:2px;"></b>';
str += '							<b id="shelf_curve_2_4_'+shelf_id+'" style="font-size: 1px; display:block;  overflow:hidden;  height:1px;  margin:0 0 0 5px; "></b>';

str += ("					</td>\n");
str += ("				</tr>\n");
str += ("			</table>\n");
str += ("		</td>\n");
str += ("	</tr>\n");
return str;
}

function set_shelf_background(img)
{
if(img == null)
	return;
var color = background_color;	
if(img.length != 0)
	{
	//document.getElementById('folder_area').style.backgroundImage = 'url('+img+')';
	color = img;
	}

var folder_elem = document.getElementById('folder_area');
if(folder_elem != null && user_view_type != DETAIL_VIEW)
	{
	folder_elem.style.backgroundImage = 'url('+color+')';
	}

//var shelf = get_shelf_info_from_shelf_id(req_shelf_id);
//shelf['picture_id'] = color;
}

function move_multiple_items(item_owner_ids, shelf_id)
{
var shelf_name = get_shelf_info_from_shelf_id(shelf_id)['name'];
var args = 						{	
											'action'					: 'move_multiple_items', 
											'item_owner_ids'	:	item_owner_ids,
											'shelf_id'				: shelf_id, 
											'shelf_name'			: shelf_name, 
											'random'					:	Math.random()
											};
ajax_get_request(ajax_request_url, args, move_multiple_items_response);
}

function move_multiple_items_response(response_array)
{
if(response_array[0] == "error")
	{
	restore_item();
	show_alert_dialog("Error", response_array[1]);
	}
else
	{
	drag_elem = null;
	var count = response_array.shift()*1;
	set_multiple_move(count, response_array);
	}
fetch_item_data();
}

function move_item(item_owner_id, shelf_id, shelf_name)
{
var args =			 		{
										'action'				: 'move_item', 
										'item_owner_id'	:	item_owner_id,
										'shelf_id'			: shelf_id, 
										'shelf_name'		: shelf_name, 
										'random'				:	Math.random()
										};
ajax_get_request(ajax_request_url, args, move_item_response);
}

function move_item_response(response_array)
{
if(response_array[0] == "error")
	{
	restore_item();
	show_alert_dialog("Error", response_array[1]);
	}
else
	{
	drag_elem = null;
	//refetch the current table view
	set_multiple_move(1, response_array);
	}
fetch_item_data();
}

function focus_input_text(obj)
{
hide_all_menu();
hide_all_popup_menu();
set_active_touch_screen_text_input(obj);
if(obj.value=='Search')
	{
	obj.value='';
	obj.style.color='#000';
	var elem = document.getElementById('search_button');
	elem.src = base_url+'/_images/new/search_button1.gif'
	}
else
	{
	obj.select();
	}

}

function blur_input_text(obj)
{
if(obj.value=='' && !touch_keyboard_active())
	{
	obj.value='Search';
	obj.style.color='b3b3b3';
	if(search_result)
		{
		//fetch_item(0);
		}
	var elem = document.getElementById('search_button');
	elem.src = base_url+'/_images/new/search_button_inactive.gif'
	}
}

function set_multiple_move(count, response_array)
{
var i;
for(i = 0; i < count; ++i)
	{
	var shelf_name 						= response_array.shift();
	var dest_shelf_id					= response_array.shift()*1;
	var src_shelf_id 					= response_array.shift()*1;
	var src_shelf_item_count 	= response_array.shift()*1;
	
	
	if(dest_shelf_id == trash_can_shelf_id)
		{
		var elem = document.getElementById('shelf_img_'+dest_shelf_id);
		if(elem == null)
			break;
	
	
		if(elem.src.indexOf('_full.gif') == -1)
			{
			elem.src = base_url+"/_images/trash_full.gif";
			}
		}
	else if(src_shelf_id == trash_can_shelf_id)
		{
		var elem = document.getElementById('shelf_img_'+src_shelf_id);
		if(elem == null)
			break;
	
	
		if(src_shelf_item_count == 0)
			{
			if(elem.src.indexOf('_empty.gif') == -1)
				{
				elem.src = base_url+"/_images/trash_empty.gif";
				}
			}
		else
			{
			if(elem.src.indexOf('_full.gif') == -1)
				{
				elem.src = base_url+"/_images/trash_full.gif";
				}
			}
		}
	}	
}

function fetch_item(start_index)
{
	if(is_ajax_object_active(main_ajax_id) == false)
		{
		query_result_count = 0;
		query_start_index = start_index;
		if(current_operation == 1)
			{
			query_total_result_count = 0;
			}
		current_operation = 0; //Indicates a general fetch
		current_search_criterion = "";
		current_search_data = "";
		
		var args = 						{	
													'action'				: 		'fetch_items', 
													'userid'				:			current_userid,
													'media_id'			: 		req_media_id,
													'shelf_id'			: 		req_shelf_id, 
													'view'					: 		user_view_type, 
													'sort_type'			:			user_sort_type,
													'sort_direction':			user_sort_direction,
													'start_index'		:			start_index,
													'item_count'		:			query_item_count,
													'total_count'		:			query_total_result_count,
													'random'				:			Math.random()
													};
		main_ajax_id = ajax_get_request(ajax_request_url, args, fetch_items_response);
		var elem = document.getElementById('search_text');
		if(elem)
			{
			elem.value='';
			elem.blur();
			}
		}
	else
		{
		//alert('ajax busy');
		}		
}

function fetch_items_response(response_array)
{
//var str = response_array.join("|");
//alert(str);
search_result = false;
display_query_result(response_array);
}

function trim(str)
{
if(str.length < 1)
	{
	return"";
	}
str = rtrim(str);
str = ltrim(str);
return str;
} //End Function

function rtrim(VALUE)
{
var w_space = String.fromCharCode(32);
var v_length = VALUE.length;
var strTemp = "";
if(v_length < 0)
	{
	return "";
	}
var iTemp = v_length -1;

while(iTemp > -1)
	{
	if(VALUE.charAt(iTemp) == w_space)
		{
		}
	else
		{
		strTemp = VALUE.substring(0,iTemp +1);
		break;
		}
	iTemp = iTemp-1;
	} //End While
return strTemp;
} //End Function

function ltrim(VALUE)
{
var w_space = String.fromCharCode(32);
if(v_length < 1)
	{
	return "";
	}
var v_length = VALUE.length;
var strTemp = "";

var iTemp = 0;

while(iTemp < v_length)
	{
	if(VALUE.charAt(iTemp) == w_space)
		{
		}
	else
		{
		strTemp = VALUE.substring(iTemp,v_length);
		break;
		}
	iTemp = iTemp + 1;
	} //End While
return strTemp;
} //End Function

function get_owner_shelf_info(shelf_id)
{
var i; 
for(i = 0; i < owner_shelf_array.length; ++i)
	{
	if(shelf_id == owner_shelf_array[i]['id'])
		{
		return owner_shelf_array[i];
		}	
	}
return null;
}

function delete_shelf(shelf_id)
{

var i;
var shelf_array1 = new Array();
var owner_shelf_array1 = new Array();
var shelf_id1;
var shelf_info;
var ret_shelf_id = 0;
var prev_shelf_id = 0;
for(i = 0; i < shelf_array.length; ++i)
	{
	shelf_info = shelf_array[i];
	shelf_id1 = shelf_info['id'];
	if(shelf_id1 == shelf_id)
		{
		if(prev_shelf_id != 0)
			{
			ret_shelf_id = prev_shelf_id;
			}
		else
			{
			ret_shelf_id = shelf_array[i+1]['id'];
			}	
		}
	else
		{
		shelf_array1[shelf_array1.length] = shelf_info;//.slice();
		prev_shelf_id = shelf_id1;
		}			
	}

for(i = 0; i < owner_shelf_array.length; ++i)
	{
	var shelf_info = owner_shelf_array[i];
	shelf_id1 = shelf_info['id'];
	if(shelf_id1 != shelf_id)
		{
		owner_shelf_array1[owner_shelf_array1.length] = shelf_info;//.slice();
		}
	}

shelf_array = shelf_array1;//.slice();
owner_shelf_array = owner_shelf_array1;//.slice();
return ret_shelf_id;
}

function update_shelf(shelf_id, shelf_name, private_shelf, picture_id)
{
var shelf_info = get_shelf_info_from_shelf_id(shelf_id);
var shelf_info1 = get_owner_shelf_info(shelf_id);
if(shelf_info != null)
	{
	shelf_info['name'] = shelf_name;
	shelf_info['private'] = private_shelf;
	shelf_info['picture_id'] = picture_id;
	}
	
if(shelf_info1 != null)	
	{
	shelf_info1['name'] = shelf_name;
	shelf_info1['private'] = private_shelf;
	shelf_info1['picture_id'] = picture_id;
	if(shelf_info1['type'] != 0)
		{
		var obj = document.getElementById('shelf_private_img_'+shelf_id);
		if(obj != null)
			{
			obj.style.visibility = (private_shelf == 1)?("visible"):("hidden");
			}
		}
	else
		{
		show_sort_shelf();
		}	
	}
}

function append_shelf(shelf_id, shelf_name, private_shelf, picture_id)
{
var count = owner_shelf_array.length;
++shelf_count;
owner_shelf_array[count] = new Array();
owner_shelf_array[count]['id'] = shelf_id;
owner_shelf_array[count]['name'] = shelf_name;
owner_shelf_array[count]['private'] = private_shelf;
owner_shelf_array[count]['type'] = 0;
owner_shelf_array[count]['picture_id'] = picture_id;

count = shelf_array.length;
shelf_array[count] = new Array();
shelf_array[count]['id'] = shelf_id;
shelf_array[count]['name'] = shelf_name;
shelf_array[count]['private'] = private_shelf;
shelf_array[count]['type'] = 0;
shelf_array[count]['picture_id'] = picture_id;
//rearrange the gurulib view
set_gurulib_view();
show_sort_shelf();
}

function request_add_shelf(shelf_name, color_id, private_shelf)
{
var args = 						{	
											'action'				: 		'add_shelf', 
											'shelf_name'		:			shelf_name,
											'background_id'	: 		color_id,
											'private_shelf'	: 		private_shelf, 
											'random'				:			Math.random()
											};
ajax_get_request(ajax_request_url, args, add_shelf_response);
disable_screen(false);
}

function add_shelf_response(response_array)
{
if(response_array[0] == "error")
	{
	show_alert_dialog("Error", response_array[1]);
	}
else
	{
	var shelf_id 			= (response_array.shift() * 1);
	var shelf_name 		= (response_array.shift());
	var private_shelf	= (response_array.shift() * 1);
	var picture_id 		= (response_array.shift() * 1);
	append_shelf(shelf_id, shelf_name, private_shelf, picture_id);
	select_shelf(shelf_id);
	}
}

function add_shelf()
	{

	if(shelf_count == 100)
		{
		var msg = ("You cannot add more shelf. You have reached the maximum shelfs for your library.");
		close_shelf_box();
		show_alert_dialog("Error", msg);		
		return;
		}

	var shelf_name = document.getElementById('shelf_name_input').value;
	shelf_name = trim(shelf_name);
	if(shelf_name.length != 0)
		{
		//check if this name exists in the name list
		var elem = document.getElementById('shelf_table_check_'+shelf_name);
		if(elem != null)
			{
			//document.getElementById('shelf_name_input').focus();
			close_shelf_box();
			var msg = ("Shelf '" +shelf_name+"' already exists. Please choose another name.");
			show_alert_dialog("Error", msg);
			return;
			}
		else
			{
			var private_shelf = 0;
			var boxes = document.getElementById('private_shelf_id');
			if(boxes != null)
				{
				private_shelf = (boxes.checked==true)?1:0;
				}
			request_add_shelf(shelf_name, shelf_color_id, private_shelf);
			close_shelf_box();
			}
		}
	else
		{
		close_shelf_box();
		show_alert_dialog("Error", "Please provide a valid shelf name");
		}	
	}

function disable_all_controls()
{
if(media_options != null)
	media_options.disabled = true;
set_silkscreen_opacity(0);
disable_screen(true);
}

function enable_all_controls()
{
if(media_options != null)
	media_options.disabled = false;
disable_screen(false);
set_silkscreen_opacity(0.4);
}

function show_loading()
{
	var div="busy_box"
	var obj=document.getElementById(div);
	if(obj == null)
		return;
	obj.style.left = (screen.availWidth)/2;
	obj.style.top = (screen.availHeight)/2 + document.body.scrollTop;
	obj.style.visibility='visible'
	disable_all_controls();
}
function hide_loading()
{
	var div="busy_box"
	var obj=document.getElementById(div);
	if(obj == null)
		return;
	obj.style.visibility='hidden'
	enable_all_controls();
}

function getImagePageLeft(img) {
  var x, obj;
    x = 0;
    obj = img;
    while (obj.offsetParent != null) {
      x += obj.offsetLeft;
      obj = obj.offsetParent;
    }
    x += obj.offsetLeft;
    return x;
}

function getImagePageTop(img) {
  var y, obj;
  y = 0;
  obj = img;
  while (obj.offsetParent != null) {
    y += obj.offsetTop;
    obj = obj.offsetParent;
  }
  y += obj.offsetTop;
  return y;
}

function select_shelf(shelf_id)
	{

	if(is_ajax_object_active(main_ajax_id))
		return;

	hide_current_shelf_controls();
	
	var prev_shelf_id = req_shelf_id;
	if(shelf_id != prev_shelf_id || current_operation == 1)
		{
		query_start_index = 0;
		query_total_result_count = 0;
		req_shelf_id = shelf_id;
		query_total_result_count = 0;
		set_shelf(shelf_id, prev_shelf_id);
		column_width_adjusted = false;
		}
	current_operation = 0;
	fetch_item(query_start_index);
	}

function fetch_item_data()
{
if(current_operation == 0)
	{
	fetch_item(query_start_index);
	}
else
	{
	search_item(query_start_index);
	}
}

function set_curved_shelf(shelf_id, set_color)
{
var elem;
var i,j;
var id;
for(i=1;i<=2;++i)
	{
	for(j=1;j<=4;++j)
		{
		id='shelf_curve_'+i+'_'+j+'_'+shelf_id;
		elem = document.getElementById(id);
		if(elem != null)
			{
			if(i==3 && set_color.length != 0)
				{
				set_color = '#444444';
				}
			elem.style.borderLeftColor = set_color;
			elem.style.backgroundColor = set_color;
			}
		}
	}

elem = document.getElementById('shelf_link_'+shelf_id);
if(set_color.length == 0)
	{
	//elem.style.fontSize = '9pt';
	elem.style.fontWeight = "normal";
	}
else
	{
	//elem.style.fontSize = '10pt';
	elem.style.fontWeight = "bold";
	}

return;
elem = document.getElementById('shelf_img_'+shelf_id);
if(set_color.length == 0)
	{
	elem.height = parseInt(elem.height) - 1;
	}
else
	{
	elem.height = parseInt(elem.height) + 1;
	}		
}

function focus_add_button(elem)
{
//var elem = document.getElementById('add_item_button');
var prev_shelf_id = req_shelf_id;
if(prev_shelf_id == trash_can_shelf_id)
	{
	elem.src = base_url+"/_images/new/empty_trash_can.gif";
	}
else
	{
	elem.src = base_url+"/_images/new/add_item.gif";
	}	
}

function blur_add_button(elem)
{
var prev_shelf_id = req_shelf_id;
//var elem = document.getElementById('add_item_button');
if(prev_shelf_id == trash_can_shelf_id)
	{
	elem.src = base_url+"/_images/new/empty_trash_can_inactive.gif";
	}
else
	{
	elem.src = base_url+"/_images/new/add_item_inactive.gif";
	}	
}

function set_silkscreen_opacity(opacity)
{
var obj = document.getElementById('silkscreen');

if(obj == null)
	return;
if(browser_is_ie == true)
	{
	opacity *= 100;
	obj.style.filter = 'alpha(opacity='+opacity+')';
	}
else
	{
	obj.style.opacity = opacity; 
	}	
}

function disable_screen(show)
{
var obj = document.getElementById('silkscreen');

if(obj == null)
	return;

if(show)
	{
	//var size = get_page_size();
	var page_height = avail_height;//size[1];
	var page_width = avail_width-20;
	page_width = Math.max(0, page_width);
	page_height = Math.max(0,page_height);
	obj.style.width = page_width + 'px';
	obj.style.height = page_height + 'px';
	}	
obj.style.visibility = show?"visible":"hidden";
obj.style.display = show?"":"none";
}

function activate_context_menu()
{
context_menu_active = true;
setTimeout('context_menu_active = false', 1000);
}

function show_current_shelf_controls(evnt)
{
if(owner == 0)
	return;		
var table1 = document.getElementById("shelf_table_"+req_shelf_id);
if(table1 == null)
	return;
get_xy(evnt);
var x = findPosX(table1);
var y = findPosY(table1);
//alert(x + "," + y);
if(elem_x > x && elem_x < shelf_area_wide && elem_y > y && elem_y < y + 40)
	{ 
	show_shelf_controls(table1, req_shelf_id);
	}
else
	{
	hide_current_shelf_controls();
	}	
}

function hide_current_shelf_controls()
{
var	shelf_edit_obj = document.getElementById('shelf_edit_control');
var	remove_edit_obj = document.getElementById('shelf_remove_control');
var shelf_controls_obj = document.getElementById('shelf_controls');
if(shelf_controls_obj == null || shelf_edit_obj == null || remove_edit_obj == null || owner == 0)
	return;

shelf_edit_obj.style.display='none';
remove_edit_obj.style.display = 'none';
shelf_controls_obj.style.visibility = 'hidden';
}

function set_shelf(shelf_id, prev_shelf_id)
{
	var shelf_id1 = prev_shelf_id;
	var table1 = document.getElementById("shelf_table_"+shelf_id1);
	var id;

	if(table1 != null)
		{
		table1.bgColor=''
		set_curved_shelf(prev_shelf_id,'');
		}
	table1 = document.getElementById("shelf_table_"+shelf_id);
	if(table1 == null)
		{
		req_shelf_id = shelf_id = 0;
		table1 = document.getElementById("shelf_table_"+shelf_id);
		}

	if(table1 != null)
		{
		table1.bgColor=theme_color;
		set_curved_shelf(shelf_id,theme_color);
		//show_shelf_controls(table1, shelf_id);
		}


	if(trash_can_shelf_id != -1)
		{
		var elem = document.getElementById('add_item_button');
		if(elem == null)
			return;
		if(shelf_id == trash_can_shelf_id)
			{
			elem.style.visibility = 'visible';
			elem.src = base_url+"/_images/new/empty_trash_can_inactive.gif";
			elem.title = "Empty trash can. Click here to empty trash can";
			}
		else if((shelf_id == borrow_list_shelf_id)||(shelf_id == recommendation_list_shelf_id))
			{
			elem.style.visibility = 'hidden';			
			}
		else 
			{
			elem.style.visibility = 'visible';
			elem.src = base_url+"/_images/new/add_item_inactive.gif";
			elem.title = "Add items to your library. Click here to open the add item page";
			}
		}
	
	if(shelf_id == 0)
		{
		var folder_elem = document.getElementById('folder_area');
		if(folder_elem != null)
			{
			folder_elem.style.backgroundImage = 'url('+background_color+')';
			}
		}
	}

function mouse_over_menulink(obj)
	{
	obj.style.border='1px solid #6100C1';
	obj.style.backgroundColor = '#F0E1FF';
	obj.style.color = 'black';
	}

function mouse_out_menulink(obj)
	{
	obj.style.border='1px solid '+theme_color;
	obj.style.backgroundColor = theme_color;
	obj.style.color = font_color;
	}

function hide_all_menu()
	{
	//if(menu_open == true)
		{
		menu_open = false;
		close_sort_menu();
		close_view_menu();
		close_shelf_sort_menu();
		close_cloud_menu();
		close_quick_add_menu();
		}
	}

function open_view_menu()
	{
	var show_menu = false;
	var elem1 = document.getElementById("view_menu");
	if(elem1.style.visibility.toLowerCase() != 'visible')
		show_menu = true;
	hide_all_popup_menu();
	hide_all_menu();
	if(show_menu == false)
		return;
		
	elem1 = document.getElementById("open_view_button");
 	var x = getImagePageLeft(elem1);
 	var y = getImagePageTop(elem1);
 	var menuTop = y + 25; // LAYER TOP POSITION
 	var menuLeft = x - 10;
	
	elem1 = document.getElementById("view_menu");
	elem1.style.top = menuTop;
	elem1.style.left = menuLeft;
	elem1.style.visibility="visible";

	menu_open = true;
	}

function close_view_menu()
	{
	var elem1 = document.getElementById("view_menu");
	if(elem1 != null)
		{
		elem1.style.visibility="hidden";
		}
	var elem1 = document.getElementById("open_view_button");
 	if(elem1 == null)
 		return;
	
	elem1.src = base_url+'/_images/new/view_inactive.gif';
	}

function show_genre_cloud()
{
}

function close_quick_add_menu()
{
var elem1 = document.getElementById("quick_add_item_menu");
	if(elem1 != null)
		{
		elem1.style.visibility="hidden";
		}
}

function open_quick_add_menu()
{
	var show_menu = false;
	var elem1 = document.getElementById("quick_add_item_menu");
	if(elem1.style.visibility.toLowerCase() != 'visible')
		show_menu = true;
	hide_all_popup_menu();
	hide_all_menu();
	if(show_menu == false)
		return;

	var elem1 = document.getElementById("add_item_button");
 	var x = getImagePageLeft(elem1);
 	var y = getImagePageTop(elem1);
 	var menuTop = y + 25; // LAYER TOP POSITION
 	var menuLeft = x - 10;

	elem1 = document.getElementById("quick_add_item_menu");
	elem1.style.top = menuTop;
	elem1.style.left = menuLeft;
	elem1.style.visibility="visible";

	menu_open = true;
	}

function open_cloud_menu()
	{
	var show_menu = false;
	var elem1 = document.getElementById("cloud_menu");
	if(elem1.style.visibility.toLowerCase() != 'visible')
		show_menu = true;
	hide_all_popup_menu();
	hide_all_menu();
	if(show_menu == false)
		return;

	switch(req_media_id)
		{
		default:
						document.getElementById('author_cloud_box').style.display='none';
						document.getElementById('director_cloud_box').style.display='none';
						break;
		case 1:
						document.getElementById('author_cloud_name').innerHTML = 'Author&nbsp;Cloud';
						document.getElementById('author_cloud_box').style.display='';
						document.getElementById('director_cloud_box').style.display='none';
						break;
		case 2:
						document.getElementById('author_cloud_name').innerHTML = 'Actor&nbsp;Cloud';
						document.getElementById('author_cloud_box').style.display='';
						document.getElementById('director_cloud_box').style.display='';
						break;
		case 3:
						document.getElementById('author_cloud_name').innerHTML = 'Artist&nbsp;Cloud';
						document.getElementById('author_cloud_box').style.display='';
						document.getElementById('director_cloud_box').style.display='none';
						
		}
		
	var elem1 = document.getElementById("open_cloud_button");
 	var x = getImagePageLeft(elem1);
 	var y = getImagePageTop(elem1);
 	var menuTop = y + 25; // LAYER TOP POSITION
 	var menuLeft = x - 10;

	elem1 = document.getElementById("cloud_menu");
	elem1.style.top = menuTop;
	elem1.style.left = menuLeft;
	elem1.style.visibility="visible";

	menu_open = true;
	}

function close_cloud_menu()
{
var elem1 = document.getElementById("cloud_menu");
	if(elem1 != null)
		{
		elem1.style.visibility="hidden";
		}
	var elem1 = document.getElementById("open_cloud_button");
 	if(elem1 == null)
 		return;
	
	elem1.src = base_url+'/_images/new/cloud_inactive.gif';
}

function open_sort_menu()
	{
	var show_menu = false;
	var elem1 = document.getElementById("sort_menu");
	//alert(elem1.style.visibility.toLowerCase());
	if(elem1.style.visibility.toLowerCase() != 'visible')
		show_menu = true;
	hide_all_popup_menu();
	hide_all_menu();
	if(show_menu == false)
		return;
	var elem1 = document.getElementById("open_sort_button");
 	if(elem1 == null)
 		return;
			
 	var x = getImagePageLeft(elem1);
 	var y = getImagePageTop(elem1);
 	var menuTop = y + 25; // LAYER TOP POSITION
 	var menuLeft = x - 10;

	var menu_name = "sort_menu";

	elem1 = document.getElementById(menu_name);
	if(elem1 == null)
		return;
	create_sort_menu(req_media_id, elem1);

	elem1.style.display = "";
	elem1.style.top = menuTop;
	elem1.style.left = menuLeft;
	elem1.style.visibility="visible";
	
	menu_open = true;	
	}

function close_sort_menu()
	{
	var menu_name = "sort_menu";

	var elem1 = document.getElementById(menu_name);
	if(elem1 != null)
		{
		elem1.style.visibility = "hidden";
		elem1.style.display = "none";
		}
	var elem1 = document.getElementById("open_sort_button");
 	if(elem1 == null)
 		return;
	
	elem1.src = base_url+'/_images/new/sort_inactive.gif';
	}

function select_media(media_num)
	{
	if(media_num == 6)
		{
		window.open(base_url+"/_community.php", "_self");
		return;
		}

	if(is_ajax_object_active(main_ajax_id))
		return;
	
	if(media_num != req_media_id || current_operation == 1)
		{	
		query_start_index = 0;
		query_total_result_count = 0;
		req_media_id = media_num;
		column_width_adjusted = false;
		//user_sort_type = 'title';
		//user_sort_direction = 0;
		}
	fetch_item(query_start_index);
	}

function add_item_to_gurulib()
{
if(req_shelf_id == trash_can_shelf_id)
	{
	if(query_result_count == 0)
		{
		show_alert_dialog("Error", "Trash Can is already empty");
		return;
		}
	//this is a request to flush the trash can
	var item = menu_names[req_media_id];
	if(req_media_id == 0)
		item = "items";
	var msg = "This will remove all the "+item+" in your 'Trash Can' permanently from your library";
	var header = "Empty Trashcan";
	window_confirm(header, msg, "empty_trash_can_confirm()", null);
	}
else
	{
	url_link = base_url+"/_add_item.php?shelf_id="+req_shelf_id+"&media_id="+req_media_id;
	window_open(url_link,'_self')
	}
}

function empty_trash_can_confirm()
{
var args = 						{	
											'action'		: 		'empty_trash_can', 
											'media_id'	:			req_media_id,
											'shelf_id'	: 		req_shelf_id,
											'random'		:			Math.random()
											};
ajax_get_request(ajax_request_url, args, empty_trash_can_response);

}

function empty_trash_can_response(response_array)
{
if(response_array[0] == "error")
	{
	show_alert_dialog("Error", response_array[1]);	
	}
else
	{
	fetch_item(0);
	var elem = document.getElementById('shelf_img_'+trash_can_shelf_id);
	if(elem != null)
		{
		if(elem.src.indexOf('_empty.gif') == -1)
			{
			elem.src = base_url+"/_images/trash_empty.gif";
			}
		}	
	}
}

function set_gurulib_view()
{
shelf_area_wide = 182;
banner_header_size = 95;	//This is the size of our top ad banner plus some extra space
folder_area_offset = 80; //this is the offset from the start of the media_shelf_table till the start of the item display area
border_x_width = 8;		//This accounts for the borders on various tables
page_x_offset = 10;		//this is from the left end of the browser to the start of our table border
page_y_offset = 10;		//this is from the top of the browser window till the start of the logo picture
horizontal_scroll_bar_height = 10;
vertical_scroll_bar_width = 20;
footer_size = 34; //this is the portion that is at the bottom where the privacy policy is shown
image_display_width = 90;									//This is the width of the thumbnail image displayed
image_display_height = 120;	//This is the height of the thumbnail image displayed
image_display_hgap = 20;			//This is the gap between the thumbnail images
image_display_vgap = 20;			//This is the gap between the thumbnail images
display_item_per_row = 7;
query_item_count = 28;  //This is the number of items that will be shown in a screen
gen_height 	= 0;
gen_width 	= 0;

if(user_view_type == THUMBNAIL_VIEW_LARGE)
	{
	image_display_width = 160;
	image_display_height = image_display_width;
	}
else if(user_view_type == THUMBNAIL_VIEW_SMALL)
	{
	image_display_width = 90;
	image_display_height = 120;
	}
else if(user_view_type == LIST_VIEW)
	{
	image_display_width = 240;
	image_display_height = 22;
	}	
else
	{
	image_display_width = 90;
	image_display_height = 120;
	}
	
gen_height 	= image_display_height;
gen_width 	= parseInt((gen_height*90)/120);

avail_width = screen.availWidth;
avail_height = screen.availHeight;

cover_flow_height = avail_height - 320;

if(avail_width < 1024)
	{
	avail_width = 1024;
	}
else
	{
	document.body.style.overflowX='auto';
	}	
avail_height = Math.max(738, avail_height);

var total_shelf_height = (shelf_count+3)*40  + banner_header_size + footer_size
+ folder_area_offset + page_y_offset + horizontal_scroll_bar_height;
//alert(total_shelf_height);
avail_height = Math.max(total_shelf_height, avail_height);

page_width = avail_width - page_x_offset - vertical_scroll_bar_width;
page_height = avail_height - page_y_offset - horizontal_scroll_bar_height;

media_area_wide = (page_width - shelf_area_wide);
media_shelf_area_high = (page_height - banner_header_size - footer_size);
folder_area_high = media_shelf_area_high - folder_area_offset;

var item_area_wide = media_area_wide;

var table_wide = (image_display_width + image_display_hgap);
var table_high = 0;
if(user_view_type != DETAIL_VIEW)
	{
	table_high = (image_display_height + image_display_vgap);
	}
else
	{
	table_high = 30;
	}	
display_item_per_row = parseInt(item_area_wide/table_wide);
image_display_hgap +=  (parseInt((item_area_wide%table_wide)/display_item_per_row) - 1);

if(display_item_per_row == 0)
	{
	display_item_per_row = 7;
	}

var num_rows = parseInt(folder_area_high/table_high);
var diff_inc = table_high - (folder_area_high%table_high) + 10;
if(diff_inc > 0)
	{
	num_rows +=1;
	folder_area_high += diff_inc;
	media_shelf_area_high += diff_inc;
	page_height += diff_inc;
	avail_height += diff_inc;
	}

if(user_view_type != DETAIL_VIEW)
	{
	query_item_count = num_rows * display_item_per_row;
	}
else
	{
	query_item_count = num_rows;//Math.min(num_rows, 30);
	}
//alert("item_count"+query_item_count + "," + num_rows + "," + display_item_per_row);			
elem = document.getElementById('main_page');
if(elem != null)
	{
	if(browser_is_ie == true)
		elem.style.width = avail_width - 26;
	else
		elem.style.width = avail_width - 24;
	}

if(user_view_type != DETAIL_VIEW)
	{
	elem = document.getElementById('folder_area');
	if(elem != null)
		{
		elem.style.width = '100%';
		}
	}
	
show_print_resize_element();
}

function init_gurulib()
{
init_ajax(40);
init_common_dialogs(theme_color, font_color, base_url);
init_cover_flow();
var i;
//preloading the images
for(i = 0; i < color_table_array.length; ++i)
	{
	color_table_array['img_obj'].src = color_array_table['path'];
	}
//query_start_index = 0;
setup_page();
disable_text_selection(false);
init_touchscreen_keyboard();
}

function edit_shelf()
{
hide_all_popup_menu();
hide_all_menu();

var elem1 = document.getElementById('shelf_box');
if(elem1 == null)
	return;
disable_screen(true);
var left1 = (screen.availWidth - 360)/2;
var top1 = (screen.availHeight - 210)/2 + document.body.scrollTop;
elem1.style.visibility = 'visible';
elem1.style.left = left1;
elem1.style.top = top1;

var shelf_info = get_shelf_info_from_shelf_id(req_shelf_id);
var picture_id = shelf_info['picture_id'];
var save_button = document.getElementById('add_shelf_button');

if(req_shelf_id != trash_can_shelf_id)
	{ 
	document.getElementById('private_shelf_id').disabled = false;
	document.getElementById('public_shelf_id').disabled = false;
	if(shelf_info['private'])
		{
		document.getElementById('private_shelf_id').checked = true;
		document.getElementById('public_shelf_id').checked = false;
		}
	else
		{
		document.getElementById('private_shelf_id').checked = false;
		document.getElementById('public_shelf_id').checked = true;
		}
	}
else
	{
	document.getElementById('private_shelf_id').checked = true;
	document.getElementById('public_shelf_id').checked = false;
	document.getElementById('private_shelf_id').disabled = true;
	document.getElementById('public_shelf_id').disabled = true;
	}		
document.getElementById('shelf_header').innerHTML = 'Edit&nbsp;Shelf';		
document.getElementById('color_name').innerHTML = color_table_array[picture_id]['name'];
document.getElementById('color_value').src = color_table_array[picture_id]['path'];
save_button.innerHTML = 'Save';
save_button.onclick = save_shelf;

elem1 = document.getElementById('shelf_name_input');
if(elem1 != null)
	{
	elem1.value = shelf_info['name'];
	if(req_shelf_id == wish_list_shelf_id || req_shelf_id == recommendation_list_shelf_id || req_shelf_id == trash_can_shelf_id | req_shelf_id == borrow_list_shelf_id)
		{
		elem1.disabled = true;
		}
	else
		{
		elem1.focus();
		elem1.disabled = false;
		}	
	}
}

function save_shelf()
{
	var shelf_name = document.getElementById('shelf_name_input').value;
	shelf_name = trim(shelf_name);
	if(shelf_name.length != 0)
		{
		//check if this name exists in the name list
		var elem = document.getElementById('shelf_table_check_'+shelf_name);
		if(elem != null && shelf_name != get_shelf_info_from_shelf_id(req_shelf_id)['name'])
			{
			close_shelf_box();
			show_alert_dialog("Error", "Shelf '" +shelf_name+"' already exists. Please choose another name.");
			//document.getElementById('shelf_name_input').focus();
			return;
			}
		else
			{
			var private_shelf = 0;
			var boxes = document.getElementById('private_shelf_id');
			if(boxes != null)
				{
				private_shelf = (boxes.checked==true)?1:0;
				}
			request_save_shelf(req_shelf_id, shelf_name, shelf_color_id, private_shelf);
			close_shelf_box();
			}
		}
	else
		{
		close_shelf_box();
		show_alert_dialog("Error", "Please provide a valid shelf name");
		//document.getElementById('shelf_name_input').focus();
		}

}

function 	request_remove_shelf(shelf_id, trash_can_shelf_id)
{
var args = 						{	
											'action'					: 		'move_items_remove_shelf', 
											'trash_shelf_id'	:			trash_can_shelf_id,
											'shelf_id'				: 		shelf_id,
											'random'					:			Math.random()
											};
ajax_get_request(ajax_request_url, args, move_items_remove_shelf_response);
}

function move_items_remove_shelf_response(response_array)
{
if(response_array[0] == "error")
	{
	show_alert_dialog("Error", response_array[1]);
	}
else
	{
	var shelf_id 			= (response_array.shift() * 1);
	shelf_id = delete_shelf(shelf_id);
	display_shelves();
	select_shelf(shelf_id);
	}
}

function request_save_shelf(shelf_id, shelf_name, color_id, private_shelf)
{
var args = 						{	
											'action'					: 		'save_shelf', 
											'shelf_name'			:			shelf_name,
											'shelf_id'				: 		shelf_id,
											'background_id'		:			color_id,
											'private_shelf'		:			private_shelf,
											'random'					:			Math.random()
											};
ajax_get_request(ajax_request_url, args, save_shelf_response);
disable_screen(false);
}

function save_shelf_response(response_array)
{
if(response_array[0] == "error")
	{
	show_alert_dialog("Error", response_array[1]);
	}
else
	{
	var shelf_id 			= (response_array.shift() * 1);
	var shelf_name 		= (response_array.shift());
	var private_shelf	= (response_array.shift() * 1);
	var picture_id 		= (response_array.shift() * 1);
	update_shelf(shelf_id, shelf_name, private_shelf, picture_id);
	req_shelf_id = 0;
	select_shelf(shelf_id);
	}
}

function remove_shelf()
{
hide_all_popup_menu();
hide_all_menu();

var shelf_name = (get_shelf_info_from_shelf_id(req_shelf_id)['name']);
var msg = "This will delete the shelf '"+shelf_name+"' and move all the titles in it to shelf 'Trash Can'";
window_confirm("Remove Shelf", msg, "remove_current_shelf()", null);
}

function remove_current_shelf()
{
request_remove_shelf(req_shelf_id, trash_can_shelf_id);
}

function show_shelf_controls(parent_obj, shelf_id)
{
var dx = 0;	
var	shelf_edit_obj = document.getElementById('shelf_edit_control');
var	remove_edit_obj = document.getElementById('shelf_remove_control');
var shelf_controls_obj = document.getElementById('shelf_controls');
//var parent_obj = document.getElementById(parent_control_id);
if(parent_obj == null || shelf_controls_obj == null || shelf_edit_obj == null || remove_edit_obj == null || owner == 0)
	return;
if(shelf_id == 0)
	{
	shelf_edit_obj.style.display = 'none';
	remove_edit_obj.style.display = 'none';
	shelf_controls_obj.style.visibility = 'hidden';
	return;
	}
	
shelf_edit_obj.style.display = '';

if(shelf_id != wish_list_shelf_id && shelf_id != recommendation_list_shelf_id && shelf_id != trash_can_shelf_id && shelf_id != borrow_list_shelf_id)
	{
	remove_edit_obj.style.display = '';
	}
else
	{
	remove_edit_obj.style.display = 'none';
	dx = 16;
	}	
var x = findPosX(parent_obj);
var y = findPosY(parent_obj);
shelf_controls_obj.style.top = (y - 3) + 'px';
shelf_controls_obj.style.left = (shelf_area_wide - 45 + dx) + 'px';
shelf_controls_obj.style.visibility = 'visible';
}

function disable_selections(list)
{
var i;
for(i = 0; i < list.length; ++i)
	{
	var obj = document.getElementById(list[i]);
	if(obj != null)
		{
		disableSelection(obj);
		}
	}
}

function setup_page()
	{
	find_browser_type();

	if(media_options == null)
		{
		var arr1 = new Array("All",'Click here to list all titles from selected shelf',base_url+'/_images/all.gif');
		var arr2 = new Array("Books",'Click here to list books from selected shelf',base_url+'/_images/book.gif');
		var arr3 = new Array("Movies",'Click here to list movies from selected shelf',base_url+'/_images/movie.gif');
		var arr4 = new Array("Music",'Click here to list music from selected shelf',base_url+'/_images/music.gif');
		var arr5 = new Array("Games",'Click here to list games from selected shelf',base_url+'/_images/games.gif');
		var arr6 = new Array("Software",'Click here to list software from selected shelf',base_url+'/_images/software.gif');
		var arr7 = new Array("<span style=\"font-size: 9pt;\">Community</span>",'Click here to open GuruLib community',base_url+'/_images/community/users.gif');
		var arr8 = new Array(arr1,arr2,arr3,arr4,arr5,arr6,arr7);

		media_options = new curved_tabs('curved_media_tabs', arr8, 'select_media', font_color, theme_color, 400);
		if(media_options == null)
			{
			show_alert_dialog("Error", "Please reload");
			}
		//media_options.select_tab(0, true);
		}
	setInterval('hover_event_check()', 500);


	detail_view_format[ALL_MEDIA] 		= new Array();
	detail_view_format[ALL_MEDIA][0]	= TITLE_INDEX;
	detail_view_format[ALL_MEDIA][1]	= AUTHOR_INDEX;
	detail_view_format[ALL_MEDIA][2]	= PUBLISHER_INDEX;
	detail_view_format[ALL_MEDIA][3]	= USER_RATING_INDEX;
	detail_view_format[ALL_MEDIA][4]	= REL_DATE_INDEX;
	detail_view_format[ALL_MEDIA][5]	= ADD_DATE_INDEX;
	detail_view_format[ALL_MEDIA][6]	= GENRE_INDEX;


	detail_view_format[BOOKS] 		= new Array();
	detail_view_format[BOOKS][0]	= TITLE_INDEX;
	detail_view_format[BOOKS][1]	= AUTHOR_INDEX;
	detail_view_format[BOOKS][2]	= PUBLISHER_INDEX;
	detail_view_format[BOOKS][3]	= USER_RATING_INDEX;
	detail_view_format[BOOKS][4]	= REL_DATE_INDEX;
	detail_view_format[BOOKS][5]	= ADD_DATE_INDEX;
	detail_view_format[BOOKS][6]	= GENRE_INDEX;

	detail_view_format[MOVIES] 		= new Array();
	detail_view_format[MOVIES][0]	= MPAA_INDEX;
	detail_view_format[MOVIES][1]	= TITLE_INDEX;
	detail_view_format[MOVIES][2]	= AUTHOR_INDEX;
	detail_view_format[MOVIES][3]	= DIRECTOR_INDEX;
	detail_view_format[MOVIES][4]	= PUBLISHER_INDEX;
	detail_view_format[MOVIES][5]	= USER_RATING_INDEX;
	detail_view_format[MOVIES][6]	= REL_DATE_INDEX;
	detail_view_format[MOVIES][7]	= ADD_DATE_INDEX;
	detail_view_format[MOVIES][8]	= GENRE_INDEX;

	detail_view_format[MUSIC] 	 	= new Array();
	detail_view_format[MUSIC][0]	= TITLE_INDEX;
	detail_view_format[MUSIC][1]	= AUTHOR_INDEX;
	detail_view_format[MUSIC][2]	= PUBLISHER_INDEX;
	detail_view_format[MUSIC][3]	= USER_RATING_INDEX;
	detail_view_format[MUSIC][4]	= REL_DATE_INDEX;
	detail_view_format[MUSIC][5]	= ADD_DATE_INDEX;
	detail_view_format[MUSIC][6]	= GENRE_INDEX;

	detail_view_format[GAMES] 	 	= new Array();
	detail_view_format[GAMES][0]	= MPAA_INDEX;
	detail_view_format[GAMES][1]	= TITLE_INDEX;
	detail_view_format[GAMES][2]	= PLATFORM_INDEX;
	detail_view_format[GAMES][3]	= PUBLISHER_INDEX;
	detail_view_format[GAMES][4]	= USER_RATING_INDEX;
	detail_view_format[GAMES][5]	= REL_DATE_INDEX;
	detail_view_format[GAMES][6]	= ADD_DATE_INDEX;
	detail_view_format[GAMES][7]	= GENRE_INDEX;

	detail_view_format[SOFTWARE] 	  = new Array();
	detail_view_format[SOFTWARE][0]	= TITLE_INDEX;
	detail_view_format[SOFTWARE][1]	= PLATFORM_INDEX;
	detail_view_format[SOFTWARE][2]	= PUBLISHER_INDEX;
	detail_view_format[SOFTWARE][3]	= USER_RATING_INDEX;
	detail_view_format[SOFTWARE][4]	= REL_DATE_INDEX;
	detail_view_format[SOFTWARE][5]	= ADD_DATE_INDEX;
	detail_view_format[SOFTWARE][6]	= GENRE_INDEX;

	//shelf_sort_menu_clicked(SHELF_NAME_SORT);

	var args = 						{	
												'action'			: 		'load_session', 
												'current'			:			current_userid,
												'logged'			: 		logged_userid,
												'random'			:			Math.random()
												};
	ajax_get_request(ajax_request_url, args, load_session_response);


	cloud_area_slide = new smooth_slide('horizontal_slider_bar', 'cloud_display_area');
	cover_flow_slide = new smooth_slide('cover_flow_bar', 'cover_flow_area');
	
	set_shelf_area_height(screen.availHeight);
	}

function load_session_response(response_array)
{
set_session_info(response_array);
var obj = document.getElementById('main_page');
obj.style.display = '';
var elem = document.getElementById('search_text');
if(elem)
	{
	elem.focus();	
	}
}

function cover_flow_opening(slider_obj, status)
{
set_cloud_position();
if(status == 1)
	{
	//slide operation is complete
	if(cover_flow_slide.end_height != 0)
		{
		show_cover_flow_message(cover_flow_slide.display_obj, press_left_right_arrow_keys_img.src);
		var obj = document.getElementById('show_full_screen_cover_flow_button');
		if(obj != null)
			{
			obj.style.visibility = 'visible';
			}
		}
	}
else if(status == 0)
	{
	hide_all_menu();
	hide_all_popup_menu();
	}
}

function cover_flow_closing(slider_obj, status)
{
set_cloud_position();
if(status == 0)
	{
	hide_all_menu();
	hide_all_popup_menu();
	}
}

function vertical_slide_opening(slider_obj, status)
{
if(status == 0)
	{
	hide_loading();
	hide_all_menu();
	hide_all_popup_menu();
	}
update_position_of_all_status_images();
}

function vertical_slide_closing(slider_obj, status)
{
if(status == 0)
	{
	hide_all_menu();
	hide_all_popup_menu();
	//THis is called when the slide starts
	var i;
	for(i = 0; i < 3; ++i)
		{
		document.getElementById('cloud_menu_'+i).innerHTML = '&nbsp';
		}
	
	cloud_width = vertical_slider_width;
	current_cloud_type = -1;
	}
else if(status == 1)
	{
	//this is called when the slide ends		
	//query_result_count = full_query_result_count;
	//update_list(item_result_array);
	update_position_of_all_status_images();	
	}
else if(status == 2)
	{
	update_position_of_all_status_images();
	}	
}

function refresh_cloud_info()
{
if(is_ajax_object_active(main_ajax_id))
	return;

var cloud_type = current_cloud_type;
current_cloud_type = -1;
show_cloud(cloud_type);
}

function set_cloud_position()
{
var slider_obj = cloud_area_slide;		
var y = (findPosY(slider_obj.obj));
var x = (findPosX(slider_obj.obj));
slider_obj.display_obj.style.top = y + 'px';
slider_obj.display_obj.style.left = x+ 'px';		
}

function update_cover_flow_status(img_id)
{
if(cover_flow_slide.end_height == 0)
	return;
var item_owner_id = img_id.substr(12);
var item_row_id 		= "item_row_"+item_owner_id;
var obj = document.getElementById(item_row_id);
if(obj != null)
	{
	obj.style.color = obj.style.backgroundColor;
	obj.style.backgroundColor='#556677';
	}

item_row_id 		= "item_row_"+current_cover_flow_target;
obj = document.getElementById(item_row_id);
if(obj != null)
	{
	obj.style.backgroundColor=obj.style.color;
	obj.style.color = 'black';
	}	
current_cover_flow_target = item_owner_id;
}

function cover_flow_size()
{
if(document.getElementById('main_page').style.display != '')
	{
	document.body.style.backgroundColor ='';
	document.getElementById('main_page').style.display = '';
	set_cover_flow_position();
	cover_flow_slide.display_obj.style.clip = prev_clip_region;
	hide_cover_flow_message();
	
	}
else
	{
	fullscreen_cover_flow();
	show_cover_flow_message(cover_flow_slide.display_obj, press_esc_to_exit_img.src);
	}	
moveTo(current);
}

function show_cover_flow_message(div_obj, img_file)
{
var obj = document.getElementById('cover_flow_message');
var left1 = parseInt(div_obj.style.left ) + (parseInt(div_obj.style.width) - 459)/2;
var top1 = parseInt(div_obj.style.top ) + (parseInt(div_obj.style.height) - 58)/2 + document.body.scrollTop;
obj.style.left = left1 + 'px';
obj.style.top = top1 + 'px';
obj.style.display='';
obj.src = img_file; //;
setTimeout('hide_cover_flow_message()',4000);
}

function hide_cover_flow_message()
{
document.getElementById("cover_flow_message").style.display="none";
}

function fullscreen_cover_flow()
{
document.getElementById('main_page').style.display = 'none';
document.body.style.backgroundColor ='black';
document.body.style.overflow = 'hidden';
//document.body.style.height = '0px';

var cover_flow_area_obj = cover_flow_slide.display_obj;
cover_flow_area_obj.style.backgroundColor = 'black';
var wide = parseInt(screen.availWidth)-10;
cover_flow_area_obj.style.width = wide + 'px';
//cover_flow_bar_obj.style.width = wide + 'px';
var high = parseInt(screen.availHeight);
cover_flow_height = parseInt(wide*0.4) + 60;
cover_flow_height = Math.min(cover_flow_height, high);
cover_flow_area_obj.style.height = cover_flow_height + 'px';
img_div.style.height= (cover_flow_height-60) + 'px';
prev_clip_region = cover_flow_area_obj.style.clip;
cover_flow_area_obj.style.clip = 		'rect(0,'+wide+','+cover_flow_height+',0)';
//cover_flow_area_obj.style.border="1px solid blue";
cover_flow_area_obj.style.top = '20px';//(parseInt(screen.availHeight)-cover_flow_height)/2 + 'px';
cover_flow_area_obj.style.left = '0px';
var prev_img = document.getElementById('cover_flow_prev_page_img');
var next_img = document.getElementById('cover_flow_next_page_img');
var status_msg = document.getElementById('cover_flow_page_status');

if(prev_img != null && next_img != null && status_msg != null)
	{
	var end_index = (query_start_index + query_result_count);
	var count = query_total_result_count;

	prev_img.style.visibility = (query_start_index == 0)?'hidden':'visible';
	next_img.style.visibility = (end_index == count)?'hidden':'visible';
	status_msg.style.visibility = 'visible';
	}
refresh_cover_flow();
full_screen_cover_flow_mode = true;
disable_text_selection(true);
}

function set_cover_flow_position()
{
document.body.style.overflow = '';
var cover_flow_area_obj = cover_flow_slide.display_obj;
var cover_flow_bar_obj = cover_flow_slide.obj;
y = findPosY(cover_flow_bar_obj);
x = findPosX(cover_flow_bar_obj);
cover_flow_area_obj.style.top = y + 'px';
cover_flow_area_obj.style.left = x + 'px';
var wide = get_media_area_width();
cover_flow_area_obj.style.width = wide + 'px';
cover_flow_bar_obj.style.width = wide + 'px';
var high = avail_height - 320;
cover_flow_height = parseInt(wide*0.4) + 45;
cover_flow_height = Math.min(cover_flow_height, high);
cover_flow_area_obj.style.height = cover_flow_height + 'px';
img_div.style.height= (cover_flow_height-45) + 'px';
var prev_img = document.getElementById('cover_flow_prev_page_img');
var next_img = document.getElementById('cover_flow_next_page_img');
var status_msg = document.getElementById('cover_flow_page_status');

if(prev_img != null && next_img != null && status_msg != null)
	{
	prev_img.style.visibility = 'hidden';
	next_img.style.visibility = 'hidden';
	status_msg.style.visibility = 'hidden';
	}
refresh_cover_flow();
full_screen_cover_flow_mode = false;
disable_text_selection(false);
}

function show_cloud(cloud_type)
{
var i;
if(is_ajax_object_active(main_ajax_id))
	return;
set_cloud_position();
var slider_obj = cloud_area_slide;		
var wide = get_media_area_width();
slider_obj.obj.style.width = wide + 'px';
slider_obj.display_obj.style.width = wide + 'px';
	
for(i = 0; i < 3; ++i)
	{
	if(i == cloud_type)
		{
		if(current_cloud_type != cloud_type)
			{
			document.getElementById('cloud_menu_'+i).innerHTML = '<img src="'+base_url+'/_images/tick.gif">';
			}
		else
			{
			document.getElementById('cloud_menu_'+i).innerHTML = '&nbsp';
			}
		}
	else
		{
		document.getElementById('cloud_menu_'+i).innerHTML = '&nbsp';
		}	
	}
	
if(current_cloud_type != cloud_type)
	{
	current_cloud_type = cloud_type;
	request_cloud_info(cloud_str[current_cloud_type]);
	}
else
	{
	cloud_area_slide.set_height(0, vertical_slide_closing);
	}	
	
}

function view_menu_clicked(view_type)
	{
	refresh_webpage = true;
	user_view_type = view_type;
	show_loading();
	save_session_info();
	}

function set_view_menu(view_type)
{
	var elem = document.getElementById('view_menu_'+user_view_type);
	if(elem != null)
		elem.innerHTML = "&nbsp;";

	elem = document.getElementById('view_menu_'+view_type);
	if(elem != null)
		elem.innerHTML = '<img src="'+base_url+'/_images/tick.gif">';

	var cmnd_str;

	if(user_view_type != view_type)
		{
		fetch_item_data();
		}
	user_view_type = view_type;
	update_sort_button_status();
}

function search_item(start_index)
	{
	if(is_ajax_object_active(main_ajax_id) == false)
		{		
		if(current_operation == 0)
			{
			query_total_result_count = 0;
			}
		current_operation = 1; //indicates a specific search operation
		query_start_index = start_index;
		qeury_result_count = 0;
		var elem = document.getElementById('search_text');
		if(elem == null)
			{
			return;
			}
		var str = trim(elem.value);
		if(str == 'Search' || str == '')
			{
			//return;
			str = '';
			current_operation = 0;
			}
		var search_array = str.split(':');
		if(search_array.length > 1)
			{
			current_search_criterion = (trim(search_array[0]));
			current_search_criterion = current_search_criterion.toLowerCase();
			current_search_data = (trim(search_array[1]));
			}
		else
			{
			current_search_criterion = "";
			current_search_data = (str);
			}
	
		var args = 						{	
													'action'						: 		'search_items', 
													'userid'						:			current_userid,
													'media_id'					: 		req_media_id,
													'shelf_id'					: 		req_shelf_id, 
													'search_data'				:			current_search_data,
													'search_criterion'	:		current_search_criterion,
													'view'							: 		user_view_type, 
													'sort_type'					:			user_sort_type,
													'sort_direction'		:			user_sort_direction,
													'start_index'				:			start_index,
													'item_count'				:			query_item_count,
													'total_count'				:			query_total_result_count,
													'random'						:			Math.random()
													};
		main_ajax_id = ajax_get_request(ajax_request_url, args, search_items_response);

		}
	else
		{
		//alert('ajax busy');
		}
	}

function search_items_response(response_array)
{
search_result = true;
display_query_result(response_array);
}

function display_query_result(response_array)
{
hide_all_popup_menu();
hide_all_menu();
var table1 = document.getElementById("shelf_table_"+req_shelf_id);
if(table1 != null)
	show_shelf_controls(table1, req_shelf_id);

if(response_array[0] == "error")
	{
	query_result_count = 0;
	show_alert_dialog("Error", response_array[1]);
	}
else
	{
	var update_cloud = true;
	clear_canvas();
	set_shelf_background(response_array.shift());
	var new_query_start_index = (response_array.shift() * 1);
	if(query_start_index != new_query_start_index)
		{
		update_cloud = false;
		query_start_index = new_query_start_index;
		}
	
	query_total_result_count = (response_array.shift() * 1);
	query_result_count = (response_array.shift() * 1);
	var temp = response_array.shift().split("_");
	if(temp.length > 1)
		{
		for(var i = 0; i < temp.length; ++i)
			{
			temp[i] = parseInt(temp[i]);
			}

		detail_view_format[req_media_id] = temp;
		}
	save_session_info();

	//full_query_result_count = query_result_count;
	if(cloud_area_slide != null)
		{
		if(cloud_area_slide.end_height == cloud_area_height)
			{
			query_result_count = Math.min(query_result_count, query_item_count);
			if(update_cloud)
				{
				refresh_cloud_info();
				}
			}
		}
	update_list(response_array);
	}
}

function handle_text_keydown(btn, event)
{
 if (document.all)
	{
	if (event.keyCode == 13)
		{
		event.returnValue=false;
		event.cancel = true;
		btn.click();
		}
	}
 else if (document.getElementById)
	{
	if (event.which == 13)
		{
		event.returnValue=false;
		event.cancel = true;
		btn.click();
		}
	}
 else if(document.layers)
	{
	if(event.which == 13)
		{
		event.returnValue=false;
		event.cancel = true;
		btn.click();
		}
	}
}

function handle_edit_this_key(id_str, event)
{
var save_img_id = id_str+'_save_img' ;
var save_btn = document.getElementById(save_img_id);
if(save_btn == null)
	return;
var close_img_id = id_str+'_close_img' ;
var close_btn = document.getElementById(close_img_id);
if(close_btn == null)
	return;

 if (document.all)
	{
	if (event.keyCode == 13)
		{
		event.returnValue=false;
		event.cancel = true;
		save_btn.onclick();
		}
	else if(event.which == 27)
		{
		event.returnValue=false;
		event.cancel = true;
		close_btn.onclick();
		}
	}
 else if (document.getElementById)
	{
	if (event.which == 13)
		{
		event.returnValue=false;
		event.cancel = true;
		save_btn.onclick();
		}
	else if(event.which == 27)
		{
		event.returnValue=false;
		event.cancel = true;
		close_btn.onclick();
		}
	}
 else if(document.layers)
	{
	if(event.which == 13)
		{
		event.returnValue=false;
		event.cancel = true;
		save_btn.onclick();
		}
	else if(event.which == 27)
		{
		event.returnValue=false;
		event.cancel = true;
		close_btn.onclick();
		}
	}
}

function search_keyword(obj)
{
query_start_index = 0;
query_total_result_count = 0;
query_total_result_count = 0;
column_width_adjusted = false;
search_item(0);
}

function query_status_clicked(start_index)
	{
	var temp;
	var temp = query_start_index;
	query_start_index = start_index;
	fetch_item_data();
	query_start_index = temp;	
	}

function fill_query_status()
{
var elem = document.getElementById('query_status');
if(elem == null)
	return;
var cover_flow_next_img = null;
var cover_flow_prev_img = null;
var cover_flow_status_msg = null;
cover_flow_next_img = document.getElementById('cover_flow_next_page_img');
cover_flow_prev_img = document.getElementById('cover_flow_prev_page_img');
cover_flow_status_msg = document.getElementById('cover_flow_page_status');

if(query_result_count != 0)
	{
	var next_rqst_str;
	var start_index;
	var end_index;
	var count;

	start_index = (query_start_index + 1);
	end_index = (query_start_index + query_result_count);
	count = query_total_result_count;

	var str = start_index+"-"+end_index+"&nbsp;of&nbsp;"+count;
	if(cover_flow_status_msg != null)
		{
		var str1 = "Listing " + str + " ";
		
		switch(req_media_id)
			{
			case BOOKS: str1 += 'books';break;
			case MOVIES: str1 += 'movies';break;
			case MUSIC: str1 += 'music';break;
			case GAMES: str1 += 'games';break;
			case SOFTWARE: str1+= 'software';break;	
			default: str1+="items";break;
			}
		var shelf_info = get_shelf_info_from_shelf_id(req_shelf_id);
		if(shelf_info != null)
			{
			str1 += " in shelf <b>" + shelf_info['name'] + "</b>";
			}
		cover_flow_status_msg.innerHTML = str1; 		
		}
	str +="&nbsp;&nbsp;&nbsp;"
	
	if(start_index == 1)
		{
		str += "<span style=\"color: gray;\">First&nbsp;|&nbsp;Previous</span>&nbsp;|&nbsp;</span>";
		if(cover_flow_prev_img != null)
			{
			cover_flow_prev_img.style.visibility = 'hidden';
			}
		}
	else
		{
		var prev_index = query_start_index - query_item_count;
		if(prev_index < 0)
			prev_index = 0;

		str += "<span title=\"Click here to fetch "+query_item_count+" items from the start\" onmouseup=\"query_status_clicked(0);\" style=\"color: "+font_color+"; cursor: pointer; cursor: hand;\">First</span>&nbsp;|&nbsp;<span title=\"Click here to fetch the previous "+query_item_count+" items\" onmouseup=\"query_status_clicked("+prev_index+");\" style=\"color: "+font_color+"; cursor: pointer; cursor: hand;\">Previous</span>&nbsp;|&nbsp;";
		if(cover_flow_prev_img != null)
			{
			if(full_screen_cover_flow_mode)
				cover_flow_prev_img.style.visibility = 'visible';
			cover_flow_prev_img.href = "javascript:query_status_clicked("+prev_index+")";
			}
		}

	if(end_index == count)
		{
		str += "<span style=\"color: gray;\">Next&nbsp;|&nbsp;Last</span>";
		if(cover_flow_next_img != null)
			{
			cover_flow_next_img.style.visibility = 'hidden';
			}
		}
	else
		{
		var last_index = count - query_item_count;
		if(last_index < 0)
			last_index = 0;

		str += "<span title=\"Click here to fetch the next "+query_item_count+" items\" onmouseup=\"query_status_clicked("+end_index+");\" style=\"color: "+font_color+"; cursor: pointer; cursor: hand;\">Next</span>&nbsp;|&nbsp;<span title=\"Click here to fetch the last "+query_item_count+" items\" onmouseup=\"query_status_clicked("+last_index+");\" style=\"color: "+font_color+"; cursor: pointer; cursor: hand;\">Last</span>";
		if(cover_flow_next_img != null)
			{
			if(full_screen_cover_flow_mode)
				cover_flow_next_img.style.visibility = 'visible';
			cover_flow_next_img.href = "javascript:query_status_clicked("+end_index+")";
			}

		}

		elem.innerHTML = str;

	}
else
	{
	elem.innerHTML = "&nbsp;";
	}
}

function sort_list(sort_type)
{
user_sort_direction = ((user_sort_type == sort_type)?((user_sort_direction == 0)?1:0):0);
user_sort_type = sort_type;
var cmnd_str = '';

fetch_item_data();
}

function update_sort_button_status()
{
var elem = document.getElementById('open_sort_button');
if(elem == null)
	return;
if(user_view_type == DETAIL_VIEW) //detail view then disable the sort button
	{
	elem.style.display = 'none';
	}
else
	{
	elem.style.display = 'block';
	}
}

function set_session_info(session_data)
	{
	var temp;
	temp = session_data.shift();
	if(req_media_id == -1)
		{
		req_media_id=temp*1;
		}
	temp = session_data.shift();
	if(req_shelf_id == -1)
		{	
		req_shelf_id=temp*1;
		}
	temp = session_data.shift();
	if(user_view_type == -1)
		{	
		user_view_type=temp*1;
		}
	temp = session_data.shift();
	if(query_start_index == -1)
		{	
		query_start_index=temp*1;
		}
	temp = session_data.shift();
	if(user_sort_direction == -1)
		{
		user_sort_direction=temp*1;
		}	
	temp = session_data.shift();
	if(user_sort_type == null)
		{
		user_sort_type=temp;
		}
	temp = session_data.shift();
	if(current_search_criterion == null)
		{	
		current_search_criterion=temp;
		}
	temp = session_data.shift();
	if(current_search_data == null)
		{	
		current_search_data=(temp);
		}	
	shelf_sort_type	= session_data.shift()*1;
	shelf_sort_direction = session_data.shift()*1;
	
	user_view_type = (user_view_type == DEFAULT_VIEW)?default_library_view:user_view_type;
	fill_shelf_info(session_data);

	//configure the browser dimension
	set_gurulib_view();
	//full_query_item_count = query_item_count;
	
	if(shelf_sort_type == null)
		{
		shelf_sort_type = SHELF_NAME_SORT;
		}
	if(shelf_sort_direction == null)
		{
		shelf_sort_direction = 1;
		}
	show_sort_shelf();
	//set the sort menu type
	set_view_menu(user_view_type);
	//set the shelf
	set_shelf(req_shelf_id, -1);


	//set the media
	//set_media(req_media_id, -1);
	if(media_options.select_tab != null)
		{
		media_options.select_tab(req_media_id, false);
		}

		
	if(current_search_data.length != 0)
		{
		var elem = document.getElementById('search_text');
		if(elem == null)
			return;
		//elem.focus();
		if(current_search_criterion.length == 0)
			{
			elem.value = current_search_data;
			}
		else
			{
			elem.value = current_search_criterion + ":" +current_search_data;
			}
		current_operation = 1;
		query_total_result_count = 0;
		}

	
	if(user_view_type != DETAIL_VIEW)
		{
		var arr = new Array('shelf_area',											
											'folder_area',
											'curved_media_tabs',
											'item_list'
											);
		disable_selections(arr);
		/*var elem = document.getElementById('show_cover_flow_button');
		if(elem != null)
			{
			elem.style.display = 'none';
			}*/
		}
	else
		{
		var arr = new Array('show_cover_flow_button', 'cover_flow_area', 'cover_flow_bar');
		disable_selections(arr);
		}
	//alert('ere');
	fetch_item_data();
	}

function fill_shelf_info(shelf_data)
{
var count = shelf_data.shift()*1;
var i;
var shelf_id;
var shelf_name;
var private_shelf;
var shelf_type;
var picture_id;
owner_shelf_array = new Array();
shelf_count += count;
for(i = 0; i < count; ++i)
	{
	shelf_id = shelf_data.shift()*1;
	shelf_name = (shelf_data.shift());
	shelf_type = shelf_data.shift()*1;	
	private_shelf = shelf_data.shift()*1;
	picture_id = shelf_data.shift()*1;
	
	owner_shelf_array[i] = new Array();
	owner_shelf_array[i]['id'] = shelf_id;
	owner_shelf_array[i]['name'] = shelf_name;
	owner_shelf_array[i]['private'] = private_shelf;
	owner_shelf_array[i]['type'] = shelf_type;
	owner_shelf_array[i]['picture_id'] = picture_id;
	}
shelf_array = new Array();
count = shelf_data.shift()*1;
for(i = 0; i < count; ++i)
	{
	shelf_id = shelf_data.shift()*1;
	shelf_name = (shelf_data.shift());
	shelf_type = shelf_data.shift()*1;	
	private_shelf = shelf_data.shift()*1;
	picture_id = shelf_data.shift()*1;

	shelf_array[i] = new Array();
	shelf_array[i]['id'] = shelf_id;
	shelf_array[i]['name'] = shelf_name;
	shelf_array[i]['type'] = shelf_type;
	shelf_array[i]['private'] = private_shelf;
	shelf_array[i]['picture_id'] = picture_id;
	}
}

function set_permalink()
{
var str = base_url+"/"+escape(current_username)+"&s="+req_shelf_id+"&m="+req_media_id+"&v="+user_view_type+"&i="+query_start_index+"&sod="+user_sort_direction+"&sot="+escape(user_sort_type);
if(current_search_criterion.length != 0)
	{
	str +="&sec="+escape(current_search_criterion);
	}
if(current_search_data.length != 0)
	{
	var str1 = string_replace(current_search_data, '"','');
	str += "&sed="+escape(str1);
	}
		
var obj = document.getElementById('permalink');
obj.href = str;
}

function save_session_info()
{
var args = 						{	
											'action'								: 		'save_session', 
											'username'							:			current_username,
											'media_id'							: 		req_media_id,
											'shelf_id'							: 		req_shelf_id, 
											'search_data'						:			current_search_data,
											'search_criterion'			:			current_search_criterion,
											'view_type'							: 		user_view_type, 
											'shelf_sort_type'				:			shelf_sort_type,
											'shelf_sort_direction'	:			shelf_sort_direction,
											'sort_type'							:			user_sort_type,
											'sort_direction'				:			user_sort_direction,
											'start_index'						:			query_start_index,
											'random'								:			Math.random()
											};
main_ajax_id = ajax_get_request(ajax_request_url, args, save_session_response, false);
}

function save_session_response(response_array)
{
set_permalink();
if(refresh_webpage)
	{
	window_open(base_url + '/'+escape(current_username),'_self');
	}
}

function show_print_view(export_view)
{
var args = 						{	
											'action'								: 		'print_view', 
											'userid'								:			current_userid,
											'media_id'							: 		req_media_id,
											'shelf_id'							: 		req_shelf_id, 
											'search_data'						:			current_search_data,
											'search_criterion'			:			current_search_criterion,
											'view'									: 		user_view_type, 
											'export_view'						:			export_view,
											'sort_type'							:			user_sort_type,
											'sort_direction'				:			user_sort_direction,
											'start_index'						:			query_start_index,
											'item_count'						:			query_item_count,
											'total_count'						:			query_total_result_count,
											'random'								:			Math.random()
											};
ajax_get_request(ajax_request_url, args, print_view_response);
}

function print_view_response(response_array)
{
if(response_array[0] == "error")
	{
	show_alert_dialog("Error", response_array[1]);
	}
else
	{
	//print sql is global
	print_sql = response_array[0];
	var export_view = response_array[1]*1;
	if(export_view)
		{
		open_export_view();
		}
	else	
		{
		open_print_view();
		}
	}
}

function open_print_view()
{
if(user_view_type != DETAIL_VIEW)
	return;
var wndw = window.open(base_url+"/_print_view.php?userid="+current_userid+"&media="+req_media_id+"&shelf="+req_shelf_id, "_blank");
if(wndw == null)
	{
	show_alert_dialog("Error", "Please disable your popup blocker for GuruLib to open the printable window");
	}
}

function open_export_view()
{
if(user_view_type != DETAIL_VIEW)
	return;
var shelf = get_owner_shelf_info_from_shelf_id(req_shelf_id);
var shelf_name = '';
if(shelf == null)
	{
	shelf_name = current_username;
	}
else
	{
	shelf_name = (shelf['name']);
	}	
export_wndw = window.open(base_url+"/_export_view.php?userid="+current_userid+"&media="+req_media_id+"&shelf_name="+(shelf_name), "_blank");
if(export_wndw == null)
	{
	show_alert_dialog("Error", "Please disable your popup blocker for GuruLib to open the export page");
	}
}

function close_export_view(obj)
{
}

function show_print_button()
{
	//show the print view button
	var print_obj = document.getElementById('print_view_button');
	if(print_obj != null)
		{
		//print_obj.style.visibility='visible';
		print_obj.style.display='block';
		}
}

function hide_print_button()
{
	//show the print view button
	var print_obj = document.getElementById('print_view_button');
	if(print_obj != null)
		{
		//print_obj.style.visibility='hidden';
		print_obj.style.display='none';
		}
}

function show_export_button()
{
	//show the print view button
	var export_obj = document.getElementById('export_view_button');
	if(export_obj != null)
		{
		//print_obj.style.visibility='visible';
		export_obj.style.display='block';
		}
}

function hide_export_button()
{
	//show the print view button
	var export_obj = document.getElementById('export_view_button');
	if(export_obj != null)
		{
		//print_obj.style.visibility='hidden';
		export_obj.style.display='none';
		}
}

function is_encoded(index_num)
{
index_num = (index_num%row_item_count);
switch(index_num)
	{
	case MEDIUM_IMG_INDEX:
	case TITLE_INDEX:					
	case PUBLISHER_INDEX:			
	case MPAA_INDEX:					
	case AUTHOR_INDEX:				
	case DIRECTOR_INDEX:			
	case GENRE_INDEX:					
	case PLATFORM_INDEX:			
	case SHELF_INDEX:					
	case BORROWER_NAME_INDEX:
	case LENDER_NAME_INDEX:
	case SMALL_IMG_INDEX:
	case LARGE_IMG_INDEX:
	case DETAIL_URL_INDEX:
	case SYNOPSIS_INDEX:
	case MY_REVIEW_INDEX:
		return true;
		
	default: 
		return false; 		
	}
}

function copy_item_result_array(result_array)
{
var count = result_array.length;
var i;
var str;
var elem;
item_result_array = new Array();
for(i = 0; i < count; ++i)
	{
	str = result_array[i];
	item_result_array[i] = str;//decode_utf8(str);
	}
}

function display_items()
{
//return;
var elem;
var str = '';
var count = query_result_count;
switch(user_view_type)
	{
	case THUMBNAIL_VIEW_LARGE:
	case THUMBNAIL_VIEW_SMALL:
	case LIST_VIEW:
			
			if(current_disp_y == -1)
				{
				hide_print_button();
				hide_export_button();
				create_view_framework();
				if(current_interval_timer != null)
					{
					clearInterval(current_interval_timer);
					current_interval_timer = null;
					}
				current_disp_y++;
				current_interval_timer = setInterval('display_items()',10);
				}
			else 
				{
				for(var i = 0; i < 4; ++i, current_disp_y++)
					{
					if(current_disp_y < (query_result_count/display_item_per_row))
						{
						if(user_view_type == LIST_VIEW)
							{
							str = list_view(current_disp_y);
							}
						else
							{
							str = thumbnail_view(current_disp_y);
							}
						elem = document.getElementById("item_row_view_"+current_disp_y);
						if(elem != null)
							{
							//elem.style.display = 'none';
							elem.innerHTML = str;
							}
						//current_disp_y++;
						//display_items();				
						}
					else
						{
						if(current_interval_timer != null)
							{
							var current_item_owner_id = get_current_item_owner_id();
							clearInterval(current_interval_timer);
							current_interval_timer = null;
							//show_all_items();
							update_position_of_all_status_images();
							set_shelf_area_height(null);						
							break;
							}				
						}
					}
				}	
							
		break;
		
	case DETAIL_VIEW:
		current_interval_timer = null;
		str = detail_view();
		elem = document.getElementById('item_list');
		if(elem != null)
			{
			elem.style.display = 'none';
			elem.style.visibility = 'hidden';
			elem.innerHTML = str;
			elem.style.display = '';
			if(column_width_adjusted == false)
				{
				//compute the width required for each column header
				init_detail_view_widths();
				}
			elem.style.display = 'none';
			show_sort_indicator(user_sort_direction, user_sort_type);
			fill_detail_view_entry(0, query_result_count);
			adjust_text_widths(elem, 0, query_result_count);
			elem.style.visibility = 'visible';
			elem.style.display = '';
			set_shelf_area_height(null);
			if(cover_flow_slide.end_height != 0)
				{
				//set_cover_flow_position();		
				//enable_image_flow_controls();
				moveTo(current);
				}
			else if(show_cover_flow_view)
				{
				show_cover_flow_view = false;
				show_cover_flow(document.getElementById('show_cover_flow_button'));
				}	
			var current_item_owner_id = get_current_item_owner_id();
			}
		break;

		//
		//calculate_text_widths();	
	default:
		break;
	}
}

function show_all_items()
{
var i;
for(i = 0; i < detail_item_objs.length; ++i)
	{
	detail_item_objs[i].style.display = '';
	}
var obj = document.getElementById('item_list');
obj.style.display = '';
}

function init_detail_view_widths()
{
var i;
var value_index;
var value_type;
var sort_type;
init_detail_view_text_display_length = new Array();
detail_view_text_display_length = new Array();
for(i = 0; i < detail_view_format[req_media_id].length; ++i)
	{
	value_index = detail_view_format[req_media_id][i]*1;	
	obj = document.getElementById("resize_"+i);
	obj1 = document.getElementById("resize_"+(i+1));
	if(obj == null || obj1 == null)
		continue;
	x1 = findPosX(obj);
	x2 = findPosX(obj1);
	wide = (x2 - x1);
	detail_view_text_display_length[value_index] = Math.max(0, wide);
	init_detail_view_text_display_length[value_index] = Math.max(0, wide);
	}
}

function adjust_text_widths(item_list_obj, start_index, end_index)
{
//return;
var i;
var item_count = 0;
var x1, x2, wide;
var str;

var obj, obj1;
var value_index;
var value_type;
var sort_type;
var count = 0;
var total_wide = 58; //the size of the Type field
var wide = avail_width - 212;
var wide1;
var wide2;

var length_arr = detail_view_text_display_length;

if(column_width_adjusted == false)
	{
	for(i = 0; i < detail_view_format[req_media_id].length; ++i)
		{
		value_index = detail_view_format[req_media_id][i]*1;	
		wide1 = detail_view_text_display_length[value_index];
		value_type 	= get_value_type_from_column(value_index);
		total_wide += (10 + wide1);
		if(value_type == TEXT_VALUE || value_type == STATIC_TEXT_VALUE)
			{	
			++count;
			}
		}
	
	while(Math.abs(total_wide - wide) > count && count != 0)
		{
		//parseInt((total_wide - wide)/count);
		var diff = (total_wide > wide)?1:-1;
		total_wide = 58;
		count = 0;
		for(i = 0; i < detail_view_format[req_media_id].length; ++i)
			{
			value_index = detail_view_format[req_media_id][i]*1;
			value_type 	= get_value_type_from_column(value_index);
			wide1 = length_arr[value_index];
			wide2 = init_detail_view_text_display_length[value_index];
			if((value_type == TEXT_VALUE || value_type == STATIC_TEXT_VALUE) && (wide1 > (wide2 + diff)))
				{
				++count;
				wide1 -= diff;
				length_arr[value_index] = wide1;
				}
			wide1 += 10;
			total_wide += wide1;
			}		
		}
	if(start_index == end_index && end_index == 0)
		{
		end_index = 1;
		}							
	}
	
	
for(i = 0; i < detail_view_format[req_media_id].length; ++i)
	{
	value_index = detail_view_format[req_media_id][i]*1;
	value_type 	= get_value_type_from_column(value_index);
	sort_type 	= get_sort_type_from_column(value_index);

	for(item_count = start_index; item_count < end_index; ++item_count)
		{		
		if(i == 0)
			{
			var item_owner_id 	= item_result_array[item_count*row_item_count + ITEM_OWNER_ID_INDEX]*1;
			var item_div_id 		= "item_img_"+item_owner_id;
			var img = document.getElementById(item_div_id);
			if(img != null)
				{
				img.style.visibility = 'visible';
				img.style.display = '';
				}
			}		
		if(detail_item_objs[item_count] == null)
			{
			var id = sort_type + "_detail_box_"+item_count;
			obj = document.getElementById(id);						
			//alert('dfd');
			}
		else
			{	
			obj = detail_item_objs[item_count][i];
			}
		if(obj == null)
			continue;
		if(length_arr!= null )
			{	
			wide = length_arr[value_index];
			if(wide != null)
				{
				obj.style.width = wide + 'px';			
				}
			}
		obj.style.visibility = 'visible';
		obj.style.display = '';
		//alert(obj.id);
		}
	}		

if(item_list_obj != null && (column_width_adjusted == false))
	{
	item_list_obj.style.overflow = 'hidden';
	if(start_index == end_index && end_index == 0)
		{
		//total_wide = avail_width-210;		
		item_list_obj.style.width = total_wide + 'px';
		}
	else
		{
		item_list_obj.style.width = total_wide + 'px';
		}
	}

column_width_adjusted = true;
}

function get_covers()
{
query_start_index += query_result_count;
fetch_item_data();	
}


function key_up_event(evnt)
{
shift_key_pressed=false;
var key = get_partial_result_key_code(evnt);
if(key == 27) //Escape key
	{
	hide_all_popup_menu();
	hide_all_menu();
	hide_all_dialogs();
	if(full_screen_cover_flow_mode == true)
		{	
		cover_flow_size();
		}
	restore_item();
	select_bar_abort();
	close_keyboard_box();
	}
}

function update_list(result_array)
	{
	if(result_array == null)
		{
		return;
		}
	var str='';
	fill_query_status();
	
	item_result_array = result_array;//.slice();
	user_view_type = user_view_type*1;
	current_disp_y = -1;
	display_items();
	hide_details_info();
	
	}

function show_cover_flow(img_obj)
{
//var x, y;
//var cover_flow_area_obj = cover_flow_slide.display_obj;
//var cover_flow_bar_obj = cover_flow_slide.obj;
//var wide = get_media_area_width();
//cover_flow_area_obj.style.width = wide + 'px';
//cover_flow_bar_obj.style.width = wide + 'px';
if(user_view_type != DETAIL_VIEW)
	{
	var obj = document.getElementById('permalink');
	if(obj != null)
		{
		var str = obj.href + '&scv=1';
		window.open(str,'_self');
		}
		
	return;
	}

if(cover_flow_slide.end_height != 0)
	{
	disable_image_flow_controls();
	update_cover_flow_status('-1');
	img_obj.src = base_url+'/_images/details/cover_flow_off.gif';
	cover_flow_slide.set_height(0, cover_flow_closing);
	var obj = document.getElementById('show_full_screen_cover_flow_button');
	if(obj != null)
		{
		obj.style.visibility = 'hidden';
		}
	}
else
	{	
	enable_image_flow_controls();
	img_obj.src = base_url+'/_images/details/cover_flow_on.gif';
	//start_cover_flow();
	set_cover_flow_position();
	moveTo(current);
	cover_flow_slide.set_height(cover_flow_height, cover_flow_opening);
	}
}

function get_media_area_width()
{
var obj = document.getElementById('folder_area_left');
var obj1 = document.getElementById('item_list');
if(obj == null || obj1 == null)
	return 0;
return (findPosX(obj) - findPosX(obj1));
}

function trim_str_for_display(str)
{
if(str == null)
	return str;
var sub_str;
if(str.length>30)
	{
	sub_str = str.substr(0, 27)+"...";
	}
else
	{
	sub_str = str;
	}
return sub_str;
}

function list_view(y)
	{
	var item_count = y*display_item_per_row;
	var str = "";
	var x, y;
	var count = query_result_count;
	var user = escape(current_username);
	var sub_title_str;
	var table_width = (image_display_width + image_display_hgap );
	var table_height = (image_display_height + image_display_vgap);
	var item_owner_id;
	var medium;
	var title_str;
	var item_row_id;
	var item_div_id;
	var str1; 
	//tabel_width = 250;
	//table_height = 15;
	//item_per_row = 3;
//count = 10;
	if(item_result_array == null)
		{
		return;
		}


	if(count > 0)
		{
		str = "<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n";
		}

	//for(y = 0; y < count/display_item_per_row; ++y)
		{
		str+= "<tr>\n";
		for(x = 0; x < display_item_per_row; ++x)
			{
			str += "<td style=\"vertical-align: middle; text-align: left;\" width=\""+table_width+"\" height=\""+table_height+"\" align=\"left\" valign=\"middle\">";
			if(item_count < count)
				{
				medium 				= item_result_array[item_count*row_item_count + 0]*1;
				item_owner_id = item_result_array[item_count*row_item_count + 2];
				title_str 		= (item_result_array[item_count*row_item_count + 3]);
				item_row_id 		= "item_row_"+item_owner_id;
				item_div_id 	= "item_img_"+item_owner_id;
				sub_title_str = trim_str_for_display(title_str);
				sub_title_str = string_replace(sub_title_str, " ", "&nbsp;");
				
				switch(medium)
					{
					default:
					case 1:
						img_url = base_url+"/_images/book.gif";
						break;

					case 2:
						img_url = base_url+"/_images/movie.gif";
						break;

					case 3:
						img_url = base_url+"/_images/music.gif";
						break;

					case 4:
						img_url = base_url+"/_images/games.gif";
						break;

					case 5:
						img_url = base_url+"/_images/software.gif";
						break;
					}
					//normal fetch
				str1 = '';
				
				str1+='<table style="vertical-align: middle; text-align: left;" onmouseout="detail_info_mouse_out(this,event);" border="0" cellspacing="0" cellpadding="0" width="'+(table_width)+'px" height="'+(table_height)+'px">\n';
				str1 +='<tr>\n';
				str1 +='<td valign="middle" align="left">';

				str1+='<table id="'+item_row_id+'" style="vertical-align: middle; overflow: hidden; display: block; cursor: hand; cursor: pointer; vertical-align: middle; text-align: left;" onmouseout="detail_info_mouse_out(this,event);" onmousemove="details_info_mouse_move(event,'+item_count+')" border="0" cellspacing="0" cellpadding="0" width1="'+(table_width)+'px" height1="'+(table_height)+'px">\n';
				str1 +='<tr>\n';
				//str1 +='<td valign="middle" align="left">&nbsp;&nbsp;</td>\n';


				str1 +='<td valign="middle" align="left">\n';	
				if(img_url.length != 0)
					{
					str1 += '<img style="margin-left: 10px; margin-right: 4px;" onmousedown="title_mouse_down('+item_owner_id+', event);" id="'+item_div_id+'" height="'+image_display_height+'" border="0" src="' + img_url + '">\n';
					}
				str1 += '</td>\n';
				//str1 +='<td valign="middle" align="left">&nbsp;</td>\n';
				str1 += "<td style=\"font-family:arial; font-size: 11pt; text-decoration: none;\" valign=\"middle\" align=\"left\" width1=\"100%\" onmouseover=\"this.style.textDecoration='underline'\" onmouseout=\"this.style.textDecoration='none'\" onmousedown=\"title_mouse_down("+item_owner_id+", event)\">\n";
				str1 += sub_title_str;
				str1 += '\n</td>\n';
				str1 += '</tr>\n';
				str1 += '</table>\n';
				
				str1+='</td>';
				str1+='</tr>';
				str1+='</table>';
				//str += "rana";
				//str1 += "</span>\n";
				str += str1;
				//str += "rana";
				++item_count;
				}
			else
				{
				item_div_id = "";
				sub_title_str = '';
				img_url = '';
				}	
			str += "</td>";
			}
		str += "</tr>\n";
		}
	if(count > 0)
		{
		str += "</table>";
		}
	return str;
	}

function fill_detail_view_entry(start_index, count)
{
if(item_result_array == null)
	{
	return -1;
	}
var item_count;
var i;
var end_count = Math.min(start_index + count, query_result_count)*1; 
var index = -1;
if(start_index == 0)
	{
	detail_item_objs = new Array();
	}
for(item_count = start_index; item_count < end_count; ++item_count, ++index)
	{
	detail_item_objs[item_count] = new Array();
	for(i = 0; i < detail_view_format[req_media_id].length; ++i)
		{
		var str = '';
		var item_owner_id 	= item_result_array[item_count*row_item_count + ITEM_OWNER_ID_INDEX]*1;
		var item_div_id 		= "item_img_"+item_owner_id;
		var value_index = detail_view_format[req_media_id][i]*1;
		//alert(value_index);
		var value_type 	= get_value_type_from_column(value_index);
		var sort_type 	= get_sort_type_from_column(value_index);
		var id = sort_type + "_detail_box_"+item_count;
		var obj = document.getElementById(id);			
		detail_item_objs[item_count][i] = obj;
		if(obj == null)
			continue;
		obj.style.display = 'none';
		var value = '';
		if(value_type == STATIC_TEXT_VALUE || value_type == TEXT_VALUE || value_type == SHELF_VALUE)
			{
			value = (item_result_array[item_count*row_item_count + value_index]);
			//item_result_array[item_count*row_item_count + value_index] = "RANA";
			//alert(value);
			}
		else
			{ 
			value = pre_process_detail_value(item_count, item_result_array[item_count*row_item_count + value_index], value_index);
			}
		switch(value_type)
			{
			case SHELF_VALUE:
				obj.title = (value);
				str = create_shelf_value_entry(obj, item_count, item_owner_id, value_index);
				break;
				
			case STATIC_TEXT_VALUE:
			case TEXT_VALUE:
				obj.title = (value);
				str = create_text_value_entry(id, item_count, value, value_index, 'save_information', (value_type!=TEXT_VALUE));
				break;

			case DATE_VALUE:
				str = create_date_value_entry(value, value_index);
				break;

			case RATING_VALUE:
				str = create_rating_value_entry('owner_rating_t_'+item_count, value, item_count, 'save_user_setting', value_index);
				break;

			case IMG_VALUE:
				str = create_img_value_entry(obj, value, 0, 0, value_index);
				break;
			
			case SMALL_IMG_VALUE:
				str = create_img_value_entry(obj, value, 60, 80, value_index);
				break;
				
			case MEDIUM_IMG_VALUE:
				str = create_img_value_entry(obj, value, 120, 160, value_index);
				break;

			case LARGE_IMG_VALUE:
				str = create_img_value_entry(obj, value, 240, 300, value_index);
				break;

			case HTML_VALUE:
				str = create_html_value_entry(value, value_index);
				break;
			}
		obj.innerHTML = str;
		}
	}
return index;
}

function start_column_resize(i, evnt)
{
set_silkscreen_opacity(0);
disable_screen(true);
var obj1 = document.getElementById('resize_'+i);
var obj = document.getElementById('column_resize_bar');
if(obj == null||obj1 == null)
	return false;

get_xy(evnt);
obj.style.left = elem_x + 'px';
obj.style.top  = (findPosY(obj1)) + 'px';

obj.style.visibility  = 'visible';
obj.style.display  = '';
obj.style.height = get_shelf_area_height() + 'px';

obj = document.getElementById('silkscreen');
if(obj == null)
	return false;

obj.style.cursor='e-resize';
resized_column_index = i-1;
resized_column_value_index = 	detail_view_format[req_media_id][resized_column_index]*1;
resized_column_obj = document.getElementById('resize_'+resized_column_index);
var wide = init_detail_view_text_display_length[resized_column_value_index];
resized_column_x = findPosX(resized_column_obj) + wide;
//resized_column_x_end = findPosX(document.getElementById('resize_'+detail_view_format[req_media_id].length));

return false;
}

function resize_column(obj, evnt)
{
var obj = document.getElementById('column_resize_bar');
if(obj == null)
	return;
//var obj1 = document.getElementById('item_list');
//if(obj1 == null)
//	return;

get_xy(evnt);
var x = elem_x;
//var y = findPosY(obj1);

if(x >= resized_column_x )
	{
	obj.style.left = x + 'px';
	}
//obj.style.top  = y + 'px';

return false;
}

function silkscreen_mouse_down(obj, evnt)
{
close_keyboard_box();
}

function stop_column_resize(obj, evnt)
{
//silkscreen_mouse_up(obj, evnt);
var obj = document.getElementById("resize_"+resized_column_index);
var obj1 = document.getElementById("column_resize_bar");
if(obj == null || obj1 == null)
	return true;
var x1 = findPosX(obj);
var x2 = parseInt(obj1.style.left);
var wide = Math.abs(x2 - x1);
wide = Math.max(init_detail_view_text_display_length[resized_column_value_index], (wide-4));
detail_view_text_display_length[resized_column_value_index] = wide;
disable_screen(false);
var obj = document.getElementById('column_resize_bar');
if(obj == null)
	return false;
obj.style.visibility  = 'hidden';
obj.style.display  = 'none';

obj = document.getElementById('silkscreen');
if(obj == null)
	return false;

obj.style.cursor='';

set_resized_columns(resized_column_index);
resized_column_index = -1;

return false;
}

function set_resized_columns(column_index)
{
//var i;
var value_index;
var value_type;
var sort_type;
var item_count;
var obj; 
var wide;
if(detail_view_format[req_media_id][column_index] == null)
	return;
value_index = detail_view_format[req_media_id][column_index]*1;
value_type 	= get_value_type_from_column(value_index);
sort_type 	= get_sort_type_from_column(value_index);
wide = 	detail_view_text_display_length[value_index];
for(item_count = 0; item_count < query_result_count; ++item_count)
	{		
	if(detail_item_objs[item_count] == null)
		{
		continue;
		}	
	obj = detail_item_objs[item_count][column_index];
	if(obj != null)
		{
		obj.style.width = wide + 'px';			
		if(obj.editting != null)
			{
			if(obj.editting == true)
				{
				set_edit_this_text(obj.id, item_count, value_index, wide);
				}
			}
		}
	}
}

function get_img_icon(item_count)
{
var detail_url = (item_result_array[item_count*row_item_count + 25]);
var index = detail_url.indexOf("/_user_manual_file/");
var	img_url = base_url+"/_images/book.gif";

if(index != -1)
	{
	detail_url = detail_url.substr(index).split('.');
	detail_url = detail_url[detail_url.length-1].toLowerCase();
	if(detail_url == 'pdf' || detail_url == 'ps' || detail_url == 'ppt' || detail_url == 'xls' || detail_url == 'doc')
		{
		img_url = base_url + "/_images/new/"+detail_url+".gif";
		}
	//alert(img_url);
	}
return img_url;
}

function detail_view()
	{
	var id;
	var item_owner_id;
	var item_count = 0;
	var str = "";
	var x, y;
	var width, height;
	var count = query_result_count;
	var user = escape(current_username);
	var medium;
	var item_per_row = 3;
	var table_width = "100%";
	var cell_style, cell_style1;
	var i;
	var img_url;
	var cell_color;
	var title_str;
	var shelf_id;

	if(item_result_array == null)
		{
		return;
		}

	width = 90;
	height = 15;

	if(count > 0)
		{
		show_print_button();
		show_export_button();
		}
	else
		{
		hide_print_button();
		hide_export_button();
		}	
	str = "<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n";
	cell_sytle = "cursor: pointer; cursor: hand; background-color: #88AACC; border-width: 0px 0px 1px 0px; border-style: solid; border-color: black; font-family: arial; font-size: 10pt; color: black;";
	cell_sytle1 = "cursor: pointer; cursor: hand; background-color: #88AACC; border: 0px; font-family: arial; font-size: 10pt;color: black;";

	if(req_media_id == 0)
		{
		str+= "<td title=\"Click here to sort by medium\" style=\""+cell_sytle+"\"  align=\"left\" height=\""+height+"\" valign=\"middle\">\n";
		str += "<table onmousedown=\"sort_list('medium');\" width=\"48\" height=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n";
		str += "<tr>\n";
		str += "<td style=\""+cell_sytle1+"\" >Type</td>";
		str += "<td width=\"8\"><div value=\"3\" id=\"medium_cell\"></div></td>";
		str += "<td>&nbsp;</td>";
		str += "</tr></table>\n";
		str += "</td>\n";
		}
	else
		{
		str+= "<td style=\""+cell_sytle+"\" align=\"center\" width=\"48\" height=\""+height+"\" valign=\"middle\">\n";
		str += "Type";
		str += "</td>\n";
		}
	var wide;	
	for(i = 0; i < detail_view_format[req_media_id].length; ++i)
		{
		var value_index = detail_view_format[req_media_id][i]*1;
		var sort_type 	= get_sort_type_from_column(value_index);
		var header_title = get_disp_name_from_column(value_index)

		var sort_help_msg = header_title
		if(sort_type.length != 0)
			{
			sort_help_msg = "Click here to sort by "+header_title;
			}

		str += '<td width="10" onmousemove="return resize_column(this, event);" ';
		if(i != 0)
			{
			str += 'onmousedown="return start_column_resize('+i+', event);"';
			}
		str +='id="resize_'+i+'" height=\""+height+"\" style="background-color: #88AACC; border-width: 0px 0px 1px 1px; border-style: solid; border-color: #000000; font-size: 10pt; ';
		if(i!= 0)
			{
			str += 'cursor: e-resize;';
			}
		str += ' ">&nbsp;&nbsp;</td>';
		
		str+= '<td id="'+sort_type+'_column_header" title="'+sort_help_msg+'" onmousedown="return check_for_right_click(this, event, -'+i+',\''+sort_type+'\');" onmouseup="return check_for_right_click_abort(null, null);" style="'+cell_sytle+'" align="left"  height="'+height+'" valign="middle">\n';
		str += "<table width=\"100%\" height=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"2\">\n";
		str += "<tr>\n";
		str += "<td style=\""+cell_sytle1+"\"";
		if(value_index == SMALL_IMG_INDEX)
			{
			wide = 60;
			}
		else if(value_index == MEDIUM_IMG_INDEX)
			{
			wide = 120;
			}
		else if(value_index == LARGE_IMG_INDEX)
			{
			wide = 240;
			}
		else
			{
			//wide = 1;
			wide = "100%";
			}
		str += " width=\""+wide+"\" >";
		str += header_title;
		str += "</td>";
		//str += "<td width=\"100%\">";
		//str +="&nbsp;";
		//str += "</td>";
		str += "<td align=\"right\" width=\"8\"><div style=\"text-align: right; vertical-align: middle;\" value=\"3\" id=\""+sort_type+"_cell\"></div></td>";
		//str += "<td >&nbsp;</td>";
		str += "</tr></table>\n";
		str += "</td>\n";
		}
	//str +='<td id="resize_'+i+'" height=\""+height+"\" style="border-width: 0px 1px 1px 0px; border-color: black; border-style:solid; background-color: #88AACC; font-size: 1pt; width: 1px; height: 100%" width="1">&nbsp;</td>';
	
	str += '<td width="10" onmousemove="return resize_column(this, event);" ';
	str += 'onmousedown="return start_column_resize('+i+', event);"';
	str +='id="resize_'+i+'" height=\""+height+"\" style="background-color: #88AACC; border-width: 0px 1px 1px 0px; border-style:solid; border-color: #000000; font-size: 10pt; ';
	str += 'cursor: e-resize;';
	str += ' ">&nbsp;</td>';

	height = "100%";
	init_cover_flow_variables();
	refresh_cover_flow();
	for(y = 0; y < count; ++y)
		{
		var str1 = '';
		medium 					= item_result_array[item_count*row_item_count + MEDIUM_INDEX]*1;
		item_owner_id 	= item_result_array[item_count*row_item_count + ITEM_OWNER_ID_INDEX]*1;
		//alert(item_owner_id);
		title_str				= (item_result_array[item_count*row_item_count + TITLE_INDEX]);
		img_url 				= trim(item_result_array[item_count*row_item_count + LARGE_IMG_INDEX]);
		var medium_img_url 	= trim(item_result_array[item_count*row_item_count + MEDIUM_IMG_INDEX]);
		if(img_url.length == 0)
			{
			if(medium_img_url.length == 0)
				{
				img_url = '';
				}
			else
				{
				img_url = medium_img_url;
				}	
			}
		else
			{					
			img_url = base_url + "/_scripts/image_flow/get_images.php?action=show_image&image="+escape(img_url);
			}
		create_cover_image(item_owner_id , title_str, img_url);		
		switch(medium)
			{
			default:
			case 1:
				//img_url = base_url+"/_images/book.gif";
				img_url = get_img_icon(item_count);
				break;

			case 2:
				img_url = base_url+"/_images/movie.gif";
				break;

			case 3:
				img_url = base_url+"/_images/music.gif";
				break;

			case 4:
				img_url = base_url+"/_images/games.gif";
				break;

			case 5:
				img_url = base_url+"/_images/software.gif";
				break;
			}


		var item_row_id 		= "item_row_"+item_owner_id;
		var item_div_id 		= "item_img_"+item_owner_id;
		cell_color = (y % 2)?("#FFFFCC"):("white");
		var cur_font_color = (y % 2)?("#000033"):("black");

		str1+= "<tr ";
		str1 += "style=\"color: "+cur_font_color+"; background-color: "+cell_color+"; text-decoration: none; font-family:arial; font-size: 10pt;\" ";
		str1+= "id=\""+item_row_id+"\" >\n";

		var item_img_id = "detail_info_link_"+item_count;
		//image
		str1 += "<td ";
		str1 += "  title=\"Click here to open detailed information about '"+title_str+"'\"  align=\"center\" height=\""+height+"\" valign=\"middle\">\n";
		str1 += '<a id="'+item_img_id+'" style="text-decoration: none;" href="'+base_url+'/title/'+item_owner_id+'">\n';
		str1 += "<img onmousedown=\"title_mouse_down("+item_owner_id+", event);\" style=\"visibility: hidden;\" id=\""+item_div_id+"\" border=\"0\" height=\""+25+"\" src=\"" + img_url + "\">\n";
		str1 += '</a>\n';
		str1 += "</td>\n";

		for(i = 0; i < detail_view_format[req_media_id].length; ++i)
			{
			var str2 = '';
			var value_index = detail_view_format[req_media_id][i]*1;
			var value_type 	= get_value_type_from_column(value_index);
			var sort_type 	= get_sort_type_from_column(value_index);
			var id = sort_type + "_detail_box_"+item_count;
			str2 +='<td width="1" ';
			if(i != 0)
				{
				str2 += 'onmousedown="return start_column_resize('+i+', event);"';
				}
			str2 +=' height=\""+height+"\" style="';
			if(i != 0)
				{
				str2 += 'cursor: e-resize;';
				}
			str2 += ' border-left: 1px dashed #BBBBBB; width: 1px; font-size: 1pt;">&nbsp;</td>';
			str2 += "<td ";
			if(owner == 0)
				{
			str2 += "onmousedown=\"title_mouse_down("+item_owner_id+", event);\" style=\"cursor: pointer; cursor: hand; color: "+cur_font_color+"; background-color: "+cell_color+"; text-decoration: none; font-family:arial; font-size: 10pt;\" ";
				}
			switch(value_type)
				{
				case SHELF_VALUE:
					str2 += "align=\"left\" height=\""+height+"\" valign=\"middle\">\n";
					str2 += "<div ";
					str2 += " id=\""+id+"\" style=\"color: black; visibility: hidden; ";
					str2 += " vertical-align: middle; text-align: left;  height: 30px; width: 1px; overflow: hidden;\">\n</div></td>";					
					break;
					
				case STATIC_TEXT_VALUE:
				case TEXT_VALUE:
					str2 += "align=\"left\" height=\""+height+"\" valign=\"middle\">\n";
					str2 += "<div id=\""+id+"\" style=\"color: black; visibility: hidden; vertical-align: middle; text-align: left;  height: 30px; width: 1px; overflow: hidden;\">\n</div></td>";
					break;

				case DATE_VALUE:
					str2 += "align=\"center\" height=\""+height+"\" valign=\"middle\"><div id=\""+id+"\" style=\"color: black; visibility: hidden; width: 1px; overflow: hidden; vertical-align: middle; text-align: left;  height: 30px;\"></div></td>\n";
					break;

				case RATING_VALUE:
					str2 += "align=\"center\" height=\""+height+"\" valign=\"middle\"><div id=\""+id+"\" style=\"visibility: hidden; width: 1px; overflow: hidden; vertical-align: middle; text-align: left;  height: 30px;\"></div></td>\n";
					break;

				case SMALL_IMG_VALUE:
				case MEDIUM_IMG_VALUE:
				case LARGE_IMG_VALUE:
				case IMG_VALUE:
					if(value_index == SMALL_IMG_INDEX)
						{
						wide = 60;
						}
					else if(value_index == MEDIUM_IMG_INDEX)
						{
						wide = 120;
						}
					else if(value_index == LARGE_IMG_INDEX)
						{
						wide = 240;
						}
					else	
						{
						wide = 50;
						}
					str2 += "align=\"center\" valign=\"middle\">";
					//str2 += "<td align=\"center\" valign=\"middle\">";
					str2 += "<div id=\""+id+"\" style=\"width: "+wide+"px; overflow: hidden; visibility: hidden; vertical-align: middle; text-align: center; \"></div>";
					str2 += "</td>\n";
					break;
									
				case HTML_VALUE:
					str2 += "align=\"center\" height=\""+height+"\" valign=\"middle\"><div id=\""+id+"\" style=\"color: black; width: 1px; overflow: hidden; vertical-align: middle; text-align: left;  height: 30px;\"></div></td>\n";
					break;
				}
			str1 += str2;
			}

		//str1 +='<td height="100%" width="1" style="width: 1px; font-size: 1pt;">&nbsp;</td>';
		str1 += '<td height="100%" width="1" onmousemove="return resize_column(this, event);" ';
		str1 += 'onmousedown="return start_column_resize('+i+', event);"';
		str1 +='style="border-width: 0px 1px 0px 0px; border-style:dashed; border-color: #BBBBBB; font-size: 1pt; ';
		str1 += 'cursor: e-resize;';
		str1 += ' ">&nbsp;</td>';
		++item_count;

		str1 += "</tr>\n";
		str += str1;
		}

	var str1 = '';
	str1 +="<tr>";
	str1 += '<td id="last_row_entry" height="1" style="font-size:1pt; height: 1px">&nbsp;</td>';

	for(i = 0; i < detail_view_format[req_media_id].length; ++i)
		{
		var value_index = detail_view_format[req_media_id][i]*1;
		var value_type 	= get_value_type_from_column(value_index);
		var sort_type 	= get_sort_type_from_column(value_index);
		var id = sort_type + "_detail_box_"+item_count;
		str1 += '<td height="1" width="1" style="width: 1px; font-size:1pt; height: 1px">&nbsp;</td>';
		str1 += '<td height="1" style="font-size:1pt; height: 1px">';
		str1 += "<div id=\""+id+"\" style=\"visibility: hidden; vertical-align: middle; text-align: left;  height: 1px; width: 1px;overflow: hidden;\"></div></td>";					
		}

	str1 +='<td height="100%" width="1" style="width: 1px; font-size: 10pt;">&nbsp;</td>';

	str1 +="</tr>";		
	str1 += "</table>";
	str += str1;
	return str;
	}

function create_shelf_value_entry(obj, item_count, item_owner_id, column_index)
{
var disp_str = (item_result_array[item_count*row_item_count + column_index]);
disp_str = modify_array(disp_str);
var str = '';
if(owner)
	{
	str += '<table style="font-family: arial; font-size: 10pt; font-weight: normal;" border="0" cellspacing="0" cellpadding="0">';
	str +=  '<tr>\n';
	str +=  '<td style="color: black; border-width: 0px 0px 1px 0px; border-style: dotted; border-color: black; cursor: pointer; cursor: hand;" onmouseup="show_shelves(this, '+item_owner_id+');"  height="100%" 	align="left" valign="middle">\n';
	}
str += disp_str;
if(owner)
	{
	str +=  '</td>\n';
	str +=  '</tr>\n';
	str +=  '</table>\n';
	}
set_column_width(str, column_index)
return str;
}

function create_text_value_entry(id, item_count, value, value_index, save_func, static_val)
{
var str = '';
//var height = 30;
//var str = "<td  id=\""+id+"\" title=\""+value+"\" align=\"left\" height=\""+height+"\" valign=\"middle\">\n";
str += get_edit_this_text(id, item_count, value_index, 'save_information', static_val);
//str += "</td>\n";
return str;
}

function create_date_value_entry(value, column_index)
{
var str = '';
str += value;
set_column_width(value, column_index);
//str += "</td>\n";
return str;
}

function create_rating_value_entry(id, value, item_count, save_func, column_index)
{
var str = '';
//var height = 30;
//var str = "<td  align=\"center\" height=\""+height+"\" valign=\"middle\">\n";
//str += user_rating;
str += set_user_rating(id, owner, value, item_count, save_func)
set_column_width(str, column_index);
//str += "</td>\n";
return str;
}

function create_img_value_entry(obj, value, wide, high, column_index)
{
//var height = 30;
//var str = "<td align=\"center\" height=\""+height+"\" valign=\"middle\">\n";
var str = '';
if(wide != 0)
	{
	str += "<img onload=\"resize_image(this, "+wide+")\" style=\"border: 1px solid gray; visibility: hidden; margin-top: 4px; margin-bottom: 4px; text-align: center; vertical-align: middle;\" border=\"0\" src=\"" + value + "\">\n";
	}
else
	{
	str += "<img style=\"text-align: center; vertical-align: middle;\" border=\"0\" src=\"" + value + "\">\n";
	}	
//str += "</td>\n";
return str;
}

function create_html_value_entry(value, column_index)
{
var str = '';
//var height = 30;
//var str = "<td  align=\"left\" height=\""+height+"\" valign=\"middle\">\n";
str += value;
set_column_width(str, column_index);
//str += "</td>\n";
return str;
}

function title_mouse_down(item_owner_id, obj_event)
{
hide_all_menu();
hide_all_popup_menu();
//alert('mouse_down');
//obj, item_div_id, item_count
//var item_owner_id = item_result_array[item_count*row_item_count + 2]*1;
//var detail_link = "./title/"+item_owner_id;
var item_row_id = get_item_row_id(item_owner_id);
var item_div_id = get_item_div_id(item_owner_id);
var obj = document.getElementById(item_row_id);
start_dragging(obj, item_div_id, item_owner_id, obj_event);
return check_for_right_click(this, obj_event, item_owner_id, item_row_id);

}

function resize_image(img_obj, size)
{
if(img_obj.width > size)
	{
	img_obj.width = size;
	}
else if(img_obj.height > size)
	{
	img_obj.height = size;
	}
img_obj.style.visibility = 'visible';
if(current_interval_timer != null)
	{
	clearTimeout(current_interval_timer);
	current_interval_timer = null;
	}
current_interval_timer = setTimeout('detail_image_resized()', 2000);
}

function detail_image_resized()
{
current_interval_timer = null;
set_shelf_area_height(null);
}

function modify_array(str)
{
//str = string_replace(str, '&', '&amp;');
//alert(str);
var i;
var arr = str.split(",");
str = '';
var search_data;
var arr1;
for(i = 0; i < arr.length; ++i)
	{
	search_data = trim(arr[i]);
	if(search_data.length == 0)
		{
		continue;
		}
	str += trim(arr[i]);
	
	if(i != arr.length-1)
		{
		str += ",&nbsp;";
		}
	} 
str = string_replace(str, '-', '&nbsp;');
str = string_replace(str, '_', '&nbsp;');
str = string_replace(str, ' ', '&nbsp;');
//str = string_replace(str, '¢', '&cent;');
str = string_replace(str, '©', '&copy;');
str = string_replace(str, '®', '&reg;');
//str = string_replace(str, '±', '&plusmn;');
str = string_replace(str, '"', '&quot;');
return str;
}

function pre_process_detail_value(row, value, column_index)
{
if(value == null)
	return '';
switch(column_index)
	{
	case SHELF_INDEX:
	case DIRECTOR_INDEX:
	case GENRE_INDEX:
	case TITLE_INDEX:
	case AUTHOR_INDEX:
	case PUBLISHER_INDEX:
		value = modify_array(value);
		break;
		
	case NEW_PRICE_INDEX:
	case BEST_PRICE_INDEX:
		if(value != 0)
			{
			var val1 = parseInt(value/100);
			var val2 = parseInt(value%100);
			var val3 = '';
			if(val2 < 10)
				{
				val3 = '0';
				}
			val3 += val2.toString();
			value = '$'+val1.toString() + '.'+val3;
			}
		else
			{
			value = 'N.A';
			}	
		break;
		
	case BORROWER_NAME_INDEX:
		var borrower_id = item_result_array[row*row_item_count + BORROWER_ID_INDEX]*1;
		var email 			= trim(item_result_array[row*row_item_count + BORROWER_LINK_INDEX]);
		if(borrower_id != 0)
			{
			value = "<a href=\""+base_url+"/"+escape(value)+"\" target=\"_blank\">"+value+"</a>";
			}
		else
			{
			value = string_replace(value, " ", "&nbsp;");
			if(email.length != 0 && owner)
				{
				value = "<a href=\"mailto:"+email+"\">"+value+"</a>";
				}
			}
		break;

	case SYNOPSIS_INDEX:
	case MY_REVIEW_INDEX:
		//value = string_replace(value, " ", "&nbsp;");
		break;
		
	case LENDER_NAME_INDEX:
		var email 			= trim(item_result_array[row*row_item_count + BORROWER_LINK_INDEX]);
		var borrow_item_owner_id = item_result_array[row*row_item_count + BORROW_ITEM_OWNER_ID_INDEX]*1;
		if(borrow_item_owner_id == 0)
			{
			if(email.length != 0 && owner)
				{
				value = "<a href=\"mailto:"+email+"\">"+value+"</a>";
				}
			}
		else
			{
			value = "<a href=\""+base_url+"/"+escape(value)+"\" target=\"_blank\">"+value+"</a>";
			}		
		break;

	case PLATFORM_INDEX:
		value = string_replace(value, " ", "&nbsp;");
		break;

	case MEDIUM_IMG_INDEX:
	case SMALL_IMG_INDEX:
	case LARGE_IMG_INDEX:
		if(value.length == 0)
			value = base_url+"/_images/blank.gif";
		break;

	case MPAA_INDEX:
		if(value.indexOf("esrb-rating.gif") > 0 || value.indexOf("/.gif") > 0 || value.length == 0)
			{
			value = "http://g-images.amazon.com/images/G/01/detail/nr.gif";
			}
		break;

	case BORROW_STATUS_INDEX:
		value *= 1;
		if(value == 1)
			{
			value = "<span style=\"font-weight: bold; border: 1px outset white; background-color: yellow; width: 60; text-align: center; vertical-align: middle;\">Out</span>";
			}
		else if(value == 3)
			{
			value = "<span style=\"font-weight: bold; border: 1px outset white; background-color: green; width: 60; text-align: center; vertical-align: middle;\">Loan</span>";
			}
		else if(value == 2 || value == 6)
			{
			value = "<span style=\"font-weight: bold; border: 1px outset white; background-color: red; width: 60; text-align: center; vertical-align: middle;\">Late!</span>";
			}
		else
			{
			value = "&nbsp;";
			}
		value = "<span style=\"width: 100%; height: 100%; text-align: center; vertical-align: middle;\">"+value+"</span>";
		break;
	}
return value;
}

function get_table_from_column(column_index)
{
switch(column_index)
	{
	case MEDIUM_INDEX:
		return 'medium';

	case TITLE_INDEX:
		return 'title';

	case PUBLISHER_INDEX:
		return 'publisher';

	case RATING_INDEX:
		return 'rating';

	case REL_DATE_INDEX:
		return 'release_date';

	case ADD_DATE_INDEX:
		return 'added_date';

	case MPAA_INDEX:
		return 'mpaa_rating_url';

	case AUTHOR_INDEX:
		return 'author';

	case DIRECTOR_INDEX:
		return 'director';

	case GENRE_INDEX:
		return 'genre';

	case PLATFORM_INDEX:
		return 'platform';

	}
}

function get_sort_type_from_column(column_index)
{
switch(column_index)
	{
	default:
		return '';

	case TITLE_INDEX:
		return 'title';

	case PUBLISHER_INDEX:
		return 'publisher';

	case RATING_INDEX:
		return 'rating';

	case REL_DATE_INDEX:
		return 'release_date';

	case ADD_DATE_INDEX:
		return 'added_date';

	case MPAA_INDEX:
		return 'mpaa';

	case AUTHOR_INDEX:
		return 'author';

	case DIRECTOR_INDEX:
		return 'director';

	case GENRE_INDEX:
		return 'genre';

	case USER_RATING_INDEX:
		return 'rating';

	case BEST_PRICE_INDEX:
		return 'best_price';

	case NEW_PRICE_INDEX:
		return 'new_price';

	case BORROW_STATUS_INDEX:
		return 'borrow_status';

	case PLATFORM_INDEX:
		return 'platform';

	case SHELF_INDEX:
		return 'shelf';

	case ITEM_CODE_INDEX:
		return 'item_code';

	case BORROWER_NAME_INDEX:
		return "borrower_name";

	case BORROW_RET_DATE_INDEX:
		return "borrow_return_date";

	case REVIEW_DATE_INDEX:
		return "user_review_update_date";

	case LENDER_NAME_INDEX:
		return 'borrower_name';
	
	case SYNOPSIS_INDEX:
		return 'synopsis';
	
	case MY_REVIEW_INDEX:
		return "user_review";
	}
}

function get_value_type_from_column(column_index)
{
	switch(column_index)
		{
		case MEDIUM_IMG_INDEX:			return MEDIUM_IMG_VALUE;
		case TITLE_INDEX:						return TEXT_VALUE;
		case PUBLISHER_INDEX:				return TEXT_VALUE;
		case RATING_INDEX:					return RATING_VALUE;
		case REL_DATE_INDEX:				return DATE_VALUE;
		case ADD_DATE_INDEX:				return DATE_VALUE;
		case MPAA_INDEX:						return IMG_VALUE;
		case AUTHOR_INDEX:					return TEXT_VALUE;
		case DIRECTOR_INDEX:				return TEXT_VALUE;
		case GENRE_INDEX:						return TEXT_VALUE;
		case USER_RATING_INDEX:			return RATING_VALUE;
		case BEST_PRICE_INDEX:			return STATIC_TEXT_VALUE;
		case NEW_PRICE_INDEX:				return STATIC_TEXT_VALUE;
		case BORROW_STATUS_INDEX:		return HTML_VALUE;
		case PLATFORM_INDEX:				return TEXT_VALUE;
		case MY_REVIEW_INDEX:				return STATIC_TEXT_VALUE;
		case SYNOPSIS_INDEX:				return STATIC_TEXT_VALUE;
		case SHELF_INDEX:						return SHELF_VALUE;
		case ITEM_CODE_INDEX:				return STATIC_TEXT_VALUE;
		case SMALL_IMG_INDEX:				return SMALL_IMG_VALUE;
		case LARGE_IMG_INDEX:				return LARGE_IMG_VALUE;
		case BORROWER_NAME_INDEX:		return HTML_VALUE;
		case LENDER_NAME_INDEX:			return HTML_VALUE;
		case BORROW_RET_DATE_INDEX:	return DATE_VALUE;
		case REVIEW_DATE_INDEX:			return DATE_VALUE;
		}
}

function get_disp_name_from_column(column_index)
{
switch(column_index)
	{
	case TITLE_INDEX:
		return 'Title';

	case PUBLISHER_INDEX:
		switch(req_media_id)
			{
			default:
			case ALL_MEDIA: return "Produced";
			case BOOKS:			return "Publisher";
			case MOVIES:		return "Studio";
			case MUSIC:			return "Studio";
			case GAMES:			return "Developer";
			case SOFTWARE:	return "Developer";
			}

	case RATING_INDEX:
		return 'Rating';

	case REL_DATE_INDEX:
		if(req_media_id == BOOKS)
			return 'Pub.&nbsp;Date';
		else
			return 'Rel.&nbsp;Date';

	case ADD_DATE_INDEX:
		return 'Add&nbsp;Date';

	case MPAA_INDEX:
		if(req_media_id == MOVIES)
			return 'MPAA';
		else if(req_media_id == GAMES)
			return 'ESRB';
		else
			return "";

	case AUTHOR_INDEX:
		switch(req_media_id)
			{
			default:
			case ALL_MEDIA: return "Creator";
			case BOOKS:			return "Authors";
			case MOVIES:		return "Actors";
			case MUSIC:			return "Artists";
			}

	case DIRECTOR_INDEX:
		return 'Directors';

	case GENRE_INDEX:
		return 'Genre';

	case USER_RATING_INDEX:
		return 'Ratings&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';

	case BEST_PRICE_INDEX:
		return 'Used&nbsp;Price';

	case NEW_PRICE_INDEX:
		return 'New&nbsp;Price';

	case BORROW_STATUS_INDEX:
		return 'Borrow&nbsp;Status';

	case PLATFORM_INDEX:
		return 'Platform';

	case SHELF_INDEX:
		return 'Shelf';

	case ITEM_CODE_INDEX:
		switch(req_media_id)
			{
			case BOOKS:	return "ISBN";
			default:
				return "UPC";
			}

	case MEDIUM_IMG_INDEX:			return "Medium&nbsp;Cover";
	case LARGE_IMG_INDEX:				return "Large&nbsp;Cover";
	case SMALL_IMG_INDEX:				return "Small&nbsp;Cover";
	case BORROWER_NAME_INDEX:		return "Borrower";
	case MY_REVIEW_INDEX:				return "My&nbsp;Review";
	case SYNOPSIS_INDEX:				return "Synopsis";
	case LENDER_NAME_INDEX:			return "Lender";
	case BORROW_RET_DATE_INDEX:	return "Ret.&nbsp;Date";
	case REVIEW_DATE_INDEX:			return "Rev.&nbsp;Date";
	}
}

function save_partial_information(item_owner_id, item_count, column_index, str)
{
var type = get_table_from_column(column_index);
var args = 						{	
											'action'					: 		'update_partial_information', 
											'item_owner_id'		:			item_owner_id,
											'index'						: 		item_count,
											'type'						:			type,
											'data'						:			str,
											'column_index'		:			column_index
											};
ajax_post_request(ajax_request_url, args, update_partial_information_response);
return true;
}

function update_partial_information_response(response_array)
{
if(response_array[0] == "error")
	{
	show_alert_dialog("Error", response_array[1]);
	}
else
	{
	var item_count = response_array.shift()*1;
	var item_owner_id = response_array.shift()*1;
	//var column_index = response_array.shift()*1;
	item_result_array[item_count*row_item_count + 2] = item_owner_id;
	var item_img_id = "detail_info_link_"+item_count;
	var elem = document.getElementById(item_img_id);
	if(elem != null)
		{
		elem.href = "./title/"+item_owner_id;
		}
	}
}

function save_information(id_str, item_count, column_index)
{
if(item_result_array == null)
	{
	return;
	}
	
var id_str1 = id_str+'_text';
var elem = document.getElementById(id_str1);
if(elem == null)
	return;
var str = trim(elem.value);
var str1 = item_result_array[item_count*row_item_count + column_index];
if(str != str1)
	{
	str = (str);
	item_result_array[item_count*row_item_count + column_index] = (str);
	var item_owner_id = item_result_array[item_count*row_item_count + 2]*1;
	save_partial_information(item_owner_id, item_count, column_index, str);
	}

close_edit_this_text(prev_id_str,prev_item_count,prev_column_index, prev_save_func);
}

function set_edit_this_text(id_str, item_count, column_index, input_width)
{
if(item_result_array == null)
	{
	return;
	}
	
var id_str1 = id_str+'_text';
var id_str2 = id_str+'_close_img';
var elem = document.getElementById(id_str1);
if(elem == null)
	return;
var edit_str = item_result_array[item_count*row_item_count + column_index];
elem.value = (edit_str);
if(input_width == null)
	{
	var elem1 = document.getElementById(id_str2);
	var elem2 = document.getElementById(id_str);
	var width = findPosX(elem1);
	width -= findPosX(elem2);
	}
else
	{
	width = input_width - 16;
	}

elem.style.width = width - 4;
elem.select();
elem.focus();
}

function close_all_edit_this_text()
{
if(prev_id_str != null)
	{
	close_edit_this_text(prev_id_str,prev_item_count,prev_column_index, prev_save_func);
	}
prev_id_str = null;
}
function edit_this_text(id_str, item_count, column_index, save_func)
{
hide_all_menu();
hide_all_popup_menu();
var item_owner_id = item_result_array[item_count*row_item_count + 2];
select_item(true, item_owner_id);
prev_id_str = id_str;
prev_item_count = item_count;
prev_column_index = column_index;
prev_save_func = save_func;
var elem = document.getElementById(id_str);
if(elem == null)
	return;
//var edit_str = item_result_array[item_count*row_item_count + column_index];
var save_img_id = id_str+'_save_img' ;
var str = '';
str += '<table border="0" cellspacing="0" cellpadding="0" width="100%" height="30">\n';
str += '<tr>\n';
str += '<td valign="middle" align="left">\n';
str += '<input ondblclick="show_keyboard_box(this);" onfocus="set_active_touch_screen_text_input(this);" onmousedown="return check_for_right_click(this, event, 0, -1);" onmouseup="return check_for_right_click_abort(null, null);" onkeydown="handle_edit_this_key(\''+id_str+'\',event)" size="0" id="'+id_str+'_text" type="text" style="height: 20; font-family: arial; font-size: 10pt; width: 20px">\n';
str += '</td>\n';
str += '<td valign="top" align="right">\n';
str += "<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\" width=\"14\" height=\"30\">\n";
str += '<tr>\n';
str += '<td valign="top" align="right" height="14">\n';
//
str += '<img title="Close" height="14" id="'+id_str+'_close_img" onclick=\"close_edit_this_text(\''+id_str+'\','+item_count+','+column_index+',\''+save_func+'\');\" style=\"cursor: pointer; cursor: hand;\" src="'+base_url+'/_images/close.gif">';
str += '</td>\n';
str += '</tr>\n';
str += '<tr>\n';
str += '<td valign="bottom" align="right" height="14">\n';
str += '<img id="'+save_img_id+'" onclick="'+save_func+'(\''+id_str+'\','+item_count+','+column_index+')" title="Save" height="14" style="cursor: pointer; cursor: hand;" src="'+base_url+'/_images/save.gif">';
str += '</td>\n';
str += '</tr>\n';

str += '</table>\n';
str += '</td>\n';

str += '</tr>\n';
str += '</table>\n';
elem.editting = true;
elem.innerHTML = str;
setTimeout('set_edit_this_text(\''+id_str+'\', '+item_count+', '+column_index+', null)',100);
}

function close_edit_this_text(id_str, item_count, column_index, save_func)
{
var elem = document.getElementById(id_str);
if(elem == null)
	return;
elem.editting = false;
elem.innerHTML = get_edit_this_text(id_str, item_count, column_index, save_func, false);
elem.title = (item_result_array[item_count*row_item_count + column_index]);
prev_id_str = null;
}

function set_column_width(disp_str, column_index)
{
if(column_width_adjusted == false)
	{
	var wide = detail_view_text_display_length[column_index];
	var wide1 = get_text_width(disp_str);
	if(wide == null || wide < wide1)
		{
		wide1 = Math.max(wide1, init_detail_view_text_display_length[column_index]);
		detail_view_text_display_length[column_index] = wide1;
		}
	}
}

function get_edit_this_text(id_str, item_count, column_index, save_func, static_text)
{
var disp_str = (item_result_array[item_count*row_item_count + column_index]);
disp_str = pre_process_detail_value(item_count, disp_str, column_index);
//disp_str = modify_array(disp_str);
var str = '';

if(owner && static_text == false)
	{
	str += '<table style="font-family: arial; font-size: 10pt; font-weight: normal;" border="0" cellspacing="0" cellpadding="0"';
	if(disp_str == '')
		{
		str += ' title="Click to edit this entry" width="95%"';
		disp_str = '&nbsp;';
		}
	str += ' >\n';
	str +=  '<tr>\n';
	str +=  '<td style="color: black; border-width: 0px 0px 1px 0px; border-style: dotted; border-color: black; cursor: pointer; cursor: hand;" onmousedown="edit_this_text(\''+id_str+'\','+item_count+','+column_index+',\''+save_func+'\')"  height="100%" 	align="left" valign="middle">\n';
	}
		
str += disp_str;
if(owner && static_text == false)
	{
	str +=  '</td>\n';
	str +=  '</tr>\n';
	str +=  '</table>\n';
	}
set_column_width(str, column_index)

return str;
}

function set_shelf_area_height(high)
{
if(high == null)
	{
	high = get_shelf_area_height();
	}
var elem = document.getElementById('item_list');
if(elem != null)
	{
	//elem.style.overflowY='auto';
	elem.style.height = high + 'px';
	}
}


function get_shelf_area_height()
{
var y = 0;
var elem1 = document.getElementById('last_row_entry');
if(elem1 != null)
	{
	y = findPosY(elem1);	
	}
var elem2 = document.getElementById('shelf_area_bottom');
if(elem2 == null)
	return 0;

elem1 = document.getElementById('item_list');
if(elem1 == null)
	return 0;

var y1 = findPosY(elem1);
var high3 = avail_height - y1 - 150; 
var high1 = y - y1;
var high2 = findPosY(elem2) - y1;
//alert(high1 + "," + high2 + "," + high3);
high1 =  Math.max(high3, Math.max(high1,high2));
//alert(high1);
return high1;
}


//function modify_title($title, $max_length, $word_length)
function fix_title_str(title, max_length, word_length)
{
if(title.length > max_length)
	{
	title = trim(title.substr(0, max_length)) + "...";
	}
var length;
var length1;
var str;
var str1;
var i;
var j;
var count;
var arr = title.split(" ");
title = '';
word_length = Math.max(1, word_length);
max_length = Math.max(1, max_length);
for(i = 0; i < arr.length; ++i)
	{
	str = arr[i];
	length = str.length;
	for(j = 0; j < length; j += word_length)
		{
		length1 = str.length;
		count = (length1 > word_length)?word_length:length1;
		str1 = str.substr(0, count);//substr($str, 0, $count);
		str = str.substr(count);//substr($str, $count);
		title += (str1 + " ");
		}
	}
title = trim(title);
return title;
}

function detail_info_mouse_out(obj,evnt)
{
//hide_details_info();
details_info_close = true;
setTimeout('check_hide_details_info()',100);
}

function check_hide_details_info()
{
if(details_info_close == true)
	hide_details_info();
}

function details_info_mouse_move(evnt,item_count)
{

if(details_info_showing == true)
	{
	if(item_count != prev_item_count)
		{
		hide_details_info();
		}
	}
else
	{
	get_xy(evnt);
 	hover_x = elem_x + 10;
        hover_y = elem_y + 10;
	hover_item_count = item_count;
	}
}

function create_view_framework()
{
var count = query_result_count;
var item_count = 0;
var table_width 	= image_display_width  + image_display_hgap;
var table_height 	= image_display_height + image_display_vgap;
var str = "<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n";
var y;
//detail_item_objs = new Array();
for(y = 0; y < count/display_item_per_row; ++y)
	{
	str+= "<tr>\n";
	str += "<td id=\"item_row_view_"+y+"\" width=\"100%\" height=\""+table_height+"\" align=\"center\" valign=\"middle\">";
	str += "</td>";
	str += "</tr>";
	}	

if(count > 0)
	{

	str+= "<tr>\n";
	str += "<td style=\"height: 1px; font-size: 1pt;\" id=\"last_row_entry\" width=\"100%\" height=\"1\" align=\"center\" valign=\"middle\">";
	str += "</td>";
	str += "</tr>";

	str += "</table>";		
	}
var elem = document.getElementById('item_list');
if(elem != null)
	{
	//elem.style.display="none";	
	elem.innerHTML = str;
	}	
}

function check_all_thumbnail_images()
{
for(var item_count = 0; (item_count < query_result_count) ; ++item_count)
	{
	var img_url 			= item_result_array[item_count*row_item_count + 1];
	var item_owner_id = item_result_array[item_count*row_item_count + 2];
	var borrow_status 	= item_result_array[item_count*row_item_count + 15];
	if(borrow_status != 0)
		{
		update_borrow_status_image(item_owner_id);
		}		
	}
}

function thumbnail_view(y)
{
	var item_count = y*display_item_per_row;
	var str = "";
	var count = query_result_count;
	var user = escape(current_username);
	var xstart = 0;//findPosX(elem);
	var x = 0;
	//var y = 0;//findPosY(elem);
	var item_owner_id;
	var img_url;
	var item_div_id;
	var item_row_id;
	var str1 = '';
	var wide = null;
	var high = null;
	var table_width 				= image_display_width  + image_display_hgap;
	var table_height 				= image_display_height + image_display_vgap;
	
	if(item_result_array == null)
		{
		return;
		}

	
	if(count > 0)
		{
		str = "<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n";
		}
	//for(y = 0; y < count/display_item_per_row; ++y)
		{
		str+= "<tr>\n";
		for(x = 0; x < display_item_per_row; ++x)
			{
			//str+= "<td width=\'align=\"center\" valign=\"middle\">\n";
			str += "<td width=\""+table_width+"\" height=\""+table_height+"\" align=\"center\" valign=\"middle\">";
			if(item_count < count)
				{
				item_owner_id = item_result_array[item_count*row_item_count + 2];
				item_div_id 	= get_item_div_id(item_owner_id);
				item_row_id 	= get_item_row_id(item_owner_id);
				//str += "<div id=\""+item_row_id+"\" style=\"text-align: center; vertical-align: middle; z-index: 0; overflow: hidden; width: "+(table_width - 4)+"px; height: "+(table_height - 4)+"px;\">\n";						

				//if(img_url.length != 0)
				//	{
					//str += "Rana";
					str += generate_image_thumbnail(item_count, 0);		
				//	}
				//else
				//	{	
				//	str += generate_generic_thumbnail(item_count);
				//	}
				//str += "</div>\n";
				++item_count;
				}
			str += "</td>";
			}
		str += "</tr>";
		}	
	if(count > 0)
		{
		str += "</table>";		
		}
	return str;
}

function get_item_row_id(item_owner_id)
{
var item_row_id = "item_row_"+item_owner_id;
return item_row_id;
}

function get_item_div_id(item_owner_id)
{
var item_div_id = "item_img_"+item_owner_id;
return item_div_id;
}

function get_status_img(borrow_status)
{
var picture_size = 0;
var status_img = '';
var status_img_visible = true;
if(borrow_status == 1)
	{
	picture_size = 45;
	status_img = base_url+"/_images/borrowed.gif";
	}
else if(borrow_status == 3)
	{
	picture_size = 45;
	status_img = base_url+"/_images/loan.gif";
	}
else if(borrow_status == 2 || borrow_status == 6)
	{
	picture_size = 49;
	status_img = base_url+"/_images/late.gif";
	}
else
	{
	picture_size = 45;
	status_img = base_url+"/_images/borrowed.gif";
	status_img_visible = false;
	}
var arr = new Array();
arr['size'] = picture_size;
arr['img'] = status_img;
arr['visible'] = status_img_visible;
return arr;
}

function generate_image_thumbnail(item_count, size_type)
{
var medium 							= item_result_array[item_count*row_item_count + 0]*1;
var img_url 						= (item_result_array[item_count*row_item_count + ((size_type == 0)?MEDIUM_IMG_INDEX:LARGE_IMG_INDEX)]);
var item_owner_id 			= item_result_array[item_count*row_item_count + 2];
var borrow_status 			= item_result_array[item_count*row_item_count + 15]*1.0;
var table_width 				= image_display_width  + image_display_hgap - 4;
var table_height 				= image_display_height + image_display_vgap - 4;
var item_row_id 				= get_item_row_id(item_owner_id);
var item_div_id 				= get_item_div_id(item_owner_id);
var arr 								= get_status_img(borrow_status);
var status_img_visible 	= arr['visible'];
var picture_size 				= arr['size']*1;
var status_img 					= arr['img'];

if(img_url.length == 0)
	{
	return generate_generic_thumbnail(item_count);
	}
var str = '';
str += "<table style=\"width: "+table_width+"px; height: "+table_height+"px; display: compact; overflow: hidden;\" id=\""+item_row_id+"\" height=\""+table_height+"\" width=\""+table_width+"\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n";
str += "<tr >\n";

str += "<td align=\"center\" height=\"100%\" width=\"100%\" valign=\"middle\">\n";

str += "			<table  onmousedown=\"title_mouse_down("+item_owner_id+", event)\" style=\"filter:shadow(color:gray, strength:5, direction:135); font-family: arial; font-size: 10pt; text-align: center; vertical-align: top;\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n";
str += "				<tr>\n";

str += "					<td valign=\"middle\" align=\"center\">\n";

var str1 = "";
if(size_type == 0)
	{
	str1 = "onerror=\"load_image_thumbnail("+item_count+")\"";
	}
else
	{
	str1 = "onerror=\"load_generic_thumbnail("+item_count+")\"";
	}	
str += "<img "+str1+"  onload=\"onload_actual_thumbnail_image(this,"+item_owner_id+");\" style=\"display: none; visibility: hidden; cursor: pointer; cursor: hand;\"  onmouseover=\"this.style.border='2px outset gray'\" onmousemove=\"details_info_mouse_move(event,"+item_count+")\" onmouseout=\"this.style.border='1px outset gray'; detail_info_mouse_out(this,event);\" id=\""+item_div_id+"\" src=\"" + img_url + "\">\n";
	
str += "					</td>\n";
str += "				</tr>\n";
str += "			</table>";

str += "</td>\n";
str += "</tr>\n";

str += "</table>\n";

//str += "<img onload=\"status_image_loaded(this,"+item_owner_id+");\" id=\"status_img_"+item_owner_id+"\" style=\"position: absolute; display: none; visibility: hidden; z-index: 25;\">\n";

return str;
}

function onload_actual_thumbnail_image(obj, item_owner_id)
{
obj.style.display = '';
adjust_dimension(obj);
obj.style.border = '1px outset gray';
obj.style.visibility = 'visible';
//var item_owner_id = item_result_array[item_count*row_item_count + 2];
var wide = parseInt(obj.width);
var high = parseInt(obj.height);
var item_row_id = get_item_row_id(item_owner_id);
var elem = document.getElementById(item_row_id);
if(elem != null)
	{
	wide += 20;
	high += 20;
	//alert(wide);
	elem.style.width = wide + 'px';
	elem.style.height = high + 'px';
	}

update_status_image(item_owner_id);

}

function generate_generic_thumbnail(item_count)
{
var medium 							= item_result_array[item_count*row_item_count + 0]*1;
var item_owner_id 			= item_result_array[item_count*row_item_count + 2];
var title_str 					= (item_result_array[item_count*row_item_count + 3]);
var authors 						= (item_result_array[item_count*row_item_count + 9]);
var borrow_status 			= item_result_array[item_count*row_item_count + 15]*1.0;
var item_row_id 				= get_item_row_id(item_owner_id);
var item_div_id 				= get_item_div_id(item_owner_id);
var str = '';
var text_color = '#505050';
var medium_img = medium_imgs[medium].src;
var display_str = '';
if(user_view_type == THUMBNAIL_VIEW_LARGE)
	{
	display_str = fix_title_str(title_str, 70, 15);
	authors			= fix_title_str(authors, 40, 17);
	}
else
	{
	display_str = fix_title_str(title_str, 28, 10);
	authors			= fix_title_str(authors, 20, 12);
	}
var arr = get_status_img(borrow_status);
var status_img_visible = arr['visible'];
var picture_size = arr['size']*1;
var status_img = arr['img'];



str += "<table id=\""+item_row_id+"\" width=\""+(gen_width+20)+"\" height=\""+(gen_height+20)+"\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n";
str += "	<tr>\n";
str += "		<td align=\"center\" valign=\"middle\">\n";
//str += "<span style=\"filter:shadow(color:gray, strength:5, direction:135); width:"+gen_width+"px; height:"+gen_height+"px;\">";
str += "<table id=\""+item_div_id+"\" onmousedown=\"title_mouse_down("+item_owner_id+", event)\" style=\"height: "+gen_height+"px; width: "+gen_width+"px; filter:shadow(color:gray, strength:5, direction:135); overflow: hidden; border: 1px outset gray; cursor: hand; cursor: pointer;\" onmouseover=\"this.style.border='2px outset gray'\" onmouseout=\"this.style.border='1px outset gray'; detail_info_mouse_out(this,event);\" onmousemove=\"details_info_mouse_move(event,"+item_count+")\" background=\""+medium_img+"\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\" width=\""+(gen_width)+"\" height=\""+(gen_height)+"\">\n";
str += "				<tr>\n";

str += "					<td width=\"100%\" height=\"100%\" align=\"center\" valign=\"middle\" >\n";
str += "					<div style=\"display: block; border: 1px solid #909090; display: block; margin: 3px 3px 3px 3px; width: "+(gen_width-6)+"; height: "+(gen_height-6)+"; font-family: arial; font-size: 10pt; text-align: center; vertical-align: middle\">\n";

//update_generic_status_image(item_owner_id);

//str += "<img onload=\"position_generic_status_image("+item_owner_id+");\" id=\"status_img_"+item_owner_id+"\" style=\"visibility: hidden; vertical-align: top; text-align: right; position: absolute; z-index: 25; left: 0px; top: 0px;\" width=\"+picture_size+\" src=\""+status_img+"\">\n";

str += "<div style=\"font-weight: bold; color: "+text_color+"; font-family: arial; font-size: 10pt; display: block; overflow: hidden; text-align: center; vertical-align: middle; width: 100%; height: "+(gen_height-53)+";\">";
str += display_str;
str += "</div>\n";
if(authors.length == 0)
	{
	str += "<div style=\"color: "+text_color+"; font-family: arial; display: block; width: 100%; height: 47; font-size: 8pt; vertical-align: bottom;\">&nbsp;</div>";
	}
else
	{
	str += "<div style=\"color: "+text_color+"; font-family: arial; display: block; width: 100%; height: 47; font-size: 8pt; vertical-align: bottom;\">By<br>"+authors+"</div>";
	}
str += "					</div>\n";

str += "					</td>\n";
str += "		<td id=\""+item_div_id+"_right\" style=\"font-size: 1pt; width: 1px;\"; align=\"left\" valign=\"top\" width=\"1\">\n";
str += "		</td>\n";
str += "				</tr>\n";
str += "			</table>";
//str += "</span>";
str += "		</td>\n";
str += "	</tr>\n";
str += "</table>\n";
return str;
}

function load_generic_thumbnail(item_count)
{
if(item_result_array == null)
	{
	return;
	}

var item_owner_id = item_result_array[item_count*row_item_count + 2];
var item_row_id 	= get_item_row_id(item_owner_id);
var elem  = document.getElementById(item_row_id);
if(elem != null)
	{
	elem = elem.offsetParent;
	elem.innerHTML = generate_generic_thumbnail(item_count);
	}
}

function load_image_thumbnail(item_count)
{
if(item_result_array == null)
	{
	return;
	}

var item_owner_id = item_result_array[item_count*row_item_count + 2];
var item_row_id 	= get_item_row_id(item_owner_id);
var elem  = document.getElementById(item_row_id);
if(elem != null)
	{
	elem = elem.offsetParent;
	elem.innerHTML = generate_image_thumbnail(item_count, 1);
	}
request_new_image(item_count);
}

function status_image_loaded()
{
if(item_result_array == null)
	{
	return;
	}
	
var status_img = this;
var item_owner_id = parseInt(status_img.id.substr(11));
var item_index = get_item_index_from_item_owner_id(item_owner_id);
var borrow_status = item_result_array[item_index*row_item_count + 15];
var arr = get_status_img(borrow_status);
var str = arr['visible']?("visible"):("hidden");
status_img.style.visibility = str;
}

function update_borrow_status_image(item_owner_id)
{
if(item_result_array == null)
	{
	return;
	}
	
var item_index = get_item_index_from_item_owner_id(item_owner_id);
var img_url = item_result_array[item_index*row_item_count + 1];
if(img_url.length != 0)
	{
	update_status_image(item_owner_id);
	}
else
	{
	update_generic_status_image(item_owner_id);
	}	
}

function disableSelection(target)
{
if (typeof target.onselectstart!="undefined") //IE route
	target.onselectstart=function(){return false}
else if (typeof target.style.MozUserSelect!="undefined") //Firefox route
	target.style.MozUserSelect="none"
else //All other route (ie: Opera)
	target.onmousedown=function(){return false}
target.style.cursor = "default"
}

function hide_borrow_status_image(item_owner_id)
{
var status_img = document.getElementById('status_img_'+item_owner_id);
if(status_img != null)
	{
	status_img.style.visibility = 'hidden';
	}
}

function show_borrow_status_image(item_owner_id)
{
var status_img = document.getElementById('status_img_'+item_owner_id);
if(status_img != null)
	{
	status_img.style.visibility = 'visible';
	}
}

function update_status_image(item_owner_id)
{
if(item_result_array == null)
	{
	return;
	}
	
var item_index = get_item_index_from_item_owner_id(item_owner_id);
var borrow_status = item_result_array[item_index*row_item_count + 15];
var arr = get_status_img(borrow_status);
var status_img = document.getElementById('status_img_'+item_owner_id);
if(arr['visible'] == false)
	{
	if(status_img != null)
		{
		update_borrow_images_array(status_img);	
		document.body.removeChild(status_img);
		}
	return;
	}
if(status_img == null)
	{
	status_img = document.createElement('img');
	status_img.id = 'status_img_'+item_owner_id;
	status_img.style.position = 'absolute';
	status_img.style.zIndex = 5;
	status_img.src = arr['img'];
	status_img.width = arr['size'];
	status_img.onload= status_image_loaded;
	document.body.appendChild(status_img);
	borrow_status_images[borrow_status_images.length] = status_img;
	}
position_status_image(item_owner_id);	
}

function remove_all_borrow_status_images()
{
var i;
for(i = 0; i < borrow_status_images.length; ++i)
	{
	if(borrow_status_images[i] != null)
		{
		document.body.removeChild(borrow_status_images[i]);
		}
	borrow_status_images[i] = null;
	}
borrow_status_images = new Array();
}

function update_borrow_images_array(img)
{
for(i = 0; i < borrow_status_images.length; ++i)
	{
	if(borrow_status_images[i] == img)
		{
		borrow_status_images[i] = null;
		break;
		}
	}
}

function update_generic_status_image(item_owner_id)
{
if(item_result_array == null)
	{
	return;
	}
	
var item_index = get_item_index_from_item_owner_id(item_owner_id);
var borrow_status = item_result_array[item_index*row_item_count + 15];
var arr = get_status_img(borrow_status);
var status_img = document.getElementById('status_img_'+item_owner_id);
if(arr['visible'] == false)
	{
	if(status_img != null)
		{
		update_borrow_images_array(status_img);
		document.body.removeChild(status_img);
		}
	return;
	}
if(status_img == null)
	{
	status_img = document.createElement('img');
	status_img.id = 'status_img_'+item_owner_id;
	status_img.style.position = 'absolute';
	status_img.style.zIndex = 5;
	status_img.src = arr['img'];
	status_img.width = arr['size'];
	status_img.style.visibility = 'hidden';
	status_img.onload= status_image_loaded;
	document.body.appendChild(status_img);
	borrow_status_images[borrow_status_images.length] = status_img;
	}
position_generic_status_image(item_owner_id);	
}

function update_position_of_all_status_images()
{
if(item_result_array == null)
	{
	return;
	}
var item_count;
for(item_count = 0; item_count < query_result_count; ++item_count)
	{
	var item_owner_id 	= item_result_array[item_count*row_item_count + 2]*1;
	var img_url 				= item_result_array[item_count*row_item_count + 1];
	//var borrow_status 	= item_result_array[item_count*row_item_count + 15];
	if(img_url == null)
		continue;
	if(img_url.length != 0)
		{
		position_status_image(item_owner_id);
		}
	else
		{
		position_generic_status_image(item_owner_id);
		}
	}
}

function create_borrow_status_images(y)
{
if(item_result_array == null)
	{
	return;
	}
if(y < 0 || y >= query_result_count/display_item_per_row)
	return;
var item_count = y*display_item_per_row;
var x;
for(x = 0; (x < display_item_per_row) && (item_count < query_result_count); ++x, ++item_count)
	{
	var item_owner_id 	= item_result_array[item_count*row_item_count + 2]*1;
	var img_url 				= item_result_array[item_count*row_item_count + 1];
	if(img_url.length == 0)
		{
		update_generic_status_image(item_owner_id);
		}
	else
		{
		
		}	
	}
}

function position_generic_status_image(item_owner_id)
{
var item_index = get_item_index_from_item_owner_id(item_owner_id);
var borrow_status = item_result_array[item_index*row_item_count + 15];
var arr = get_status_img(borrow_status);
var visible = arr['visible'];
var status_img = document.getElementById('status_img_'+item_owner_id);
if(status_img == null)
	{
	return;
	}
var item_div_id = 'item_img_'+item_owner_id;	
var title_img = document.getElementById(item_div_id);
if(title_img == null)
	{
	return;
	}

var x = findPosX(title_img);
var y = findPosY(title_img);
var img = document.getElementById(item_div_id+"_right");
if(img == null)
	{
	return;
	}

var x2 = findPosX(img);
//var y2 = findPosY(img);

//var x1 = findPosX(status_img);
//var y1 = findPosY(status_img);

x = x2 - status_img.width;
//y = y - y1;

status_img.style.left = (x+3) + "px";
status_img.style.top 	= (y+1) + "px";
if(visible)
	{
	status_img.style.visibility = 'visible';
	//status_img.style.display = '';
	}
else
	{
	status_img.style.visibility = 'hidden';
	}	
}

function position_status_image(item_owner_id)
{
var item_index = get_item_index_from_item_owner_id(item_owner_id);
var borrow_status = item_result_array[item_index*row_item_count + 15];
var arr = get_status_img(borrow_status);
var visible = arr['visible'];
if(visible == false)
	{
	return;
	}
var status_img = document.getElementById('status_img_'+item_owner_id);
if(status_img == null)
	{
	return;
	}

var title_img = document.getElementById('item_img_'+item_owner_id);
if(title_img == null)
	{
	return;
	}

var x = findPosX(title_img);
var y = findPosY(title_img);
var wide = title_img.width;
var wide1 = status_img.width;

x = x + wide - wide1;

status_img.style.left = (x+3) + 'px';
status_img.style.top 	= (y+1) + 'px';
if(visible)
	{
	//status_img.style.visibility = 'visible';
	status_img.style.display = 'block';
	}
else
	{
	status_img.style.display = 'none';
	//status_img.style.visibility = 'hidden';
	}	
}

function adjust_dimension(obj)
{
if(user_view_type == THUMBNAIL_VIEW_LARGE)
	{
	var max_dim = image_display_width;
	var wide = obj.width;
	var high = obj.height;
	var ratio;
	var high1;
	var wide1;
	if(wide > max_dim || high > max_dim)
		{
		wide1 = max_dim;
		ratio = wide1/wide;
		high1 = high * ratio;
		if(high1 > max_dim)
			{
			high1 = max_dim;
			ratio = high1/high;
			wide1 = wide * ratio;
			}

		wide = parseInt(wide1);
		high = parseInt(high1);
		obj.width = wide;
		obj.height = high;
		}
	}
else
	{
	obj.width = image_display_width;
	obj.height = image_display_height;
	}
//obj.style.visibility = 'visible';
return new Array(obj.width, obj.height);
}

function show_sort_indicator(sort_direction, sort_type)
	{
	var elem = document.getElementById(sort_type + "_cell");
	if(elem == null)
		return false;

	if(sort_direction == 0)
		{
		elem.innerHTML = "<img src=\""+base_url+"/_images/sort_down.gif\">";
		}
	else
		{
		elem.innerHTML = "<img src=\""+base_url+"/_images/sort_up.gif\">";
		}
	}

function clear_canvas()
{
if(current_interval_timer != null)
	{
	clearInterval(current_interval_timer);
	current_interval_timer = null;
	}
remove_all_borrow_status_images();	
item_result_array = null;
var elem = document.getElementById('item_list');
if(elem != null)
	elem.innerHTML = "&nbsp;";
elem = document.getElementById('query_status');
if(elem != null)
	elem.innerHTML = "&nbsp;";

}


function decode_utf8(utftext) 
{   
var string = "";   
var i = 0;   
var c = c1 = c2 = 0;   

while ( i < utftext.length ) {   

    c = utftext.charCodeAt(i);   

    if (c < 128) {   
        string += String.fromCharCode(c);   
        i++;   
    }   
    else if((c > 191) && (c < 224)) {   
        c2 = utftext.charCodeAt(i+1);   
        string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));   
        i += 2;   
    }   
    else {   
        c2 = utftext.charCodeAt(i+1);   
        c3 = utftext.charCodeAt(i+2);   
        string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));   
        i += 3;   
    }   

}   

return string;   
}   

function show_details_info(x, y, item_count)
{
var item_owner_id 	= item_result_array[item_count*row_item_count + 2];
if(drag_item_owner_id == item_owner_id)
	return;
hide_all_popup_menu();
if(item_result_array == null || select_bar_obj != null)
	{
	return;
	}
if(user_view_type == DETAIL_VIEW )//|| item_clicked == true)
	return;
var img_url 				= item_result_array[item_count*row_item_count + 1];
var details_link		= "./title/"+item_owner_id;
var author_list 		= (item_result_array[item_count*row_item_count + 9]);
var medium 					= item_result_array[item_count*row_item_count + 0]*1;
var	title_str 			= (item_result_array[item_count*row_item_count + 3]);
var	publisher 			= (item_result_array[item_count*row_item_count + 4]);
//var	rating 					= item_result_array[item_count*row_item_count + 5];
//var	released_date 	= item_result_array[item_count*row_item_count + 6];
//var	added_date_ext 	= item_result_array[item_count*row_item_count + 7];
var	mpaa_rating_url = item_result_array[item_count*row_item_count + 8];

var	directors 			= (item_result_array[item_count*row_item_count + 10]);
//var	genres 					= item_result_array[item_count*row_item_count + 11];
var	user_rating 		= item_result_array[item_count*row_item_count + 12];
var	platform 				= item_result_array[item_count*row_item_count + 16];
var	shelf_name 			= (item_result_array[item_count*row_item_count + 17]);
var borrower_id 		= item_result_array[item_count*row_item_count + 18];
var borrower_name		= (item_result_array[item_count*row_item_count + 19]);
var return_date			= (item_result_array[item_count*row_item_count + 20]);
var borrower_email	= item_result_array[item_count*row_item_count + 24];
var borrow_status 	= item_result_array[item_count*row_item_count + 15]*1;
var lender_name 		= item_result_array[item_count*row_item_count + 26];
var synopsis				= item_result_array[item_count*row_item_count + SYNOPSIS_INDEX];
var my_review				= item_result_array[item_count*row_item_count + MY_REVIEW_INDEX];

var borrow_item_owner_id = item_result_array[item_count*row_item_count + 27]*1;
var star;
if(shelf_name == null)
	return;
var str = '';
//str += "<b>Title: </b><a style=\"color: #9C7600;\" href=\""+details_link+"\" title=\"click to see the details\">"+title_str+"</a><BR>";
var text_color = '#505050';
var medium_img = medium_imgs[medium].src;
var display_str = '';
display_str = fix_title_str(title_str, 70, 15);
var authors			= fix_title_str(author_list, 40, 17);
var gen_width1 = 120;
var gen_height1 = 160;
var table_width 				= gen_width1  + 8;
var table_height 				= gen_height1 + 8;
var page_width = 250;

str += '<table id="details_info_table" style="display: block; font-family: arial; font-size: 9pt; color: #9C7600;" border="0" cellspacing="0" cellpadding="4" width="100%">\n';
str += '<tr>\n';
str += '<td align="left" valign="top">\n';
str += '<b style="color: black;">'+title_str+'</b>\n';
str += '</td>\n';
str += '</tr>\n';

str += '<tr>\n';
str += '<td align="center" valign="middle">\n';
str += '<a style="color: #9C7600;" href="'+details_link+'" title="click to see the details">\n';
if(img_url.length != 0)
	{
	str += '<img onerror="request_new_image('+item_count+');" id=\"details_info_image\" src="'+img_url+'">\n';
	}
else
	{
	str += "<table id=\"details_info_image\" style=\"overflow: hidden;\" border=\"0\" width=\""+table_width+"\" height=\""+table_height+"\" cellspacing=\"0\" cellpadding=\"0\">\n";
	str += "	<tr>\n";
	str += "		<td align=\"center\" valign=\"middle\" width=\"100%\" height=\"100%\">\n";
	
	str += "			<table background=\""+medium_img+"\" style=\"overflow: hidden; border: 1px outset gray;\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\" width=\""+gen_width1+"\" height=\""+gen_height1+"\">\n";
	str += "				<tr>\n";
	
	str += "					<td width=\"100%\" height=\"100%\" align=\"center\" valign=\"middle\" >\n";
	str += "					<div style=\"display: block; border: 1px solid #909090; display: block; margin: 3px 3px 3px 3px; width: "+(gen_width1-6)+"; height: "+(gen_height1-6)+"; cursor: pointer; cursor: hand; font-family: arial; font-size: 10pt; text-align: center; vertical-align: middle\">\n";
	
	
	str += "<div style=\"font-weight: bold; color: "+text_color+"; font-family: arial; font-size: 10pt; display: block; overflow: hidden; text-align: center; vertical-align: middle; width: 100%; height: "+(gen_height1-53)+";\">";
	str += display_str;
	str += "</div>\n";
	if(authors.length == 0)
		{
		str += "<div style=\"color: "+text_color+"; font-family: arial; display: block; width: 100%; height: 47; font-size: 8pt; vertical-align: bottom;\">&nbsp;</div>";
		}
	else
		{
		str += "<div style=\"color: "+text_color+"; font-family: arial; display: block; width: 100%; height: 47; font-size: 8pt; vertical-align: bottom;\">By<br>"+authors+"</div>";
		}
	str += "					</div>\n";
	
	str += "					</td>\n";
	str += "				</tr>\n";
	str += "			</table>";
	
	str += "		</td>\n";
	str += "	</tr>\n";
	str += "</table>\n";	
	}
	
str += '</a>\n';
str += '</td>\n';
str += '</tr>\n';

str += '<tr>\n';
str += '<td align="left" valign="top">\n';
if(medium == 1)
	{
	if(author_list.length != 0)
		{
		str += "<b>Author: </b>"+author_list+"<br>";
		}
	if(publisher.length != 0)
		{
		str += "<b>Publisher: </b>"+publisher+"<br>";
		}
	}
else if(medium == 2)
	{
	if(author_list.length != 0)
		{
		str += "<b>Actors: </b>"+author_list+"<br>";
		}
	if(directors.length!=0)
		{
		str += "<b>Director: </b>"+directors+"<br>";
		}
	if(mpaa_rating_url.length != 0)
		{
		str += "<b>MPAA: </b><img src=\""+mpaa_rating_url+"\"><br>";
		}
	if(publisher.length != 0)
		{
		str += "<b>Studio: </b>"+publisher+"<br>";
		}
	}
else if(medium == 3)
	{
	if(author_list.length != 0)
		{
		str += "<b>Artist: </b>"+author_list+"<br>";
		}
	if(publisher.length != 0)
		{
		str += "<b>Studio: </b>"+publisher+"<br>";
		}
	}
else if(medium == 4)
	{
	if(platform.length != 0)
		{
		str += "<b>Platform: </b>"+platform+"<br>";
		}
	if(publisher.length != 0)
		{
		str += "<b>Studio: </b>"+publisher+"<br>";
		}
	if(mpaa_rating_url.indexOf("esrb-rating.gif") > 0 || mpaa_rating_url.indexOf("/.gif") > 0 || mpaa_rating_url.length == 0)
		{
		mpaa_rating_url = "http://g-images.amazon.com/images/G/01/detail/nr.gif";
		}
	str += "<b>ESRB: </b><img src=\""+mpaa_rating_url+"\"><br>";

	}
else if(medium == 5)
	{
	if(platform.length != 0)
		{
		str += "<b>Platform: </b>"+platform+"<br>";
		}
	if(publisher.length != 0)
		{
		str += "<b>Publisher: </b>"+publisher+"<br>";
		}
	}

if(req_media_id == 0)
	{
	str += "<b>Media: </b>" + media_names[medium]+"<br>";
	}

if(shelf_name.length != 0 && req_shelf_id == 0)
	{
	str += "<b>Shelf: </b>"+shelf_name;
	}

str += "<table style=\"font-size: 10pt; color: #9C7600; \" border=\"0\" cellpadding=\"0\" cellspacing=\"0\" height=\"18\">\n";
str += "<tr>\n";
str += "<td>\n";
str += "<b>Rating:</b>";
str += "</td>\n";
str += "<td>\n";
str += set_user_rating('owner_rating', owner, user_rating, item_count, 'save_user_setting')
str += '</td>\n';
str += '</tr>\n';

str += '</table>\n';

if(synopsis.length != 0)
	{
	//str += "<tr>\n";
	str += "<div style=\"font-weight: bold; text-align: left; vertical-align: top;\">\n";
	str += "Synopsis:";
	str += "</div>\n";
	str += "<div style=\"text-align: left; margin-left: 4px; vertical-align: top;\">\n";
	str += synopsis;
	str += '</div>\n';
	//str += '</tr>\n';
	if(synopsis.length > 100)
		page_width = 350;
	}

if(my_review.length != 0)
	{
	str += "<div style=\"font-weight: bold; text-align: left; vertical-align: top;\">\n";
	str += "My&nbsp;Review:";
	str += "</div>\n";
	str += "<div style=\"margin-left: 4px; text-align: left: vertical-align: top;\">\n";
	str += my_review;
	str += '</div>\n';
	//str += "<tr>\n";
	//str += "<td valign=\"top\">\n";
	//str += "<b>My&nbsp;Review:</b>";
	//str += "</td>\n";
	//str += "<td valign=\"top\">\n";
	//str += my_review;
	//str += '</td>\n';
	//str += '</tr>\n';
	if(my_review.length > 100)
		page_width = 350;
	}
str += "<div id=\"details_info_bottom\" style=\"font-size: 1pt; width: 1px; height: 1px;\">&nbsp;</div>\n";

var value;
if(borrow_status != 0)
	{
	if(borrow_status == 1 || borrow_status == 2)
		{
		if(borrower_id != 0)
			{
			value = string_replace(borrower_name, " ", "&nbsp;");
			value = "<a href=\""+base_url+"/"+escape(borrower_name)+"\" target=\"_blank\">"+value+"</a>";
			}
		else
			{
			value = string_replace(borrower_name, " ", "&nbsp;");
			if(borrower_email.length != 0 && owner)
				{
				value = "<a href=\"mailto:"+borrower_email+"\">"+borrower_name+"</a>";
				}
			}
		str += "<b>Loaned&nbsp;to:&nbsp;</b>"+value;	
		}
	else //3 or 6
		{
		value = string_replace(lender_name, " ", "&nbsp;");
		if(borrow_item_owner_id == 0)
			{
			if(borrower_email.length != 0 && owner)
				{
				value = "<a href=\"mailto:"+borrower_email+"\">"+value+"</a>";
				}
			}
		else
			{
			value = "<a href=\""+base_url+"/"+escape(lender_name)+"\" target=\"_blank\">"+value+"</a>";
			}		

		//value = "<a href=\""+base_url+"/"+escape(lender_name)+"\" target=\"_blank\">"+value+"</a>";		
		str += "<b>Borrowed&nbsp;from:&nbsp;</b>"+value;	
		}	
	str += "<br><b>Return&nbsp;date:&nbsp</b>"+return_date+"<br>";
	}

var elem = document.getElementById('details_info');
if(elem != null)
	{
	elem.style.width = page_width + 'px';
	}

var elem = document.getElementById('details_info_text');
if(elem == null)
	return;

elem.innerHTML = str;
prev_item_count = item_count;
setTimeout('set_info_page_position('+x+','+y+','+item_owner_id+')', 100);
}

function save_user_setting(item_count, rating)
{
if(owner)
	{
	if(item_result_array == null)
	{
	return;
	}

	item_result_array[item_count*row_item_count + 12] = rating;
	var item_owner_id 	= item_result_array[item_count*row_item_count + 2]*1;
	var args = 						{	
												'action'								: 		'update_user_rating', 
												'item_owner_id'					:			item_owner_id,
												'user_rating'						: 		rating,
												'random'								:			Math.random()
												};
	ajax_get_request(ajax_request_url, args, update_user_rating_response, false);
	}
}

function update_user_rating_response(response_array)
{
if(response_array[0] == "error")
	{
	show_alert_dialog("Error", response_array[1]);
	}
}

function set_info_page_position(x, y, item_owner_id)
{
if(item_result_array == null)
	{
	return;
	}
	
var elem1 = document.getElementById('details_info');
if(elem1 == null)
	return;
var height1 = findPosY(document.getElementById('details_info_bottom')) - findPosY(elem1) + 40;
var width1 = parseInt(elem1.style.width);
var tble = document.getElementById('details_info_table');
if(tble != null)
	{
	tble.style.width = (width1-5)+'px';
	}
	
if(y + height1 + banner_header_size + folder_area_offset + page_y_offset > screen.availHeight)
	{
	y = y - height1;
	}
if(x + width1 > avail_width )
		{
		x = x - width1 - 10;
		}
if( y < document.body.scrollTop )
	{
	y = document.body.scrollTop;
	}
	
elem1.style.left = x;
elem1.style.top = y;

var obj = document.getElementById("details_info_image");
if(obj != null)
	{
	var width = parseInt(obj.width);
	var high = parseInt(obj.height);
	x = findPosX(obj);
	y = findPosY(obj);
	var item_index = get_item_index_from_item_owner_id(item_owner_id);
	var borrow_status = item_result_array[item_index*row_item_count + 15]*1;
	if(borrow_status != 0)
		{
		var arr = get_status_img(borrow_status);	
		var status_img = document.getElementById('details_info_borrow_image');
		if(status_img == null)
			{
			status_img = document.createElement('img');
			status_img.id = 'details_info_borrow_image';
			status_img.style.position = 'absolute';
			status_img.style.zIndex = 100;
			document.body.appendChild(status_img);
			}	
		status_img.src = arr['img'];
		status_img.width = arr['size'];
		status_img.style.left = (x + width - status_img.width) + 'px';
		status_img.style.top = y + 'px';
		status_img.style.visibility = 'visible';
		}
	}

elem1.style.visibility="visible";
details_info_showing = true;
}

function detail_display_complete()
{
details_info_showing = true;
//details_info_timer = null;
}

function hide_details_info()
{
var elem1 = document.getElementById('details_info');
if(elem1 == null)
	return;

elem1.style.visibility = "hidden";
details_info_showing = false;
hover_x = 0;
hover_y = 0;
var status_img = document.getElementById('details_info_borrow_image');
if(status_img != null)
	{
	status_img.style.visibility = 'hidden';
	}
}

function make_visible_details_info()
{
var elem1 = document.getElementById('details_info');
if(elem1 == null)
	return;

elem1.style.visibility = "visible";
details_info_showing = true;
}

function get_print_sql()
{
return print_sql;
}

function set_user_rating(id_rating, owner, user_rating, item_index, handle_function)
{
var title_str;
var str = '';
var back_val;
var mouseup_func = '';
var mouseover_func = '';
var rating_star = 'rating_star';

if(owner)
	{
	mouseup_func = "user_rating_mouse_up(0,"+item_index+",'"+handle_function+"');";
	mouseover_func = "user_rating_mouse_over('"+id_rating+"',0,"+item_index+");";
	str += "<table onmouseout=\""+mouseover_func+"\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"100%\" height=\"18\">\n";
	str += "<tr>\n";

	str += "<td style=\"cursor:pointer; cursor:hand;\" onmouseup=\""+mouseup_func+"\">\n";
	str += "&nbsp\n";
	str += "</td>\n";
	}
else
	{
	str += "<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"100%\" height=\"18\">\n";
	str += "<tr>\n";
	}

str += "<td>\n";

title_str = "";
if(owner == false)
	{
	if(user_rating != 0)
		{
		title_str = user_rating + " star";
		}
	else
		{
		title_str = "No rating";
		}
	title_str = "title=\""+title_str+"\"";
	}
str += "<table "+title_str+" id=\""+id_rating+"\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"100\" height=\"18\">\n";
str += "<tr>\n";
for(i=1;i<=5;++i)
	{
	if(owner)
		{
		title_str = "title=\""+i + " star\"";
		mouseup_func = "user_rating_mouse_up("+i+","+item_index+",'"+handle_function+"');";
		mouseover_func = "user_rating_mouse_over('"+id_rating+"',"+i+","+item_index+");";
		rating_star = 'rating_owner_star';
		}
	if(i <= user_rating)
		{
		back_val='1px -76px';
		}
	else
		{
		back_val='1px 0px';
		}
str += "<td "+title_str+" onmouseup=\""+mouseup_func+mouseover_func+"\" onmouseover=\""+mouseover_func+"\" style=\"background-position: "+back_val+"\" width=\"19\" class=\""+rating_star+"\"></td>\n";
	}
str += "</tr>\n";
str += "</table>\n";
str += "<td>\n";
str += "</tr>\n";
str += "</table>\n";
return str;
}

function user_rating_mouse_over(id_rating, pos, item_index)
{
if(item_result_array == null)
	{
	return;
	}
	
var container=document.getElementById(id_rating);
if(container == null)
	return;
var elements=container.getElementsByTagName('TD');
if(elements == null)
	return;
var user_rating = item_result_array[item_index*row_item_count + 12]*1;
var numElements=elements.length;
for(var i=1;i<=numElements;i++)
	{
	var curElem=elements[i-1];
	if((i<=user_rating)&&(i<=pos))
		{
		curElem.style.backgroundPosition='1px -56px';
		}
	else if((i>user_rating)&&(i<=pos))
		{
		curElem.style.backgroundPosition='1px -56px';
		}
	else if((i>user_rating)&&(i>pos))
		{
		curElem.style.backgroundPosition='1px 0px';
		}
	else if((i<=user_rating)&&(i>pos))
		{
		curElem.style.backgroundPosition='1px -76px';
		}
	}
}

function user_rating_mouse_up(pos, item_index, handle_function)
{
var func = new Function(handle_function+'('+item_index+','+pos+')');
func();
}

function hover_event_check()
{
var diff_x = Math.abs(hover_x - prev_hover_x);
var diff_y = Math.abs(hover_y - prev_hover_y);
if(diff_x > 20 || diff_y > 20)
	{
	if(details_info_showing == true)
		{
		hide_details_info();
		}
	else
		{
		prev_hover_x = hover_x;
		prev_hover_y = hover_y;
		}
	}
else
	{
	if(details_info_showing == false)
		{
		//var str = (hover_item_count + "," + hover_x + "," + hover_y);
		//document.getElementById('search_text').value=str;
		if(hover_item_count != -1 && hover_x > 0 && hover_y > 0)
			{
			var func = new Function("show_details_info("+hover_x+","+hover_y+","+hover_item_count+")");
			func();
			}
		}
	}

//check_all_thumbnail_images();
}

function body_mouse_move(evnt)
{
//show_current_shelf_controls(evnt);
}

function hide_help(obj)
{
var elem = document.getElementById('help_box');
	if(elem == null)
		return;
elem.style.visibility = "hidden";
obj.src=base_url+'/_images/inactive_help.gif';
}

function show_help(obj, msg_type)
{
	var elem = document.getElementById('help_box');
	if(elem == null)
		return;
	var y1 = getImagePageTop(obj);
	var x1 = getImagePageLeft(obj);
	elem.style.left = x1 + 30;
	elem.style.top = y1;
	obj.src=base_url+'/_images/active_help.gif';
	var msg = '';
	if(msg_type == 1)
		{
		//msg = '<p style="text-align: justify; vertical-align: top"><u><b>Search Text Format</b></u><br>																																																	';
		//msg +='All searches are case insensitive. To search for <b>exact phrase</b> put double quotes around the search text.                   ';
		//msg +='For. e.g. to search for exact phrase <i>thomas friedman</i> type in <b>"thomas friedman"</b>.                                    ';
		//msg +='If you want to search for items containing <i>thomas</i> but not <i>friedman</i> then type in <b>+thomas -friedman</b> in the    ';
		//msg +='search box. <br>                                                                                                                 ';
		//msg +='If you want to search for items that contain both <i>thomas</i> and <i>friedman</i>, type in <b>+thomas +friedman</b><br>        ';
		msg +='<u><b>Narrowing Search Scope</b></u><br>                                                                                               ';
		msg +='You can narrow down the scope of search by specifying search options or selecting a shelf or media type. For e.g. to search for authors named <i>thomas</i> type in <b>author:thomas</b>. If you have selected a shelf then        ';
		msg +='the search scope is restricted to that shelf. If you have selected a media such as books, movies, music etc. then the search is restricted within that media type. if you want to search on all items in your library select <b>Entire Library</b> and   ';
		msg +='<b>All</b> then type in the search.<br>                                                                                            ';
		msg +='<br>Following search options are available                                                                                         ';
		msg +='<ol>                                                                                                                             ';
		msg +='<li><b>author:</b> searches for authors                                                                                                 ';
		msg +='<li><b>actor:</b> searches for actors                                                                                                   ';
		msg +='<li><b>artist:</b> searches for music artists                                                                                           ';
		msg +='<li><b>director:</b> searches for directors                                                                                             ';
		msg +='<li><b>publisher:</b> searches for publisher                                                                                            ';
		msg +='<li><b>genre:</b> searches for genre                                                                                                    ';
		//msg +='<li><b>director:</b> searches for directors                                                                                             ';
		msg +='<li><b>isbn:</b> searches for isbn code on the book                                                                                     ';
		msg +='<li><b>upc:</b> searches for upc on the book                                                                                            ';
		msg +='<li><b>title:</b> searches for the title of books, movies, music, games and software                                                    ';
		msg +='<li><b>track:</b> searches for a music track                                                    																				';
		msg +='<li><b>platform:</b> searches the platform on which a game or software can work. for e.g. playstation, windows etc.                     ';
		msg +='<li><b>shared:</b> lists files that this library shares with another library for e.g. to check what <i>coolabcgirl</i> shares           ';
		msg +='with <i>ranabasheer</i> first open the library of <i>coolabcgirl</i> by navigating to '+base_url+'/coolabcgirl and then    ';
		msg +='on the search box type <b>shared:ranabasheer</b>.                                                                                ';
		msg +='</ol></p>																																																														';
		elem.style.width = 600;
		}
	else
		{
		msg = msg_type;
		elem.style.width='';
		}
	elem.innerHTML = msg;
	elem.style.visibility = "visible";
}

function right_mouse_clicked(item_owner_id, evnt, obj_id)
{
hide_all_menu();
hide_all_popup_menu();
get_xy(evnt);
hide_details_info();
generate_right_click_menu(item_owner_id, elem_x, elem_y);
}

function add_seperator()
{
var str = '';
str += "<tr rowspan=\"1\">\n";
str += "	<td align=\"center\" valign=\"top\">\n";
str += "		<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\" width=\"100%\">\n";
str += "			<tr>\n";
str += "				<td width=\"5\">\n";
str += "				&nbsp\n";
str += "				</td>\n";
str += "				<td>\n";
str += "				<hr>\n";
str += "				</td>\n";
str += "				<td width=\"5\">\n";
str += "				&nbsp\n";
str += "				</td>\n";
str += "			</tr>\n";
str += "		</table>\n";
str += "	</td>\n";
str += "</tr>\n";
return str;
}

function string_replace(str, search, replace)
{
	str = str.split(search).join(replace);
	return str;
}

function add_menu_row(title_id, title_str, help_str,click_func, keep_format)
{
var str;
keep_format *= 1;
if(keep_format == 0)
	{
	title_str = string_replace(title_str, " ", "&nbsp;");
	}

str  = '				<tr >\n';
str += '					<td height="17" align="left" valign="top">\n';
str += '						<table style="border: 1px solid '+theme_color+'; font-family: arial; font-size: 10pt" title="'+help_str+'" style="cursor: pointer; cursor: hand; border: 1px solid '+theme_color+';" onmouseover="mouse_over_menulink(this)" onmouseup="'+click_func+'('+title_id+'); hide_all_menu(); hide_all_popup_menu();"  onmouseout="mouse_out_menulink(this)" border="0" cellspacing="0" cellpadding="0" width="100%" height="100%">\n';
str += '							<tr>\n';

str += '								<td style="cursor: pointer; cursor: hand;" width="100%" align="left" valign="middle">';
if(keep_format == 0)
	{
	str += '&nbsp;&nbsp;';
	}
str += title_str;
if(keep_format == 0)
	{
	str += '&nbsp;&nbsp;';
	}
str += '</td>\n';

str += '							</tr>\n';
str += '						</table>\n';
str += '					</td>\n';
str += '				</tr>\n';
return str;
}

function generate_right_click_menu(item_owner_id, x, y)
{
close_right_click_menu = false;
var elem = document.getElementById('right_click_menu');
if(elem == null)
	return;
var str = '<table style="border: 1px outset white; font-family: arial; font-size: 10pt" border="0" bgcolor="'+theme_color+'" cellspacing="0" cellpadding="0" width="100" height="100%">\n';
str += "<th style=\"color: black; background-color: #88AACC\">"
str += '<table style="font-size: 10pt;" border="0" cellspacing="0" cellpadding="2" width="100%" height="100%"><tr>';
str += '<td align="center" valign="middle" width="100%" height="100%"><b>Actions</b></td>';
str += '<td align="center" valign="middle" width="100%" height="100%">';
str += '<img title="Close" onclick="hide_all_popup_menu();" src="'+base_url+'/_images/comment_close.gif" style="cursor: pointer; cursor: hand;">';
str += '</td></tr></table>';
str +="</th>";


str +=	add_menu_row(item_owner_id, "<b>Open&nbsp;catalog</b>","Open the cataloging information", "open_link", 0);

var arr = get_item_info_from_item_owner_id(item_owner_id);
if(arr == null)
	return;
var detail_url = arr['detail_url'];
media_str = media_str_names[arr['medium']];
var index = detail_url.indexOf("/_user_manual_file/");
if(index != -1)
	{
	//detail_url = base_url + detail_url.substring(index);
	//Open only happens if this is a soft document.
	str +=	add_menu_row(item_owner_id, "Open&nbsp;document","Open the soft copy", "open_item_from_item_owner_id", 0);
	}
if(owner == true)
	{
	if(req_shelf_id != recommendation_list_shelf_id && req_shelf_id != trash_can_shelf_id && req_shelf_id != borrow_list_shelf_id && arr['borrow_status'] != 6 && arr['borrow_status'] != 3)
		{
		//Delete option is only allowed if this is not borrowed from some one else
		str +=	add_menu_row(item_owner_id, "Delete","Move this title to Trash Can", "delete_item_from_item_owner_id", 0);
		}

	if(req_shelf_id != recommendation_list_shelf_id && req_shelf_id != trash_can_shelf_id && req_shelf_id != wish_list_shelf_id && req_shelf_id != borrow_list_shelf_id)
		{
		/*if(arr['borrow_status'] == 7 || arr['borrow_status'] == 0)
			{
			str +=	add_menu_row(item_owner_id, "Set as borrowed...","Set this title as borrowed", "set_borrow_item_from_item_owner_id", 0);
			}
		else */
		if(arr['borrow_status'] == 1 || arr['borrow_status'] == 2)
			{
			str +=	add_menu_row(item_owner_id, "Set as received","Set this borrowed title as received", "set_received_item_from_item_owner_id", 0);
			}
		else if(arr['borrow_status'] != 3 && arr['borrow_status'] != 6)
			{
			str +=	add_menu_row(item_owner_id, "Set as borrowed\\lent...","Set this title as borrowed or lent", "set_borrow_item_from_item_owner_id", 0);
			}
		else if(arr['borrow_status'] == 3)
			{
			str +=	add_menu_row(item_owner_id, "Set as returned","Set this title as returned to the owner", "set_returned_item_from_item_owner_id", 0);
			}	
		}
	}
else
	{
	if(req_shelf_id != recommendation_list_shelf_id && req_shelf_id != trash_can_shelf_id && req_shelf_id != wish_list_shelf_id && req_shelf_id != borrow_list_shelf_id && logged_in)
		{
		//var arr = get_item_info_from_item_owner_id(item_owner_id);
		if((arr['borrow_status'] != 3) && (arr['borrow_status'] != 6))
			{
			str +=	add_menu_row(item_owner_id, "Send request to borrow...","Send a request to borrow this title", "request_borrow_item_from_item_owner_id", 0);
			}
		}
	}

if(owner)
	{
	if(req_shelf_id != recommendation_list_shelf_id && req_shelf_id != trash_can_shelf_id)
		{
		str +=	add_menu_row(item_owner_id, "Create duplicate","Create a duplicate of this title", "duplicate_item", 0);
		}
	var temp = generate_sub_menu("Move to shelf", "shelf_menu", item_owner_id, "open_shelf_list");
	//str +=	add_menu_row(item_owner_id, temp,"Click here to open your list of shelves", "open_shelf_list", 1);
	 str += temp;
	}
else if(logged_in)
	{
	var temp = generate_sub_menu("Add to my shelf", "shelf_menu", item_owner_id, "open_shelf_list");
	//str +=	add_menu_row(item_owner_id, temp,"Click here to open your list of shelves", "open_shelf_list", 1);
	 str += temp;
	}


str += "<tr><td align=\"right\" valign=\"bottom\" width=\"100%\" height=\"1\"><span id=\"right_menu_bottom_right\" style=\"width: 1px; height: 1px; font-size: 1pt; display: block;\"></span></td></tr>";
str += "</table>\n";

elem.innerHTML = str;
elem.style.visibility="visible";
elem.style.left = x;
elem.style.top = y;
set_right_click_menu_pos();
}

function generate_sub_menu(menu_name, menu_id, id, click_func)
{
//return '';
menu_name = string_replace(menu_name, " ", "&nbsp;");
var temp =  '<tr><td><table onmouseover="mouse_over_menulink(this);" onmouseout="mouse_out_menulink(this);" onmouseup="check_for_right_click_abort(this, event); '+click_func+'('+id+');" style="cursor: pointer; cursor: hand; border: 1px solid '+theme_color+'; color: '+font_color+'; font-family: arial; font-size: 10pt" border="0" cellspacing="2" cellpadding="0" width="100%"><tr>';
	temp += '<td align="left" valign="middle"><img style="visibility: hidden" id="'+menu_id+'_left_arrow" src="'+left_arrow_img.src+'"></td>';
	temp += '<td align="left" valign="middle">'+menu_name+'</td>';
	temp += '<td align="right" valign="middle"><img style="visibility: hidden" id="'+menu_id+'_right_arrow" src="'+right_arrow_img.src+'"></td>';
	temp += '</tr></table></td></tr>';
return temp;
}

function open_shelf_list(item_owner_id)
{
var elem = document.getElementById('shelf_list_menu');
if(elem == null)
	return;
if(elem.style.visibility.toLowerCase() != 'visible')
	{
	generate_shelf_menu(1);
	}
else
	{
	hide_shelf_list_menu();
	}	
}

function shelf_sort_menu_clicked(sort_type)
{
if(prev_shelf_sort_type == shelf_sort_type)
	{
	shelf_sort_direction = (shelf_sort_direction == 0)?1:0;
	}
else
	{
	shelf_sort_direction = 1;
	}	
shelf_sort_type = sort_type;
show_loading();
save_session_info();
hide_all_menu();
hide_all_popup_menu();
setTimeout('shelf_sort_menu_clicked_timeout()',10);
}

function show_sort_shelf()
{

owner_shelf_array.sort(sort_shelf_array);
display_shelves();

var elem = document.getElementById('shelf_sort_menu_'+prev_shelf_sort_type);
if(elem != null)
	{
	elem.innerHTML = "";
	}
elem = document.getElementById('shelf_sort_menu_direction_'+prev_shelf_sort_type);
if(elem != null)
	{
	elem.innerHTML = "";
	}

elem = document.getElementById('shelf_sort_menu_'+shelf_sort_type);
if(elem != null)
	{
	elem.innerHTML = "<img  border=\"0\" src=\""+base_url+"/_images/tick.gif\">";
	}
elem = document.getElementById('shelf_sort_menu_direction_'+shelf_sort_type);
if(elem != null)
	{
	var img = (shelf_sort_direction == 0)?(base_url+"/_images/sort_up.gif"):(base_url+"/_images/sort_down.gif");
	elem.innerHTML = "<img src=\""+img+"\">";
	}
prev_shelf_sort_type = shelf_sort_type;
}

function shelf_sort_menu_clicked_timeout()
{
show_sort_shelf();
if(req_shelf_id != 0)
	{
	set_shelf(req_shelf_id, -1);
	}

hide_loading();
}

function display_shelves()
{
var count = owner_shelf_array.length;
var i;
var shelf_name;
var shelf_id;
var private_shelf;
var shelf_type;
var elem;
var str = '';
var table_filled = false;
str += '<table border="0" cellspacing="0" cellpadding="0" width="100%">';
for(i = 0; i < count; ++i)
	{
	shelf_name = owner_shelf_array[i]['name'];
	private_shelf = owner_shelf_array[i]['private'];
	shelf_id = owner_shelf_array[i]['id'];
	shelf_type = owner_shelf_array[i]['type'];
	if(shelf_type == 0)
		{
		if(private_shelf == false || (private_shelf && owner))
			{
			str += generate_shelf_info(shelf_name, shelf_id, private_shelf);
			}
		table_filled = true;
		}
	}
str += '</table>';
elem = document.getElementById('shelf_space');
if(elem != null)
	{
	if(table_filled)
		{
		elem.innerHTML = str;
		}
	else
		{
		elem.innerHTML = '';
		}	
	}	
}

function sort_shelf_array(a, b)
{
var ret;
switch(shelf_sort_type)
	{
	case SHELF_NAME_SORT:
		var str1 = a['name'];
		var str2 = b['name'];
		str1 = str1.toLowerCase();
		str2 = str2.toLowerCase();
		if(str1 < str2)
			{
			ret =  -1;
			}
		else if(str1 > str2)			
			{
			ret = 1;
			}
		else
			{
			ret = 0;
			}	
		break;
	
	case SHELF_ADD_DATE_SORT:
		ret = a['id'] - b['id'];
		break;	
	
	default:
		ret = 0;
		break;
	}
if(shelf_sort_direction == 0)
	{
	ret *= -1;
	}
return ret;
}

function duplicate_item(item_owner_id)
{
var arr = get_item_info_from_item_owner_id(item_owner_id);
if(arr == null)
	return;
var title = arr['title'];
var shelf_name = arr['shelf_name'];
var shelf = get_shelf_info_from_shelf_name(shelf_name);
var msg = '"'+title+"\" will be added as a duplicate entry in shelf "+shelf_name+"";
window_confirm("Create Duplicate Item", msg, "create_duplicate_item()", null);
}

function create_duplicate_item()
{
var current_item_owner_id = get_current_item_owner_id();
var arr = get_item_info_from_item_owner_id(current_item_owner_id);
if(arr == null)
	return;
var title = arr['title'];
var shelf_name = arr['shelf_name'];
var shelf = get_shelf_info_from_shelf_name(shelf_name);
add_item_to_shelf(current_item_owner_id, shelf['id'], 1);
}

function generate_shelf_menu(mode)
{
var elem = document.getElementById('right_click_menu');
if(elem != null)
	{
	elem.style.visibility = 'visible';
	}

elem = document.getElementById('shelf_list_menu');
if(elem == null)
	return;
if(mode == 1)
	{
	close_shelf_list_menu = false;
	//setTimeout("close_shelf_list_menu = true", 500);
	elem.style.visibility = 'visible';
	show_shelf_menu_arrow(3);
	return;
	}

var elem1 = document.getElementById('shelf_menu_right_arrow');
if(elem1 == null)
	return;
var x = findPosX(elem1);
var y = findPosY(elem1);

var str = generate_shelf_list(0, get_current_item_owner_id());
elem.innerHTML = str;
elem.style.left = x + 10;
elem.style.top = y;
elem.style.visibility="visible";

set_shelf_menu_pos();
}

function show_shelves(obj, item_owner_id)
{
var elem = document.getElementById('shelf_list_menu');
if(elem == null)
	return;
close_all_edit_this_text();
close_header_config_menu();
close_header_right_click_menu();
hide_right_click_menu();
if(elem.style.visibility.toLowerCase() == 'visible' && get_current_item_owner_id() == item_owner_id)
	{
	//hide_shelf_list_menu();
	hide_all_popup_menu();
	return;
	}
select_item(true, item_owner_id);
var str = generate_shelf_list(1, item_owner_id);
elem.innerHTML = str;
elem.style.left = findPosX(obj) + 'px';
elem.style.top = (findPosY(obj) + 30) + 'px';
elem.style.visibility="visible";
}

function generate_shelf_list(mode, item_owner_id)
{
var str = '<table style="border: 1px outset white; font-family: arial; font-size: 10pt" border="0" bgcolor="'+theme_color+'" cellspacing="0" cellpadding="0" width="100" height="100%">\n';
/*if(mode == 0)
	{
	str += "<th style=\"color: black; background-color: #88AACC\">Shelf&nbsp;List</th>";
	}
else*/
	{
	str += "<th style=\"color: black; background-color: #88AACC\">"
	str += '<table style="font-size: 10pt;" border="0" cellspacing="0" cellpadding="2" width="100%" height="100%"><tr>';
	str += '<td align="center" valign="middle" width="100%" height="100%"><b>Shelf&nbsp;List</b></td>';
	str += '<td align="center" valign="middle" width="100%" height="100%">';
	str += '<img title="Close" onmouseup="hide_shelf_list_menu();" src="'+base_url+'/_images/comment_close.gif" style="cursor: pointer; cursor: hand;">';
	str += '</td></tr></table>';
	str +="</th>";
	}	
var arr = get_item_info_from_item_owner_id(item_owner_id);
if(arr == null)
	return;
var i;
var temp_str = '';
var shelf_add_count = 0;
for(i = 0; i < shelf_array.length; ++i)
	{
	var shelf_id = shelf_array[i]['id'];
	var shelf_name = shelf_array[i]['name'];
	var private_shelf = shelf_array[i]['private'];
	var shelf_type = shelf_array[i]['type'];
	var img = '';
	//alert(shelf_id + "," + shelf_name + "," + private_shelf + ',' + shelf_type);
	if(shelf_type == 4)
		{
		//recommendation shelf;
		continue;
		}

	if(owner)
		{
		if(shelf_name == arr['shelf_name'])
			{
			continue;
			}
		}
	shelf_name = string_replace(shelf_name, " ", "&nbsp;");
	if(shelf_type == 0)	//normal shelf
		{
	 	img = base_url+"/_images/shelf.gif";
		}
	else if(shelf_type == 1) //trash can shelf
		{
		continue;
		}
	else if(shelf_type == 2) //wish list shelf
		{
		img = base_url+"/_images/wish.gif";
		if(shelf_add_count > 0)
			{
			temp_str += add_seperator();
			}
		}
	else if(shelf_type == 3) //borrowed shelf
		{
		continue;
		}
	else if(shelf_type == 4) //recommendation shelf
		{
		continue;
		}



	var temp =  '<tr><td><table onmouseup="add_move_to_shelf('+shelf_id+');hide_all_menu(); hide_all_popup_menu();" onmouseover="mouse_over_menulink(this);" onmouseout="mouse_out_menulink(this);" style="cursor: pointer; cursor: hand; display: block; border: 1px solid '+theme_color+'; color: '+font_color+'; font-family: arial; font-size: 10pt" border="0" cellspacing="2" cellpadding="0" width="100%" ><tr>';
	temp += '<td align="left" valign="top"><img height="20" src="'+img+'">';
	if(private_shelf == 1)
		{
		temp += '<img style="position: relative; left: 0; top: -10" border="0" src="'+base_url+'/_images/lock.gif" height="15">';
		}
	temp += '</td>';
	temp += '<td width="100%" align="left" valign="middle">'+shelf_name+'</td>';
	temp += '</tr></table></td></tr>';

	if(shelf_type != 2)
		{
		str +=	temp;//add_menu_row(shelf_id, temp,"", "add_move_to_shelf", 1);
		}
	else
		{
		temp_str +=	temp;//add_menu_row(shelf_id, temp,"", "add_move_to_shelf", 1);
		}

	shelf_add_count++;
	}

str += temp_str;

str += "<tr><td align=\"right\" valign=\"bottom\" width=\"100%\" height=\"1\"><span id=\"shelf_menu_bottom_right\" style=\"width: 1px; height: 1px; font-size: 1pt; display: block;\"></span></td></tr>";
str += "</table>\n";

return str;
}

function request_cloud_info(cloud_type)
{
if(is_ajax_object_active(main_ajax_id) == false)
	{
	var args = 						{	
												'action'						: 		'get_cloud_info', 
												'cloud_type'				:			cloud_type,
												'userid'						: 		current_userid,
												'media_id'					:			req_media_id,
												'shelf_id'					:			req_shelf_id,
												'search_data'				:			current_search_data,
												'search_criterion'	:			current_search_criterion,
												'view'							:			user_view_type,												
												'random'						:			Math.random()
												};
	main_ajax_id = ajax_get_request(ajax_request_url, args, get_cloud_info_response);
	}
}

function get_cloud_info_response(response_array)
{
if(response_array[0] == "error")
	{
	current_cloud_type = -1;
	show_alert_dialog("Error", response_array[1]);	
	}
else 
	{
	update_cloud_info(response_array);
	}
}

function update_cloud_info(response_array)
{
var cloud_type = current_cloud_type;
var count = response_array[0]*1;
var count1;
var i;
var str = '';
var str1;
var genre_name;
var count1;
var genre_id;
var cloud_head = '';

switch(req_media_id)
	{
	case BOOKS:
		switch(cloud_type)
			{
			case GENRE_CLOUD:
				cloud_head = "Genre";
				break;
				
			case AUTHOR_CLOUD:
				cloud_head = "Author";
				break;
			}
		break;
	case MOVIES:
		switch(cloud_type)
			{
			case GENRE_CLOUD:
				cloud_head = "Genre";
				break;
				
			case AUTHOR_CLOUD:
				cloud_head = "Actor";
				break;
				
			case DIRECTOR_CLOUD:
				cloud_head = "Director";
				break;
			}
		break;

	case MUSIC:
		switch(cloud_type)
			{
			case GENRE_CLOUD:
				cloud_head = "Genre";
				break;
				
			case AUTHOR_CLOUD:
				cloud_head = "Artist";
				break;
			}
		break;
	default:
		cloud_head = "Genre";
		break;
	}
cloud_head = "&nbsp;"+cloud_head+"&nbsp;Cloud&nbsp;&nbsp;";
document.getElementById('cloud_head_area').innerHTML = cloud_head;	
for(i = 0; i < count; ++i)
	{
	genre_name = (response_array[i*3 + 0 + 1]);
	genre_id = (response_array[i*3 + 1 + 1])*1;
	count1 = response_array[i*3 + 2 + 1]*1 + 1;
	genre_name = (genre_name);
	str1 = '<a href="javascript:fetch_cloud_items('+genre_id+');" style="color: black; font-family: arial; font-size: '+count1+'pt;">'+genre_name+'</a> ';
	str += str1;
	}

var obj = document.getElementById('cloud_data_area');
if(obj == null)
	return;
obj.innerHTML = str;
cloud_area_height = get_cloud_height();
if(cloud_area_slide.end_height != cloud_area_height)
	{	
	cloud_area_slide.set_height(cloud_area_height, vertical_slide_opening);
	}
}

function get_cloud_height()
{
var obj = document.getElementById('cloud_display_area');
var obj1 = document.getElementById('cloud_display_area_bottom');
if(obj == null)
	return;
var str = obj.style.display;
obj.style.display = '';
var y = findPosY(obj);
y =  (findPosY(obj1) - y);
obj.style.display = str;
return y;
}

function fetch_cloud_items(search_id)
{
var str = "cloud:"+cloud_str[current_cloud_type]+"_"+search_id;
var elem = document.getElementById('search_text');
if(elem == null)
	{
	return;
	}
elem.focus();	
elem.value = str;
search_item(0);
}

function add_move_to_shelf(shelf_id)
{
var msg;
var header;
var func = null;
window_confirm_args = new Array();
window_confirm_args[0] = shelf_id;
window_confirm_args[1] = 0;

var arr = get_item_info_from_item_owner_id(get_current_item_owner_id());
if(arr == null)
	return;
var title = arr['title'];
var shelf = get_shelf_info_from_shelf_id(shelf_id);
var shelf_name = (shelf['name']);

if(owner)
	{
	func = "move_current_item()";
	msg = '"'+title+'" will be moved to shelf "'+shelf_name+'"';
	header = "Move Items To Shelf";
	}
else if(logged_in)
	{
	func = "add_current_item_to_shelf()";
	msg = '"'+title+'" will be added to your shelf "'+shelf_name+'"';
	header = "Add Item To Shelf";
	}
if(func != null)
	{
	window_confirm(header, msg, func, null);
	}
}

function move_current_item()
{
var shelf_id = window_confirm_args[0]*1;
var arr = get_item_info_from_item_owner_id(get_current_item_owner_id());
if(arr == null)
	return;
var title = arr['title'];
var shelf = get_shelf_info_from_shelf_id(shelf_id);
var shelf_name = (shelf['name']);
if(owner)
	{
	move_item(get_current_item_owner_id(), shelf['id'],shelf_name);
	}
}

function add_current_item_to_shelf()
{
var shelf_id = window_confirm_args[0]*1;
var forceful = window_confirm_args[1]*1;
var arr = get_item_info_from_item_owner_id(get_current_item_owner_id());
if(arr == null)
	return;
var title = arr['title'];
var shelf = get_shelf_info_from_shelf_id(shelf_id);
if(logged_in)
	{
	add_item_to_shelf(get_current_item_owner_id(), shelf['id'], forceful);
	}
}

function add_item_to_shelf(item_owner_id, shelf_id, forceful)
{
var args = 						{	
											'action'						: 		'add_item_to_my_shelf', 
											'item_owner_id'			:			item_owner_id,
											'shelf_id'					:			shelf_id,
											'forceful'					:			forceful,												
											'random'						:			Math.random()
											};
ajax_get_request(ajax_request_url, args, add_item_to_my_shelf_response);
}

function add_item_to_my_shelf_response(response_array)
{
if(response_array[0] == "error")
	{
	show_alert_dialog("Error", response_array[1]);
	}
else if(response_array[0] == "suggestion")
	{
	var title = response_array[1];
	var shelf_name = response_array[2];
	var item_owner_id = response_array[3]*1;
	var shelf_id = response_array[4]*1;
	var msg = '"'+title+'" is already cataloged under shelf "'+shelf_name+'". This will add a duplicate copy';
	var header = "Add Item To Shelf";
	window_confirm_args = new Array();
	window_confirm_args[0] = shelf_id;
	window_confirm_args[1] = 1;
	window_confirm(header, msg, "add_current_item_to_shelf()", null)
	}
else
	{
	if(owner)
		{
		//refresh the current view
		fetch_item_data();
		}
	}
}

function set_returned_item_from_item_owner_id(item_owner_id)
{
var arr = get_item_info_from_item_owner_id(item_owner_id,'borrow_item_returned');
if(arr == null)
	return;
var title = arr['title'];
var msg = '"'+title+"\" will be set as returned";
var header = "Item Returned";
window_confirm_args = new Array();
window_confirm_args[0] = item_owner_id;
window_confirm_args[1] = 0;
window_confirm(header, msg, "set_returned_received_item_from_item_owner_id_confirm()", null);
}

function set_returned_received_item_from_item_owner_id_confirm()
{
var item_owner_id = window_confirm_args[0]*1;
var type_id = window_confirm_args[1];
var type = (type_id == 1)?('borrowed_item_received'):('borrowed_item_returned');

var args = 						{	
											'action'						: 		type, 
											'item_owner_id'			:			item_owner_id,
											'random'						:			Math.random()
											};
ajax_get_request(ajax_request_url, args, borrowed_item_returned_response);
}

function borrowed_item_returned_response(response_array)
{
if(response_array[0] == "error")
	{
	show_alert_dialog("Error", response_array[1]);
	}
else
	{	
	var item_owner_id = response_array[1]*1;
	update_borrow_item_status(item_owner_id, 0, 0, '', '', '', '');
	}
}

function set_received_item_from_item_owner_id(item_owner_id)
{
var arr = get_item_info_from_item_owner_id(item_owner_id);
if(arr == null)
	return;
var title = arr['title'];
var msg = '"'+title+"\" will be set as received";
var header = "Item Received";
window_confirm_args = new Array();
window_confirm_args[0] = item_owner_id;
window_confirm_args[1] = 1;
window_confirm(header, msg, "set_returned_received_item_from_item_owner_id_confirm()", null);
}

function open_item_from_item_owner_id(item_owner_id)
{
var arr = get_item_info_from_item_owner_id(item_owner_id);
if(arr == null)
	return;
var detail_url = arr['detail_url'];
var index = detail_url.indexOf("/_user_manual_file/");
if(index != -1)
	{
	detail_url = base_url + detail_url.substring(index);
	}
window.open(detail_url, "_blank");
}

function delete_item_from_item_owner_id(item_owner_id)
{
var arr = get_item_info_from_item_owner_id(item_owner_id);
if(arr == null)
	return;
var title = arr['title'];
var msg = "\""+title+"\" will be moved to Trash Can";
var header = "Delete Item";
window_confirm_args = new Array();
window_confirm_args[0] = item_owner_id;
window_confirm(header, msg, "delete_item_from_item_owner_id_confirm()", null);
}

function delete_item_from_item_owner_id_confirm()
{
var item_owner_id = window_confirm_args[0]*1;
move_item(item_owner_id, trash_can_shelf_id, 'Trash Can');
}

function window_open(link_id, target_type)
{
//this is done to prevent the detail information page from being opened during
//multiple selection operation
if(select_bar_obj != null)
	return;

show_loading();
setTimeout("window.open('"+link_id+"','"+ target_type+"')",100);
}

function set_borrow_item_from_item_owner_id(item_owner_id)
{
show_set_borrowed_item(item_owner_id);
}

function request_borrow_item_from_item_owner_id(item_owner_id)
{
if(logged_in)
	{
	show_borrow_box(item_owner_id);
	}
else
	{
	var msg = "You will have to login to GuruLib to send a request to borrow this title";
	var header = "Login To GuruLib";
	window_confirm(header, msg, "login_to_gurulib_confirm()", null);	
	}
}

function login_to_gurulib_confirm()
{
window.open(base_url+"/index.php?redirect="+(current_username), '_self')
}

function add_to_wish_list_from_item_owner_id(item_owner_id)
{
if(logged_in)
	{
	var arr = get_item_info_from_item_owner_id(item_owner_id);
if(arr == null)
	return;
	var title = arr['title'];
	window_confirm_args = new Array();
	window_confirm_args[0] = item_owner_id;
	var msg = "\""+title+"\" will be added to your wish list";
	var header = "Add To WishList";
	window_confirm(header, msg, "add_to_wishlist_confirm()", null);
	}
else
	{
	var msg = "You will have to login to GuruLib to add this title to your wish list";
	var header = "Login To GuruLib";
	window_confirm(header, msg, "login_to_gurulib_confirm()", null);
	}
}

function add_to_wishlist_confirm()
{
var args = 						{	
											'action'						: 		'add_to_wish_list', 
											'item_owner_id'			:			item_owner_id,
											'random'						:			Math.random()
											};
ajax_get_request(ajax_request_url, args, add_to_wish_list_response);
}

function add_to_wish_list_response(response_array)
{
if(response_array[0] == "error")
	{
	show_alert_dialog("Error", response_array[1]);
	}
}

function set_right_click_menu_pos()
{
var elem = document.getElementById('right_menu_bottom_right');
if(elem == null)
	return;
var elem1 = document.getElementById('right_click_menu');
if(elem1 == null)
	return;

var x1 = findPosX(elem);
var y1 = findPosY(elem);
var x  = findPosX(elem1);
var y  = findPosY(elem1);

if(x1 > avail_width - 10)
	{
	x = 2*x - x1 + 10;
	}
else
	{
	x -= 10;
	}

if(y1 + banner_header_size > avail_height)
	{
	y = 2*y - y1 + 10;
	}
else
	{
	y -= 10;
	}

elem1.style.left = x;
elem1.style.top = y;
//close_right_click_menu = false;
generate_shelf_menu(0);
}

function set_shelf_menu_pos()
{
var shelf_menu_arrow = 0;
var elem2 = document.getElementById('right_menu_bottom_right');
if(elem2 == null)
	return;
var elem3 = document.getElementById('right_click_menu');
if(elem3 == null)
	return;

var x1 = findPosX(elem3);
var y1 = findPosY(elem3);
var x  = findPosX(elem2);
var y  = findPosY(elem2);

var wide = x - x1;
var high = y - y1;

var elem = document.getElementById('shelf_menu_bottom_right');
if(elem == null)
	return;
var elem1 = document.getElementById('shelf_list_menu');
if(elem1 == null)
	return;

x1 = findPosX(elem);
y1 = findPosY(elem);
x  = findPosX(elem1);
y  = findPosY(elem1);

if(x1 > avail_width - 10)
	{
	x = 2*x - x1 + 10 - wide;
	shelf_menu_arrow = 1;
	}
else
	{
	x -= 5;
	shelf_menu_arrow = 2;
	}

if(y1 + banner_header_size > avail_height)
	{
	y = 2*y - y1;
	if(y < 0)
		{
		y = 0;
		elem1.style.height = avai_height - 20;
		elem1.style.overflow = 'scroll';
		}
	else
		{
		elem1.style.overflow = 'hidden';
		}
	}
else
	{
	y -= 5;
	elem1.style.overflow = 'hidden';
	}

elem1.style.left = x;
elem1.style.top = y;
elem1.style.visibility = 'hidden';
show_shelf_menu_arrow(shelf_menu_arrow);
}

function show_shelf_menu_arrow(shelf_menu_arrow)
{
var elem;
if(shelf_menu_arrow == 2)
	{
	elem = document.getElementById('shelf_menu_right_arrow');
	if(elem != null)
		{
		elem.style.visibility = 'visible';
		//elem.style.display = 'block';
		}
	elem = document.getElementById('shelf_menu_left_arrow');
	if(elem != null)
		{
		elem.style.visibility = 'hidden';
		//elem.style.display = 'block';
		}
	}
else if(shelf_menu_arrow == 1)
	{
	elem = document.getElementById('shelf_menu_left_arrow');
	if(elem != null)
		{
		elem.style.visibility = 'visible';
		//elem.style.display = 'block';
		}
	elem = document.getElementById('shelf_menu_right_arrow');
	if(elem != null)
		{
		elem.style.visibility = 'hidden';
		//elem.style.display = 'none';
		}
	}
else if(shelf_menu_arrow == 0)
	{
//	alert('here');
	elem = document.getElementById('shelf_menu_left_arrow');
	if(elem != null)
		{
		elem.style.display = 'none';
		}
	elem = document.getElementById('shelf_menu_right_arrow');
	if(elem != null)
		{
		elem.style.display = 'none';
		}
	}
else if(shelf_menu_arrow == 3)
	{
	elem = document.getElementById('shelf_menu_left_arrow');
	if(elem != null)
		{
		elem.style.display = 'block';
		}
	elem = document.getElementById('shelf_menu_right_arrow');
	if(elem != null)
		{
		elem.style.display = 'block';
		}
	}
}

function try_hide_right_click_menu()
{
if(close_right_click_menu == false)
	return;
hide_right_click_menu();
}

function try_hide_shelf_list_menu()
{
if(close_shelf_list_menu == false)
	return;
hide_shelf_list_menu();
}

function hide_right_click_menu()
{
var elem = document.getElementById('right_click_menu');
if(elem == null)
	return;
elem.style.visibility = "hidden";
item_clicked = false;
show_shelf_menu_arrow(0);

}


function get_item_info_from_item_owner_id(search_item_owner_id)
{
var item_count = 0;
for(item_count = 0; item_count < query_result_count; ++item_count)
	{
	var medium 					= item_result_array[item_count*row_item_count + 0]*1;
	var img_url 				= item_result_array[item_count*row_item_count + 1];
	var item_owner_id 	= item_result_array[item_count*row_item_count + 2]*1;
	var	title_str 			= (item_result_array[item_count*row_item_count + 3]);
	var	publisher 			= (item_result_array[item_count*row_item_count + 4]);
	var	rating 					= item_result_array[item_count*row_item_count + 5];
	var	released_date 	= item_result_array[item_count*row_item_count + 6];
	var	added_date_ext 	= item_result_array[item_count*row_item_count + 7];
	var	mpaa_rating_url = item_result_array[item_count*row_item_count + 8];
	var	authors 				= (item_result_array[item_count*row_item_count + 9]);
	var	directors 			= (item_result_array[item_count*row_item_count + 10]);
	var	genres 					= (item_result_array[item_count*row_item_count + 11]);
	var	user_rating 		= item_result_array[item_count*row_item_count + 12];
	var best_price 			= item_result_array[item_count*row_item_count + 13]*1.0;
	var	new_price 			= item_result_array[item_count*row_item_count + 14]*1.0;
	var	borrow_status 	= item_result_array[item_count*row_item_count + 15]*1;
	var	platform 				= (item_result_array[item_count*row_item_count + 16]);
	var	shelf_name 			= (item_result_array[item_count*row_item_count + 17]);
	var borrower_id			= item_result_array[item_count*row_item_count + 18]*1;
	var detail_url			= (item_result_array[item_count*row_item_count + 25]);
	if(search_item_owner_id == item_owner_id)
		{
		var arr = new Array();
		arr['medium'] 					= medium;
		arr['image']  					= img_url;
		arr['item_owner_id'] 		= item_owner_id;
		arr['title'] 						= title_str;
		arr['publisher'] 				= publisher;
		arr['rating'] 					= rating;
		arr['release_date'] 		= released_date;
		arr['added_date']	 			= added_date_ext;
		arr['mpaa'] 						= mpaa_rating_url;
		arr['authors'] 					= authors;
		arr['directors'] 				= directors;
		arr['genres'] 					= genres;
		arr['user_rating'] 			= user_rating;
		arr['platform'] 				= platform;
		arr['shelf_name'] 			= shelf_name;
		arr['borrow_status'] 		= borrow_status;
		arr['best_price'] 			= best_price;
		arr['new_price'] 				= new_price;
		arr['borrower_id']			= borrower_id;
		arr['detail_url']				= detail_url;
		return arr;
		}
	}
return null;
}

function get_item_index_from_item_owner_id(search_item_owner_id)
{
var item_count = 0;
for(item_count = 0; item_count < query_result_count; ++item_count)
	{
	var item_owner_id 	= item_result_array[item_count*row_item_count + 2]*1;
	if(search_item_owner_id == item_owner_id)
		{
		return item_count;
		}
	}
return -1;
}

function close_shelf_box()
{
var elem1 = document.getElementById('shelf_box');
if(elem1 == null)
	return;
elem1.style.visibility = 'hidden';
hide_color_chooser();
disable_screen(false);
}

function show_add_new_shelf_box()
{
hide_all_popup_menu();
hide_all_menu();

var elem1 = document.getElementById('shelf_box');
if(elem1 == null)
	return;
disable_screen(true);
var left1 = (screen.availWidth - 360)/2;
var top1 = (screen.availHeight - 210)/2 + document.body.scrollTop;
elem1.style.visibility = 'visible';
elem1.style.left = left1;
elem1.style.top = top1;
var add_button = document.getElementById('add_shelf_button');

document.getElementById('private_shelf_id').checked = false;
document.getElementById('public_shelf_id').checked = true;
document.getElementById('shelf_header').innerHTML = 'Add&nbsp;New&nbsp;Shelf';		
document.getElementById('color_name').innerHTML = color_table_array[1]['name'];
document.getElementById('color_value').src = color_table_array[1]['path'];
add_button.innerHTML = 'Add';
document.getElementById('private_shelf_id').disabled = false;
document.getElementById('public_shelf_id').disabled = false;
add_button.onclick = add_shelf;

elem1 = document.getElementById('shelf_name_input');
if(elem1 != null)
	{
	elem1.disabled = false;
	elem1.value = '';
	elem1.focus();
	}
}

function close_set_borrow_edit_box()
{
var elem1 = document.getElementById('borrower_email_row');
if(elem1 == null)
	return;
elem1.style.visibility = 'hidden';
document.getElementById('set_borrow_edit_box').style.visibility='hidden';
disable_screen(false);
}

function change_borrow_date(days, set_obj_id)
{
var months       = ['Jan','Feb','Mar','Apr','May','Jun',
                                       'Jul','Aug','Sep','Oct','Nov','Dec'];
days = days*1;
//var elem = document.getElementById('return_date');
var elem = document.getElementById(set_obj_id);
if(elem == null)
	return;
var now  = new Date();
//elem.value =
if(days!=30)
	{
	now.setDate(now.getDate()+days);
	}
else
	{
	now.setMonth(now.getMonth()+1);
	}
elem.value = months[now.getMonth()] + " " + now.getDate().toString() + ", " + now.getFullYear().toString();
}

function change_who_borrowed(val)
{
var elem = document.getElementById('borrower_type_str');
var elem1 = document.getElementById('borrower_type');
var elem2 = document.getElementById('borrower_email_str');
var elem3 = document.getElementById('borrower_type_select');
var elem4 = document.getElementById('message_to_borrower');
var elem5 = document.getElementById('set_borrow_button_id');
var elem6 = document.getElementById('borrow_dialog_head');
var elem7 = document.getElementById('borrower_email_row');
var elem8 = document.getElementById('borrower_email');
if(elem == null || elem1 == null || elem2 == null || elem3 == null || elem4 == null || elem5 == null || elem6 == null || elem7 == null || elem8 == null)
	return;
if(val == 0)
	{
	//This is a title borrowed by this gurulib user
	elem3.disabled = true;
	elem3.value = 1;
	elem3.selectedIndex = 1;
	elem.innerHTML = "Is&nbsp;this&nbsp;"+media_str+"'s&nbsp;owner&nbsp;a&nbsp;GuruLib&nbsp;user?";
	if(elem3.value == 0)
		{
		elem1.innerHTML = "Owner's&nbsp;GuruLib&nbsp;username";
		}
	else
		{
		elem1.innerHTML = "Owner's&nbsp;name";
		}	
	elem2.innerHTML = "Owner's&nbsp;email:";
	elem4.innerHTML = "Message to "+media_str+" owner";
	elem5.innerHTML = "&nbsp;Set&nbsp;As&nbsp;Borrowed&nbsp;";
	elem6.innerHTML = 'borrowed';
	elem7.style.visibility = 'visible';
	elem7.style.display = '';
	elem8.disabled = false;
	}
else
	{
	//This is a title that is borrowed from this gurulib user
	//This is a title borrowed by this gurulib user
	elem3.disabled = false;
	elem.innerHTML = "Is&nbsp;this&nbsp;borrower&nbsp;a&nbsp;GuruLib&nbsp;user?";
	if(elem3.value == 0)
		{
		elem1.innerHTML = "Borrower's&nbsp;GuruLib&nbsp;username";
		}
	else
		{
		elem1.innerHTML = "Borrower's&nbsp;name";
		}	
	elem2.innerHTML = "Borrower's&nbsp;email:";
	elem4.innerHTML = "Message to borrower";
	elem5.innerHTML = "&nbsp;Set&nbsp;As&nbsp;Lent&nbsp;";
	elem6.innerHTML = 'lent';
	}

}

function change_borrower_type(borrower_type)
{
var elem, elem1, elem2, elem3;
elem = document.getElementById('borrower_type');
if(elem == null)
	return;
elem1 = document.getElementById('borrower_email_row');
if(elem1 == null)
	return;
elem2 = document.getElementById('borrower_email');
if(elem2 == null)
	return;
elem3 = document.getElementById('who_borrowed_this');
if(elem3 == null)
	return;


if(borrower_type == 0)
	{
	//gurulib user
	if(elem3.value == 1)
		{
		elem.innerHTML = "Borrower's&nbsp;GuruLib&nbsp;username";
		}
	else
		{
		elem.innerHTML = "Owner's&nbsp;GuruLib&nbsp;username";	
		}
	elem1.style.visibility = 'hidden';
	elem1.style.display = 'none';
	elem2.disabled = true;
	}
else
	{
	if(elem3.value == 1)
		{
		elem.innerHTML = "Borrower's&nbsp;name";
		}
	else
		{
		elem.innerHTML = "Owner's&nbsp;name";
		}	
	elem1.style.visibility = 'visible';
	elem1.style.display = '';
	elem2.disabled = false;
	}
document.getElementById('borrower_name').focus();
}

function username_keydown(obj, evnt)
{
var elem = document.getElementById('borrower_type_select');
if(elem == null)
	return true;
if(elem.value == 1)
	return true;
return handle_partial_result_key(obj,evnt);
}

function username_keyup(obj)
{
var elem = document.getElementById('borrower_type_select');
if(elem == null)
	return;
if(elem.value == 1)
	return;
fetch_partial_results(obj,0, 'username')
}

function set_borrow_keypressed()
{
var elem1 = document.getElementById('set_borrow_area');
if(elem1 == null)
	return;
var length = elem1.value.length;
var elem = document.getElementById('set_borrow_status_area');
if(elem == null)
	return;
var space_left = 500 - length;
if(space_left < 0)
	{
	elem1.value = elem1.value.substring(0, 499);
	}
else
	{
	elem.innerHTML = "Space available for " + space_left + " more letters.";
	}
}

function set_borrow_item()
{
	disable_screen(false);
	var mesg_text 		= document.getElementById('set_borrow_edit_area').value;
	var borrower_type = document.getElementById('borrower_type_select').value*1;
	var borrower_name = trim(document.getElementById('borrower_name').value);
	var email 				= document.getElementById('borrower_email').value;
	var borrowed_item = document.getElementById('who_borrowed_this').value;
	if(borrower_name.length == 0)
		{
		var str = '';
		if(borrower_type == 0)
			{
			str = "GuruLib username";
			}
		else
			{
			str = "name";
			}
		show_alert_dialog("Error", "Please enter a valid "+str);
		document.getElementById('borrower_name').focus();
		return;
		}
	if(borrower_name == current_username && borrower_type==0)
		{
		show_alert_dialog("Error", "You cannot borrow your own item.");
		document.getElementById('borrower_name').focus();
		return;
		}

	close_set_borrow_edit_box();
	var month_array = new Array();
	month_array['Jan'] = 1;
	month_array['Feb'] = 2;
	month_array['Mar'] = 3;
	month_array['Apr'] = 4;
	month_array['May'] = 5;
	month_array['Jun'] = 6;
	month_array['Jul'] = 7;
	month_array['Aug'] = 8;
	month_array['Sep'] = 9;
	month_array['Oct'] = 10;
	month_array['Nov'] = 11;
	month_array['Dec'] = 12;

	var disp_return_date = document.getElementById('set_borrow_return_date').value;
	var days = document.getElementById('set_borrow_for').value*1;
	var arr = disp_return_date.split(',');
	var year = trim(arr[1])*1;
	var arr1 = trim(arr[0]).split(' ');
	var month = month_array[arr1[0]];
	var day = trim(arr1[1]);
	if(month.length == 1)
		month = '0' + month;
	if(day.length == 1)
		day = '0' + day;

	var return_date = year+"-"+month+"-"+day;
	var args = 						{	
												'action'							: 'set_borrow_item', 
												'item_owner_id'				:	get_current_item_owner_id(),
												'return_date'					: return_date,
												'mesg_text'						:	mesg_text,
												'disp_return_date'		:	disp_return_date,
												'days'								:	days,
												'borrower_name'				:	borrower_name,
												'borrower_type'				: borrower_type,
												'email'								: email,
												'borrowed_item'				: borrowed_item
												};
	ajax_post_request(ajax_request_url, args, set_borrow_item_response);

}

function set_borrow_item_response(response_array)
{
if(response_array[0] == 'error')
	{
	show_alert_dialog("Error", response_array[1]);
	}
else
	{
	var borrow_status 	= response_array[1]*1;
	var borrower_name 	= response_array[3];
	var return_date			= response_array[5];
	var borrower_id 		= response_array[6]*1;
	var this_is_the_owner		= response_array[7]*1;
	var email						= response_array[8];
	var lender_name 		= response_array[9];
	var item_owner_id 	= response_array[10]*1;
	if(borrow_status == 5)
		{
		//var arr = get_item_info_from_item_owner_id(get_current_item_owner_id());
		//var title = arr['title'];
		var msg = ("An email was sent to notify "+borrower_name+" about this setting. \nThe borrow status will be set as \"loaned\" after "+borrower_name+" accepts this setting");
		show_alert_dialog("Email Sent", msg);
		}
	else
		{
		if(this_is_the_owner == 0)
			{
			borrow_status = 3;
			}
		update_borrow_item_status(item_owner_id, borrower_id, borrow_status, borrower_name, return_date, email, lender_name);
		}
	}
}

function show_set_borrowed_item(item_owner_id)
{
var arr = get_item_info_from_item_owner_id(item_owner_id);
if(arr == null)
	return;
var title = arr['title'];
if(title.length > 40)
	{
	title = title.substring(0,40);
	title += "...";
	}

var left1 = (screen.availWidth - 520)/2;
var top1 = (screen.availHeight - 460)/2 + document.body.scrollTop;
var elem = document.getElementById('set_borrow_edit_box');
if(elem == null)
	{
	return;
	}
elem.style.backgroundColor = theme_color;
elem.style.visibility = 'visible';
elem.style.left = left1;
elem.style.top = top1;

elem = document.getElementById('set_borrow_for');
if(elem != null)
	change_borrow_date(elem.value,'set_borrow_return_date');
//document.getElementById('borrower_name').focus();
elem = document.getElementById('borrower_type_select');
if(elem != null)
	{
	var borrower_type = elem.value*1;
	change_borrower_type(borrower_type);
	}
elem = document.getElementById('set_borrow_header');
if(elem != null)
	{
	var str = "Set \""+title+"\" as borrowed";
	string_replace(str, " ", "&nbsp;");
	elem.innerHTML = str;
	}
//select_item(true, item_owner_id);
disable_screen(true);
}

function update_borrow_item_status(item_owner_id, borrower_id, borrow_status, borrower_name, return_date, email, lender_name)
{
if(item_result_array == null)
	{
	return;
	}

var item_index = get_item_index_from_item_owner_id(item_owner_id);
item_result_array[item_index*row_item_count + 15] = borrow_status;
item_result_array[item_index*row_item_count + 18] = borrower_id;
item_result_array[item_index*row_item_count + 19] = borrower_name;
item_result_array[item_index*row_item_count + 20] = return_date;
item_result_array[item_index*row_item_count + 24] = email;
item_result_array[item_index*row_item_count + 26] = lender_name;

if(user_view_type == THUMBNAIL_VIEW_SMALL || user_view_type == THUMBNAIL_VIEW_LARGE)
	{
	update_borrow_status_image(item_owner_id);
	}
}

function request_borrow_item()
{
	close_borrow_box();
	var month_array = new Array();
	month_array['Jan'] = 1;
	month_array['Feb'] = 2;
	month_array['Mar'] = 3;
	month_array['Apr'] = 4;
	month_array['May'] = 5;
	month_array['Jun'] = 6;
	month_array['Jul'] = 7;
	month_array['Aug'] = 8;
	month_array['Sep'] = 9;
	month_array['Oct'] = 10;
	month_array['Nov'] = 11;
	month_array['Dec'] = 12;

	var disp_return_date = document.getElementById('return_date').value;
	var days = document.getElementById('borrow_for').value*1;
	var arr = disp_return_date.split(',');
	var year = trim(arr[1])*1;
	var arr1 = trim(arr[0]).split(' ');
	var month = month_array[arr1[0]];
	var day = trim(arr1[1]);
	if(month.length == 1)
		month = '0' + month;
	if(day.length == 1)
		day = '0' + day;

	var return_date = year+"-"+month+"-"+day;
	var mesg_text = document.getElementById('borrow_edit_area').value;

	var args = 						{	
												'action'							: 'request_borrow_item', 
												'item_owner_id'				:	get_current_item_owner_id(),
												'return_date'					: return_date,
												'mesg_text'						:	mesg_text,
												'disp_return_date'		:	disp_return_date,
												'days'								:	days
												};
	ajax_post_request(ajax_request_url, args, request_borrow_item_response);

}

function request_borrow_item_response(response_array)
{
if(response_array[0] == "error")
	{
	show_alert_dialog("Error", response_array[1]);	
	}
else
	{
	var borrow_status = response_array[1]*1;
	var item_owner_id = response_array[8]*1;
	var borrower_id = response_array[6]*1;
	var duplicate = response_array[7];
	if(duplicate == 'duplicate')
		{
		var msg = ("A previous borrow request made by you is still pending "+current_username+"'s action.");
		show_alert_dialog("Error", msg);
		}
	else
		{
		var arr = get_item_info_from_item_owner_id(item_owner_id);
if(arr == null)
	return;
		var title = arr['title'];
		var msg = ("Borrow request for \""+title+"\" was successfully sent to "+current_username);
		show_alert_dialog("Borrow Request Sent", msg);
		}
	}
}

function close_borrow_box()
{
var elem = document.getElementById('borrow_box');
if(elem == null)
	return;

elem.style.visibility='hidden';
disable_screen(false);
}

function borrow_edit_keypressed()
{
var elem1 = document.getElementById('borrow_edit_area');
if(elem1 == null)
	return;
var length = elem1.value.length;
var elem = document.getElementById('borrow_edit_status_area');
if(elem == null)
	return;
var space_left = 500 - length;
if(space_left < 0)
	{
	elem1.value = elem1.value.substring(0, 499);
	}
else
	{
	elem.innerHTML = "Space available for " + space_left + " more letters.";
	}
}

function show_borrow_box(item_owner_id)
{
var arr = get_item_info_from_item_owner_id(item_owner_id);
if(arr == null)
	return;
var title = arr['title'];
if(title.length > 40)
	{
	title = title.substring(0,40);
	title += "...";
	}

var left1 = (screen.availWidth - 520)/2;
var top1 = (screen.availHeight - 325)/2 + document.body.scrollTop;
var elem = document.getElementById('borrow_box');
if(elem == null)
	{
	return;
	}
elem.style.backgroundColor = theme_color;
elem.style.visibility = 'visible';
elem.style.left = left1;
elem.style.top = top1;
elem = document.getElementById('borrow_for');
if(elem == null)
	return;
change_borrow_date(elem.value,'return_date');
elem = document.getElementById('borrow_edit_area');
if(elem != null)
	{
	elem.value = '';
	elem.focus();
	}
elem = document.getElementById('borrow_request_header');
if(elem != null)
	{
	var str = "&nbsp;Send&nbsp;Request to borrow \""+title+"\"";
	string_replace(str, " ", "&nbsp;");
	elem.innerHTML = str;
	}
//select_item(true, item_owner_id);
disable_screen(true);
}

function hide_shelf_list_menu()
{
var elem = document.getElementById('shelf_list_menu');
if(elem != null)
	{
	elem.style.visibility = 'hidden';
	}
}

function get_owner_shelf_info_from_shelf_id(shelf_id)
{
var i;
for(i = 0; i < owner_shelf_array.length; ++i)
	{
	if(owner_shelf_array[i]['id'] == shelf_id)
		{
		return owner_shelf_array[i];
		}
	}
return null;
}

function get_shelf_info_from_shelf_id(shelf_id)
{
var i;
for(i = 0; i < shelf_array.length; ++i)
	{
	if(shelf_array[i]['id'] == shelf_id)
		{
		return shelf_array[i];
		}
	}
return null;
}

function get_shelf_info_from_shelf_name(shelf_name)
{
var i;
for(i = 0; i < shelf_array.length; ++i)
	{
	if(shelf_array[i]['name'] == shelf_name)
		{
		return shelf_array[i];
		}
	}
return null;
}

function try_close_header_config_menu()
{
if(show_header_config_menu == false)
	{
	close_header_config_menu();
	}
}


function generate_header_right_click_menu(x, y)
{
if(owner == false)
	return;
show_header_right_click_menu = true;
//setTimeout("show_header_right_click_menu = false", 500);
//get_xy(evnt);
//var x = elem_x;
//var y = elem_y;
var str = '<table style="border: 1px outset white; font-family: arial; font-size: 10pt" border="0" bgcolor="'+theme_color+'" cellspacing="0" cellpadding="0" width="100" height="100%">\n';

str += "<th style=\"color: black; background-color: #88AACC\">"
str += '<table style="font-size: 10pt;" border="0" cellspacing="0" cellpadding="2" width="100%" height="100%"><tr>';
str += '<td align="center" valign="middle" width="100%" height="100%"><b>Columns</b></td>';
str += '<td align="center" valign="middle" width="100%" height="100%">';
str += '<img title="Close" onmouseup="hide_all_popup_menu();" src="'+base_url+'/_images/comment_close.gif" style="cursor: pointer; cursor: hand;">';
str += '</td></tr></table>';
str +="</th>";

var temp = generate_sub_menu("Insert new column", "insert_column", 0, "insert_new_column_func");
//str += add_menu_row(0, temp, "Click here to insert a new column","insert_new_column_func", 1);
str += temp;

temp = generate_sub_menu("Change this column", "change_column", 0,"change_column_func");
str += temp;
//str += add_menu_row(0, temp, "Click here to change this column","change_column_func", 1);

str += add_seperator();
str += add_menu_row(0, "Remove this column", "Click here to remove this column","remove_column_func", 0);

//str += add_seperator();
//str += add_menu_row(0, "Save current columns", "Click here to save the current columns","save_column_func", 0);

str += "<tr><td align=\"right\" valign=\"bottom\" width=\"100%\" height=\"1\"><span id=\"header_right_click_bottom_right\" style=\"width: 1px; height: 1px; font-size: 1pt; display: block;\"></span></td></tr>";
str += "</table>\n";

var elem = document.getElementById('header_right_click_menu');
if(elem == null)
	return;
elem.innerHTML = str;
elem.style.visibility = "visible";
elem.style.left = x;
elem.style.top = y;

var elem1 = document.getElementById('header_right_click_bottom_right');
if(elem1 == null)
	{
	return;
	}
var x1 = findPosX(elem1);
var y1 = findPosY(elem1);
var wide = x1 - x;
var high = y1 - y;

if(x1 > avail_width - 10)
	{
	x = 2*x - x1 + 20;
	}
else
	{
	x -= 10;
	}
y -= 10;
elem.style.left = x;
elem.style.top = y;

//purpose is to check where the arrow should be pointing for the sub menu
x+=wide;
generate_header_config_menu(x, y, wide, "", 0);
}

function generate_header_config_menu(x, y, wide1, header, mode)
{
if(owner == false)
	return;
if(mode == 1)
	{
	show_header_right_click_menu = true;
	//setTimeout("show_header_right_click_menu = false", 500);

	show_header_config_menu = true;
	//setTimeout("show_header_config_menu = false", 500);
	var elem = document.getElementById('header_right_click_menu');
	if(elem == null)
		return;
	elem.style.visibility = "visible";
	elem = document.getElementById('header_config_menu');
	if(elem == null)
		return;
	elem.style.visibility = "visible";
	elem.style.top = y*1;
	elem = document.getElementById('header_config_header');
	if(elem == null)
		return;
	elem.innerHTML = header;

	set_sub_menu_arrow_display("change_column", "left", true);
	set_sub_menu_arrow_display("insert_column", "left", true);
	set_sub_menu_arrow_display("change_column", "right", true);
	set_sub_menu_arrow_display("insert_column", "right", true);
	return;
	}
var str = '<table style="border: 1px outset white; font-family: arial; font-size: 10pt" border="0" bgcolor="'+theme_color+'" cellspacing="0" cellpadding="0" width="100" height="100%">\n';
str += "<th style=\"color: black; background-color: #88AACC\">"
str += '<table style="font-size: 10pt;" border="0" cellspacing="0" cellpadding="2" width="100%" height="100%"><tr>';
str += '<td style="font-weight: bold;" id="header_config_header" align="center" valign="middle" width="100%" height="100%"><b>Actions</b></td>';
str += '<td align="center" valign="middle" width="100%" height="100%">';
str += '<img title="Close" onmouseup="close_header_config_menu();" src="'+base_url+'/_images/comment_close.gif" style="cursor: pointer; cursor: hand;">';
str += '</td></tr></table>';
str +="</th>";

str += add_menu_row(TITLE_INDEX, "Title", "Click here to set this header as Title","header_change_func", 0);
switch(req_media_id)
	{
	default:
	case ALL_MEDIA:
		str += add_menu_row(AUTHOR_INDEX, "Creator", "Click here to set this header as Creator","header_change_func", 0);
		str += add_menu_row(PUBLISHER_INDEX, "Produced", "Click here to set this header as produced","header_change_func", 0);
		break;
	case BOOKS:

		str += add_menu_row(AUTHOR_INDEX, "Author", "Click here to set this header as Author","header_change_func", 0);
		str += add_menu_row(PUBLISHER_INDEX, "Publisher", "Click here to set this header as publisher","header_change_func", 0);
		break;
	case MOVIES:

		str += add_menu_row(AUTHOR_INDEX, "Actors", "Click here to set this header as Actors","header_change_func", 0);
		str += add_menu_row(DIRECTOR_INDEX, "Director", "Click here to set this header as Director","header_change_func", 0);
		str += add_menu_row(PUBLISHER_INDEX, "Studio", "Click here to set this header as Studio","header_change_func", 0);
		break;
	case MUSIC:
		str += add_menu_row(AUTHOR_INDEX, "Artist", "Click here to set this header as Artist","header_change_func", 0);
		str += add_menu_row(PUBLISHER_INDEX, "Studio", "Click here to set this header as Studio","header_change_func", 0);
		break;
	case GAMES:
		str += add_menu_row(PLATFORM_INDEX, "Platform", "Click here to set this header as platform","header_change_func", 0);
		str += add_menu_row(PUBLISHER_INDEX, "Developer", "Click here to set this header as Developer","header_change_func", 0);
		break;
	case SOFTWARE:
		str += add_menu_row(PLATFORM_INDEX, "Platform", "Click here to set this header as platform","header_change_func", 0);
		str += add_menu_row(PUBLISHER_INDEX, "Developer", "Click here to set this header as Developer","header_change_func", 0);
		break;
	}

str += add_menu_row(GENRE_INDEX, "Genre", "Click here to set this header as Genre","header_change_func", 0);
str += add_menu_row(SHELF_INDEX, "Shelf", "Click here to set this header as Shelf","header_change_func", 0);
str += add_menu_row(USER_RATING_INDEX, "Rating", "Click here to set this header as Rating","header_change_func", 0);

switch(req_media_id)
	{
	default:
	case ALL_MEDIA:
		break;
	case BOOKS:
		str += add_seperator();
		str += add_menu_row(ITEM_CODE_INDEX, "ISBN", "Click here to set this header as ISBN","header_change_func", 0);
		break;
	case MOVIES:
		str += add_seperator();
		str += add_menu_row(ITEM_CODE_INDEX, "UPC", "Click here to set this header as UPC","header_change_func", 0);
		str += add_menu_row(MPAA_INDEX, "MPAA", "Click here to set this header as MPAA","header_change_func", 0);
		break;
	case MUSIC:
		str += add_seperator();
		str += add_menu_row(ITEM_CODE_INDEX, "UPC", "Click here to set this header as UPC","header_change_func", 0);
		break;
	case GAMES:
		str += add_seperator();
		str += add_menu_row(ITEM_CODE_INDEX, "UPC", "Click here to set this header as UPC","header_change_func", 0);
		str += add_menu_row(MPAA_INDEX, "ESRB", "Click here to set this header as ESRB","header_change_func", 0);
		break;
	case SOFTWARE:
		str += add_seperator();
		str += add_menu_row(ITEM_CODE_INDEX, "UPC", "Click here to set this header as UPC","header_change_func", 0);
		break;
	}

str += add_seperator();
str += add_menu_row(SMALL_IMG_INDEX, "Small Front Cover", "Click here to set this header as small front cover","header_change_func", 0);
str += add_menu_row(MEDIUM_IMG_INDEX, "Medium Front Cover", "Click here to set this header as medium front cover","header_change_func", 0);
str += add_menu_row(LARGE_IMG_INDEX, "Large Front Cover", "Click here to set this header as large front cover","header_change_func", 0);

str += add_seperator();
str += add_menu_row(BORROWER_NAME_INDEX, 		"Borrower", 			"Click here to set this header as Borrower","header_change_func", 0);
str += add_menu_row(BORROW_RET_DATE_INDEX, 	"Lender", 		"Click here to set this header as Lender","header_change_func", 0);
str += add_menu_row(BORROW_STATUS_INDEX, 		"Borrow status", 	"Click here to set this header as Title","header_change_func", 0);
str += add_menu_row(BORROW_RET_DATE_INDEX, 	"Return date", 		"Click here to set this header as Return Date","header_change_func", 0);

str += add_seperator();
if(req_media_id == BOOKS)
	{
	str += add_menu_row(REL_DATE_INDEX, "Published date", "Click here to set this header as Published Date","header_change_func", 0);
	}
else
	{
	str += add_menu_row(REL_DATE_INDEX, "Released date", "Click here to set this header as Release Date","header_change_func", 0);
	}

str += add_menu_row(ADD_DATE_INDEX, "Added date", "Click here to set this header as Added Date","header_change_func", 0);
str += add_menu_row(REVIEW_DATE_INDEX, "Review date", "Click here to set this header as Reviewed Date","header_change_func", 0);

str += add_seperator();
str += add_menu_row(BEST_PRICE_INDEX, "Used Price", "Click here to set this header as Used Price","header_change_func", 0);
str += add_menu_row(NEW_PRICE_INDEX, "New Price", "Click here to set this header as New Price","header_change_func", 0);

str += add_seperator();
str += add_menu_row(SYNOPSIS_INDEX, "Synopsis", "Click here to set this header as Synopsis","header_change_func", 0);
str += add_menu_row(MY_REVIEW_INDEX, "My&nbsp;Review", "Click here to set this header as My Review","header_change_func", 0);


str += "<tr><td align=\"right\" valign=\"bottom\" width=\"100%\" height=\"1\"><span id=\"header_config_bottom_right\" style=\"width: 1px; height: 1px; font-size: 1pt; display: block;\"></span></td></tr>";
str += "</table>\n";

var elem = document.getElementById('header_config_menu');
if(elem == null)
	return;
elem.innerHTML = str;
elem.style.visibility = "visible";
elem.style.left = x;
elem.style.top = y;

var elem1 = document.getElementById('header_config_bottom_right');
if(elem1 == null)
	{
	return;
	}
var x1 = findPosX(elem1);
var y1 = findPosY(elem1);
var wide = x1 - x;
var high = y1 - y;

if(x1 > avail_width - 10)
	{
	x = 2*x - wide1 - x1 + 5;
	set_sub_menu_arrow_visible("change_column", "left", true);
	set_sub_menu_arrow_visible("insert_column", "left", true);
	set_sub_menu_arrow_visible("change_column", "right", false);
	set_sub_menu_arrow_visible("insert_column", "right", false);
	}
else
	{
	x -= 10;
	set_sub_menu_arrow_visible("change_column", "right", true);
	set_sub_menu_arrow_visible("insert_column", "right", true);
	set_sub_menu_arrow_visible("change_column", "left", false);
	set_sub_menu_arrow_visible("insert_column", "left", false);
	}
/*if(browser_is_ie)
	{
	x -= 20;
	}*/
elem.style.left = x+10;
elem.style.top = y - 10;
elem.style.visibility = "hidden";
}

function set_sub_menu_arrow_visible(menu_id, arrow_type, show)
{
var elem = document.getElementById(menu_id + "_" + arrow_type + "_arrow");
if(elem == null)
	{
	return;
	}
elem.style.visibility = show?("visible"):("hidden");
}

function set_sub_menu_arrow_display(menu_id, arrow_type, show)
{
var elem = document.getElementById(menu_id + "_" + arrow_type + "_arrow");
if(elem == null)
	{
	return;
	}
elem.style.display = show?("block"):("none");
}

function header_change_func(column_index)
{
show_loading();
if(column_insert_mode == false)
	{
	setTimeout("change_column_func_timeout("+column_index+")",10);
	}
else
	{
	setTimeout("insert_new_column_func_timeout("+column_index+")",10);
	}
}

function change_column_func_timeout(column_index)
{
hide_loading();
if(check_if_column_already_exists(column_index))
	return;
detail_view_format[req_media_id][current_column] = column_index;
save_detail_view_columns();
current_disp_y = -1;
display_items();
}

function check_if_column_already_exists(column_index)
{
var i;
for(i = 0; i < detail_view_format[req_media_id].length; ++i)
	{
	if(detail_view_format[req_media_id][i] == column_index)
		{
		var value_index = detail_view_format[req_media_id][i];
		var header_title = get_disp_name_from_column(value_index)
		header_title = string_replace(header_title, '&nbsp;', ' ');
		var msg = ("Column \""+header_title+"\" is already in your detail view");
		show_alert_dialog("Error", msg);
		return true;
		}
	}
return false;
}

function close_header_config_menu()
{
var elem = document.getElementById('header_config_menu');
if(elem != null)
	elem.style.visibility = 'hidden';
}

function close_header_right_click_menu()
{
var elem = document.getElementById('header_right_click_menu');
if(elem != null)
	elem.style.visibility = 'hidden';
set_sub_menu_arrow_display("change_column", "left", false);
set_sub_menu_arrow_display("insert_column", "left", false);
set_sub_menu_arrow_display("change_column", "right", false);
set_sub_menu_arrow_display("insert_column", "right", false);
}
function header_config_left_click(sort_type)
{
var right_click_menu_active = false;
var elem = document.getElementById('header_right_click_menu');
if(elem != null)
	{
	if(elem.style.visibility.toLowerCase() == 'visible')
		right_click_menu_active = true; //the right click menu is already up don;'t bother sorting based on this header click
	}
hide_all_menu();
hide_all_popup_menu();
if(right_click_menu_active == false)
	{
	sort_list(sort_type);
	}
}

function header_config_right_click(column, evnt)
{
hide_all_menu();
hide_all_popup_menu();
get_xy(evnt);
var x = elem_x;
var y = elem_y;
current_column = column;
generate_header_right_click_menu(x,y);
return false;
}

function remove_column_func()
{
hide_all_popup_menu();
if(detail_view_format[req_media_id].length > 1)
	{
	var value_index = detail_view_format[req_media_id][current_column];
	//var sort_type 	= get_sort_type_from_column(value_index);
	var header_title = get_disp_name_from_column(value_index)
	header_title = string_replace(header_title, '&nbsp;', ' ');
	var header = "Remove Column";
	var msg = "Column \""+header_title+"\" will be removed";
	window_confirm(header, msg, "remove_column_func_timeout()", null);
	}
else
	{
	show_alert_dialog("Error", "You cannot delete all columns");
	}
}

function remove_column_func_timeout()
{
detail_view_format[req_media_id].splice(current_column, 1)
save_detail_view_columns();
current_disp_y = -1;
display_items();
}

function insert_new_column_func()
{
var elem = document.getElementById('header_config_menu');
if(elem == null)
	return;
if(elem.style.visibility.toLowerCase() == 'visible')
	{
	close_header_config_menu();
	return;
	}

var y;
var elem = document.getElementById('header_right_click_menu');
if(elem == null)
	return;
y = parseInt(elem.style.top) + 10;
generate_header_config_menu(0, y, 0, "Insert&nbsp;Column", 1)
column_insert_mode = true;
}

function insert_new_column_func_timeout(column_index)
{
hide_loading();
if(check_if_column_already_exists(column_index))
	return;
detail_view_format[req_media_id].splice(current_column, 0, column_index);
save_detail_view_columns();
current_disp_y = -1;
display_items();
}

function change_column_func()
{
var elem = document.getElementById('header_config_menu');
if(elem == null)
	return;
if(elem.style.visibility.toLowerCase() == 'visible')
	{
	close_header_config_menu();
	return;
	}
	
var y;
var elem = document.getElementById('header_right_click_menu');
if(elem == null)
	return;
y = parseInt(elem.style.top) + 30;

generate_header_config_menu(0, y, 0, "Change&nbsp;Column", 1)
column_insert_mode = false;
}

function hide_all_popup_menu()
{
check_for_right_click_abort(null, null);
close_header_config_menu();
close_header_right_click_menu();
hide_right_click_menu();
hide_shelf_list_menu();
close_all_edit_this_text();
}

function hide_all_dialogs()
{
close_borrow_box();
cancel_confirm_dialog();
close_alert_dialog();
close_shelf_box();
close_set_borrow_edit_box();
close_borrow_box();
close_header_config_menu();
close_header_right_click_menu();
}

function save_detail_view_columns()
{
column_width_adjusted = false;

var columns = detail_view_format[req_media_id].join("_");
var args = 						{	
											'action'			: 		'save_detail_view_columns', 
											'columns'			:			columns,
											'medium'			:			req_media_id,
											'random'			:			Math.random()
											};
ajax_get_request(ajax_request_url, args, save_detail_view_columns_response);
}

function save_detail_view_columns_response(response_array)
{
}

function show_print_resize_element(current_size)
{
var print_str = '';
var export_str = '';
if(user_view_type == DETAIL_VIEW)
	{
	print_str = '<img height="25" title="Click here to open a printable list" id="print_view_button" alt="print" src="'+base_url+'/_images/print.gif" onmouseup="show_print_view(0);" style="display: none; cursor: pointer; cursor: hand;">';
	export_str = '<img title="Click here to export this catalog" id="export_view_button" alt="export" src="'+base_url+'/_images/new/import.jpg" onmouseup="show_print_view(1);" style="display: none; cursor: pointer; cursor: hand;">';
	}
else
	{	
	return;
	}
var elem = document.getElementById('export_resize_id');
if(elem != null)
	{
	elem.innerHTML = export_str;
	}	
var elem = document.getElementById('print_resize_id');
if(elem != null)
	{
	elem.innerHTML = print_str;
	}
}

function open_color_box()
{
var elem = document.getElementById('color_chooser');
if(elem == null)
	return;
if(elem.style.visibility.toLowerCase() == 'visible')
	{
	elem.style.visibility = 'hidden';
	return;
	}
var elem = document.getElementById('select_color');
var x = getImagePageLeft(elem);
var y = getImagePageTop(elem);
var menuTop = y - 75; // LAYER TOP POSITION
var menuLeft = x + 35;

var elem1 = document.getElementById('color_chooser');
elem1.style.top = menuTop;
elem1.style.left = menuLeft;
elem1.style.visibility="visible";
//Edit_shelf();
}

function hide_color_chooser()
{
var elem1 = document.getElementById('color_chooser');
elem1.style.visibility="hidden";
}

function color_item_clicked(color_id, color_name, color_path)
{
var elem = document.getElementById('color_name');
if(elem == null)
	return;

elem.innerHTML = color_name;

elem = document.getElementById('color_value');
if(elem == null)
	return;
elem.src = color_path;


elem = document.getElementById('color_'+color_id);
if(elem == null)
	return;
elem.innerHTML = "<img  border=\"0\" src=\""+base_url+"/_images/tick.gif\">";

elem = document.getElementById('color_'+shelf_color_id);
if(elem == null)
	return;
elem.innerHTML = "&nbsp";
shelf_color_id = color_id;
hide_color_chooser();
}

function hidelayer(lay)
{
	document.all[lay].style.visibility = "hidden";
}

function showlayer(lay)
{
	document.all[lay].style.visibility = "visible";
}

var select_bar_start_x = 0;
var select_bar_start_y = 0;
var select_bar_obj = null;
function select_bar_start(obj, evnt)
{
if(owner == false || select_bar_obj != null)
	return;
if(user_view_type == DETAIL_VIEW )
	return;
if(is_right_mouse_button_clicked(evnt))
	return;
get_xy(evnt);
select_bar_start_x = elem_x;
select_bar_start_y = elem_y;
select_bar_obj = document.getElementById('select_bar');
select_bar_obj.style.top = elem_x + 'px';
select_bar_obj.style.left = elem_y + 'px';
select_bar_obj.style.width = '0px';
select_bar_obj.style.height = '0px';
select_bar_obj.style.display = '';
select_bar_obj.style.visibility = 'visible';
restore_item();
disable_text_selection(true);
}

function select_bar_move(obj, evnt)
{
if(select_bar_obj == null)
	return;
	
get_xy(evnt);
var start_x; 
var start_y;
var end_x;
var end_y;
var wide;
var high;
start_x = Math.min(elem_x, select_bar_start_x);
start_y = Math.min(elem_y, select_bar_start_y);
end_x = Math.max(elem_x, select_bar_start_x);
end_y = Math.max(elem_y, select_bar_start_y);
wide = Math.abs(end_x - start_x);
high = Math.abs(end_y - start_y);
if(select_bar_obj != null)
	{
	select_bar_obj.style.top = start_y + 'px';
	select_bar_obj.style.left = start_x + 'px';
	select_bar_obj.style.width = wide + 'px';
	select_bar_obj.style.height = high + 'px';	
	} 

if(elem_y - 200 < document.body.scrollTop)
	{
	window.scroll(document.body.scrollLeft, elem_y - 200);
	}
else if((elem_y + 200 > screen.availHeight) && (elem_y  < avail_height + 50))
	{		
	var diff_y = elem_y - screen.availHeight + 200; 
	if(diff_y > document.body.scrollTop)
		{
		window.scroll(document.body.scrollLeft, diff_y);
		}
	}
if(elem_x - 200 < document.body.scrollLeft)
	{
	window.scroll(elem_x - 200, document.body.scrollTop);
	}
else if((elem_x > screen.availWidth) && (elem_x  < avail_width))
	{		
	var diff_x = elem_x - screen.availWidth; 
	if(diff_x > document.body.scrollLevt)
		{
		window.scroll(diff_x, document.body.scrollTop);
		}
	}
}

function select_bar_abort()
{
if(select_bar_obj == null)
	return;
select_bar_obj.style.display = 'none';
select_bar_obj.style.visibility = 'hidden';
select_bar_obj = null;
disable_text_selection(false);
}

function select_bar_end(obj, evnt)
{
if(select_bar_obj == null)
	return;
var wide = parseInt(select_bar_obj.style.width);
var high = parseInt(select_bar_obj.style.height);
if(wide * high > 400)
	{
	select_all_select_bar_selected_items(evnt);
	hide_all_menu();
	hide_all_popup_menu();
	}
select_bar_abort();
//select_bar_obj.style.display = 'none';
//select_bar_obj.style.visibility = 'hidden';
//select_bar_obj = null;
}

function select_all_select_bar_selected_items(evnt)
{
var item_count = 0;
var box_start_x = findPosX(select_bar_obj);
var box_start_y = findPosY(select_bar_obj);
var box_end_x = box_start_x + parseInt(select_bar_obj.style.width);
var box_end_y = box_start_y + parseInt(select_bar_obj.style.height);

for(item_count = 0; item_count < query_result_count; ++item_count)
	{
	var item_owner_id 	= item_result_array[item_count*row_item_count + 2]*1;
	var item_row_id = get_item_row_id(item_owner_id);
	var item_div_id = get_item_div_id(item_owner_id);
	var img_obj = document.getElementById(item_div_id);
	var x = findPosX(img_obj);
	var y = findPosY(img_obj);
	
	if(img_obj.height != null)
		{
		x += parseInt(img_obj.width)/2;
		y += parseInt(img_obj.height)/2;
		}
	else
		{
		x += parseInt(img_obj.style.width)/2;
		y += parseInt(img_obj.style.height)/2;
		}	
	//alert(x + ",y=" + y + ",box_start_x=" + box_start_x + ",box_end_x=" + box_end_x + ",box_start_y=" + box_start_y + ",box_end_y=" + box_end_y);
	if(x > box_start_x && x < box_end_x && y > box_start_y && y < box_end_y)
		{
		//start_dragging(obj, item_div_id, item_owner_id, evnt);	
		var obj = document.getElementById(item_row_id);
		add_selected_object(obj, img_obj, item_owner_id);
		}
	}
}

var right_click_state = 0;
var right_click_start_x = 0;
var right_click_start_y = 0;
var right_click_timer = null;

function check_for_right_click(obj, evnt, item_owner_id, row_obj_id)
{
//alert(obj.id);
active_touch_screen_text_input = null;
if(is_right_mouse_button_clicked(evnt))
	{
	if(item_owner_id >  0)
		{
		right_mouse_clicked(item_owner_id, evnt, row_obj_id);
		}
	else
		{
		header_config_right_click(-item_owner_id , evnt);
		}
	return false;
	}
else
	{
	if(row_obj_id < 0)
		{
		active_touch_screen_text_input = obj;	
		}
	else
		{
		if(item_owner_id <= 0)
			{
			current_header_sort_type = row_obj_id;
			current_column = -item_owner_id;
			}
		}	
	}
//if(right_click_state != 0)
//	return;
get_xy(evnt);
right_click_state = 1;
right_click_start_x = elem_x;
right_click_start_y = elem_y;
right_click_timer = setTimeout('fire_right_click_event( \''+item_owner_id+'\',\''+row_obj_id+'\')', 1000);
return true;
}

function fire_right_click_event(item_owner_id, row_obj_id)
{
if(right_click_state != 1)
	return;

right_click_state = 2;
current_header_sort_type = null;
//fire the right click event
//alert('right clicked');
if(!(row_obj_id < 0 && (active_touch_screen_text_input != null)))
	{
	hide_all_menu();
	hide_all_popup_menu();
	restore_item();
	hide_details_info();
	}
if(item_owner_id > 0 )
	{
	select_item(true, item_owner_id);
	generate_right_click_menu(item_owner_id, right_click_start_x, right_click_start_y);
	}
else
	{	
	//alert(row_obj_id);
	if(row_obj_id < 0)
		{
		if(active_touch_screen_text_input != null)
			{
			show_keyboard_box(active_touch_screen_text_input);
			}
		}
	else
		{
		//current_column = column;
		generate_header_right_click_menu(right_click_start_x, right_click_start_y);
		//current_header_sort_type = null;
		//alert(current_header_sort_type);
		}			
	}
}

function document_mouse_down(obj, evnt)
{
if(is_right_mouse_button_clicked(evnt) == false)
	return true;
else
	return context_menu_active;
}

function document_mouse_up(obj, evnt)
{
select_bar_end(obj, evnt);
return check_for_right_click_abort(obj, evnt);
}

function check_for_right_click_abort(obj, evnt)
{
var ret = context_menu_active;
if(right_click_timer != null && right_click_state != 0)
	{
	clearTimeout(right_click_timer);
	right_click_timer = null;
	setTimeout('right_click_state = 0;', 1);
	if(current_header_sort_type != null)
		{
		header_config_left_click(current_header_sort_type);
		current_header_sort_type = null;
		}
	}
if(evnt != null)
	{
	if(is_right_mouse_button_clicked(evnt)==false)
		{
		ret = true;
		}	
	}
//alert(ret);
return ret;
}

function check_for_right_click_mouse_move(obj, evnt)
{
if(right_click_state != 1)
	return;
get_xy(evnt);
var val_x = abs_val(elem_x - elem_start_x - elem_dx);
var val_y = abs_val(elem_y - elem_start_y - elem_dy)
var max_val_x = abs_val(max_elem_x - elem_start_x - elem_dx);
var max_val_y = abs_val(max_elem_y - elem_start_y - elem_dy)


if((val_x > 10 || val_y > 10) || (max_val_x > 10 || max_val_y > 10))
	{
	check_for_right_click_abort(obj, evnt);
	}
}

function disable_text_selection(disable)
{
return;
if(disable)
	{
	document.onselectstart = function() {return false;} // ie
  document.onmousedown = function() { return false;} // mozilla
	}
else
	{
	document.onselectstart = function() { return true;};
  document.onmousedown = function(obj, evnt) { return document_mouse_down(obj, evnt);}
	}
}

function select_item(show_selected, item_owner_id)
{
var row_id = get_item_row_id(item_owner_id);
var img_id = get_item_div_id(item_owner_id);
var elem = document.getElementById(row_id);
var img_id_obj = document.getElementById(img_id);
if(elem == null || img_id_obj == null)
	{
	restore_item();
	return;
	}
if(show_selected)
	{
	//if(get_current_item_owner_id() !=0)
		restore_item();
	//elem.style.backgroundColor = theme_color;
	//elem.style.filter = "alpha(opacity=50)";
	//elem.style.opacity = '0.50';
	add_selected_object(elem, img_id_obj, item_owner_id);
	//urrent_item_owner_id = item_owner_id;
	}
else
	{
	//current_item_owner_id = 0;
	//restore_item();
	remove_selected_object(elem);
	//elem.style.backgroundColor = "";
	//elem.style.filter = "alpha(opacity=100)";
	//elem.style.opacity = '1.0';
	}	
}

function is_right_click_menu_active()
{
var elem = document.getElementById('right_click_menu');
if(elem != null)
	{
	return (elem.style.visibility.toLowerCase()=='visible');
	}
return false;
}

function is_right_mouse_button_clicked(evnt)
{
	if(navigator.appName == "Microsoft Internet Explorer")
		{
		if(evnt.button == 2 || evnt.button == 3)
			{
			return true;
			}
		else
			{
			return false;
			}
		}
	else
		{
		if(evnt.which == 3 || evnt.which == 2)
			{
			return true;
			}
		else
			{
			return false;
			}
		}
}

var cover_flow_glide_state = 0;
var cover_flow_x_pos = 0;
var cover_flow_caption_id = 0;
function start_glide(x_pos, caption_id)
{
//if(cover_flow_glide_state != 0)
//	return;
cover_flow_glide_state = 1;
cover_flow_x_pos = x_pos;
cover_flow_caption_id = caption_id;
}

function glide_cover_flow_item(obj, evnt)
{
if(cover_flow_glide_state != 1)
	return;
cover_flow_glide_state = 2;
var x_pos = 0;
var caption_id = 0;

glideTo(cover_flow_x_pos, cover_flow_caption_id);
}

function start_new_cover_flow_glide(obj, evnt)
{
cover_flow_glide_state = 0;
}

function add_item_button_clicked(obj, evnt)
{
get_xy(evnt);
var x = elem_x-findPosX(obj);
if(x > 70)
	{
	//open the menu
	open_quick_add_menu();
	}
else
	{
	add_item_to_gurulib();
	}	
}

function open_quick_add_item_dialog(obj, evnt)
{
}


function set_active_touch_screen_text_input(obj)
{
active_touch_screen_text_input = obj;	
//insertionS = 0;
//insertionE = 0;
if(obj == null)
	return;
if(obj.style.display.toLowerCase() != 'none' || obj.style.visibility.toLowerCase() != 'hidden')
	{
	getCaretPositions(obj);
	}
}

function show_keyboard_box(box)
{
set_active_touch_screen_text_input(box);

touchscreen_keyb.Show(true);
//hide_all_menu();
//hide_all_popup_menu();
disable_screen(true);
var y = findPosY(box);// + document.body.scrollTop;
if(y + 250 > avail_height)
	y = y - 220;
var obj = document.getElementById('keyboard_box');
obj.style.top = (y + 30) + 'px';
obj.style.left = (screen.availWidth - 794)/2;
obj.style.visibility = "visible";

}

function close_keyboard_box()
{
var obj = document.getElementById('keyboard_box');
if(touch_keyboard_active())
	{
	/*if(active_touch_screen_text_input != null)
		{
		if(active_touch_screen_text_input.style.visibility.toLowerCase() != 'hidden' && active_touch_screen_text_input.style.display.toLowerCase() != 'none')
			active_touch_screen_text_input.focus();
		}*/
	obj.style.visibility = "hidden";
	touchscreen_keyb.Show(false);
	disable_screen(false);
	}
}

function touch_keyboard_active()
{
var obj = document.getElementById('keyboard_box');
return (obj==null)?false:(obj.style.visibility.toLowerCase() != 'hidden');
}

var touchscreen_keyb = null;
function init_touchscreen_keyboard()
{
touchscreen_keyb = new VKeyboard("keyboard",    // container's id
                   keyb_callback, // reference to the callback function
                   true,          // create the arrow keys or not? (this and the following params are optional)
                   true,          // create up and down arrow keys? 
                   false,         // reserved
                   true,          // create the numpad or not?
                   "Arial",            // font name ("" == system default)
                   "16px",        // font size in px
                   font_color,        // font color
                   "#777",        // font color for the dead keys
                   "#88AACC",        // keyboard base background color
                   theme_color,        // keys' background color
                   "#DDD",        // background color of switched/selected item
                   "#777",        // border color
                   "#CCC",        // border/font color of "inactive" key (key with no value/disabled)
                   "#88AACC",        // background color of "inactive" key (key with no value/disabled)
                   "#F77",        // border color of the language selector's cell
                   true,          // show key flash on click? (false by default)
                   "#CC3300",     // font color during flash
                   "#FF9966",     // key background color during flash
                   "#CC3300",     // key border color during flash
                   false,         // embed VKeyboard into the page?
                   true,          // use 1-pixel gap between the keys?
                   0);            // index(0-based) of the initial layout


}

