//reCaptcha
var RecaptchaOptions = {
   theme : 'white'
};

//GUI stuff
toggleBlind=function(elementName,callingElement){
	if ($(elementName).style.display=="none"){
		Effect.BlindDown($(elementName),{duration:0.25, queue: { position: 'end', scope: 'catScope' } });
		Effect.Appear($(elementName),{duration:0.25, from:0, to:1, queue: { position: 'end', scope: 'catFadeScope' } });
		//$(callingElement).update("Hide");
	} else {
		Effect.BlindUp($(elementName),{duration:0.25, queue: { position: 'end', scope: 'catScope' } });
		Effect.Fade($(elementName),{duration:0.25, from:1, to:0, queue: { position: 'end', scope: 'catFadeScope' } });
		//$(callingElement).update("Show");
	}
}

var zoomerOpen=false;

function startImageZoom(targetId){
	if (zoomerOpen==true){
		$('image_zoomer').fade({ duration: 0.25,from:1,to:0, afterFinish: function(){ openImageZoom(targetId); }, queue: { position: 'end', scope: 'zoomFadeScope' } });			
	} else {
		openImageZoom(targetId);
	}

}

function openImageZoom(targetId){
	var targetElement=$('image:'+targetId);
	var targetElementBig=$('imageNormalised:'+targetId);
	var offset=$(targetElement).cumulativeOffset();
	
	var width=window.innerWidth || document.body.offsetWidth;
	
	if ($(targetElementBig).getHeight()>40){ //Unloaded images seem to give low non-zero values
		$('image_zoomer').style.left=offset['left']+"px";
		$('image_zoomer').style.top=offset['top']+"px";
		$('image_zoomer').style.width=$(targetElement).getWidth()+"px";
		$('image_zoomer').style.height=$(targetElement).getHeight()+"px";
		$('image_zoomer').update('<a href="#" onclick="closeImageZoom(); return false;" title="Click to close"><img src="'+$(targetElement).src+'&size=image_normalised" style="width:'+$(targetElement).getWidth()+'px;height:'+$(targetElement).getHeight()+'px;" id="zooming_image" /></a>');
	
		var newLeft=( (width/2)-($(targetElementBig).getWidth()/2) )-4;
		var newTop=(offset['top'])-( ($(targetElementBig).getHeight()/2) - ($(targetElement).getHeight()/2) );
	
		new Effect.Morph($('image_zoomer'),{style:'width:'+($(targetElementBig).getWidth()-4)+'px;height:'+($(targetElementBig).getHeight()-4)+'px;left:'+newLeft+'px;top:'+newTop+'px;',duration:1, queue: { position: 'end', scope: 'zoomMorphScope' } });
		new Effect.Morph($('zooming_image'),{style:'width:'+($(targetElementBig).getWidth()-4)+'px;height:'+($(targetElementBig).getHeight()-4)+'px;',duration:1, queue: { position: 'end', scope: 'zoomImageScope' } });
		$('image_zoomer').appear({ duration: 0.5,from:0,to:1, queue: { position: 'end', scope: 'zoomFadeScope' } });
		zoomerOpen=true;
	}
}

function closeImageZoom(){
	$('image_zoomer').fade({ duration: 0.25,from:1,to:0, queue: { position: 'end', scope: 'zoomFadeScope' } });
	zoomerOpen=false;
}

