COMP.LoadStylesheet('rws_details.css');

RWS.Details = 
{
	scrollConfig: {
		duration: .2,
		easing: 'easeIn'
	}
	,mouseOverFunc:function() { 
		if(!RWS.Details.detailBrowse_full)
			return;
		RWS.Details.detailBrowse_full.removeClass('mouseover_transparent');
		RWS.Details.detailBrowse_full.toBack();
		shouldSwapBar = true;
	}
	,detailBrowse_left: null
	,detailBrowse_right: null
	,detailBrowse_full: null
	,detailMain: null
	,mask: null
	,maskBorder: null
	,copyAvailable: null
	,detailMainBar: null
	,closeBtn: null
	,dataView: null
	,storeIndex: 0
	,curNode: null
	,curRecord: null
	,active:false
	,lastResizeRequest:0
	,resizedCounter:0
	,templates:{browse: null, item: null}
	,t:''
	,Resize : function() {} // will soon be overwritten
	,resizeTask: new Ext.util.DelayedTask(function(){
		var detailsWindow = RWS.Details.detailMain;
		var width = Ext.lib.Dom.getViewWidth();
		var height = Ext.lib.Dom.getViewHeight();
		detailsWindow.setHeight(height);
		detailsWindow.center();
		var qty = parseInt(width / 120);
		RWS.Details.mQuantity = qty;
		RWS.Details.UpdateRange();
		var scrollBody = Ext.getCmp('browse_scroll');
		var pos = scrollBody.getPosition();
		var nodeIndex = RWS.Details.storeIndex;
		scrollBody.setPagePosition((width / 2 - 75) - 150 * nodeIndex, pos[1]);
		var mainBar = Ext.get('detailMainBar');
		if(mainBar != null) mainBar.setX(width / 2 - 330, true);
		var stupidInnerBox = Ext.get('stupid-innerbox');
		if (stupidInnerBox) stupidInnerBox.setHeight(Ext.lib.Dom.getViewHeight() - 50);
	})
	,RequestResize : function()
	{
		if(RWS.Details.active){
			var height = Ext.lib.Dom.getViewHeight();
			var width = Ext.lib.Dom.getViewWidth();
			RWS.Details.maskMid.setHeight(height - 29);
			if(COMP.isSafari4 || COMP.isSafari3){ // For now, doing live resizing only on Safari
				var detailsWindow = RWS.Details.detailMain;
				var mainBar = Ext.get('detailMainBar');
				if(mainBar != null) mainBar.setX(width / 2 - 330);
				detailsWindow.setHeight(height);
				detailsWindow.center();
				var qty = parseInt(width / 120);
				RWS.Details.mQuantity = qty;
				RWS.Details.UpdateRange();
				var scrollBody = Ext.getCmp('browse_scroll');
				var pos = scrollBody.getPosition();
				var nodeIndex = RWS.Details.storeIndex;
				scrollBody.setPagePosition((width / 2 - 75) - 150 * nodeIndex, pos[1]);
			}
			else this.resizeTask.delay(250);
		}
	}
	,StartSearch : function(dataType,dataString)
	{
		this.CloseAll();
		
		var o={};
		o['SearchField0'] = dataString;
		o['SearchType0'] = dataType;
		RWS.Search.SendRequest(o,true, false);
	}
	,UpdateRange : function()
	{
		var c = Ext.getCmp('id_ResultRange');
		var html = L(25776) + '</br>'; // Displaying Results
		var from = Math.max(1, RWS.Details.storeIndex - Math.round(RWS.Details.mQuantity/2) + 2);
		var to = from + Math.min(RWS.Details.mQuantity - ((Math.round(RWS.Details.mQuantity/2)) - RWS.Details.storeIndex), RWS.Details.mQuantity - 1);
		var total = RWS.Search.Results.stores.main.getCount();
		if (from < 1) from = 1;
		if (to > total) to = total;
		html += from + ' - ' + to + ' : ' + total;
		RWS.Details.range[0] = from;
		RWS.Details.range[1] = to;
		if (!c) return;
		c.setHtml(html);
	}
	,Init: function(view, index, node, e)
	{
		RWS.Details.active = true;
		RWS.Details.domHeight = Ext.lib.Dom.getViewHeight();
//		if (RWS.Details)
		RWS.Details.range = [0, 0];
		var d = RWS.Details;
		d.dataView = view;
		d.curNode = node;
		d.curRecord = RWS.Search.Results.stores.main.getAt(index);
		d.storeIndex = RWS.Search.Results.stores.main.indexOf(d.curRecord);
		//centerPanel.el.dom.firstChild.firstChild.style.overflow = "hidden";
		//Load up the main detail window
		d.detailMain = new Ext.ux.detailWindow({
			id:'detailWindow'
			,cls: 'yellowWindow'
			,height:Ext.lib.Dom.getViewHeight()
			,html:RWS.Details.templates.item
		});	
		
		d.detailMain.loadRecord(d.curRecord);
		
		RWS.Search.AddPastItem(d.curRecord);
		
		d.detailBrowseFull = {
			xtype:'dataview'
			,id:'detailBrowseFull'
			,overClass:'resultItemOver'
			,titleClicked:false
			,trackOver:true
			,tpl:RWS.Search.Results.templates.browse
			,itemSelector:'div'
			,store:RWS.Search.Results.stores.main
			,multiSelect:true
			,autoWidth:true
			,autoHeight:true
			,bodyStyle:''
			,listeners:{
				mouseenter:function(dataView, index, node, e){
					RWS.Details.showTitle(node.id);
				}
				,mouseleave:function(){
					RWS.Details.hideTitle();
				}
			}
		};
		
		d.detailBrowse_full = new Ext.ux.detailBrowseWindow({
			id:'detailBrowse_full'
			,x:0
			,type:'full'
			,style:'width: 100%'
		});
		d.detailBrowse_full.doLayout();
		d.detailBrowse_full.show();
		d.detailView = {
			xtype:'dataview'
			,store:d.curRecord.data
			,tpl:RWS.Details.templates.item
			,autoHeight:true
			,itemSelector:'div'
		};
		d.detailBrowse_full.body.on(
		       	'mouseover',
		        function() {
					if(d.detailMain.isVisible()){
						if(shouldSwapBar == true){
							d.detailBrowse_full.addClass('mouseover_transparent');
							d.detailBrowse_full.toFront();
						}
						var stupidInnerBox = Ext.get('stupid-innerbox');
						if (stupidInnerBox) stupidInnerBox.setHeight(Ext.lib.Dom.getViewHeight() - 50);
					}
		});
		
		var scrollBody = Ext.getCmp('browse_scroll');
		var pos = scrollBody.getPosition();
		var nodeIndex = RWS.Details.storeIndex;
		scrollBody.el.setX((Ext.lib.Dom.getViewWidth() / 2 - 75) - 150 * nodeIndex);
		
		var leftEdge = d.detailMain.x;
		
		//Load the window mask to make the details page modul
		d.detailMain.show('Image' + d.curRecord.data.rsn);
		d.mask = new Ext.Element(
			Ext.DomHelper.insertBefore(d.detailMain.el.dom, {cls:'ext-el-mask',style:'z-index:9000'}, true));
		d.maskBorder = new Ext.Element(
			Ext.DomHelper.insertBefore(d.detailMain.el.dom, {tag:'div', cls:'maskBorder', id:'mask', children:[
				{tag:'div', cls:'maskBorder-tl', children:[
					{tag:'div', cls:'maskBorder-tr', children:[
						{tag:'div', cls:'maskBorder-tc', html:'&nbsp;'}]}]}
				,{tag:'div', cls:'maskBorder-ml', children:[
					{tag:'div', cls:'maskBorder-mr', children:[
						{tag:'div', id:'maskMid', cls:'maskBorder-mc',style:'height:' + (Ext.lib.Dom.getViewHeight() - 29), html:'&nbsp;'}]}]}
				,{tag:'div', cls:'maskBorder-bl', children:[
					{tag:'div', cls:'maskBorder-br', children:[
						{tag:'div', cls:'maskBorder-bc', html:'&nbsp;'}]}]}
			]}, true)
		);
		var mask = Ext.get('mask');
		RWS.Details.maskMid = Ext.get('maskMid');
		mask.on('mouseover', RWS.Details.mouseOverFunc);
		d.detailMain.body.on('mouseover', RWS.Details.mouseOverFunc);
		var domWidth = Ext.lib.Dom.getViewWidth();
		var copyWidth = 150;
		if(domWidth < 1100)
			copyWidth = 120;
		d.copyAvailable = new Ext.Panel({
			id:'copyAvailable'
			,shadow:false
			,border:false
			,frame:false
			,width: copyWidth
			,style:'position:absolute; top:60; right:10; z-index:9050'
			,items:[{
				xtype:'label'
				,cls:'copyAvailableText'
				,id:'id_ResultRange'
				,html:'Displaying Results</br>5 - 9 of 256'
			}]
		});
		d.copyAvailable.render(Ext.getBody());
		d.rightArrowButton = new Ext.Element(Ext.DomHelper.insertAfter(RWS.Details.detailMain.el.dom, {
				tag:'div'
				,id:'browseArrow_right'
				,style: 'top:219;right:-7; z-index:200000;'
				,width:'20'
				,height:'50'
			}));
		d.rightArrowButton.addListener({
			'click' : {fn:function() {
				RWS.Details.ScrollRight();
			}}
		});
		d.rightArrow = new Ext.Element(Ext.DomHelper.insertAfter(RWS.Details.detailMain.el.dom, {
				tag:'div'
				,id:'browseArrow_right_cap'
				,style: 'top:155;right:0; z-index:200000;'
			}));
		d.leftArrowButton = new Ext.Element(Ext.DomHelper.insertAfter(RWS.Details.detailMain.el.dom, {
				tag:'div'
				,id:'browseArrow_left'
				,style: 'top:219;left:0; z-index:200000;'
				,width:'20'
				,height:'50'
			}));
		d.leftArrowButton.addListener({
			'click' : {fn:function() {
				RWS.Details.ScrollLeft();
			}}
		});
		d.leftArrow = new Ext.Element(Ext.DomHelper.insertAfter(RWS.Details.detailMain.el.dom, {
				tag:'div'
				,id:'browseArrow_left_cap'
				,style: 'top:155;right:0; z-index:200000;'
			}));
		//Load the close button for the detail page
		d.closeBtn = new Ext.Element(Ext.DomHelper.insertAfter(d.detailMain.el.dom, {
			tag:'div'
			,id:'detailBrowseClose'
			,html:L(27679)//ls_Close_Details
		}));
		d.closeBtn.addListener({
			'click' : {fn:function() {
				RWS.Details.CloseAll();
				//RWS.Bread.RemoveLastCrumb();
			}}
		});
		
		RWS.Details.UpdateRange();
	}
	
	,RefocusMain: function(itemID, didClickIcon){
		didClickIcon = didClickIcon || false;
		if(didClickIcon){
			RWS.Details.hideTitle();
		}
		var d = RWS.Details;
		itemID = itemID.toString();
		if(d.detailBrowse_full){
			d.storeIndex = RWS.Search.Results.stores.main.indexOfId(itemID);
			d.curRecord = RWS.Search.Results.stores.main.getById(itemID);
		}
		shouldSwapBar = false;
		didClickItem = true;
		
		if(!d.detailMain.isVisible())
		{
			d.detailMain = new Ext.ux.detailWindow({
				id:'detailWindow'
				,cls:'yellowWindow'
				,height:Ext.lib.Dom.getViewHeight()
			});
			d.detailMain.loadRecord(d.curRecord);
			var fadeOrigin = didClickIcon ? '' + itemID : 'detailMainBar';
			d.detailMain.show(fadeOrigin);
			d.detailMain.body.on('mouseover', RWS.Details.mouseOverFunc);
			d.detailMainBar.remove();
		}
		else d.detailMain.loadRecord(d.curRecord);
		if(d.detailBrowse_full){
			d.detailBrowse_full.removeClass('mouseover_transparent')
			d.detailBrowse_full.toBack();
			var scrollBody = Ext.getCmp('browse_scroll');
			var nodeIndex = RWS.Details.storeIndex;
			var opt = {
			    duration: .1,
			    easing: 'elasticIn'
			};
			scrollBody.el.setX((Ext.lib.Dom.getViewWidth() / 2 - 75) - 150 * nodeIndex, opt);
			RWS.Details.UpdateRange();
		}
	}
	,ScrollLeft: function(btnID)
	{
		var d = RWS.Details;
		if(d.storeIndex <= 0)
			return;
		d.storeIndex --;
		d.curRecord = RWS.Search.Results.stores.main.getAt(d.storeIndex);
		if(d.detailMain.isVisible()) 
		{
				d.detailMain.loadRecord(d.curRecord);
		}
		RWS.Details.doScroll();
	}
	,ScrollRight: function()
	{
		var d = RWS.Details;
		d.storeIndex++;
		d.curRecord = RWS.Search.Results.stores.main.getAt(d.storeIndex);
		if(d.detailMain.isVisible()) 
		{
				if(d.storeIndex == RWS.Search.Results.stores.main.getCount())				
					return;
				d.detailMain.loadRecord(d.curRecord);
		}
		RWS.Details.doScroll();
	}
	,doScroll: function(){
		RWS.Details.UpdateRange();
		var scrollBody = Ext.getCmp('browse_scroll');
		var nodeIndex = RWS.Details.storeIndex;
		scrollBody.el.setX((Ext.lib.Dom.getViewWidth() / 2 - 75) - 150 * nodeIndex, this.scrollConfig);
	}
	,CloseMain: function()
	{
		var d = RWS.Details;
		var leftEdge = d.detailMain.x;
		d.detailMainBar = new Ext.Element(
			Ext.DomHelper.insertBefore(d.detailMain.el.dom, {tag:'div', cls:'detailMainBar', style:'left:'+leftEdge, id: 'detailMainBar', children:[
				{tag:'div', cls:'detailMainBar-l', children:[
					{tag:'div', cls:'detailMainBar-r', children:[
						{tag:'div', cls:'detailMainBar-c', children:[
							{tag:'div', cls:'detailMainBar-content', children:[
								{tag:'span', cls:'detailMainBar-title', html:Ext.util.Format.ellipsis(d.curRecord.data.title,75)}
								,{tag:'span', cls:'detailMainBar-author', html:Ext.util.Format.ellipsis(('by '+d.curRecord.data.author_printable+' (Author)'),50)}
								,{tag:'div', cls:'detailMainBar-open', onclick:'RWS.Details.RefocusMain('+d.curRecord.data.rsn+')'}]}]}]}]}]}
		), true);
	}
	,CloseAll: function()
	{
		var d = RWS.Details;
		if(d.detailBrowse_left)
			d.detailBrowse_left.close();
		if(d.detailBrowse_right)
			d.detailBrowse_right.close();		
		if(d.detailMain)
			d.detailMain.close();
		if(d.detailBrowse_full)
			d.detailBrowse_full.close();		
		if(d.detailMainBar)
			d.detailMainBar.remove();
		if (d.copyAvailable) d.copyAvailable.destroy();
		if (d.closeBtn) d.closeBtn.remove();
		if (d.mask) d.mask.remove();
		if (d.maskBorder) d.maskBorder.remove();
		if (d.rightArrow) d.rightArrow.remove();
		if (d.rightArrowButton) d.rightArrowButton.remove();
		if (d.leftArrow) d.leftArrow.remove();
		if (d.leftArrowButton) d.leftArrowButton.remove();
		//centerPanel.el.dom.firstChild.firstChild.style.overflow = "auto";
		d.active = false;
		
		// Also remove the last bread crumb
		
	}
	,showTitle:function(rsn){
		var bottomBar = Ext.getCmp('browseCapBottom');
		var oldItem = bottomBar.get('hoverText');
		if(oldItem && oldItem.rsn == rsn) return;
		bottomBar.ClearItems();
		var hoverItem = RWS.Search.Results.stores.main.getById('' + rsn);
		var title = hoverItem.json.title;
		var label = new Ext.form.Label({text:title, cls: 'detailHoverText', id:'hoverText', rsn:rsn});
		bottomBar.add(label);
		bottomBar.doLayout();
		var width = label.getWidth();
		var screenWidth = Ext.lib.Dom.getViewWidth();
		var index = RWS.Search.Results.stores.main.indexOf(hoverItem);
		var centerIndex = RWS.Details.storeIndex;
		var xOffset = (((screenWidth / 2 - 75) - 150 * centerIndex) + 150 * (index - centerIndex) + 150 * centerIndex) + 75 - width / 2;
		if(xOffset < 5) xOffset = 5;
		else if (xOffset + width > screenWidth - 5) xOffset = screenWidth - width - 5;
		label.el.setX(xOffset);
		label.el.setY(label.el.getY() + 5);
	}
	,hideTitle:function(){
		bottomBar = Ext.getCmp('browseCapBottom');
		bottomBar.ClearItems();
	}
	,ShowSingleDetails:function(o)
	{

		if(o.data.rsn == 0){
            alert('Record not found.');
            return;
        }
        
		RWS.Details.active = true;
		RWS.Details.domHeight = Ext.lib.Dom.getViewHeight();
		var d = RWS.Details;
		d.curRecord = o;
		Ext.get('centerPanel').dom.firstChild.firstChild.style.overflow = "hidden";
		//Load up the main detail window
		d.detailMain = new Ext.ux.detailWindow({
			id:'detailWindow'
			,cls: 'yellowWindow'
			,height:Ext.lib.Dom.getViewHeight()
			,html:RWS.Details.templates.item
		});	

		d.detailMain.loadRecord(d.curRecord);
		RWS.Search.AddPastItem(d.curRecord);

		//Load the window mask to make the details page modul
		d.detailMain.show('Image' + d.curRecord.data.rsn);
		d.mask = new Ext.Element(
			Ext.DomHelper.insertBefore(d.detailMain.el.dom, {cls:'ext-el-mask',style:'z-index:9000'}, true));
		d.maskBorder = new Ext.Element(
			Ext.DomHelper.insertBefore(d.detailMain.el.dom, {tag:'div', cls:'maskBorder', id:'mask', children:[
				{tag:'div', cls:'maskBorder-tl', children:[
					{tag:'div', cls:'maskBorder-tr', children:[
						{tag:'div', cls:'maskBorder-tc', html:'&nbsp;'}]}]}
				,{tag:'div', cls:'maskBorder-ml', children:[
					{tag:'div', cls:'maskBorder-mr', children:[
						{tag:'div', cls:'maskBorder-mc', style:'height:' + (Ext.lib.Dom.getViewHeight() - 29), html:'&nbsp;'}]}]}
				,{tag:'div', cls:'maskBorder-bl', children:[
					{tag:'div', cls:'maskBorder-br', children:[
						{tag:'div', cls:'maskBorder-bc', html:'&nbsp;'}]}]}
			]}, true)
		);
		var mask = Ext.get('mask');
		RWS.Details.maskMid = Ext.get('maskMid');
		var domWidth = Ext.lib.Dom.getViewWidth();
		var copyWidth = 150;
		if(domWidth < 1100)
			copyWidth = 120;

		//Load the close button for the detail page
		d.closeBtn = new Ext.Element(Ext.DomHelper.insertAfter(d.detailMain.el.dom, {
			tag:'div'
			,id:'detailBrowseClose'
			,html:L(27679)//ls_Close_Details
		}));
		d.closeBtn.addListener({
			'click' : {fn:function() {
				RWS.Details.CloseAll();
				//RWS.Bread.RemoveLastCrumb();
			}}
		});

	}
	,CloseSingleMain:function()
	{
		var d = RWS.Details;
		d.closeBtn.remove();
		d.mask.remove();
		d.maskBorder.remove();
		d.detailMain.close();
		centerPanel.el.dom.firstChild.firstChild.style.overflow = "auto";
	}
	,GetMLACitation : function(data)
	{
		var citation='';
		
		if (data.author_printable.length) 
			citation += data.author_printable + ". ";
			
		if (data.title.length)
			citation += '<u>' + data.title + '</u>' + ". ";

		var needsPeriod = false;
		for(i = 0; i < data.series.length; i ++){
			if(i == 0 && data.series[0].length > 0) 
				citation += String.format(L(3944).toString(), data.series[0]);
			else if(data[i] == data[i - 1]){
				i ++; 
				if(i >= data.series.length)
					break;
			} 
			else if (data.series[0].length > 0){
				citation += ", " + data.series[i];
				needsPeriod = true; 
			} 
		} 
		if(needsPeriod) 
			citation += '.';
		citation += ' ';
		if (data.place.length)
			citation += data.place + ": ";
		
		if (data.publisher.length)
			citation += data.publisher + ", ";
		
		if (data.year.length)
			citation += data.year + '. ';
		return citation;
	}

}
var shouldSwapBar = true;
var didClickItem = false;

