/**
 * @desc    what2do JavaScript Library
 * @author  designersfactory
 * @package what2do
 */



// === WINDOW =====================================================================================

/**
 * @desc   Opens new window and set focus to it
 * @param  url to open, name of window, options of windows
 * @return void
 */
function openWindow(url, name, options)
{
	var f = window.open(url, name, options);
	f.focus();
}



/**
 * @desc   Opens addtional window for picture preview
 * @param  url to open
 * @return void
 */
function openPicture(url)
{
	openWindow(
		url, 'picture',
		'scrollbars=0,resizable=1,menubar=0,toolbar=0,location=0,status=0'
	);
}



/**
 * @desc
 * @param  width of document, height of document
 * @return void
 */
function setMinimalDocumentSize(width, height)
{
    if (width > screen.availWidth || height > screen.availHeight)
    {
        return;
    }

    // get current document width and height
    if (self.innerWidth)
    {
        documentWidth  = top.innerWidth;
        documentHeight = top.innerHeight;
    }
    else if (document.documentElement && document.documentElement.clientWidth)
    {
        documentWidth  = document.documentElement.clientWidth;
        documentHeight = document.documentElement.clientHeight;
    }
    else if (document.body)
    {
        documentWidth  = document.body.clientWidth;
        documentHeight = document.body.clientHeight;
    }
    else
    {
        return;
    }

    // set minimal document width
    if (width != 0 && documentWidth < width)
    {
        window.resizeBy(width - documentWidth, 0);
    }

    // set minimal document height
    if (height != 0 && documentHeight < height)
    {
        window.resizeBy(0, height - documentHeight);
    }
}



// === RADIO ======================================================================================

/**
 * @desc   Gets value of given radio object
 * @param  radio object
 * @return radio value
 */
function getRadioValue(radioObject)
{
    for (var i = 0; i < radioObject.length; i++)
    {
        if (radioObject[i].checked)
        {
            return radioObject[i].value;
        }
    }

    return false;
}



/**
 * @desc   Sets value of given radio object
 * @param  radio object, radio value
 * @return void
 */
function setRadioValue(radioObject, radioValue)
{
    for (var i = 0; i < radioObject.length; i++)
    {
        radioObject[i].checked = false;

        if (radioObject[i].value == radioValue.toString())
        {
            radioObject[i].checked = true;
        }
    }
}



// === SELECT =====================================================================================

/**
 * @desc   Gets entry of given select menu
 * @param  selectMenu, selectValue
 * @return void
 */
function getSelectMenu(selectMenu)
{
    index = selectMenu.selectedIndex;

    if (index > 0)
    {
        return selectMenu.options[index].value;
    }
    else
    {
        return false;
    }
}



/**
 * @desc   Sets entry of given select menu
 * @param  selectMenu, selectValue
 * @return void
 */
function setSelectMenu(selectMenu, selectValue)
{
    for (var i = 0; i < selectMenu.length; i++)
    {
        if (selectMenu.options[i].value == selectValue)
        {
            selectMenu.selectedIndex = i;
            return;
        }
    }
}



// === SHOW / HIDE ================================================================================

/**
 * @desc   Shows or hides id, dependig on current state
 * @param  id
 * @return void
 */
function showHideID(id)
{
    element = document.getElementById(id);

    if (element.className == 'visible')
    {
        element.className = 'invisible';
    }
    else
    {
        element.className = 'visible';
    }
}



/**
 * @desc   Hides id
 * @param  id
 * @return void
 */
function hideID(id)
{
    element = document.getElementById(id);
    element.className = 'invisible';
}



/**
 * @desc   Shows id
 * @param  id
 * @return void
 */
function showID(id)
{
    element = document.getElementById(id);
    element.className = 'visible';
}



/**
 * @desc   Shows or hides id, dependig on length
 * @param  id, minimum length for showing
 * @return void
 */
function showHideIDByLength(id, length)
{
    element = document.getElementById(id);

    if (element.innerHTML.length >= length)
    {
        element.className = 'visible';
    }
    else
    {
        element.className = 'invisible';
    }
}



// === ENABLE / DISABLE ===========================================================================

/**
 * @desc   Enables element of form
 * @param  form name, element name
 * @return void
 */
function enableFormElement(form_name, element_name){
    document.forms[form_name].elements[element_name].disabled = false;
}



/**
 * @desc   Disables element of form
 * @param  form name, element name
 * @return void
 */
function disableFormElement(form_name, element_name){
    document.forms[form_name].elements[element_name].disabled = true;
}



// === ELEMENT ====================================================================================

/**
 * @desc   Sets text of element
 * @param  element id, element text
 * @return void
 */
function setElementText(id, text){
    element = document.getElementById(id);
    element.innerHTML = text;
}



/**
 * @desc   Gets text of element
 * @param  element id
 * @return element text
 */
function getElementText(id){
    element = document.getElementById(id);
    return element.innerHTML;
}



/**
 * @desc   Copies text of one element to another
 * @param  source element id, target element id
 * @return void
 */
function copyElementText(source_id, target_id)
{
    setElementText(target_id, getElementText(source_id));
}



// === INDEX ======================================================================================

/**
 * @desc   Returns index of next list element (round robin)
 * @param  current index, list length
 * @return next index
 */
function nextIndex(current_index, list_length)
{
    return ((current_index % list_length + 1) % list_length);

//    return (current_index + 1 < list_length ? current_index + 1 : 0);
}



/**
 * @desc   Returns index of previous list element (round robin)
 * @param  current index, list length
 * @return previous index
 */
function previousIndex(current_index, list_length)
{
    prev_index = (current_index % list_length - 1) % list_length;
    return (prev_index < 0 ? prev_index + list_length : prev_index);

//    return (current_index - 1 < 0 ? list_length - 1 : current_index - 1);
}
