var AdvSearchSlide = null;
var Const_PagingSize = 32000;
var searchSlide;		 		 

function DoLoadSearch()
{
	var oDiv = $('search');
	oDiv.empty();
	var url='SearchItems.asp';
	var myAjax = new Ajax(url, {method: 'get',update: oDiv, 
		onComplete: function() {InitGlobalSearch();searchSlide.slideIn();},
		evalScripts: true});
	myAjax.request();			
}


function InitGlobalSearch () {
	if ($('btnAdvancedSearch') && $('divAdvancedSearch'))
	{
		AdvSearchSlide = new Fx.Slide('divAdvancedSearch');
		 
		$('btnAdvancedSearch').addEvent('click', function(e){
			e = new Event(e);
			if ($('btnAdvancedSearch').getProperty('src').indexOf('ShowAdvancedSearch.gif')!=-1) $('btnAdvancedSearch').setProperties({src: 'Pictures/HideAdvancedSearch.gif',alt:$('hidHideAdvancedSearchButtonCaption').getProperty('value')});
			else $('btnAdvancedSearch').setProperties({src: 'Pictures/ShowAdvancedSearch.gif',alt:$('hidShowAdvancedSearchButtonCaption').getProperty('value')});
				
			AdvSearchSlide.toggle();
			e.stop();
		});	
	}
	
	if ($('formGlobalSearch'))
	{
		$('formGlobalSearch').addEvent('keypress', function(e){
			e = new Event(e);
			
			if (e.event.keyCode == 13 && $('btnStartSearch')) {
				 $('btnStartSearch').click();
				 e.stop();
			}
			
		});			
	}

	if ($('btnStartSearch'))
	{
		$('btnStartSearch').addEvent('click', function(e){
			e = new Event(e);

			$('ExplorerPane').setStyle('display','none')

			var oDiv = $('divSearchResultPane').setHTML('&nbsp;').addClass('ajax-loading');

			$('formGlobalSearch').set('send',{
				onSuccess: function(jsonObjString) {oDiv.removeClass('ajax-loading');addPagingImages(Json.evaluate(jsonObjString));},
				evalScripts: true
			});
			$('formGlobalSearch').send();

			e.stop();
		});			
	}

	if ($('GlobalSearchExplorer'))
	{
		$('GlobalSearchExplorer').addEvent('click', function(e){
			e = new Event(e);

			$('divSearchResultPane').setHTML('&nbsp;');
			$('ExplorerPane').setStyle('display','');

			e.stop();
		});			
	}
	
	if ($('GlobalSearchNewDoc'))
	{
		$('GlobalSearchNewDoc').addEvent('click', function(e){
			e = new Event(e);

			$('ExplorerPane').setStyle('display','none');
			var oDiv = $('divSearchResultPane').setHTML('&nbsp;').addClass('ajax-loading');
	
			oForm = App.createFormGlobalSearch();
			oForm.SearchModel.value = '2';
			oForm.SearchUrlSuffix.value = '';
			oForm.Namespace.value = '';
			oForm.set('send',{
				onSuccess: function(jsonObj) {addPagingImages(Json.evaluate(jsonObj));oDiv.removeClass('ajax-loading');},
				evalScripts: true
			});
			oForm.send();

			//var url='SearchItems.asp?SearchModel=2';
			//var request = new Json.Remote(url, {onComplete: function(jsonObj){oDiv.removeClass('ajax-loading');addPagingImages(jsonObj);},evalScripts: true}).send();

			e.stop();
		});			
	}

	if ($('GlobalSearchUpload'))
	{
		$('GlobalSearchUpload').addEvent('click', function(e){
			e = new Event(e);

			$('ExplorerPane').setStyle('display','none');
			var oDiv = $('divSearchResultPane').setHTML('&nbsp;').addClass('ajax-loading');
	
			var url='uploadFileV2.asp';
			var myAjax = new Ajax(url, {method: 'get',update: oDiv});
			myAjax.request();

			e.stop();
		});			
	}

	AdvSearchSlide.hide();
}