Ext.ux.detailRow = Ext.extend(Ext.Panel , {
	data:''
	,dataLabel:''
	,initComponent: function(){
		Ext.apply(this,{
			layout:'column'
			,cls:'detailRow'
			,items:[{
				id:this.id+'_label' 
				,width:100
				,xtype:'label'
				,cls:'datalabel'
				,html:this.dataLabel + ":"
			},{
				columnWidth:1
				,id:this.id+'_data' 
				,xtype:this.dataCont
				,cls:this.labelCls 
				,text:this.data 
			}]
		});
		Ext.ux.detailRow.superclass.initComponent.apply(this, arguments);
	}
	,updateData: function(data){
		switch(this.dataCont)
		{
			case 'label':
				this.findById(this.id+'_data').setHtml(data);
				break;
			case 'panel':
				var p = this.findById(this.id+'_data')
					p.html = data;
					if(p.rendered)
						p.el.update(data);
				break;
		}
	}
})
Ext.reg('detailRow', Ext.ux.detailRow);

Ext.ux.studyProgram = Ext.extend(Ext.Panel , {
	readCntData:''
	,readCntDataLabel:''
	,accReadData:''
	,accReadDataLabel:''
	,populatePrograms: function(data) {
		var studyProgramInfo = Ext.getCmp('studyProgramInfo');
		if(studyProgramInfo)
		{
			if(studyProgramInfo.items)
			{
				for(var i = studyProgramInfo.items.length-1; i >= 0; i--)
					studyProgramInfo.remove(studyProgramInfo.items.items[i],true);	 
			}
		}		
			
		for(var i = 0; i < data.length; i++)
			studyProgramInfo.insert(i,CreateObjectForStudyPrograms(data[i]));
	}
})
Ext.reg('studyProgram', Ext.ux.studyProgram);

