﻿//img preloading: http://engineeredweb.com/blog/09/12/preloading-images-jquery-and-javascript
(function($) {
  var cache = [];
  // Arguments are image paths relative to the current page.
  $.preLoadImages = function() {
    var args_len = arguments.length;
    for (var i = args_len; i--;) {
      var cacheImage = document.createElement('img');
      cacheImage.src = arguments[i];
      cache.push(cacheImage);
    }
  }
})(jQuery)

//Collapsible fieldset control:
function initCollapsibleControls() {
    $('fieldset.collapsible legend').click(function () {
        var colcontent = $(this).parent();
        if (colcontent.hasClass('collapsed'))
            colcontent.removeClass('collapsed');
        else
            colcontent.addClass('collapsed')
    });
}


function closeFancybox()
{
    parent.$.fn.fancybox.close(); 

    return false;
}


function showPlaylists()
{
    var elms = $('.playlist_select_inner');
    if (elms!=null && elms[0]!=null)
    {
        
        $('.playlist_select_inner').animate({"height": "toggle"});
        //else
        //    $('.playlist_select_inner').animate({height: '0', display:'none'});
    }
}

function showBigImage(path, w ,h )
{
    //var path = obj.getAttribute("imagePath");
    //var w = obj.getAttribute("imageWidth");
    //var h = obj.getAttribute("imageHeight");
    //newWindow = 
    window.open(path,'newImageWin','toolbar=no,menubar=no,scrollbars=yes,resizable=yes,location=no,directories=no,status=no,width='+w+',height='+h)
}

function autoFocus()
{
    var skip = false;
    for (f=0; f < document.forms.length; f++) 
    {
        for(i=0; i < document.forms[f].length; i++)
        {
            skip = false;
            if (document.forms[f][i].type != "hidden" && document.forms[f][i].tagName != 'FIELDSET') 
            { 
                //alert(document.forms[f][i].style.display);
                if (document.forms[f][i].disabled != true) 
                {
                    if(document.forms[f][i].style.display == 'none' )
                    {
                        skip = true;
                    }
                    try 
                    {
                        if(!skip)
                        {
                            //alert(document.forms[f][i].id);
                            //TODO: not working:
                            //document.forms[f][i].select();
                            document.forms[f][i].focus();
                            //alert(document.forms[f][i].tagName);
                            var bFound = true;
                        }
                    }
                    catch(er) {

                    }
                }
            }
            if (bFound == true)
                break;  
        }
        if (bFound == true)
            break;
    }
}


function logError(method, ex) {
    try {
        var msg = "An unexpected error had occured:\nMethod: " + method;
        if (ex != null) {
            if (ex._message != null) msg += "\nMessage: " + ex._message;
            if (ex.description != null) msg += "\nDescription: " + ex.description;
            //if (ex.number != null) msg += "\nNumber: " + ex.number;
            if (ex.stack != null) msg += "\nStack: " + ex.stack;
        }
        //write to console:        
        if (window.console != undefined)
            console.error(msg);
    }
    catch (e) {
        alert("Error in handleEx");
    }
}