function updateLiveIdea(dateString){
	var newText;
	var auditStampTxt;
	if ($('ideaText')!=null){

		newText=$('idea_text').value;
		newText=newText.replace(/[\n]/g,"<br/>");

		auditStampTxt='<br/><i>'+dateString+' Some of the content of this post may have been edited by the moderator.</i>';
		if ($('auditStamp')!=null){
			$('auditStamp').update(auditStampTxt);
		}

		$('ideaText').update(newText);
	}


	if ($('ideaTitle')!=null){
		$('ideaTitle').update($('idea_title').value);
	}	

	var displayFilename="";
	var imageFile="";
	if ( ($('idea_image_element')!=null) && ($('image:'+$('edit').value)!=null) ){
		if ($('idea_image_element').tagName.toLowerCase()=="img"){ imageFile=$('idea_image_element').src; } else { imageFile=$('idea_image_element').href; }
		if ( (imageFile.slice(imageFile.length-4).toLowerCase()==".jpg") || (imageFile.slice(imageFile.length-4).toLowerCase()==".png") || (imageFile.slice(imageFile.length-4).toLowerCase()==".gif") || (imageFile.slice(imageFile.length-5).toLowerCase()==".jpeg") ){
			$('idea_image_show').update('<img src="'+$('idea_image_element').src+'" border="0" id="image:'+$('edit').value+'" title="Note: Image scaling is disabled until the page is refreshed." />');
			$('idea_image_show').style.display="block";
		} else {
			if ($('idea_image_element').href.indexOf("/")==-1){
				displayFilename=$('idea_image_element').href;
			} else {
				displayFilename=$('idea_image_element').href.substr(imageFile.lastIndexOf("/")+1);			
			}
			$('idea_image_show').update('Attached file: <a href="'+$('idea_image_element').href+'" id="image:'+$('edit').value+'" />'+displayFilename+'</a>');
			$('idea_image_show').style.display="block";			
		}
	} else if ($('idea_image').value!=''){
		if ($('idea_image_element').tagName.toLowerCase()=="img"){ imageFile=$('idea_image_element').src; } else { imageFile=$('idea_image_element').href; }
		if ( (imageFile.slice(imageFile.length-4).toLowerCase()==".jpg") || (imageFile.slice(imageFile.length-4).toLowerCase()==".png") || (imageFile.slice(imageFile.length-4).toLowerCase()==".gif") || (imageFile.slice(imageFile.length-5).toLowerCase()==".jpeg") ){
			$('idea_image_show').update('<img src="'+$('idea_image_element').src+'" border="0" id="image:'+$('edit').value+'" title="Note: Image scaling is disabled until the page is refreshed." />');
			$('idea_image_show').style.display="block";
		} else {
			if ($('idea_image_element').href.indexOf("/")==-1){
				displayFilename=$('idea_image_element').href;
			} else {
				displayFilename=$('idea_image_element').href.substr(imageFile.lastIndexOf("/")+1);			
			}
			$('idea_image_show').update('Attached file: <a href="'+$('idea_image_element').href+'" id="image:'+$('edit').value+'" />'+displayFilename+'</a>');
			$('idea_image_show').style.display="block";			
		}
	} else if ($('idea_image').value==''){
		$('idea_image_show').fade({ duration: 0.25, from: 1, to: 0, queue: { position: 'end', scope: 'ideaImageScope' }});
	}

}

function insertImage(imageKey,imageFile,windowRef,imageElementName,fileElementName){
	if ( (imageFile.slice(imageFile.length-4).toLowerCase()!=".jpg") && (imageFile.slice(imageFile.length-4).toLowerCase()!=".png") && (imageFile.slice(imageFile.length-4).toLowerCase()!=".gif") && (imageFile.slice(imageFile.length-5).toLowerCase()!=".jpeg") ){
		var displayFilename="";
		if (imageFile.indexOf("/")==-1){
			displayFilename=imageFile;
		} else {
			displayFilename=imageFile.substr(imageFile.lastIndexOf("/")+1);			
		}
		windowRef.$(imageElementName).update('Attached file: <a href="getfile.php?key='+imageKey+'\u0026pathname=./files/'+imageFile+'" id="idea_image_element" />'+displayFilename+'</a><br/><input type="button" id="uploadButton" onclick="window.open(\'./upload.php\',\'Change\',\'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, width=540, height=160\'); return false;" value="Change" />');
		windowRef.$(fileElementName).value=imageFile;		
	} else {
		windowRef.$(imageElementName).update('<img src="getimage.php?key='+imageKey+'\u0026pathname=./files/'+imageFile+'" id="idea_image_element" /><br/><input type="button" id="uploadButton" onclick="window.open(\'./upload.php\',\'Change\',\'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, width=540, height=160\'); return false;" value="Change" />');
		windowRef.$(fileElementName).value=imageFile;
	}
}

function userList(listType,start){
	if (start==undefined){ start=0; }
	if (document.getElementById('user_list')!=null){
		$('user_list').update('<div style="height:100px;text-align:center;line-height:100px;">Retrieving...</div>');
		new Ajax.Updater('user_list', 'admin_listUsers.php?listType='+listType+'&start='+start.toString(),
				{ method: 'get',
				evalScripts: true
				});
		document.getElementById("tab_administrator").className="adminTab_off";
		document.getElementById("tab_user").className="adminTab_off";
		document.getElementById("tab_moderator").className="adminTab_off";
		document.getElementById("tab_"+listType).className="adminTab_on";
	} else {
		alert("Error occurred: user_list (id)="+document.getElementById('user_list')+", start="+start.toString());
	}
}