//The main detail window
Ext.ux.detailWindow = Ext.extend(Ext.Window, {
	resizable:false
	,draggable:false
	,shadow:false
	,layout: 'column'
	,style: "background: none;"
	,listeners: {
		'beforeclose': { fn: function(){
				//Notify the RWS.Details that the main detail window is closing
				RWS.Details.CloseMain();
			}
		}
	}
	,loadRecord: function(r){
		RWS.Details.curRecord = r;
		d = r.data;
		var studyPrograms = CreateDivsForStudyPrograms(d.studyPrograms);
		var attachments = CreateDivsForAttachments(d.attachments);
		var html = 	'<div align = "center"><img src = ' + d.image + ' class = "detailsImage"></div> <br>';
		if(COMP.Prefs.reviewSystemOn)
			html += '<div align= "center" style="padding-bottom: 10px;"> <img id="detailStars_' + d.rsn + '" onmouseover="LoadTooltip(\'detailStars_' + d.rsn + '\')" src="/images/rws_stars_' + d.averageStars + '.png" style="width:126;height:28"></div>';
			html += '<table width = 155>';
			if(COMP.Config.GetDefault('allowReservations'))
				html +=	'<tr> <td class = "detailWestButtonRow"> <label class="detailWestButtonText" onclick="RWS.Circ.Show(' + d.rsn + ')">' + L(5891) + '</label> </td> </tr>';
			if(COMP.Config.GetDefault('allowHolds'))
				html +=	'<tr> <td class = "detailWestButtonRow"> <label class="detailWestButtonText" onclick="RWS.Holds.Request(' + d.rsn + ')">' + L(3667) + '</label> </td> </tr>';
			html +=		'<tr> <td class = "detailWestButtonRow" id = "addToList"> <label class="detailWestButtonText" onclick="RWS.SavedLists.ShowAddToListMenu(d, \'addToList\')">' + L(6899) + '</label> </td> </tr>' +
						'<tr> <td class = "detailWestButtonRow"> <label class="detailWestButtonText" onclick="RWS.Search.Results.ShowClipboardDialog(\'item\', ' + d.rsn + ')">' + L(26603) + '</label> </td> </tr>'; //Link to this Item
			if(d.capstone.length <= 0)
				html +=	'<tr style="padding-top:5px;"><td width=48 height=48 style="background: url(/images/small_icon_sprite_map.png); background-position: -48 -2600;" ' +
							'onClick="COMP.OpenWindow(\'' + String.format(COMP.V.sneakpeek, d.isbn) + '\')" ' +
							'class="sneakPeek" id="sneakPeak">' +
						'</td></tr>';
			html += '</table>';	
		this.left = new Ext.Panel({style: "width: 160; valign: top", html: html, autoScroll: true});
		html = '<table>' +
					'<tr>' +
						'<td width = 440>' +
							'<label id="year">' + d.year + '</label>' +
							'<label id="title">' + d.title + '</label><br>';
		if(d.subtitle.length > 0)
			html +=			'<label id="subtitle">' + d.subtitle + '</label><br>';
		if(d.statement.length > 0)
			html +=			'<label id="statement">' + d.statement + '</label><br><br>';
		html +=				'<table width = "430"><tr><td width="300">' +
								'<label id="author"  class="textlink" onclick="RWS.Details.StartSearch(\'author\',' + '\'' + d.author_printable + '\'' + ')">' + d.author_printable + '</label>' +
							'</td><td align="right">' +
								'<label class="textlink" style="padding-left:5px" id="callnum" onclick="var cn = \'' + d.callnum + '\';' +
									'var spaceAt = cn.indexOf(\' \');' +
									'var periodAt = cn.indexOf(\'.\');' +
									'if (spaceAt > 0 && (periodAt < 0 || periodAt > spaceAt)){' +
										'cn = cn.substr(0,spaceAt+2);' +
									'}' +
									'else if (periodAt > 0 && (spaceAt < 0 || spaceAt > periodAt)){' +
										'cn = cn.substr(0,periodAt);' +
									'}' +
									'RWS.Details.StartSearch(\'callnum\',cn)">' + d.callnum + '</label>' +
									
							'</td></tr><tr><td width="300">' +
								'<label id="authors_printable" class="textlink">' + d.authors_printable + '</label>' +
							'</td><td align="right">' +
								'<label id="medium">' + d.medium + '</label>' +
							'</td></tr></table><br>' + 
							'<label id="summary">' + d.summary + '</label><br><br>';
		if(d.summary2.length > 0)
			html +=						'<label id="summary2">' + d.summary2 + '</label><br><br>';
		html +=			'</td>' +
					'</tr>' +
					'<tr>' +
						'<td>' +
							'<table width = 100%>';
		if(d.capstone.length > 0) 
			html +=				'<tr>' + 
									'<td style="background: url(/images/large_icon_sprite_map.png); background-position: 0 -1600; background-repeat:no-repeat; cursor:pointer;" ' + 
							 			'onClick="COMP.OpenWindow(\'' + d.capstone + '\')" class="detailsCapstone" width=170 height=75></td>' + 
							 	'</tr>'; 
		html +=					'<tr width = 100%>'; 
		if(d.lexile.length > 0) 
			html += 				'<td width = 100">' +
										'<label id="lexile"><b>' + L(17705) + ':</b> ' + d.lexile + '</label>';
									'</td>'; 
		if(d.capstone.length <= 0)  
			html +=					'<td align="right">' + 
									 	'<label id="copyInfo" class="copyInfo textlink" onclick="RWS.Search.SendCopiesRequest(' + d.rsn + ');">' + 
									 		String.format(L(13243).toString(),d.copiesavail,d.copycount,d.holds) + '</label>' + 
									 '</td>'; 
		html +=					'</tr>' +
							'</table>' +
						'</td>' +
					'</tr>' +
					'<tr>' +
						'<td id="pubInfo" class="detailSection">' +
							'<div style="border-style: solid; border-width:0px 0px 2px 0px;">' +
								'<label><b>' + L(8126) + '</b></label>' +
							'</div>' +
								GetPanelData(d, [{label:L(3691) ,data:'publisher' ,search:true},
											{label:L(3692) ,data:'series' ,search:true},
											{label:L(18623) ,data:'otherTitles'},
											{label:L(4095) ,data:'extent'},
											{label:L(4091) ,data:'edition'},
											{label:L(13244) ,data:'curfrequency'},
											{label:L(13245) ,data:'forfrequency'},
											{label:L(13246) ,data:'pubDates'},
											{label:L(5887) ,data:'mlaCitation'},
											{label:L(4083) ,data:'lccn'},
											{label:L(4084) ,data:'isbn'},
											{label:L(3693) ,data:'subjects' ,search:true}]) +
						'</td>' +
					'</tr>' +
					'<tr>' +
						'<td id="notesInfo" class="detailSection">' +
							'<div style="border-style: solid; border-width:0 0 2 0">' +
								'<label><b>' + L(4076) + '</b></label>' +
							'</div>' +
								GetPanelData(d, [{label:L(4080) ,data:'existsAtPublicLibrary'},
											{label:L(4716) ,data:'contentnotes'},
											{label:L(4141) ,data:'generalnotes'},
											{label:L(5139) ,data:'biblioCodes'},
											{label:L(5140) ,data:'curriculum'},
											{label:L(5888) ,data:'interestCodes'},
											{label:L(4444) ,data:'withData'},
											{label:L(12484) ,data:'dissertation'},
											{label:L(12485) ,data:'biblioetc'},
											{label:L(12486) ,data:'restrictions'},
											{label:L(12487) ,data:'scale'},
											{label:L(12488) ,data:'creationCreds'},
											{label:L(12489) ,data:'citationRefs'},
											{label:L(12490) ,data:'participants'},
											{label:L(12491) ,data:'typeOfReport'},
											{label:L(12492) ,data:'numPeculiarities'},
											{label:L(12493) ,data:'typeOfCompFile'},
											{label:L(12494) ,data:'dateTimePlace'},
											{label:L(12495) ,data:'geoCoverage'},
											{label:L(12496) ,data:'prefCitation'},
											{label:L(12497) ,data:'supplement'},
											{label:L(12498) ,data:'addionalPhycForms'},
											{label:L(9503) ,data:'bioHistData'},
											{label:L(5568) ,data:'language'},
											{label:L(12500) ,data:'awards' ,search:true}]) +
						'</td>' +
					'</tr>';
		if(studyPrograms){
		html += 			'<tr>' +
						'<td class="detailSection">' +
							'<div style="border-style: solid; border-width:0 0 2 0">' +
								'<label><b>' + L(7778) + '</b></label>' +
							'</div>' +
								studyPrograms +
						'</td>' +
					'</tr>';
		}
		if(attachments){
		html +=				'<tr>' +
						'<td id="attachmentInfo" class="detailSection">' +
							'<div style="border-style: solid; border-width:0 0 2 0">' +
								'<label><b>' + L(5124) + '</b></label>' +
							'</div>' +
							'<div>' +
								attachments +
							'</div>' +
						'</td>' +
					'</tr>';
		}
		if((COMP.Config.GetDefault('allowViewMarcRecords') || COMP.Config.GetDefault('showAmazonLinks') || COMP.Prefs.reviewSystemOn) && d.isbn.length >= 8){
		html +=				'<tr>' +
						'<td id="otherInfo" class="otherSection detailSection">' +
						'<div style="border-style: solid; border-width:0 0 2 0">' +
								'<label><b>'+L(4227)+'</b></label>' +//Other
							'</div>' +
							'<div>';
		if(COMP.Config.GetDefault('allowViewMarcRecords'))
		html +=					'<label id="viewMARCLink" class="detailLinkBtn textLink" onclick="ShowMarcRecords(\'' + d.rsn + '\',true);">'+L(5890)+'</label><br>'; //View MARC Record

		if(COMP.Prefs.reviewSystemOn)
		{
			if(RWS.Patron.current.data.rsn > 0)
				html +=					'<label id="addReviewLink" class="detailLinkBtn textLink" onclick="ShowAddReviewDialog(\'' + d.rsn + '\',true);"> '+L(27384)+'</label><br>';//ls_Add_Review
			html +=					'<label id="ShowReviewLink" class="detailLinkBtn textLink" onclick="ShowReviews(\'' + d.rsn + '\',0);"> '+L(27385)+'</label><br>';//ls_Show_Reviews
		}

		if(COMP.Config.GetDefault('showAmazonLinks') && d.isbn.length >= 8)
		html +=					'<label>' + L(13005).append(':')+ '</label><br>' +
								'<img src = "/images/blank.gif" height = 25 width = 117 onClick="ShowAmazonLink(\'' + d.isbn + '\',true)" class = "amazonLink"></img>'
		html +=				'</div>' +
						'</td>' +
					'</tr>';
		}
		html +=	'</table>'
		this.main = new Ext.Panel({
			style: "width: 450px; height: 100%; overflow-y: auto;",
			id:'stupid-innerbox',
			html: html,
			autoScroll: true,
			bodyStyle: "height: 100%; overflow-y: auto;"
		});
		this.removeAll();
		this.add(this.left);
		this.add(this.main);
		this.doLayout();
	}
	,initComponent: function(){
		Ext.ux.detailWindow.superclass.initComponent.apply(this, arguments);
	}
});
Ext.reg('detailWindow', Ext.ux.detailWindow);

