LLO.Photosynth = new Object();

LLO.Photosynth.ToggleDisplay = function(elShow, elHide)
{
    if (elShow)
    {
        LLO.Util.RemoveClass( elShow, "collapsed" );
  
    }
    
    if (elHide)
    {
        LLO.Util.AddClass( elHide, "collapsed" );    
    }    
}

LLO.Photosynth.GotoPage = function(url)
{
    location.href = url;
    return;
}


LLO.Photosynth.SwapImage = function(imgName, index, imgTo)
{
    if( index > 0 )
    {
        var image = document.getElementById( imgName + index ).src = imgTo;
        LLO.Photosynth.SwapImage( imgName, --index, imgTo );
    }
}

//------------------------------------------------------------------------------
/*
   name - name of the cookie
   value - value of the cookie
   [expires] - expiration date of the cookie
     (defaults to end of current session)
   [path] - path for which the cookie is valid
     (defaults to path of calling document)
   [domain] - domain for which the cookie is valid
     (defaults to domain of calling document)
   [secure] - Boolean value indicating if the cookie transmission requires
     a secure transmission
   * an argument defaults when it is assigned null as a placeholder
   * a null placeholder is not required for trailing omitted arguments
*/
function setCookie(name, value, expires, path, domain, secure) {
  var curCookie = name + "=" + escape(value) +
      ((expires) ? "; expires=" + expires.toGMTString() : "") +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      ((secure) ? "; secure" : "");
  document.cookie = curCookie;
}


//------------------------------------------------------------------------------
/*
  name - name of the desired cookie
  return string containing value of specified cookie or null
  if cookie does not exist
*/
function getCookie(name) {
  var dc = document.cookie;
  var prefix = name + "=";
  var begin = dc.indexOf("; " + prefix);
  if (begin == -1) {
    begin = dc.indexOf(prefix);
    if (begin != 0) return null;
  } else
    begin += 2;
  var end = document.cookie.indexOf(";", begin);
  if (end == -1)
    end = dc.length;
  return unescape(dc.substring(begin + prefix.length, end));
}