function modList(listType,start){
	if (start==undefined){ start=0; }
	if (document.getElementById('moderation_list')!=null){
		$('moderation_list').update('<div style="height:100px;text-align:center;line-height:100px;">Retrieving...</div>');
		new Ajax.Updater('moderation_list', 'mod_listIdeas.php?listType='+listType+'&start='+start.toString(),
				{ method: 'get',
				evalScripts: true
				});
		document.getElementById("tab_recent").className="adminTab_off";
		document.getElementById("tab_archived").className="adminTab_off";
		document.getElementById("tab_removed").className="adminTab_off";
		document.getElementById("tab_"+listType).className="adminTab_on";
	}
}

//Popup
var loaderShowing=false;

function showLoader(){
	$('GB_popupLoader').appear({ duration: 0.5, from: 0, to: 1 , queue: { position: 'end', scope: 'popupScope' } });
	$('GB_popupLoader').update('Loading...<br/><img src="/images/hubbub_loader.gif" />');
	loaderShowing=true;
}

function popUp(url,params){
	var isIE6Min = getIEVersionNumber() <=6;
	var t; //timeout
	
	//If user is on IE6 or earlier... hide all the select thingies in the document, otherwise IE6 renders them on top of our overlay elements. I hate IE6.
	if (isIE6Min) {
		var x = document.getElementsByTagName("select");
		for (i = 0; i < x.length; i++) {
			x[i].style.visibility = "hidden"
		}
	}

	$('GB_overlay').appear({ duration: 0.25, from: 0, to: 0.2 , queue: { position: 'end', scope: 'popupOverlayScope' } });
	
	t=setTimeout("showLoader()",500);

	var popUpTitle='';
	var i=0;
	var paramList='';
	var element='GB_popup';
	var titleElement='popUpTitle';
	var bodyElement='popUpBody';
	var closeValue='';
	if (params!=undefined){
		for(key in params){
			if (i!=0){
				paramList=paramList+'&';
			}
			paramList=paramList+key+"="+params[key];
			i++;
		}
		paramList='?'+paramList;
	}
	
	if (params['popUpTitle']!=undefined){
		popUpTitle=params['popUpTitle'];
	}

	if (params['dialog']!=undefined){
		element='GB_popupDialog';
		titleElement='popUpDialogTitle';
		bodyElement='popUpDialogBody';
		closeValue='true';
	}
	
	
	new Ajax.Updater(element, url+paramList,
			{ method: 'get',
			evalScripts: true,
			onComplete: function() {
				if (loaderShowing==false){
					clearTimeout(t);
				} else {
					$('GB_popupLoader').fade({ duration: 0.05, from: 1, to: 0 , queue: { position: 'end', scope: 'popupScope' } });
					loaderShowing=false;
				}
				$(element).innerHTML='<div class="'+titleElement+'"><div style="float:right;"><a href="#" onclick="closePopUp('+closeValue+');return false;">Close</a></div><b>'+popUpTitle+'</b></div><div class="'+bodyElement+'" id="popUpBodyId">'+$(element).innerHTML+'</div><br/>';
				$(element).appear({ duration: 0.25, from: 0, to: 1 , queue: { position: 'end', scope: 'popupScope' } });
			}
			});
	
}

function closePopUp(dialog){
	var element='GB_popup';
	if (dialog!=undefined){
		element='GB_popupDialog';	
	}
	$(element).fade({ duration: 0.25, from: 1, to: 0 , queue: { position: 'end', scope: 'popupScope' } });
	$('GB_overlay').fade({ duration: 0.25, from: 0.2, to: 0, queue: { position: 'end', scope: 'popupOverlayScope' }});
	var isIE6Min = getIEVersionNumber() <=6;
	
	//If user is on IE6 or earlier... show all the select thingies in the document, after hiding them before.
	if (isIE6Min) {
		var x = document.getElementsByTagName("select");
		for (i = 0; i < x.length; i++) {
			x[i].style.visibility = "visible"
		}
	}
}