function LoadTooltip(id)
{
	var rsn = id.toString().split('_');
	rsn = rsn[1];
	var myID = id + "_reviewPanel";
	


	var store = new Ext.data.JsonStore({
		url: 'ajax/rws_get_review_history_for_title'
		,baseParams: { 
			sessionid : COMP.V.sessionid
			,rsn: rsn
		}
		,fields: ['ave', 'numWritten', 'five', 'four', 'three', 'two', 'one', 'perc_five', 'perc_four', 'perc_three', 'perc_two', 'perc_one', 'rsn']
	});

	//var html = 
	// if(RWS.Patron.current.data.rsn > 0)
	// {
	// 	html += ;
	// }
	// html +=	  

	var tpl = new Ext.XTemplate(
		'<tpl for=".">',
			'<div class="review-history">',
				'<div style="text-align:center">',
					'<span class="reviewText" style="font-size:12px" onclick="ShowReviews({rsn}, 0);">',
						'Read All Reviews ({numWritten})',
					'</span>',
				'</div>',
			'<table><tr><td>',
					'<span class="reviewText" style="font-size:12px" onclick="ShowReviews({rsn}, 5);">5 Stars</span>',
				'</td><td>',
					'<div style="width:100; height:15; background-color:white">', 
						'<div style="width:{perc_five};height:15;background-color:blue"></div>',
					'</div>',
				'</td><td>',
					'<div style="font-size:12px">({five})</div>', 
				'</td></tr><tr><td>',
					'<span class="reviewText" style="font-size:12px" onclick="ShowReviews({rsn}, 4);">4 Stars</span>',
				'</td><td>',
					'<div style="width:100;height:15;background-color:white">', 
						'<div style="width:{perc_four};height:15;background-color:blue"></div>',
					'</div>',
				'</td><td>',
					'<div style="font-size:12px">({four})</div>', 
				'</td></tr><tr><td>',
					'<span class="reviewText" style="font-size:12px" onclick="ShowReviews({rsn}, 3);">3 Stars</span>',
				'</td><td>',
					'<div style="width:100;height:15;background-color:white">', 
						'<div style="width:{perc_three};height:15;background-color:blue"></div>',
					'</div>',
				'</td><td>',
					'<div style="font-size:12px">({three})</div>', 
				'</td></tr><tr><td>',
					'<span class="reviewText" style="font-size:12px" onclick="ShowReviews({rsn}, 2);">2 Stars</span>',
				'</td><td>',
					'<div style="width:100;height:15;background-color:white">', 
						'<div style="width:{perc_two};height:15;background-color:blue"></div>',
					'</div>',
				'</td><td>',
					'<div style="font-size:12px">({two})</div>', 
				'</td></tr><tr><td>',
					'<span class="reviewText" style="font-size:12px" onclick="ShowReviews({rsn}, 1);">1 Star</span>',
				'</td><td>',
					'<div style="width:100;height:15;background-color:white">', 
						'<div style="width:{perc_one};height:15;background-color:blue"></div>',
					'</div>',
				'</td><td>',
					'<div style="font-size:12px">({one})</div>',
				'<tpl if="RWS.Patron.current.data.rsn &gt; 0">', 
					'</td></tr><tr><td colspan=2 style="text-align:center">',
					 '<span class="reviewText" style="font-size:12px" onclick="ShowAddReviewDialog({rsn},true);">Add Review</span>', 
				 '</tpl>', 
				 '</td></tr></table>',
			 '</div></tpl>',
		'<div class="x-clear"></div>'
	); 
	                                 
	
	var existing = Ext.getCmp(myID); 

	if(existing != undefined)
	{
		//existing.findById('dv').refresh();
		existing.destroy();
		//return;  
	}

	var tool = new RWS.Controls.ToolTip({
		target: id
		,id: myID
		,anchor:'top'
		,store:store
		,dismissDelay:500
		,width:190
		,items:{
			xtype:'dataview'
			,store:store
			,tpl: tpl
			,id:'dv'
			,itemSelector: 'div.review-history'
			,emptyText : L(27386)//ls_No_reviews
			,listeners:{click:function(dv, index, node, e){
				this.ownerCt.hide(null, true);
			}}
		}
		,listeners:{
			beforerender:function(tool){
				tool.store.load();
			}
		}
	});
	tool.show();
}

