﻿// Mythric Control Procedures for Javascript

function Jump(sURL)
{
	window.location.href = sURL;
}

function GoToSite(site) 
{ 
    if (site != "") { self.location=site; }
}

//Toggles Document Object Visibility
function ToggleVisible(sObject) {
    var lItem = document.getElementById(sObject);

    if (lItem==null) { return; }

    //table-row
    if (lItem.style.display == "") {
        lItem.style.display = "none";
    }
    else {
        lItem.style.display = "";
    }
}

function SetVisible(sObject, bVisible) {
    var lItem = document.getElementById(sObject);

    if (lItem==null) { return; }

    if (bVisible==true) {
        lItem.style.display = "";
    }
    else {
        lItem.style.display = "none";
    }
}

function SetTabState(sTab, sObject, bVisible) {
    var lTab = document.getElementById(sTab);
    var lItem = document.getElementById(sObject);

    if (lTab==null) { return; }
    if (lItem==null) { return; }

    if (bVisible==true) {
        lTab.setAttribute("class", "TabActive"); 
        lTab.setAttribute("className", "TabActive"); 
        lItem.style.display = "";
    }
    else {
        lTab.setAttribute("class", "TabInActive");
        lTab.setAttribute("className", "TabInActive");  
        lItem.style.display = "none";
    }
}

function SetCurrentTab(iTab, iTotal) {
    var i=1;
    for (i=1;i<=iTotal;i++) {
        var lTab = document.getElementById('tdTab'+i);
        var lItem = document.getElementById('divTab'+i);
        
        if (lTab==null) { return; }
        if (lItem==null) { return; }
        
        if (iTab==i) {
            lTab.setAttribute("class", "TabActive"); 
            lTab.setAttribute("className", "TabActive"); 
            lItem.style.display = "";
        }
        else {
            lTab.setAttribute("class", "TabInActive");
            lTab.setAttribute("className", "TabInActive");  
            lItem.style.display = "none";
        }
    }
}

function ScaleImage(sBox, sElement) {
    var box = document.getElementById(sBox);
    var img = document.getElementById(sElement);

    // Grab the image's dimensions
    var imgH = img.clientHeight;
    var imgW = img.clientWidth;

    // Find which dimension is scaled the most
    var scaleH = box.clientHeight / img.clientHeight;
    var scaleW = box.clientWidth / img.clientWidth;

    // Scale the image
    if (scaleH < scaleW) {
        //Landscape
	    img.style.height = box.clientHeight + "px";
	    img.style.width = Math.round(imgW * scaleH) + "px";
    } else {
        //Portrait
	    img.style.width = box.clientWidth + "px";
	    img.style.height = Math.round(imgH * scaleW) + "px";
    }

}

function ScaleImageByWidth(sElement, iWidth) {
    var img = document.getElementById(sElement);

    // Grab the image's dimensions
    var imgH = img.clientHeight;
    var imgW = img.clientWidth;

    if (imgW <= iWidth) {
        return false;
    } else {
        // Find which dimension is scaled the most
        var scale = (imgW / imgH);

        // Scale the image
        img.style.width = iWidth + "px";
        img.style.height = Math.round(imgH * scale) + "px";
    }
}

function shake(n) {
    if (self.moveBy) {
        for (i = 10; i > 0; i--) {
            for (j = n; j > 0; j--) {
                self.moveBy(0,i);self.moveBy(i,0);self.moveBy(0,-i);self.moveBy(-i,0);         
                }
            }
        }
}

// Open a new window.
function reqWin(desktopURL, alternateWidth, alternateHeight, noScrollbars)
{
	if ((alternateWidth && self.screen.availWidth * 0.8 < alternateWidth) || (alternateHeight && self.screen.availHeight * 0.8 < alternateHeight))
	{
		noScrollbars = false;
		alternateWidth = Math.min(alternateWidth, self.screen.availWidth * 0.8);
		alternateHeight = Math.min(alternateHeight, self.screen.availHeight * 0.8);
	}
	else
		noScrollbars = typeof(noScrollbars) != "undefined" && noScrollbars == true;

	window.open(desktopURL, 'requested_popup', 'toolbar=no,location=no,status=no,menubar=no,scrollbars=' + (noScrollbars ? 'no' : 'yes') + ',width=' + (alternateWidth ? alternateWidth : 480) + ',height=' + (alternateHeight ? alternateHeight : 220) + ',resizable=no');

	// Return false so the click won't follow the link ;).
	return false;
}

// Remember the current position.
function storeCaret(text)
{
	// Only bother if it will be useful.
	if (typeof(text.createTextRange) != "undefined")
		text.caretPos = document.selection.createRange().duplicate();
}

