if (typeof console == "undefined" || typeof console.log == "undefined") var console = { log: function() {} }; 

function dialog( msg, title, throb, okbutton, callbackfn, buttonconfig, otherconfig ) { 
	var theDialog = $('<div class="temp_dialog" />');
	var theTitle = typeof title != 'undefined' ? title : "Processing Your Request...";
	var theMessage = typeof msg != 'undefined' ? msg : "<p>Please wait a moment while we process your request.</p>";
	var buttonconfig = typeof buttonconfig != 'undefined' ? buttonconfig : {};	
	if( typeof okbutton != 'undefined' && okbutton ){
		var okbuttonconfig = buttonconfig = {
			"OK": function(){
				$(this).dialog('close');
			}
		};
		$.extend( buttonconfig, okbuttonconfig );
	}
	var that = this;
	var closeMe = function(){
		$(that).dialog('close');
	}
	var config = {
		modal: true,
		resizable: true,
		minWidth: 300,
		show: 'fade',
		hide: 'fade',
		dialogClass: 'wait-modal',
		close: function(){ 
			if( typeof callbackfn == 'function' ){
				callbackfn.call();
			}
			$(this).dialog('destroy'); 
		},
		buttons: buttonconfig	
	};	
	if( typeof otherconfig != 'undefined' ){
		$.extend( config, otherconfig );
	}		
	if( typeof throb == 'undefined' || throb ) {
		var theThrobber = '<img alt="Loading..." src="/images/ajax-loader-circle-1A6EAE.gif" style="display: block; margin-left: auto; margin-right: auto; padding-top: 10px;" />';
	} else {
		var theThrobber = '';
	}	
	theDialog.attr("title",theTitle).html(theMessage + theThrobber).dialog(config);
};

function slowDialog(){
	setTimeout(function(){
		dialog();
	},500 );
}

function interstitial( type, theTitle, theMessage, destination_positive, buttonconfig, audit ){ 
	var theDialog = $('<div class="temp_dialog" />'),
		audit = typeof audit !== 'undefined' ? audit : false,
		theTitle = typeof title !== 'undefined' ? title : "Important",
		theMessage = typeof msg !== 'undefined' ? msg : "<p>Important details you must read before proceeding to this resource.</p>",
		buttonconfig = typeof buttonconfig !== 'undefined' ? buttonconfig : {
			"Continue": function(){
				if( $(this).closest('div.ui-dialog').find('#agree').is(':checked') ){
					window.open( destination_positive );
					// audit here
					$(this).dialog('close');
				} else {
					alert('You must check the "I Agree" checkbox to indicate your agreement with these policies.');
				}
				
			},
			"Cancel": function(){
				$(this).dialog('close');
			}
	};	

	var config = {
		modal: true,
		resizable: false,
		width: 600,
		height: 600,
		show: 'fade',
		hide: 'fade',
		dialogClass: 'wait-modal',
		close: function(){ 
			if( typeof callbackfn == 'function' ){
				callbackfn.call();
			}
			$(this).dialog('destroy'); 
		},
		buttons: buttonconfig,
		show: {
			effect: 'fade',
			options: {
    			easing: 'linear'
			},
			speed: 1,
			complete: function() {
				var $d = $('div.temp_dialog').closest('div.ui-dialog');
				$d.find('div.ui-dialog-buttonset')
					  .css({'float':'none'})
					.find('button:contains(Cancel)')
					  .css({'color':'red','float':'left','margin-left':'12px','border-color':'red'})
					.end().find('button:contains(Continue)')
					  .css({'color':'green','float':'right','border-color':'green'});
				$('.ui-widget-overlay').css('background','black');
			}
		}
	};	

	function makeDialog(content){	
		theDialog
			.attr("title",theTitle)
			.html( content )
			.dialog(config);
	}
	
	var license_content = 'XXX';
	/* get standard boilerplate */
	$.get('/asyncload/boilerplate_clicklicense.cfm',function(d){
			license_content = d;
			makeDialog(license_content);
	});

	$('div.temp_dialog').find('.ui-dialog-buttonpane button:first').css({'color':'red'});
	
}

function newWin(theURL,winName,features) {
  if ( features.length == 0 ) {
  	theseFeatures = 'menubar=yes,scrollbars=yes,resizable=yes,width=460,height=300';
  } else {
  	theseFeatures = features;
  }
  newWindow = window.open(theURL,winName,theseFeatures);
  newWindow.focus();
}

function popWGI() { 
	var thispubid = $(this).data('publicationid') || $(this).metadata().publicationid;
	if( !Boolean(thispubid) ) return false;
	var wgiwindowURL = '/asyncload/who_gets_it.cfm?publicationid=' + thispubid;
	var thetitle = $(this).data('publicationname') || $(this).metadata().publicationname;
	var wgidata;
	$.ajax({
		url: wgiwindowURL, 
		beforesend: dialog(),
		success: function(data){ 
			var dialogtext = data;
			$('div.temp_dialog').dialog('destroy');
			$('<div></div>').html( dialogtext ).dialog( {
				width: 600,
				height: 600,
				title: 'Who Gets ' + thetitle,
				buttons: { 
					"Close": function() { 			
						$(this).dialog("close");
					}
				},
				modal: true,
				resizable: true,
				show: 'fade',
				hide: 'fade'
			}).find('table.wgi tr:odd').addClass('odd');
		}
			
	});
	return false;
}

$(document).ready( function(){
	/* globals */
	
		$('table#results').find('tr')
			.mouseenter( function(e){ $(this).addClass('results-hover'); } )
			.mouseleave( function(e){ $(this).removeClass('results-hover'); } );	
	
});