function GetPanelData(d, fields){
	var html = '<table>';
	for(var i = 0; i < fields.length; i ++){
		var data = d[fields[i].data]; 
		var label = fields[i].data;
		var displayLabel = fields[i].label.toString();
		var search = fields[i].search || false;
		if (label == 'mlaCitation' && COMP.Config.GetDefault('showCitationReferences')){ 
			data = RWS.Details.GetMLACitation(RWS.Details.curRecord.data); 
			html += '<tr><td class="fieldLabel" valign="top"><label>' + displayLabel + ':</label></td><td>'; 
			html += '<label>' + data + '</label>';
		}
		else if (label == 'existsAtPublicLibrary')		{
			if(data && COMP.Prefs.showAvailAtLocalLibSavedLists){
				data=L(26604); //This item is available at your public library
				html += '<tr><td class="fieldLabel" valign="top"><label>' + displayLabel + ':</label></td><td>'; 
				html += '<label>' + data + '</label>';
			}
		}
		if (typeof(data) == 'string')
			data = data.trim();
		else if (typeof(data) == 'object')
		{
			for (var j=data.length-1; j>=0; j--)
			{
				if (typeof(data[j]) == 'string')
				{
					data[j] = data[j].trim();
					if (data[j].length==0)
						data.removeRange(j,j);
				}
				if(data[j] == undefined)
					data.removeRange (j, j);
			}
		}
		if(d[fields[i].data] && d[fields[i].data].length > 0){
			if(label != 'series' || data[0].length > 0) 
				html += '<tr><td class="fieldLabel" valign="top"><label>' + displayLabel + ':</label></td><td>';
			if (label == 'series' && data){
				for (var j=0; j<data.length; j++){
					if(j > 0)
						html += '<br>';
					if(data[j + 1] != undefined && data[j] == data[j + 1]){
						j ++; 
					}  
					html += '<label class="textlink" onclick="RWS.Details.StartSearch(\''+label+'\',\'' + data[j].before(';') + '\')">'+data[j] + '</label>';
				}
			}
			else if (label == 'publisher' && data)
			{
				var item = RWS.Details.curRecord.data;
				html +='<label class="textlink" onclick="RWS.Details.StartSearch(\'publisher\', \'' + d[fields[i].data] + '\')">'
				if (item.place && item.place.length)
					html += '[' + item.place + ']: ' + data;
				if (data.length && item.year && item.year.length)
					html += ', ' + item.year;
				html += '</label>'
			}
			else if (typeof(data) == 'object')
			{
				
				for (var j=0; j<data.length; j++){
					if(j > 0){
						html += '<br>';
					}
					html += '<label class="textlink" onclick="RWS.Details.StartSearch(\''+label+'\',\'' + data[j] + '\')">'+data[j] + '</label>';
				}
			}
			
			else{
				data = data.replace(/\r/g, '<br>');//24817
				html += '<label';
				if(search)
					html += ' class="textlink" onclick="RWS.Details.StartSearch(\''+label+'\',\'' + data + '\')"';
				html += '> ' + data + '</label>';
			}
		}
	}
	html += '</td></tr></table>';
	return html;
}

