
// Logging the EJunkie shopping cart opens to Google Analytics
function EJEJC_shown()
{
    if( pageTracker != undefined || pageTracker != null)
    {
        pageTracker._trackPageview('/tracks/cart_opened');
    }
}


function change_font(size)
{
    var db = $(document.body);
    
    db.removeClass("small_font");
    db.removeClass("normal_font");
    db.removeClass("large_font");
    
    switch(size)
    {
        case -1:
            db.addClass("small_font");
            break
        case 0:
            db.addClass("normal_font");
            break;
        case 1:
            db.addClass("large_font");
            break;
    }
}

function show_hidden_javascript()
{
    $$('.hide_if_no_js').each(function(el){ el.removeClass('hide_if_no_js'); });
}
addOnLoad(show_hidden_javascript);


function focusText(sender, defaultText)
{
    /* This function allows you to pass an input field (i.e., type=[text] or textarea) with default text.
        The default text will be populated in the value and title attributes. 
        When the field is focused, if the text hasn't changed, this function will erase the default text*/
    var el = $(sender);
    if(defaultText == undefined){ defaultText = ""; }
    if(el.value != undefined)
    {
        defaultText = el.value;
        el.value = "";
        if(el.title == "")
        {
            el.title = defaultText;
        }
        el.removeClass('focus_text');
        el.onblur = function()
                    {
                        if(el.value == "")
                        {
                            el.addClass('focus_text');
                            el.value = defaultText;
                        }
                        else
                        {
                            el.onfocus = function(){};
                            el.onblur = function(){};
                        }
                    };
    }
    else if(el.innerText != undefined)
    {
        defaultText = el.innerText;
        el.innerText = "";
        if(el.title == "")
        {
            el.title = defaultText;
        }
        el.removeClass('focus_text');
        el.onblur = function()
                    {
                        if(el.innerText == "")
                        {
                            el.addClass('focus_text');
                            el.innerText = defaultText;
                        }
                        else
                        {
                            el.onfocus = function(){};
                            el.onblur = function(){};
                        }
                    };
    }
}

function addFocusText(){
    $$(".focus_text").each(function(el){
        if(el.value != undefined)
        {
            defaultText = el.value;
        }
        else if(el.innerText != undefined)
        {
            defaultText = el.innerText;
        }
        else
        {
            defaultText = "";
        }
        
        if(el.title != defaultText)
        {
            el.title = defaultText;
        }
        
        el.onfocus = function() { focusText(el, defaultText); }
    });
}
addOnLoad(addFocusText);





// Hash Listeners 
// (c) Jordan Sherer 2008
// For back button support and such in the history, use: http://photogallery.neilj.fastmail.fm/revision5/js/HistoryManager.js
var hash_callbacks = new Array();
var hash_previous = "";
var hash_listeners = new Object();

// Split the hash
function hash_get_args()
{
    hash = hash_read();
    return hash.split('/');
}

// Set the current Hash
function hash_set(new_hash)
{
    self.document.location.hash = new_hash;
    document.location.hash = new_hash;
    document.title = document.title + " "
}

// Get the current Hash, return "" if none.
function hash_get()
{
    hash = self.document.location.hash;
    if($chk(hash))
    {
        return unescape(hash.substring(1));
    }
    else
    {
        return "";
    }
}

// This returns the registered listener for a specific hash.
function hash_get_listener(key)
{
    key = key.toLowerCase();
    return hash_listeners[key];
}

// This registeres a listener for a specific hash
function hash_set_listener(key, func)
{
    key = key.toLowerCase();
    hash_listeners[key] = func;
}

// This function is the starting function of the hash listener.
function hash_listen()
{
    hash_add_callback(hash_changed);
    setInterval("hash_check()", 500);
}

// This function checks to see if the document hash has changed since the last interval. If it has, it runs the callbacks that are registered. 
function hash_check()
{
    if(hash_previous == null){ 
        hash_previous = hash_read();
        }
    
    hash_now = hash_get();
    if(hash_previous != hash_now)
    {
        if(hash_callbacks.length > 0)
        {
            hash_run_callbacks(hash_previous, hash_now);
        }
        hash_previous = hash_now;  
    }
}
addOnLoad(hash_listen);

// This function registers a method as a callback to the hash change listener.
function hash_add_callback(func)
{
	id = hash_callbacks.length;
	hash_callbacks[id] = func;
	return id;
}

// This function runs the callbacks that are registered
function hash_run_callbacks(prev_hash, new_hash)
{
    count = hash_callbacks.length;
	for(i=0; i<count; i++)
	{
		func = hash_callbacks[i];
        if(func != undefined)
        {
            func(prev_hash, new_hash);
        }
	}
}

// This function is the default callback when the hash changes. It checks to see if a listener has been registered to the specific hash. If it has, then it runs it.
function hash_changed(prev_hash, new_hash)
{
    var f = hash_listeners[new_hash];
    if($chk(f))
    {
        f(prev_hash, new_hash);
    }
}