// Replaces the currently selected text with the passed text.
function replaceText(text, textarea)
{
	// Attempt to create a text range (IE).
	if (typeof(textarea.caretPos) != "undefined" && textarea.createTextRange)
	{
		var caretPos = textarea.caretPos;

		caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? text + ' ' : text;
		caretPos.select();
	}
	// Mozilla text range replace.
	else if (typeof(textarea.selectionStart) != "undefined")
	{
		var begin = textarea.value.substr(0, textarea.selectionStart);
		var end = textarea.value.substr(textarea.selectionEnd);
		var scrollPos = textarea.scrollTop;

		textarea.value = begin + text + end;

		if (textarea.setSelectionRange)
		{
			textarea.focus();
			textarea.setSelectionRange(begin.length + text.length, begin.length + text.length);
		}
		textarea.scrollTop = scrollPos;
	}
	// Just put it on the end.
	else
	{
		textarea.value += text;
		textarea.focus(textarea.value.length - 1);
	}
}

// Surrounds the selected text with text1 and text2.
function surroundText(text1, text2, textarea)
{
	// Can a text range be created?
	if (typeof(textarea.caretPos) != "undefined" && textarea.createTextRange)
	{
		var caretPos = textarea.caretPos, temp_length = caretPos.text.length;

		caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? text1 + caretPos.text + text2 + ' ' : text1 + caretPos.text + text2;

		if (temp_length == 0)
		{
			caretPos.moveStart("character", -text2.length);
			caretPos.moveEnd("character", -text2.length);
			caretPos.select();
		}
		else
			textarea.focus(caretPos);
	}
	// Mozilla text range wrap.
	else if (typeof(textarea.selectionStart) != "undefined")
	{
		var begin = textarea.value.substr(0, textarea.selectionStart);
		var selection = textarea.value.substr(textarea.selectionStart, textarea.selectionEnd - textarea.selectionStart);
		var end = textarea.value.substr(textarea.selectionEnd);
		var newCursorPos = textarea.selectionStart;
		var scrollPos = textarea.scrollTop;

		textarea.value = begin + text1 + selection + text2 + end;

		if (textarea.setSelectionRange)
		{
			if (selection.length == 0)
				textarea.setSelectionRange(newCursorPos + text1.length, newCursorPos + text1.length);
			else
				textarea.setSelectionRange(newCursorPos, newCursorPos + text1.length + selection.length + text2.length);
			textarea.focus();
		}
		textarea.scrollTop = scrollPos;
	}
	// Just put them on the end, then.
	else
	{
		textarea.value += text1 + text2;
		textarea.focus(textarea.value.length - 1);
	}
}

// Checks if the passed input's value is nothing.
function isEmptyText(theField)
{
	// Copy the value so changes can be made..
	var theValue = theField.value;

	// Strip whitespace off the left side.
	while (theValue.length > 0 && (theValue.charAt(0) == ' ' || theValue.charAt(0) == '\t'))
		theValue = theValue.substring(1, theValue.length);
	// Strip whitespace off the right side.
	while (theValue.length > 0 && (theValue.charAt(theValue.length - 1) == ' ' || theValue.charAt(theValue.length - 1) == '\t'))
		theValue = theValue.substring(0, theValue.length - 1);

	if (theValue == '')
		return true;
	else
		return false;
}

// Find a specific radio button in its group and select it.
function selectRadioByName(radioGroup, name)
{
	if (typeof(radioGroup.length) == "undefined")
		return radioGroup.checked = true;

	for (var i = 0; i < radioGroup.length; i++)
	{
		if (radioGroup[i].value == name)
			return radioGroup[i].checked = true;
	}

	return false;
}

// Invert all checkboxes at once by clicking a single checkbox.
function invertAll(headerfield, checkform, mask)
{
	for (var i = 0; i < checkform.length; i++)
	{
		if (typeof(checkform[i].name) == "undefined" || (typeof(mask) != "undefined" && checkform[i].name.substr(0, mask.length) != mask))
			continue;

		if (!checkform[i].disabled)
			checkform[i].checked = headerfield.checked;
	}
}

// Resize Avatars If Needed
function avatarResize()
{
	var possibleAvatars = document.getElementsByTagName ? document.getElementsByTagName("img") : document.all.tags("img");

	for (var i = 0; i < possibleAvatars.length; i++)
	{
		if (possibleAvatars[i].className != "avatar")
			continue;

		var tempAvatar = new Image();
		tempAvatar.src = possibleAvatars[i].src;

		if (smf_avatarMaxWidth != 0 && tempAvatar.width > smf_avatarMaxWidth)
		{
			possibleAvatars[i].height = (smf_avatarMaxWidth * tempAvatar.height) / tempAvatar.width;
			possibleAvatars[i].width = smf_avatarMaxWidth;
		}
		else if (smf_avatarMaxHeight != 0 && tempAvatar.height > smf_avatarMaxHeight)
		{
			possibleAvatars[i].width = (smf_avatarMaxHeight * tempAvatar.width) / tempAvatar.height;
			possibleAvatars[i].height = smf_avatarMaxHeight;
		}
		else
		{
			possibleAvatars[i].width = tempAvatar.width;
			possibleAvatars[i].height = tempAvatar.height;
		}
	}

	if (typeof(window_oldAvatarOnload) != "undefined" && window_oldAvatarOnload)
	{
		window_oldAvatarOnload();
		window_oldAvatarOnload = null;
	}
}


