/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * This function limits a textarea's max length. To use it, see the following example:
 *
 * [textarea maxlength='40' onkeyup='return enforceMaxLength(this)'][/textarea]
 */
function enforceMaxLength(obj)
{
    var mlength=obj.getAttribute? parseInt(obj.getAttribute("maxlength")) : ""
    if (obj.getAttribute && obj.value.length>mlength)
        obj.value=obj.value.substring(0,mlength)
}

var newwindow;
/**
 * Creates a new popup window navigating to the given url of the given height and width
 */
function popup(url, width, height, windowName)
{
    if (windowName==null)
        windowName='name';
    newwindow = window.open(url,windowName,'height='+height+',width='+width+',menubar=no,status=no,directories=no,location=no,dependant=yes');
    if (window.focus)
    {
        newwindow.focus()
    }
}

function popupScriptWindow(url, scriptId)
{
    popup(url, 800, 600, 'script'+scriptId);
}
//This set of functions allow tabs to be inserted into textarea documents.
// Use the following code in a textarea:  onkeydown="return catchTab(this,event)"
// START
function setSelectionRange(input, selectionStart, selectionEnd) {
  if (input.setSelectionRange) {
    input.focus();
    input.setSelectionRange(selectionStart, selectionEnd);
  }
  else if (input.createTextRange) {
    var range = input.createTextRange();
    range.collapse(true);
    range.moveEnd('character', selectionEnd);
    range.moveStart('character', selectionStart);
    range.select();
  }
}

function replaceSelection (input, replaceString) {
	if (input.setSelectionRange) {
		var selectionStart = input.selectionStart;
		var selectionEnd = input.selectionEnd;
                var scrollTop = input.scrollTop; // fix scrolling issue with Firefox
		input.value = input.value.substring(0, selectionStart)+ replaceString + input.value.substring(selectionEnd);
                input.scrollTop = scrollTop;

		if (selectionStart != selectionEnd){
			setSelectionRange(input, selectionStart, selectionStart + 	replaceString.length);
		}else{
			setSelectionRange(input, selectionStart + replaceString.length, selectionStart + replaceString.length);
		}

	}else if (document.selection) {
		var range = document.selection.createRange();

		if (range.parentElement() == input) {
			var isCollapsed = range.text == '';
			range.text = replaceString;

			 if (!isCollapsed)  {
				range.moveStart('character', -replaceString.length);
				range.select();
			}
		}
	}
}

// We are going to catch the TAB key so that we can use it, Hooray!
function catchTab(item,e){
	c = e.which ? e.which : e.keyCode;
	if(c==9){
		replaceSelection(item,String.fromCharCode(9));
		setTimeout("document.getElementById('"+item.id+"').focus();",0);
		return false;
	}

}
// END


function initializeTogglePanelView(imgButton, controlPanelId, controlPanelDisplayProperty, ajaxToggleId, buttonsOnClass, buttonsOffClass)
{
    imgButton.panelToControlDisplayProperty = controlPanelDisplayProperty;
    imgButton.panelToControl = jQuery('#'+controlPanelId);
    imgButton.ajaxToggleId = ajaxToggleId;
    imgButton.buttonsOnClass = buttonsOnClass;
    imgButton.buttonsOffClass = buttonsOffClass;
}

function togglePanelView(imgButton)
{
    if (imgButton.src.indexOf('images/hide.png')>-1)
    {
        imgButton.panelToControl.css('display', 'none');
        imgButton.src = 'images/show.png';
        var jqBtn = jQuery(imgButton);
        jqBtn.removeClass(imgButton.buttonsOnClass);
        jqBtn.addClass(imgButton.buttonsOffClass);
        basicAjaxRequest('ChangePlayerGameViewConfig?'+imgButton.ajaxToggleId+'=false');
    }
    else
    {
        imgButton.panelToControl.css('display', imgButton.panelToControlDisplayProperty);
        imgButton.src = 'images/hide.png';
        var jqBtn = jQuery(imgButton);
        jqBtn.removeClass(imgButton.buttonsOffClass);
        jqBtn.addClass(imgButton.buttonsOnClass);
        basicAjaxRequest('ChangePlayerGameViewConfig?'+imgButton.ajaxToggleId+'=true');
    }
}

function addPieMenu(elementId, buttons)
{
    jQuery(document).ready(function()
    {
        var element = jQuery("#"+elementId);
//        element.prettypiemenu({   // Use this one if you want to have the pie menu activate on holding right click
        element.parent().append("<span id='"+elementId+"_piemenu'></span>");
        jQuery("#"+elementId+"_piemenu").prettypiemenu({  //Use this if you want to activate the menu on left click
            buttons: buttons,
            onSelection: function(item, element)
            {
                var img = jQuery(element.children[0].children[0]);
                img.trigger('click.cluetip');
            },
            showTitles: true
        });
        element.mousedown(function(event)
        {
            if (event.which==3)
            {
                event.preventDefault();
                jQuery("#"+elementId+"_piemenu").prettypiemenu("show", {top: event.clientY, left: event.clientX});
                return false;
            }
        });
        element.find('*').mousedown(function(event)
        {
            if (event.which==3)
            {
                event.preventDefault();
                jQuery("#"+elementId+"_piemenu").prettypiemenu("show", {top: event.clientY, left: event.clientX});
                return false;
            }
        });
    });
}



