// obligatory shout out to emurse.com, whence this came.

var vetoFlag = 0;
var origPosX = 0;
var origPosY = 0;
var dialogWidth = 400;
var dialogAlert = false;
var dialogHeight = '';
var orgDialogHeight = 0;
var stickyDialog = '';
var stickyContent = '';
				
function cancelDialog(ignoreAlert) {

	if(vetoFlag == 1) {
		vetoFlag = 0;
		return false;
	}

	if(ignoreAlert == false) {
		dialogAlert = false;
	}

	if(!($.browser.msie && $.browser.version=="6.0")) {
		document.getElementsByTagName("html")[0].style.overflow = "";
	}


	if(stickyDialog != '') {
		stickyContent = $('#'+stickyDialog).html();
		stickyDialog = '';
	}

	dialogWidth = 400;
    dialogHeight = '';
	$('#dialogContent').height(dialogHeight);
    $('.dialogHide').show();
	if(vetoFlag != 1) {
		if(dialogAlert) { if (!confirm('Are you sure you want to close this dialog? You will lose your changes.')) { return false; }}
		document.getElementById("dialogContent").style.visibility="hidden";
		document.getElementById("dialog").style.visibility="hidden";
		document.getElementById("screen").style.visibility="hidden";
		document.getElementById("dialogContent").style.display="none";
		document.getElementById("dialog").style.display="none";
		document.getElementById("screen").style.display="none";
		var allSelects = document.getElementsByTagName("select");
		for (var i=0;i<allSelects.length;i++) {
			allSelects[i].style.visibility = "visible";
		}
	} else {
		vetoFlag = 0;
		return false;
	}
	window.scroll(origPosX,origPosY);
	return false;
}

function vetoCancelDialog() {
	vetoFlag = 1;
}

function showDialogOK(content) {
	working();
	if(content == '@@@') {
		document.getElementById("dialogContent").innerHTML = '<div id="dialog_wait">Loading...</div>';
	} else {
		document.getElementById("dialogContent").innerHTML = content;
	}
	
	document.getElementById("screen").style.visibility="visible";
	document.getElementById("dialog").style.visibility="visible";
	document.getElementById("dialogContent").style.visibility="visible";
	document.getElementById("dialogContent").style.display="block";
	document.getElementById("dialog").style.display="block";
	document.getElementById("screen").style.display="block";

	if(document.getElementById("dialog").offsetHeight < document.body.offsetHeight) {
		document.getElementById("screen").style.height=document.body.offsetHeight+"px";
	} else {
		document.getElementById("screen").style.height=document.getElementById("container").offsetHeight+"px";
	}

	if (document.getElementById("team_activity")) {
		document.getElementById("team_activity").style.overflow = "hidden";
	}
	var allSelects = document.getElementsByTagName("select");
	for (var i=0;i<allSelects.length;i++) {
		if (allSelects[i].className.indexOf("dialog_select")<0) {
			allSelects[i].style.visibility = "hidden";
		}
	}

    $('.dialogHide').hide();

	if(window.pageYOffset > '0')  {
		origPosX = window.pageXOffset;
		origPosY = window.pageYOffset;
	} else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
		origPosX = document.body.scrollLeft;
		origPosY = document.body.scrollTop;
	} else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
		origPosX = document.documentElement.scrollLeft;
		origPosY = document.documentElement.scrollTop;
	}

	document.getElementById('dialogContent').style.marginTop = (origPosY+20)+'px';
	
	if (dialogWidth) {
		document.getElementById('dialogContent').style.width=dialogWidth+'px';
	}

	if (dialogHeight == 'dynamic') {
		document.getElementById('dialogContent').style.marginTop = '2em';
		windowHeight = $(window).height();
		$('#dialogContent').height(windowHeight-75);
	}

	if(!($.browser.msie && $.browser.version=="6.0")) {
		//document.getElementsByTagName("html")[0].style.overflow = "hidden";
	}

	orgDialogHeight = $('#dialogContent').height();

	$("input:text:first:visible").focus();
	doneWorking();
	return false;
}

function showArticleMediaSelectDialog() {
	showDialogOK('@@@');
    //dialogWidth  = 900;
	//dialogHeight = 600;
	//stickyDialog = 'wire_results_wrapper';
	$.getScript('/?a=dialog-article-select-media', function data(){
		if (swfobject.getFlashPlayerVersion().major > 0 ) {
			var dHeight = $('#dialogContent').height();
			$('#image_viewer').height(0);
			$('#article_media_upload').uploadify({
				'uploader'      : '/lib/uploadify/uploadify.swf',
				'expressInstall': '/lib/uploadify/expressInstall.swf',
				'script'		: '/?a=ajax-adam-upload&licenseid=10026',
				'buttonText'    : 'Upload',
				'fileDataName'  : 'fileData',
				'auto'	  		: true,
				'scriptAccess'  : 'always',
				'cancelImg'     : '/lib/uploadify/cancel.png',
				'fileDesc'      : 'Image files',
				'fileExt'       : '*.jpg;*.jpeg;*.png;*.gif',
				'multi'	 		: true,
				'hideButton'	: false,
				'simUploadLimit': 1,
				'queueID'       : 'fileQueue',
				'scriptData'    : {licenseid : '10026', byline: byline, accountid: _accountid, contentid: $('#contentid').val() },
				'onError'       : function (event, queueID, fileObj, errorObj) {
				    alert("Error");
			    	return true;
				},
				'onSelectOnce'  : function (event, data) {
			    //$('#image').html('');
				},
				'onComplete'    : function (event, queueID, fileObj, response, data) {
			    	response    = eval('(' + response + ')');

				    if (response.isOk != true) {
						alert('There was an error uploading your image. Please try again.');
				    } else {
						imagePick(response.result.id, 1);
						cancelDialog();
			    	}

					needsSaved = true;
				    return true;
				},
				'onAllComplete' : function (event, data) {
				},
				'onOpen'	: function (event, queueID, fileObj) {
				    return true;
				}
		    });
		}

	});

	return false;
}


function showArticleMediaDialog(term) {
	showDialogOK('@@@');
    dialogWidth  = 900;
	dialogHeight = 600;
	$.getScript('/?a=dialog-article-media', function data(){
		var dHeight = $('#dialogContent').height();
		$('#image_viewer').height(0);
		$('#article_media_q').val(term);
		searchAdam(0);
	});

	return false;
}

function showCreateDialog() {
	showDialogOK('@@@');
    dialogWidth  = 400;
//	dialogHeight = 200;
	$.getScript('/?a=dialog-dashboard-create');
    return false;
}

function showApprovalDialog() {
	showDialogOK('@@@');
    dialogWidth  = 800;
	dialogHeight = 'dynamic';
	$.get('/?a=dialog-admin-approval');
    return false;
}

function showFeedbackDialog(){
	showDialogOK('@@@');
	dialogAlert = false;
	var window = location.href;
	$.getScript("/?a=dialog-feedback&location="+window);
	return false;
}

function feedBack() {
	$('#feedback').hide();
	$('#sentMessage').show();
	$.post("/?a=ajax-feedback", $("#feedback").serialize(), function () {
    });
    return false;
}

function showWhyDialog() {
    showDialogOK('@@@');
    dialogWidth  = 400;
    dialogHeight = 400;
    $.getScript('/?a=dialog-why-dialog');
    return false;
}

function showReqReminder(reqid) {
    showDialogOK('@@@');
    dialogWidth  = 700;
    $.getScript("/?a=dialog-request-reminder&reqid="+reqid);
    return false;	
}