// Floating DIV Code

isIE=document.all;
isNN=!document.all&&document.getElementById;
isN4=document.layers;
isHot=false;

function ddInit(e){
  topDog=isIE ? "BODY" : "HTML";
  whichDog=isIE ? document.all.divDialog : document.getElementById("divDialog");  
  hotDog=isIE ? event.srcElement : e.target;  
//  while (hotDog.id!="titleBar"&&hotDog.tagName!=topDog){
//    hotDog=isIE ? hotDog.parentElement : hotDog.parentNode;
//  }  
//  if (hotDog.id=="titleBar"){
//    offsetx=isIE ? event.clientX : e.clientX;
//    offsety=isIE ? event.clientY : e.clientY;
//    nowX=parseInt(whichDog.style.left);
//    nowY=parseInt(whichDog.style.top);
//    ddEnabled=true;
//    document.onmousemove=dd;
//  }
}

//function dd(e){
//  if (!ddEnabled) return;
//  whichDog.style.left=isIE ? nowX+event.clientX-offsetx : nowX+e.clientX-offsetx; 
//  whichDog.style.top=isIE ? nowY+event.clientY-offsety : nowY+e.clientY-offsety;
//  return false;  
//}

function ddN4(whatDog){
  if (!isN4) return;
  N4=eval(whatDog);
  N4.captureEvents(Event.MOUSEDOWN|Event.MOUSEUP);
  N4.onmousedown=function(e){
    N4.captureEvents(Event.MOUSEMOVE);
    N4x=e.x;
    N4y=e.y;
  }
  N4.onmousemove=function(e){
    if (isHot){
      N4.moveBy(e.x-N4x,e.y-N4y);
      return false;
    }
  }
  N4.onmouseup=function(){
    N4.releaseEvents(Event.MOUSEMOVE);
  }
}

function hideMe(){
  if (isIE||isNN) whichDog.style.display="none";
  else if (isN4) document.divDialog.display="none";
}

function showMe(){
  if (isIE||isNN) whichDog.style.display="";
  else if (isN4) document.divDialog.display="";
}

function showDialog(sTitle, sURL, iWidth, iHeight){

    // backdrop
    var lItem = document.getElementById('divDialog');
    //lItem.top = document.documentElement.scrollTop;
    
    // title
    lItem = document.getElementById('divDialogTitle');
    lItem.innerHTML = sTitle
    
    // content
    lItem = document.getElementById('divDialogForm');
    //lItem.height = iHeight;
    lItem.width = iWidth; 
    
    // iframe
    var lfrm = document.getElementById('ifDialog');
    lfrm.src = sURL;
    lfrm.height = iHeight ;//- 20;
    
    showMe();
    scroll(0,0);
}

function showCollectionItemED(sQS) {
    showDialog('Location Item', 'http://www.aurcade.com/admin/collection_item.aspx?id=' + sQS, 450, 400);
}

function showLocationItemED(sQS) {
    showDialog('Location Item', 'http://www.aurcade.com/admin/location_item.aspx?id=' + sQS, 450, 500);
}

function showSubmissionED(sQS) {
    showDialog('Submission', 'http://www.aurcade.com/admin/submission.aspx?id=' + sQS, 450, 550);
}

function hideDialog(){
    hideMe();
    __doPostBack();
}

function resizeImg(imgobj)
{
    var img = new resizeImgOO(imgobj);
    img.resize();
}

function resizeImgOO(el) 
{ 
	function imgRatio()
	{ 
		return (el.height / el.width); 
	} 
	function holderRatio()
	{ 
		return (el.offsetParent.offsetHeight / el.offsetParent.offsetWidth); 
	} 
	function fitToContainer()
	{ 
		if(imgRatio>holderRatio) 
		{ 
			el.height = el.offsetParent.offsetHeight; 
		} 
		else 
		{ 
			el.width = el.offsetParent.offsetWidth;	
		} 
	}
	
	this.imgRatio = imgRatio; 
	this.holderRatio = holderRatio; 
	this.resize = fitToContainer; 
}

function disableSelection(target){
    if (typeof target.onselectstart!="undefined") //IE route
	    target.onselectstart=function(){return false}
    else if (typeof target.style.MozUserSelect!="undefined") //Firefox route
	    target.style.MozUserSelect="none"
    else //All other route (ie: Opera)
	    target.onmousedown=function(){return false}
    target.style.cursor = "default"
}



//Sample usages
//disableSelection(document.body) //Disable text selection on entire body
//disableSelection(document.getElementById("mydiv")) //Disable text selection on element with id="mydiv"

document.onmousedown=ddInit;
document.onmouseup=Function("ddEnabled=false");