function CreateDivsForStudyPrograms(dataArray, cnt)
{
	var html = '';
	for(var i = 0; i < dataArray.length; i ++){
		var data = dataArray[i];
		html += '<div>' +
					'<table>' +
						'<tr><td colspan = 2><label><b>' + data.programName + '</b></label></td></tr>' +
						'<tr><td align = "right"><label>' + data.interestLevelHeader + ':</label></td><td><label>' + data.interestLevel + '</label></td></tr>' +
						'<tr><td align = "right"><label>' + L(6914) + ':</label></td><td><label>' + data.readingLevel + '</label></td></tr>' +
						'<tr><td align = "right"><label>' + L(6918) + ':</label></td><td><label>' + data.titlePoints + '</label></td></tr>' +
						'<tr><td align = "right"><label>' + L(6919) + ':</label></td><td><label>' + data.testNumber + '</label></td></tr>' +
						'<tr><td align = "right"><label>' + L(6920) + ':</label></td><td><label>' + data.holdingCode + '</label></td></tr>' +
					'</table>' +
				'</div>';
	}
	if(html == '') return false;
	return html;
}


function CreateDivsForAttachments(data){
	var html = '';
	for (var i=0; i<data.length; i++)
		html += '<div class="textlink" onclick="COMP.OpenWindow(\'' + data[i].url + '\')">' + data[i].name + '</div>';
	if(html == '') return false;
	return html;
}

