var downloadUrl = null;
function indexSet(url){
	if(url != null)
		downloadUrl = url;
	//this.downloadLightboxObj = new lightbox('div#downloadIndex', this, 'users/download_index_set', this.applyDownloadIndexHandlers);
	//this.downloadLightboxObj = new lightbox('div#downloadIndex', this, 'users/download_index_set/' + $('#ddlIndexSet').val(), this.applyDownloadIndexHandlers);
	this.createLightboxObj = new lightbox('div#createIndex', this, 'users/create_index_set', this.applyCreateIndexHandlers);
	this.init();
}

indexSet.prototype = {
	init: function(){
		var instance = this;
		//download index box button
		$('a#btnDownloadIndexSet').click(function(){
			instance.showDownloadIndexBox();
			return false;
		});
		
		$('a#btnCreateIndexSet').click(function(){
			instance.showCreateIndexBox();
			return false;
		});
	},
	
	showDownloadIndexBox: function(){
		//show download index box
		$('#downloadIndex').remove();
		if($('#ddlIndexSet').val() == '')
			return;
		if(downloadUrl != null)
			this.downloadLightboxObj = new lightbox('div#downloadIndex', this, downloadUrl + $('#ddlIndexSet').val(), this.applyDownloadIndexHandlers);
		else
			this.downloadLightboxObj = new lightbox('div#downloadIndex', this, 'users/download_index_set/' + $('#ddlIndexSet').val(), this.applyDownloadIndexHandlers);
		
		this.downloadLightboxObj.showLightbox();
		
		if ($('div#downloadIndex').length > 0){
			this.applyDownloadIndexHandlers();
		}
	},
	
	showCreateIndexBox: function(){
		//show create index box
		this.createLightboxObj.showLightbox();
		
		if ($('div#createIndex').length > 0){
			this.applyCreateIndexHandlers();
		}
	},
	
	hideDownloadIndexBox: function(){
		//hide download index box
		this.downloadLightboxObj.hideLightbox();
	},
	
	hideCreateIndexBox: function(){
		//hide create index box
		this.createLightboxObj.hideLightbox();
	},
	
	applyDownloadIndexHandlers: function(){
		var instance = this;
		//INITIALIZE THE DOWNLOAD INDEX BOX HERE

		$('div#downloadIndex a.btnCancel, div#downloadIndex div.close a').unbind().click(function(){
			instance.hideDownloadIndexBox();
			return false;
		});
	},
	
	applyCreateIndexHandlers: function(){
		var instance = this;
		//INITIALIZE THE CREATE INDEX BOX HERE
		
		$('div#createIndex input.parent').live('click', function(){
			var subCheckboxes = $(this).parents('li').find('ul input[type=checkbox]');
			if (subCheckboxes.length > 0){
				if ($(this).attr('checked')){
					subCheckboxes.attr('checked', 'checked');
				}
				else{
					subCheckboxes.removeAttr('checked');
				}
			}
		});
		
		$('div#createIndex a.expand').live('click', function(){
			$(this).addClass('collapse').removeClass('expand');
			$(this).parents('li').find('ul').addClass('shown');
			return false;
		});
		
		$('div#createIndex a.collapse').live('click', function(){
			$(this).addClass('expand').removeClass('collapse');
			$(this).parents('li').find('ul').removeClass('shown');
			return false;
		});
		
		$('div#createIndex a.btnCancel, div#createIndex div.close a').unbind().click(function(){
			instance.hideCreateIndexBox();
			return false;
		});
	}
}
