/*
 * util.js
 *
 * javascript functionality for plexifilm.com
 * author: john berry | john / ulfmagnet \ com
 *
 * [code from external sources where noted]
 *
 */

/* Register an onload function. */
function WindowOnload(f) 
{
    var prev=window.onload;
    window.onload=function(){ if(prev)prev(); f(); }
}

/*----------------------------------------------------------------------------------------*/
/* BEGIN scrolling code adapted from http://www.quirksmode.org/js/layerscroll.html        */

var scrollers = new Array();
var clipTops = new Array();
var clipBottoms = new Array();
var clipWidths = new Array();
var toppers = new Array();
var lyrheights = new Array();

var amount,theTime,theHeight,DHTML;
var time = new Array();

function get_scroller_id(layername) {
    if (layername == 'featured-content-scroller') {
	return 0;
    } 
    else if (layername == 'featured-news-scroller') {
	return 1;
    } 
    else if (layername == 'sidebar-news-scroller') {
	return 2;
    } 
    else if (layername == 'title-detail-scroller') {
	return 3;
    } 
}

function init_scrollers()
{
    DHTML = (document.getElementById || document.all || document.layers);
    if (!DHTML) return;

    /*
     * TO ADD A SCROLLER:
     * define scroller params here, then add an entry to the get_scroller_id() function.
     *
     */
    scrollers[0] = 'featured-content-scroller';
    clipTops[0] = 0;
    clipBottoms[0] = 150; /* clipBottom - clipTop defines scroller's height */
    clipWidths[0] = 300;  /* should be same as width of scroller from stylesheet */
    toppers[0] = 410;     /* should be same as absolute:top of scroller from stylesheet */
    lyrheights[0] = 0;

    scrollers[1] = 'featured-news-scroller';
    clipTops[1] = 0;
    clipBottoms[1] = 100; /* clipBottom - clipTop defines scroller's height */
    clipWidths[1] = 300;  /* should be same as width of scroller from stylesheet */
    toppers[1] = 620;     /* should be same as absolute:top of scroller from stylesheet */
    lyrheights[1] = 0;

    scrollers[2] = 'sidebar-news-scroller';
    clipTops[2] = 0;
    clipBottoms[2] = 150; /* clipBottom - clipTop defines scroller's height */
    clipWidths[2] = 300;  /* should be same as width of scroller from stylesheet */
    toppers[2] = 320;     /* should be same as absolute:top of scroller from stylesheet */
    lyrheights[2] = 0;

    scrollers[3] = 'title-detail-scroller';
    clipTops[3] = 0;
    clipBottoms[3] = 200; /* clipBottom - clipTop defines scroller's height */
    clipWidths[3] = 320;  /* should be same as width of scroller from stylesheet */
    toppers[3] = 385;     /* should be same as absolute:top of scroller from stylesheet */
    lyrheights[3] = 0;

    for (var i = 0; i < scrollers.length; i++) {
	var x = new getObj(scrollers[i]);
	if (!x.obj) { continue; }
	var clipTop = clipTops[i];
	var clipBottom = clipBottoms[i];
	var clipWidth = clipWidths[i];
	var topper = toppers[i];
	var lyrheight = lyrheights[i];
	if (document.layers)
	    {
		lyrheights[i] = x.style.clip.bottom;
		lyrheights[i] += 20;
		x.style.clip.top = clipTop;
		x.style.clip.left = 0;
		x.style.clip.right = clipWidth;
		x.style.clip.bottom = clipBottom;
	    }
	else if (document.getElementById || document.all)
	    {
		lyrheights[i] = x.obj.offsetHeight;
		x.style.clip = 'rect('+clipTop+'px,'+clipWidth+'px,'+clipBottom+'px,0)';
	    }
	x.style.visibility = 'visible';
    }

}

function scroll_layer(layername,amt,tim)
{
    if (!DHTML) return;
    thelayer = new getObj(layername);
    if (!thelayer) return;
    amount = amt;
    theTime = tim;
    var id = get_scroller_id(layername);
    realscroll(id);
}

function realscroll(id)
{
    if (!DHTML) return;
    clipTops[id] += amount;
    clipBottoms[id] += amount;
    toppers[id] -= amount;

    if (clipTops[id] < 0 || clipBottoms[id] > lyrheights[id])
	{
	    clipTops[id] -= amount;
	    clipBottoms[id] -= amount;
	    toppers[id] += amount;
	    return;
	}
    if (document.getElementById || document.all)
	{
	    clipstring = 'rect('+clipTops[id]+'px,'+clipWidths[id]+'px,'+clipBottoms[id]+'px,0)';
	    thelayer.style.clip = clipstring;
	    thelayer.style.top = toppers[id] + 'px';
	}
    else if (document.layers)
	{
	    thelayer.style.clip.top = clipTops[id];
	    thelayer.style.clip.bottom = clipBottoms[id];
	    thelayer.style.top = toppers[id];
	}
    time[id] = setTimeout('realscroll('+id+')',theTime);
}

function stop_scroll(layername)
{
    var id = get_scroller_id(layername);
    if (time[id]) clearTimeout(time[id]);
}

/* END scrolling code from http://www.quirksmode.org/js/layerscroll.html          */
/*--------------------------------------------------------------------------------*/

/* safe DOM function [from http://www.quirksmode.org/js/dhtmloptions.html] */
function getObj(name)
{
    if (document.getElementById)
	{
	    this.obj = document.getElementById(name);
	    if (this.obj) {
		this.style = document.getElementById(name).style;
	    }
	}
    else if (document.all)
	{
	    this.obj = document.all[name];
	    if (this.obj) {
		this.style = document.all[name].style;
	    }
	}
    else if (document.layers)
	{
	    this.obj = document.layers[name];
	    if (this.obj) {
		this.style = document.layers[name];
	    }
	}
}

function g_openWindow(page, height, width, win, scrollbars, resizable, toolbar, menubar)
{
    if (!height)
	height = 260;
    if (!width)
	width = 500;
    if (!win)
	win = "_new"

	    height += 25; // hack for safari popups		    

    var h, w, scrol, siz;
    w = "WIDTH=" + width.toString();
    h = "HEIGHT=" + height.toString();
    scrol = ",scrollbars=" + ( (scrollbars) ? "yes" : "no" );
    siz = ",resizable=" + ( (resizable) ? "yes" : "no" );
    tool = ",toolbar=" + ( (toolbar) ? "yes" : "no" );
    menu = ",menubar=" + ( (menubar) ? "yes" : "no" );
    attr = w + "," + h + scrol + siz + tool + menu;
    popupWin = window.open(page,win,attr);
    popupWin.focus();
}

/*----------------------------------------------------------------------------------------*/
/* Mini-catalog and search related functions						  */

function show_title(_id) {
   var x = new getObj('cat-callout');
   x.obj.innerHTML = '&raquo;' + titles[_id];   
}

function clear_title() {
   var x = new getObj('cat-callout');
   x.obj.innerHTML = '&nbsp;';
}

function handle_browse(_select) {
   _val = _select.options[_select.selectedIndex].value;
   if (_val == null || _val.length == 0) {
      return false;
   }
   document.forms.minicatform.submit();
}

function setup_search(_txt) {
   if (_txt.value == 'search') {
      _txt.value = '';
   }
}


