//--------------------------------------
// Portfolio page UA implementation
//
//--------------------------------------
function port_initialize(container) {
	// install focus handlers on each thumbnail button object in the tray container; select the first button/slide for display

	var container_obj = $(container);
	container_obj.next_slide = '';
	container_obj.animating = false;

	var elements = container_obj.select('li.port_nav_button');
	var idx = 0;
	elements.each(function(e) {
		Event.observe(e, 'click', bfocus_in.bindAsEventListener(e, elements));
		Event.observe(e, 'mouseout',  bfocus_out.bindAsEventListener(e, elements));
		if (idx == 0) {
			e.port_focus = true;
			e.addClassName('port_nav_button_selected');
		} else {
			e.port_focus = false;
		}
		e.port_slide = 'port_property_frame' + idx;
		e.port_frame = container;
		$(e.port_slide).port_button = e;
		idx++;
	});
}

function bfocus_in(event, elements) {
	// serialize animation and ignore re-selection of current slide
	var frame = $(this.port_frame);
	if (this.port_focus)
		return;
	if (frame.animating) {
		frame.next_slide = this;
		return;
	}
	frame.animating = true;
	// remove focus from previous button / start transition-out on current slide
	elements.each(function(e) {
		if (e.port_focus == true) {
			e.port_focus = false;
			Effect.Fade(e.port_slide, {duration: 0.5,
                                       queue: 'end',
                                       afterFinish: function(e) {e.element.port_button.removeClassName('port_nav_button_selected');}});
		}
	});
	// give focus to current button / start transition-in on current slide
	this.port_focus = true;
	Effect.Appear(this.port_slide, {duration: 0.5,
                                    queue: 'end',
                                    afterSetup: function(e) {
						e.element.port_button.addClassName('port_nav_button_selected');
                                    },
									afterFinish: function(e) {
                        var frame = $(e.element.port_button.port_frame);
                        frame.animating = false;
                        if (frame.next_slide != '') {
                          // fake mouseover event to switch to next_slide
                          var next_button = frame.next_slide;
                          frame.next_slide = '';
                          bfocus_in.bind(next_button)('', elements);
                        }
                                    }});
}

function bfocus_out(event, elements) {
}




/* Added Scroller abilities */
// below code gives up and down scroll buttons to images, spans, ... named up_name, down_name, respectively.
// will keep the default scroll_box's style overflow if it encounters errors (so make overflow: auto;)

// usage: put this after the scrollbox div:  var div_scroll1 = new TextScroll('div_scroll1', 'scroll_box');
function TextScroll(scrollname, div_name, up_name, down_name)
{
    this.div_name = div_name;
    this.name = scrollname;
    this.scrollCursor = 0;
    this.speed = 5;
    this.timeoutID = 0;
    this.div_obj = null;
    this.up_name = up_name;
    this.dn_name = down_name;

	{
        if (document.getElementById) {
            div_obj = document.getElementById(this.div_name);
            if (div_obj) {
                this.div_obj = div_obj;
                this.div_obj.style.overflow = 'hidden'; // change this to auto if you want a scrollbar
            }
            div_up_obj = document.getElementById(this.up_name);
            div_dn_obj = document.getElementById(this.dn_name);
            if (div_up_obj && div_dn_obj) {
				div_up_obj.onmouseover = function() { eval(scrollname + ".scrollUp();") };
				div_up_obj.onmouseout = function() { eval(scrollname + ".stopScroll();") };

				div_dn_obj.onmouseover = function() { eval(scrollname + ".scrollDown();") };
				div_dn_obj.onmouseout = function() { eval(scrollname + ".stopScroll();") };
            }
        }
    }

	this.stopScroll = function() {
        clearTimeout(this.timeoutID);
    }

	this.scrollUp = function() {
        if (this.div_obj) {
            this.scrollCursor = (this.scrollCursor - this.speed) < 0 ? 0 : this.scrollCursor - this.speed;
            this.div_obj.scrollTop = this.scrollCursor;
            this.timeoutID = setTimeout(this.name + ".scrollUp()", 60);
        }
    }
	
	this.scrollDown = function() {
		if (this.div_obj) {
			this.scrollCursor += this.speed;
			this.div_obj.scrollTop = this.scrollCursor;
			if (this.div_obj.scrollTop == this.scrollCursor) {
				this.timeoutID = setTimeout(this.name + ".scrollDown()", 60);
			} else {
				this.scrollCursor = this.div_obj.scrollTop;
			}
		}
	}



this.resetScroll = function() {
        if (this.div_obj) {
            this.div_obj.scrollTop = 0;
            this.scrollCursor = 0;
        }
    }
}

/* end Scroller addition */