//------------------------------------------------------------------------------
/*
   name - name of the cookie
   [path] - path of the cookie (must be same as path used to create cookie)
   [domain] - domain of the cookie (must be same as domain used to
     create cookie)
   path and domain default if assigned null or omitted if no explicit
     argument proceeds
*/
function deleteCookie(name, path, domain) {
  if (getCookie(name)) {
    document.cookie = name + "=" +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
}

//------------------------------------------------------------------------------
// date - any instance of the Date object
// * hand all instances of the Date object to this function for "repairs"

function fixDate(date) {
  var base = new Date(0);
  var skew = base.getTime();
  if (skew > 0)
    date.setTime(date.getTime() - skew);
}

//------------------------------------------------------------------------------
function isEnter(event)
{
    var keycode;
    if(window.event) 
        keycode = window.event.keyCode;
    else if(event) 
        keycode = event.which;
    else 
        return false;

    if(keycode == 13)
    {
        return true;
    }
    else
        return false;
}

function addClass( node, className )
{
    var oldClass = node.className;
    if ( className.indexOf( " " ) != -1 )
    {
        //DebugManager.Debug( "addClass: Attempting to add a class with illegal white space: '" + className + "'" );
        return;
    }
    
    var reg = new RegExp( "(^|\\s)" + className + "(\\s|$)" );
    
    // don't add dupes
    if ( oldClass.match( reg ) )
        return;
    
    node.className = oldClass + " " + className;
}


// removeClass takes the class to remove, the original class string, and
// returns the modified class string

function removeClass( node, className )
{
    var oldClass = node.className;
    if ( className.indexOf( " " ) != -1 )
    {
        //DebugManager.Debug( "RemoveClass: Attempting to remove a class with illegal white space: '" + className + "'" );
        return;
    }
    
    var reg = new RegExp( "(^|\\s)" + className + "(\\s|$)" );
    
    node.className = oldClass.replace( reg, "$2" );
}

function ltrim(str)
{
    return str.replace(/^\s+/g, "" );
}

function rtrim(str)
{
    return str.replace(/\s+$/g, "" );
}

function trim(str)
{
    return rtrim(ltrim(str));
}

function getUrlParameter(key, query)
{
    var returnValue = null;

    if (!query)
    {
        return returnValue;
    }
    
    var local = trim(query);
    if (query && local.length == 0)
    {
        return returnValue;
    }

    if (local.charAt(0) == '?' && local.length > 1)
    {
        local = local.substring(1);
    }
    
    var keyValuePairs = local.split('&');
    var parsedKVP;
    
    for (i=0; i < keyValuePairs.length; i++)
    {
        parsedKVP = keyValuePairs[i].split('=');
        if (parsedKVP.length == 2 && parsedKVP[0] == key)
        {
            returnValue = parsedKVP[1];
            break;            
        }
    }
    return returnValue;
}

function userSystemCheck(target, unsupportedOSUrl, unsupportedBrowserUrl)
{  
    var userAgent = navigator.userAgent.toLowerCase();

    if (userAgent.indexOf("windows nt 6.1") == -1 && // Windows 7 
        userAgent.indexOf("windows nt 6.0") == -1 && // Windows Vista 
        userAgent.indexOf("windows nt 5.2") == -1 && // Windows Server 2003; Windows XP x64 Edition
        userAgent.indexOf("windows nt 5.1") == -1)   // Windows XP 
    {
        if (target == 'location')
        {
            location.href  = unsupportedOSUrl;
            return;
        }
        if (target == 'parent')
        {
            parent.location  = unsupportedOSUrl;
            return;
        }        
    }
    
    if (userAgent.indexOf("msie 7") == -1 &&
        userAgent.indexOf("msie 6") == -1 &&
        userAgent.indexOf("firefox/1.5") == -1 &&
        userAgent.indexOf("firefox/2.0") == -1)
    {
        if (target == 'location')
        {
            location.href = unsupportedBrowserUrl;
            return;            
        }
        if (target == 'parent')
        {
            parent.location  = unsupportedBrowserUrl;
            return;
        }         
    }                   
}

function GetNonTextChildElements(parent)
{
    var result = new Array();
    var childNodes = parent.childNodes;
    var length = childNodes.length;
    for(var i = 0; i < length; i++)
    {
        var element = childNodes[i];
        if (element && element.nodeType!=3)
        {
            result.push(element);
        }
        element = null;
    }
    
    return result;
}

//----------------------------------------------------------------------------
//
//    Method:     InsertElementChildAt
//
//    Synopsis:   Inserts a child element for parent element at a 
//                specified index.
//
//    Arguments:  parent               - The parent element.
//                child                - The element to insert
//                index                - The index to insert at
//                flashNext            - If this value is true and there are
//                                       elements after the insertion point,
//                                       the element immediately after the
//                                       insertion point will be hidden before
//                                       the insertion and redisplayed afterward.
//
//    Returns:    nothing
//
//----------------------------------------------------------------------------
function InsertElementChildAt(parent, child, index, flashNext)
{
    if ((index == null) || (index > parent.childNodes.length))
    {
        index = parent.childNodes.length;
    }
    
    if (index <= 0)
    {
        index = 0;
    }
    
    if (index == parent.childNodes.length)
    {
        parent.appendChild(child);
    }
    else
    {
        var next = parent.childNodes[index];
        
        if (flashNext)
        {
            next.style.display = "none";
        }

        parent.insertBefore(child, next);

        if (flashNext)
        {
            next.style.display = "block";
        }
    }
}

/// Defines a set of valid characters 0-9, A-Z and a-z and automatically encodes any characters not
/// defined in that valid set.
function HtmlEncode(s)
{
    safeString = '';
    /// Loop thought the input string building the safe output string
    for (i = 0; i < s.length; i++)
    {
        charCode = s.charCodeAt(i);
        if ((charCode < '0'.charCodeAt(0) || charCode > '9'.charCodeAt(0)) &&
            (charCode < 'A'.charCodeAt(0) || charCode > 'Z'.charCodeAt(0)) &&
            (charCode < 'a'.charCodeAt(0) || charCode > 'z'.charCodeAt(0)))
        {
            /// Not a safe character replace it.
            safeString += ['&#', charCode, ';'].join('');
        }
        else
        {
            /// A safe character.
            safeString += s.charAt(i);
        }
    }
    return safeString;
}