Ext.ux.detailBrowseWindow = Ext.extend(Ext.Window, {
	height:230
	,y:127
	,cls:'detailBrowseWindow'
	,shadow:'sides'
	,shadowOffset:9
	,frame:false	
	,border:true
	,modal:false
	,draggable:false
	,resizable:false
	,closable:false
	//,layout:'fit'
	,defaults:{border:false}
	,addItem: function(index, record) {
		if(record)
		{
			var body = Ext.getCmp('browse_scroll');
			var newItem = new Ext.ux.imageButton({
				id:record.data.rsn
				,labelText:record.data.title
				,image:record.data.image
				,ground:'fore'
				,imgHeight:132
				,cls:'detailBrowseImage'
				,width:150
				,x: (index * 150)
				,style:'text-align: center;'
				,onClick: function(){
					RWS.Details.RefocusMain(this.id);
					RWS.Details.UpdateRange();
					Ext.getCmp('detailBrowse_full').loadItems();
				}
			});
			body.add(newItem);
			//Ext.getCmp(record.data.rsn).items[0].setPosition(20,0);
			body.hasItem[index] = true;
		}
	}
	,initComponent: function(){
		type:'full'
		,Ext.apply(this, {
			items:[
			{
				xtype:'panel'
				,id: 'browseCapTop'
				,cls:'detailBrowseCap'
				,height:27
			},{
				xtype:'panel'
				,id:this.id + '_body'
				,cls:'detailBrowseBody'
				,height:176
				,layout:'absolute'
			},{
				xtype:'panel'
				,id: 'browseCapBottom'
				,cls:'detailBrowseCap'
				,height:27
			}]
		});		
		Ext.ux.detailBrowseWindow.superclass.initComponent.apply(this, arguments);
			
		var width = Ext.lib.Dom.getViewWidth();
		
		if(this.type == 'full')	
			width -= 86;
		else
			width -= 43;
		var qty = parseInt(width / 120);
		RWS.Details.mQuantity = qty;
		var body = Ext.getCmp(this.id+'_body');
		var leftArrow = {
			id:'browseArrow_left'
			,xtype:'icon'
			,style: 'background: url(/images/small_icon_sprite_map.png); background-position: -0 -2000;'
			,width:40
			,height:50
			,onClick: function(){RWS.Details.ScrollLeft(this.id)}
		}
		var itemsWidth = RWS.Search.Results.stores.main.data.items.length * 150;
		var screenWidth = Ext.lib.Dom.getViewWidth();
		var scrollWidth = itemsWidth;
		if(screenWidth > itemsWidth)
			scrollWidth = screenWidth;
		var scrollingPanel = {
			xtype:'panel'
			,border:false
			,layout:'absolute'
			,id:'browse_scroll_enclose'
			,height:180
			,style:'overflow-x: hidden;'
			,items:[{
				xtype:'panel'
				,id:'browse_scroll'
				,border:false
				,layout:'absolute'
				,height:180
				,width:scrollWidth
				,style:'overflow-x: hidden;'
				,x:0
				,y:0
				,hasItem:[false]
				,items:RWS.Details.detailBrowseFull
			}]
		}
		body.add({
			border:false
			,x:0
			,y:0
			,width: scrollWidth
			,items:[scrollingPanel]
		});
		RWS.Details.UpdateRange();
		//this.loadItems();
	}
	// ,toFront: function(){
	// 	//if(Ext.getCmp('detailWindow'))	
	// 	//	return false;
	// }
});
Ext.reg('detailBrowseWindow', Ext.ux.detailBrowseWindow);

// Cheap hack to handle resizing.
Ext.lib.Event.on(window, "resize", function() {
	RWS.Details.RequestResize();
	// var task = new Ext.util.DelayedTask(function(){
	//     });
	// task.delay(250);
}, this);

function GetAuthorsHtml(authors)
{
	var html=''
	for (var i=0; i<authors_printable.length; i++)
	{
		var author = authors_printable[i].trim();
		if (author.length)
		{
			html += '<label onclick="RWS.Details.StartSearch(\'author\',\'' + author + '\')">'+author + '</label><br>';
		}
	}
	return html;
}