function Categories_Click(URLSuffix)
{
		var oDiv = $('divSearchResultPane').setHTML('&nbsp;').addClass('ajax-loading');

		$('ExplorerPane').setStyle('display','none');

		if (!URLSuffix) return;

		oForm = App.createFormGlobalSearch();
		oForm.SearchModel.value = '';
		oForm.SearchUrlSuffix.value = URLSuffix;
		oForm.Namespace.value = 'www.ActivPortal.mc:Folder';
		oForm.set('send',{
			onSuccess: function(jsonObj) {addPagingImages(Json.evaluate(jsonObj));oDiv.removeClass('ajax-loading');},
			evalScripts: true
		});
		oForm.send();

		
		//var url='SearchItems.asp?SearchUrlSuffix=' + URLEncode(URLSuffix) + '&Namespace=' + URLEncode('www.ActivPortal.mc:Folder');
		//var request = new Json.Remote(url, {onComplete: function(jsonObj){oDiv.removeClass('ajax-loading');
		//	addPagingImages(jsonObj);
		//	},evalScripts: true}).send();
}

function GlobalTreeview_ItemClick()
{
		var oDiv = $('divSearchResultPane').setHTML('&nbsp;').addClass('ajax-loading');

		if (window.event.URLSuffix=='//') return;

		oForm = App.createFormGlobalSearch();
		oForm.SearchModel.value = '';
		oForm.SearchUrlSuffix.value = window.event.URLSuffix;
		oForm.Namespace.value = window.event.Namespace;
		oForm.set('send',{
			onSuccess: function(jsonObj) {addPagingImages(Json.evaluate(jsonObj));oDiv.removeClass('ajax-loading');},
			evalScripts: true
		});
		oForm.send();

		//var url='SearchItems.asp?SearchUrlSuffix=' + URLEncode(window.event.URLSuffix) + '&Namespace=' + URLEncode(window.event.Namespace);
		//var request = new Json.Remote(url, {onComplete: function(jsonObj){oDiv.removeClass('ajax-loading');
		//	addPagingImages(jsonObj);
		//	},evalScripts: true}).send();
}

function SearchUploadSubmitForm()
{
	var oForm = getElByID('SearchUploadFileForm');

	if (oForm.elements["txtFile"].value == '')
	{
		alert('Select a file first!');
		return false;
	}
		
	oForm.submit();
	$('SearchUploadTableForm').setStyle('display','none');
	$('wndUploading').setStyle('display','');	

}

var addImages = function(items, galleryName) {
	var gallery = $(galleryName);
	var ThumbSrc;
	items.each(function(item) {
		var ItemName=item.Name, ItemDesc = item.description;
		if (ItemName.length > 30) {ItemDesc = ItemName + ((ItemDesc && ItemDesc!='')?'\n\n' + ItemDesc:''); ItemName = ItemName.substring(0,30) + '...'}
		var el = new Element('div', {'id':item.Info, 'class': 'GlobalSearchItemPreview', 'title': ItemDesc});
		if (item.URLSuffix) el.setProperty('URLSuffix',item.URLSuffix);
		if (item.DocType) el.setProperty('ContentClass',item.DocType);
		if (item.Thumbnail != '') ThumbSrc = item.Thumbnail; 
		else ThumbSrc = 'Pictures/DefaultDashboard.jpg';
		var img = new Element('img', {'src': ThumbSrc, 'class':'SearchItemImg','style':'height:60px'}).injectInside(el);
		var name = new Element('span', {'class':'GlobalSearchItemName'}).setHTML(ItemName).injectAfter(img);
		el.inject(gallery);
		//DragMe(el,el,true);
		App.DragMe(el,el, function(){}, function(){}
									, function(itemId,parentId,order){
													var MyLayoutForm = App.createFormLayout();
													MyLayoutForm.Zone.value = parentId;
													MyLayoutForm.Order.value = order;
													if (parentId.charAt(0) == '/')
													{
														var oWebpart = App.getWebpartByUrlSuffix(parentId);
														MyLayoutForm.Cmd.value = 'adddocument';
														MyLayoutForm.WebpartID.value = parentId;
														MyLayoutForm.DocumentID.value = (el.URLSuffix)?el.URLSuffix:'';
														MyLayoutForm.ContentClass.value = el.ContentClass;
														MyLayoutForm.wpQualifier.value = oWebpart.getNewDocQualifier();
														
														MyLayoutForm.set('send',{
															onSuccess: function(jsonObj) {
																					var docJson = Json.evaluate(jsonObj).document;
																					docJson.isNew = true;
																					var oDoc = new Activportal.UI.Document(oWebpart, docJson);
																					//oDoc.data.isNew = true;
																					oWebpart.addDocument(oDoc, true);
																					oWebpart.setDocumentOrders(Json.evaluate(jsonObj).urllist, parentId);
															},
															onFailure: function(jsonObj) {alert('erreur')},
															evalScripts: true
														});
													}
													else
													{														
														MyLayoutForm.Cmd.value = 'addwebpart';
														MyLayoutForm.WebpartID.value = (el.URLSuffix && el.ContentClass == 'www.ActivPortal.mc:WebPart')?el.URLSuffix:'';
														MyLayoutForm.DocumentID.value = (el.URLSuffix && el.ContentClass != 'www.ActivPortal.mc:WebPart')?el.URLSuffix:'';
														MyLayoutForm.ContentClass.value = el.ContentClass;
														MyLayoutForm.wpQualifier.value = App.GetNewWPQualifier();
														
														MyLayoutForm.set('send',{
															onSuccess: function(jsonObj) {
																					var oJsonResult = Json.evaluate(jsonObj);
																					for ( var i=0 ; i<oJsonResult.webpart.documents.length ; i++ )
																							oJsonResult.webpart.documents[i].isNew = true;
																					App.addWebpart(oJsonResult.webpart);
																					App.setWebpartOrders(oJsonResult.urllist);
															},
															onFailure: function(jsonObj) {alert('erreur')},
															evalScripts: true
														});
													}

													MyLayoutForm.send();
										}
			);
		//el.addEvent('click', function(e){e = new Event(e).stop();doSelect(item.Info)});
	});
}


