// caps lock
function check_caps(o)
{

}


//Report/moderation stuff
function toggleReports(id, type, loadReports)
{
	loadReports = true;
	if($("showreport"+id).visible()) loadReports = false;
	if(loadReports)
	{
		new Ajax.Request('/moderation/ax-reports', 
		{
		  method: 'POST',
		  parameters: { type: type, id: id},
		  onSuccess: function(transport) 
		  {
		  	$(id+"_reports").update(transport.responseText);
			$("showreport"+id).toggle();
			if($("showreport"+id).visible()) $("showreport"+id+'_toggle').update("Hide");
			else $("showreport"+id+'_toggle').update("Show");
		  },
		  onFailure: function(transport) 
		  {
		  	$(id+"_reports").update(transport.responseText);  
			$("showreport"+id).toggle();
			if($("showreport"+id).visible()) $("showreport"+id+'_toggle').update("Hide");
			else $("showreport"+id+'_toggle').update("Show");
		  } 	  			
		});
	}
	else 
	{
		$("showreport"+id).toggle();
		if($("showreport"+id).visible()) $("showreport"+id+'_toggle').update("Hide");
		else $("showreport"+id+'_toggle').update("Show");
	}
}


function clearCache(path) {

    new Ajax.Request('/categories/clear-cache?path=' + path,
    {
        method: 'POST',
        onSuccess: function(transport) 
        {
            alert(transport.responseText); 
            document.location.reload();
        },
        onFailure: function(transport) 
        {
          alert(transport.responseText);  
        }        
    });
}

function deleteComment(id)
{
	var popup = showPopup('popupDelete', 220, 40, true);
	popup.update('Deleting comment, please wait...');
	new Ajax.Request('/feedback/ax-remove-comment', 
	{
	  method: 'POST',
	  parameters: { comment: id, ref: document.location.pathname },
	  onSuccess: function(transport) 
	  {
	  	document.location.reload();	  	  	
	  },
	  onFailure: function(transport) 
	  {
	  	closePopup('popupDelete');  
	  	alert(transport.responseText);  
	  } 	  			
	});

}

function fileReport(type, id, reason)
{
	if(!(type && id)) return false;
	var popup = showPopup('popup', 220, 240);
	var method = 'get'; 
	var params = { item: id, type: type };
	if(!Object.isUndefined(reason))
	{
		method = "post";
		params['report'] = reason;
	}
	new Ajax.Request('/moderation/report', 
	{
	  method: method,
	  parameters: params,
	  onSuccess: function(transport) 
	  {
	  	popup.update(transport.responseText);		  	  	
	  },
	  onFailure: function(transport) 
	  {
	  	closePopup('popup');
	  	alert(transport.responseText);  
	  } 	  			
	});
}

function unpublish(type, id, publish, reason)
{
	if(!(type && id)) return false;
	var popup = showPopup('popup', 220, 240);
	var method = 'get'; 
	var params = { item: id, type: type, publish: publish, ref: document.location.pathname};
	if(!Object.isUndefined(reason))
	{
		method = "post";
		params['report'] = reason;
	}
	new Ajax.Request('/moderation/unpublish', 
	{
	  method: method,
	  parameters: params,
	  onSuccess: function(transport) 
	  {
	  	  popup.update(transport.responseText);		  	  	
	  },
	  onFailure: function(transport) 
	  {
	  	closePopup('popup');
	  	alert(transport.responseText);  
	  } 	  			
	});
}

function handleReport(id, status)
{
	new Ajax.Request('/moderation/handle-report', 
	{
	  method: 'POST',
	  parameters: { report: id, status: status },
	  onSuccess: function(transport) 
	  {
	  	if(status == "DELETE") $('report-'+id).remove();
	  	else $('report-'+id+'-status').update(status);		  		  	  	
	  },
	  onFailure: function(transport) 
	  {
	  	alert('Error: '+transport.responseText);  
	  } 	  			
	});
}