function ShowAddReviewDialog(value) 
{
	var reviewDialog = new Ext.Window({
		cls:'dialog'
		//,layout: 'vbox'
		,title:L(27387)+':'//ls_Add_a_review_for_this
		,width:500
		,height:600
		,maximizable:true
		,resizable:false
		,id:'reviewDialog'
		,modal: true	
		,listeners:{
			beforerender : function() {
				COMP.SendAjaxCommand('rws_get_patron_title_review', {
					title_rsn : value
					,patron_rsn : RWS.Patron.current.data.rsn
				},
				function(o) {
					if (o.stars) {
						RWS.StarSelector.Setstars(o.stars);
						RWS.StarSelector.Fillstars(o.stars);
						var btn = Ext.getCmp('removeButton');
						if(btn) {
							btn.setVisible(true);
							reviewDialog.doLayout();
						}
					}
					else
					{
						RWS.StarSelector.Setstars(3);
						RWS.StarSelector.Fillstars(3);
					}
					if (o.comment) {
						var text = Ext.getCmp('reviewComment');
						if (text) {
							text.setValue(o.comment);
						}
					}
				}, this);
			}
		}
		,items: [{
			xtype: 'panel'
			,layout:'hbox'
			,height:30
			,id:'starpanel'
			,width:'100%'
			,items:[{
				xtype:'box'
				,id:'star1'
				,autoEl: {
					tag:'img'
					,src:'/images/rws_star_empty.png'
					,onMouseOver:'RWS.StarSelector.Fillstars(1)'
					,onMouseOut:'RWS.StarSelector.Fillstars(0)'
					,onClick:'RWS.StarSelector.Setstars(1)'
				}
				,width:29
				,height:28
			},{
				xtype:'box'
				,id:'star2'
				,autoEl: {
					tag:'img'
					,src:'/images/rws_star_empty.png'
					,onMouseOver:'RWS.StarSelector.Fillstars(2)'
					,onMouseOut:'RWS.StarSelector.Fillstars(0)'
					,onClick:'RWS.StarSelector.Setstars(2)'
				}
				,width:29
				,height:28
			},{
				xtype:'box'
				,id:'star3'
				,autoEl: {
					tag:'img'
					,src:'/images/rws_star_empty.png'
					,onMouseOver:'RWS.StarSelector.Fillstars(3)'
					,onMouseOut:'RWS.StarSelector.Fillstars(0)'
					,onClick:'RWS.StarSelector.Setstars(3)'
				}
				,width:29
				,height:28
			},{
				xtype:'box'
				,id:'star4'
				,autoEl: {
					tag:'img'
					,src:'/images/rws_star_empty.png'
					,onMouseOver:'RWS.StarSelector.Fillstars(4)'
					,onMouseOut:'RWS.StarSelector.Fillstars(0)'
					,onClick:'RWS.StarSelector.Setstars(4)'
				}
				,width:29
				,height:28
			},{
				xtype:'box'
				,id:'star5'
				,autoEl: {
					tag:'img'
					,src:'/images/rws_star_empty.png'
					,onMouseOver:'RWS.StarSelector.Fillstars(5)'
					,onMouseOut:'RWS.StarSelector.Fillstars(0)'
					,onClick:'RWS.StarSelector.Setstars(5)'
				}
				,width:29
				,height:28
			}]
		},{
			xtype:'textarea'
			,id:'reviewComment'
			,autoScroll:true
			,height:300
			,width:'100%'
			,text:L(6734) + ':'//Comment
		},{
			layout:'hbox'
			,xtype:'panel'
			,width:'100%'
			,height: 50
			,items : [{
				xtype:'button'
				,iconCls:'actions'
				,id:'removeButton'
				,text:L(4653)//Delete
				,hidden:true
				,handler:function() {
					COMP.SendAjaxCommand('rws_remove_review', {
						title_rsn : value
						,patron_rsn : RWS.Patron.current.data.rsn
					},
					function(o) {
						if(o.success)
							alert(o.success);
						reviewDialog.close();
					}, this);
				}
			},{
				xtype:'button'
				,iconCls:'actions'
				,text:L(19432)//ls_submit
				,handler:function() {
					var star = RWS.StarSelector.Stars;
					var review = reviewDialog.findById('reviewComment').getValue();
					if(!star)
						star = 3;
					COMP.SendAjaxCommand('rws_add_review_for_title', {
						title_rsn : value
						,patron_rsn : RWS.Patron.current.data.rsn
						,stars : star
						,comment : review
					},
					function(o) {
						if (o.error)
							alert(o.error);

						reviewDialog.close();
					},this);
					RWS.StarSelector.Setstars(3);
				}
			},{
				xtype:'button'
				,iconCls:'actions'
				,text:L(17140)//Cancel
				,handler:function() {
					RWS.StarSelector.Setstars(3);
					reviewDialog.close();
				}
			}]
		}]
	});
		
	reviewDialog.show();
}

var FlagReview = function(revUID, patRSN)
{
	COMP.SendAjaxCommand('rws_flag_review_as_naughty', {
		review_uid : revUID
		,patron_rsn : patRSN
	},
	function(o)
	{
		if (o.error)
			alert(o.error);

		if(o.success)
			alert("This review has been flagged as abusive.");
	},this);
}

function ShowReviews(rsn, stars)
{
	if(stars == undefined)
		stars = 0;

	var store = new Ext.data.JsonStore({
		url: 'ajax/rws_get_written_reviews_for_title'
		,baseParams: { 
			sessionid : COMP.V.sessionid
			,title_rsn: rsn
			,star_type: stars
		}
		,fields: ['patronUID', 'patronName', 'stars', 'comment', 'reviewUID']
	});

	var html = '<tpl for=".">' +
			'<div class="show-reviews" style="border-style: solid; border-width:0 0 1 0">' +
				'<img src="/images/rws_stars_{stars}0.png" style="width:126;height:28"> <b style="float:right">{patronName}</b>' +
				'<p>{comment}</p><br>'; 
	if(RWS.Patron.current.data.rsn > 0)
	{
		html +=	'<a href="#" onclick="FlagReview({reviewUID}' +
			',' + RWS.Patron.current.data.rsn + ')">'+L(27388)+'</a>';//ls_Report_Abuse
	}
	html += '</div>' +
		'</tpl>' +
		'<div class="x-clear"></div>';

	var tpl = new Ext.XTemplate(html
	);

	var reviewDialog = new Ext.Window({
		cls:'dialog'
		,title:L(27389)//ls_Title_Reviews
		,id:'reviewDialog'
		,modal: true
		,width: 500
		,height:600
		,autoScroll:true
		,resizable:false
		,items: [{
			xtype:'dataview'
			,store:store
			,tpl: tpl
			,deferEmptyText:true
			,itemSelector:'div.show-reviews'
			,emptyText : L(27390)//ls_No_reviews_have_been_submitted
		}]
	});
	store.load();
	reviewDialog.show();
}
if (window.COMP) COMP.Broadcast('scriptLoaded_rws_details');