function  addPaging(paging, pagingName) {
	var pagingDest = $(pagingName);

	if (paging.Start != '1')
	{
		var previousLink = new Element('span', {'class': 'PagingLink'}).setHTML((eval(paging.Start)-Const_PagingSize).toString()+' - '+(eval(paging.Start)-1).toString());
		previousLink.inject(pagingDest);
		previousLink.addEvent('click', function(e){e = new Event(e).stop();goToPage(eval(paging.Start),-1)});
	}
	var currentResult = new Element('span', {'class': 'PagingCurrent'}).setHTML(paging.Start+' - '+paging.End);
	currentResult.inject(pagingDest);
	currentResult.addEvent('click', function(e){e = new Event(e).stop();previousResult()});
	if (eval(paging.End) < eval(paging.TotalCount))
	{
		var nextLink = new Element('span', {'class': 'PagingLink'}).setHTML((eval(paging.End)+1).toString()+' - '+(eval(paging.End)+Const_PagingSize>eval(paging.TotalCount)?paging.TotalCount:(eval(paging.Start)+Const_PagingSize).toString()));
		nextLink.inject(pagingDest);
		nextLink.addEvent('click', function(e){e = new Event(e).stop();goToPage(eval(paging.End),1)});
	}
}

function addPagingImages(JsonImages) {
	var pagingResult = JsonImages.pagingresult;
	var items = pagingResult[0].result;
	var paging = pagingResult[1].resultcount;
	var gallery = $('divSearchResultPane');
	gallery.empty();
	gallery.removeClass('ResultPane');
	if (eval(paging.TotalCount) == 0) {
		var el = new Element('span', {'class':'SearchItemName'}).setHTML(items);
		el.inject(gallery);
		return
	}
	else 
	{
		if (paging.TotalCount > Const_PagingSize)
		{
			gallery.removeClass('ResultPane');
			var picturesDiv = new Element('div',{'style':'width:100%;height:'+(gallery.offsetHeight-25)+'px;overflow:auto','class':'ResultPaneWithPaging','id':'divSearchResultContainer'});
			picturesDiv.inject(gallery);

			var pagingCell = new Element('div', {'id':'divSearchPagingContainer','class':'paging'});
			pagingCell.inject(gallery);

			addImages(items, 'divSearchResultContainer');
			addPaging(paging, 'divSearchPagingContainer');
			
		}
		else
		{
			gallery.addClass('ResultPane');
			addImages(items, 'divSearchResultPane');
		}
	}
}

function AddSearchFile(sUrl, sCaption, sDocType, sNamespace) {
	$('wndUploading').setStyle('display','none');	
	$('divSearchResultPane').empty();
addImages({result:[{Info:sUrl,URLSuffix:sUrl,Name:sCaption,Description:"",DocType:sNamespace,Thumbnail:"Pictures/Catalog/" + sDocType + ".jpg"}]}.result,'divSearchResultPane');
}