function getIEVersionNumber() {
    var ua = navigator.userAgent;
    var MSIEOffset = ua.indexOf("MSIE ");
    if (MSIEOffset == -1) {
        return 7;
    } else {
        return parseFloat(ua.substring(MSIEOffset + 5, ua.indexOf(";", MSIEOffset)));
    }
}

//	YAHOO.namespace('BigPage');
	
//	YAHOO.util.Event.addListener("dialog_forgotten_password", "click", complexDialogStart, 'dialog_forgotten_password.php');
	// listeners
	
//	function dialog_rss_email(id , cat_id, idea_id)
//	{
//	   complexDialogStart(null, 'dialog_rss_feed.php?list='+id+'&cat='+cat_id+'&idea='+idea_id);
//		return false;
//	}
	
//	function dialog_promote_idea(id)
//	{
//		complexDialogStart(null, 'dialog_promote_idea.php?idea='+id);
//		return false;
//	}
//	function dialog_report_comment(id)
//	{
//		complexDialogStart(null, 'dialog_report_comment.php?comment='+id);
//		return false;
//	}
//	function dialog_report_idea(id)
//	{
//		complexDialogStart(null, 'dialog_report_idea.php?idea='+id);
//		return false;
//	}
	
//	function initComplexDialog() {
//		YAHOO.BigPage.complexDialog = new YAHOO.widget.Dialog("complex_dialog",
//		{
//			//defaults
//			hideaftersubmit : false,
//			fixedcenter : true,
//			//visible : false,
//			draggable:false,
//			close: false,
//			constraintoviewport : true,
//			modal: true,
//			width : "600px"
//			//height: "500px",
//			//zindex:400
//			//effect:{effect:YAHOO.widget.ContainerEffect.FADE, duration: 0.5}
//		}
//		);
//		YAHOO.BigPage.complexDialog.doClose = function() {
//			YAHOO.BigPage.complexDialog.hide();
//			YAHOO.BigPage.complexDialog.destroy();
//		}
//	
//		YAHOO.BigPage.complexDialog.callback = { success: YAHOO.BigPage.complexDialog_Success, failure: YAHOO.BigPage.complexDialog_Failure };
//	}
	
//	YAHOO.BigPage.complexDialog_Success = function(o) {
//		var parsedResponse = o.responseText;
//		if (parsedResponse == "closeDialog") {
//			YAHOO.BigPage.complexDialog.hide();
//		}
//		else {
//			YAHOO.BigPage.complexDialog.setBody(parsedResponse);
//		}
//	};
	
	
	
//	var complexDialogStart_callback = {
//		success : function(o) {
//			var response = o.responseText;
//			if (response == "not_logged_in") {
//				window.location = 'register.php';
//			}
//			else if (response != "failure") {
//				YAHOO.BigPage.complexDialog.setBody(response);
//				YAHOO.BigPage.complexDialog.render(document.body);
//				YAHOO.BigPage.complexDialog.show();
//			}
//			//YAHOO.BigPage.complexDialog.center();
//			/*
//			 YAHOO.BigPage.waitDialog.hide();
//			YAHOO.BigPage.waitDialog.destroy();
//			YAHOO.BigPage.waitDialog = null;
//			*/
//		},
//		failure: function(o) {
//			alert("failure");
//			YAHOO.BigPage.complexDialog.destroy();
//			/*
//			 YAHOO.BigPage.waitDialog.hide();
//			YAHOO.BigPage.waitDialog.destroy();
//			YAHOO.BigPage.waitDialog = null;
//			*/
//		}
//	}
	
//	function complexDialogStart(e, strUrl)
//	{
//		initComplexDialog();
//	
//		/*
//		YAHOO.BigPage.waitDialog =
//						new YAHOO.widget.Panel("wait",
//										{ width: "240px",
//										  fixedcenter: true,
//										  close: false,
//										  draggable: false,
//										  zindex:4,
//										  modal: true,
//										  visible: false
//										}
//									);
//		YAHOO.BigPage.waitDialog.setHeader("Loading, please wait...");
//		YAHOO.BigPage.waitDialog.setBody("<img src=\"http://us.i1.yimg.com/us.yimg.com/i/us/per/gr/gp/rel_interstitial_loading.gif\"/>");
//		YAHOO.BigPage.waitDialog.render(document.body);
//		YAHOO.BigPage.waitDialog.show();
//		*/
//		var conn = YAHOO.util.Connect.asyncRequest("POST", strUrl, complexDialogStart_callback);
//	}