function showPopup(id, width, height, disableClose)
{
	var wpOff = document.viewport.getScrollOffsets();
	var left = (document.viewport.getWidth() / 2) + wpOff.left - (width / 2);
	var top = (document.viewport.getHeight() / 2) + wpOff.top - (height / 2);
	var popup = new Element('div', { 'id' : id, 'class': 'popup', 
		'style': 'padding:10px; border:1px solid black; background:white; position:absolute; top:'+top+'px; left:'+left+'px; width: '+width+'px; height: '+height+'px;' });
	if($(id)) 
	{
		$(id).remove();	
		if($(id+'_close')) $(id+'_close').remove();	
	}
	if(!disableClose) var close = new Element('a', {'href': 'javascript:closePopup("'+id+'")','id': id+'_close', 'style': 'position:absolute; top:'+(top+2)+'px; left:'+(left + width - 32)+'px;'}).update('Close [X]');	
	//close.onclick = function() { closePopup(id); }; 
	$('body').insert(popup);
	if(!disableClose) $('body').insert(close); 	
	return popup;
}
function closePopup(id)
{
	$(id).remove();	
	$(id+'_close').remove();
} 
//Image/file manager callback
var selImg = null;
var selPrevSize = null;
function ibibiHandleEvent(e) 
{
	if(e.type == 'mouseup')
	{
		var sel = tinyMCE.activeEditor.selection.getNode();		
		if(sel.tagName == 'IMG')
		{		
			selImg = sel;	 
			scaleImage();						
			selPrevSize = sel.getAttribute('width')+'_'+sel.getAttribute('height'); 
			setTimeout('scaleImage()', 10);
		}
		else 
		{
			selImg = null;
			selPrevSize = null;
		}			
	} else  if(e.type == 'mousedown')
	{
		var sel = tinyMCE.activeEditor.selection.getNode();		
		if(sel.tagName == 'IMG')
		{			 			
			selImg = sel;			
			selPrevSize = sel.getAttribute('width')+'_'+sel.getAttribute('height');
		}
	}
}
var imageServer = "http://images.thesimsresource.com";
function scaleImage()
{
	if(!imageServer)  
	{
		alert("Image server not set (by php)");
		return;
	}
	var currSize = selImg.getAttribute('width')+'_'+selImg.getAttribute('height');
	if(selPrevSize != null && currSize != selPrevSize) 
	{				
		if(selImg.getAttribute('origSrc') == null) selImg.setAttribute('origSrc', selImg.getAttribute('src'));
		var origSrc = selImg.getAttribute('origSrc');
		var rImageServer = imageServer.replace(/\//g, '\\/');
		var pat = new RegExp('^(' + rImageServer +')scaled\/w-[0-9]+h-[0-9]+-([0-9]+)(\..*$)','i');
		var matches = origSrc.match(pat); 
		if(matches != null) 
		{
			origSrc = matches[1]+Math.ceil(matches[2] / 1000)+'/'+matches[2]+matches[3];
			selImg.setAttribute('origSrc', origSrc);
		}
		
		pat = new RegExp('^('+ rImageServer + ')([0-9]+)\/([0-9]+\..*$)','i');
		matches = origSrc.match(pat);
		if(matches != null)
		{
			var newSrc = matches[1]+'scaled/w-'+selImg.getAttribute('width')+'h-'+selImg.getAttribute('height')+'-'+matches[3];
			selImg.setAttribute('src', newSrc);
			selImg.setAttribute('mce_src', newSrc);
		}   	
	}
}
function standaloneFileBrowser(callback, url)
{
 	window.open('/mytsr/content/pick-file/method/standalone/callback/'+callback, 'TitleImage', "status=no,width=420,height=400,resizable=yes");
}
function ibibiFileBrowser (field_name, url, type, win) {
    var cmsURL = "/mytsr/content/pick-file/type/"+type;    // script URL - use an absolute path!
    tinyMCE.activeEditor.windowManager.open({
        file : cmsURL,
        title : 'File Browser',
        width : 420,  // Your dimensions may differ - toy around with them!
        height : 420,
        resizable : "yes",
        inline : "yes",  // This parameter only has an effect if you use the inlinepopups plugin!
        close_previous : "no"
    }, {
        window : win,
        input : field_name
    });
    return false;  
}


var LightBox =
{
	contentDiv : null,
	shadeDiv : null,
 	LoadTemplate : function(templateName, noframework, onComplete)
 	{
 	 	new Ajax.Request(templateName,
 	 	{
 	 		onSuccess : function(e)
 	 		{
 	 		 	LightBox.Show(e.responseText, noframework);
 	 		 	if(onComplete)
 	 		 	{
 	 		 		setTimeout(function() { onComplete() }, 1000);
				}
			}
			,
			onFailure: function(e)
			{
			 	alert('Error: ' + e.responseText);
			}
		});  
		return(false);
	}
	,
	Show : function(content, noframework)
	{            
		var bodySize = getBodySize();
		var w = bodySize.width;
		var h = bodySize.height;
		var div = $('lightboxcontent');
		var shade = $('lightboxshade');
		if(!div)
		{
		 	div = document.createElement("DIV");
		 	div.style.position = "absolute";
		 	div.style.zIndex = 10010;
		 	div.style.left = "10px";
		 	div.style.top = "10px";
		 	div.id = "lightboxcontent"
		 	document.body.appendChild(div);
		}
		if(!shade)
		{
		 	shade = document.createElement("DIV");
		 	shade.style.position = "absolute";
		 	shade.style.zIndex = 10000;
		 	shade.style.left = "0px";
		 	shade.style.top = "0px";
		 	shade.style.width = w + "px";
		 	shade.style.height = document.documentElement.scrollHeight + "px";
		 	shade.id = "lightboxshade"; 
		 	document.body.appendChild(shade);
		}
	 	div.className = noframework ? "nobg" : "";
	 	shade.className = noframework ? "nobg" : "";
		//Gecko don't support deferred javascript execution unless we add it to the DOM					
		if (navigator.product == "Gecko")
		{
			while (div.firstChild) div.removeChild(div.firstChild);
			var range = document.createRange();
			range.selectNode(document.body);
			var parsedHTML = range.createContextualFragment(content);
			div.appendChild(parsedHTML);
		}				
		else
		{			
			div.innerHTML = content;
		}		
		Behaviour.apply();	
		div.style.visibility = "hidden";
		shade.style.display = "none";
		shade.style.visibility = "hidden";
				
		this.contentDiv = div;
		this.shadeDiv = shade;
		setTimeout(function()
		{
			LightBox.Position();
		}, 200);		
	}
	,
	Position : function()
	{
		var bodySize = getBodySize();
		var w = bodySize.width;
		var h = bodySize.height;	
		var div = this.contentDiv;
		var shade = this.shadeDiv;
		
		div.style.left = ((w / 2) - (div.scrollWidth / 2)) + "px";
		div.style.top = (((h/2) - (div.scrollHeight/2)) + getScrollTop()) + "px"; // ((h / 2) - (div.scrollHeight/2) - 200
		div.style.visibility = "visible";	
		
		shade.style.display = "block";
		shade.style.visibility = "visible";
		// 
		var podtoolbar = $('podtoolbar');
		if(podtoolbar)
		{
			podtoolbar.style.visibility = "hidden"; 
		}
	}
	,
	AlertClose : function(text)
	{
		alert(text);
		LightBox.Close();
	}
	,
	AlertCloseReload : function(text)
	{
		alert(text);
		LightBox.Close();
		document.location.reload();
	}
	,
	Close : function()
	{
		var div = $('lightboxcontent');
		if(div) div.style.visibility = "hidden";
		var shade = $('lightboxshade');
		if(shade) 
		{
			shade.style.visibility = "hidden";
			shade.style.display = "none";
		}           
	}
	
}
