/* VIEWER STATE */
var quilt_width = -1;
var quilt_height = -1;
var viewer_width = -1;
var viewer_height = -1;
var viewer_start_left = -1;
var viewer_start_top = -1;

/* SCROLL STATE */
var clipTop = 0;
var clipBottom = 0;
var clipLeft = 0;
var clipRight = 0;
var posLeft = 0;
var posTop = 0;

/* MOUSE STATE */
var last_x = -1;
var last_y = -1;

/* DEBUGGING STATE */
var DEBUG = 0;

/* CALLOUT */
var callout_display_left = '267px';
var callout_hidden_left = '1500px';

/* FAKE POPUP */
var fake_popup_display_left = '50px';
var fake_popup_hidden_left = '1500px';

/* QUILT FUNCTIONS */
function quilt_setup(_x, _y, vis_x, vis_y) {
    quilt_width = _x;
    quilt_height = _y;
    viewer_width = vis_x;
    viewer_height = vis_y;

    var _viewer = document.getElementById('quilt-viewer');
    if (_viewer) {
	clipTop = 0;
	clipBottom = vis_y;
	clipLeft = 0;
	clipRight = vis_x;

	posLeft = 0; /* FIXME: pull this in dynamically */
	posTop = 0; /* FIXME: pull this in dynamically */

	_viewer.style.visibility = 'visible';
	_viewer.style.clip = 'rect('+clipTop+'px,'+clipRight+'px,'+clipBottom+'px,'+clipLeft+'px)';

	_viewer.onmousemove = handle_mousemove;
    }
						
    var _callout = document.getElementById('quilt-callout');
    if (_callout) {
	_callout.onmousemove = handle_mousemove;
    }
}

function handle_mousemove(e) {
    var _viewer = document.getElementById('quilt-viewer');
    var _x = getMouseX(e);
    var _y = getMouseY(e);

    if (last_x < 0) { last_x = _x; }
    if (last_y < 0) { last_y = _y; }

    if (DEBUG) {
	var debug = window.parent.document.getElementById('debug');	    
	if (debug) {
	    str = "x: " + _x + ", y: " + _y + ", last_x: " + last_x + ", last_y: " + last_y;
	    debug.innerHTML = str;
	}
    }

    // If the tribute popup is visible, don't do anything.
    if (Element.hasClassName('quilt-callout', 'onscreen') || Element.hasClassName('quilt-fake-popup', 'onscreen')) {
	// update coordinates to avoid a 'jump' when the popup closes
	last_x = _x;
	last_y = _y;
    
	return false;
    }

    // figure out if we've moved and, if so, how much
    var SCALE_FACTOR = 4;
    var delta_x = SCALE_FACTOR * (_x - last_x);
    var delta_y = SCALE_FACTOR * (_y - last_y);
    
    // adjust clipping accordingly
    if (_viewer) {
	// horizontal scrolling...
	if (delta_x < 0 && clipLeft > 0) {
	    // scroll left
	    clipLeft += delta_x;	    
	    if (clipLeft < 0) { 
		clipLeft = 0;   /* handle the edge case */
		posLeft = 0;
	    }
	    else {
		posLeft -= delta_x;
	    }
	}
	else if (delta_x > 0 && clipRight < quilt_width) {
	    // scroll right
	    clipLeft += delta_x;
	    if (clipLeft + viewer_width > quilt_width) { 
		clipLeft = quilt_width - viewer_width; /* handle the edge case */
		posLeft = 0 - clipLeft;
	    }
	    else {
		posLeft -= delta_x;
	    }
	}
	clipRight = clipLeft + viewer_width;

	// ... and vertical scrolling.
	if (delta_y < 0 && clipTop > 0) {
	    // scroll up
	    clipTop += delta_y;	    
	    if (clipTop < 0) { 
		clipTop = 0;   /* handle the edge case */
		posTop = 0;
	    }
	    else {
		posTop -= delta_y;
	    }
	}
	else if (delta_y > 0 && clipBottom < quilt_height) {
	    // scroll down
	    clipTop += delta_y;
	    if (clipTop + viewer_height > quilt_height) { 
		clipTop = quilt_height - viewer_height; /* handle the edge case */
		posTop = 0 - clipTop;
	    }
	    else {
		posTop -= delta_y;
	    }
	}
	clipBottom = clipTop + viewer_height;	

	// adjust position of pane to give illusion of smooth scroll.
	_viewer.style.left = posLeft + viewer_start_left;
	_viewer.style.top = posTop + viewer_start_top;

	_viewer.style.clip = 'rect('+clipTop+'px,'+clipRight+'px,'+clipBottom+'px,'+clipLeft+'px)';
    }
    
    last_x = _x;
    last_y = _y;
}

/* "CALLOUT" FUNCTIONS */

function handle_click(_host, _id) {
    /* grab HTML for tribute details via AJAX (using prototype.js functions) */
    var url = 'http://' + _host + '/tributes/fetch_details';
    var params = 'id=' + _id + '&width=' + Element.getStyle('quilt-callout', 'width');	
    var myAjax = new Ajax.Updater(
				  'tribute-details',
				  url, 
				  { method: 'get', parameters: params, insertion: show_callout }
				  );

}

function show_callout(element, html) {	
    /* display the tribute HTML */
    element.innerHTML = html;   

    /* move the callout to the center */    
    $('quilt-callout').style.left = callout_display_left;

    /* add a style so the scrolling handler code knows that the callout is visible */
    Element.addClassName('quilt-callout', 'onscreen');
}

function hide_callout() {	
    /* move the callout back "offscreen" */
    $('quilt-callout').style.left = callout_hidden_left;

    /* tell the scrolling code it's okay to scroll again */
    Element.removeClassName('quilt-callout', 'onscreen');
}

/* "FAKE POPUP" FUNCTIONS */

function display_about(_host, _version) {
    /* grab HTML for about page via AJAX (using prototype.js functions) */
    var url = 'http://' + _host + (_version ? _version : '/tributes') + '/about';
    var params = '';
    var myAjax = new Ajax.Updater(
				  'fake-popup-details',
				  url, 
				  { method: 'get', parameters: params, insertion: show_fake_popup }
				  );

}

function display_contribute(_host, _version) {
    /* grab HTML for contribute page via AJAX (using prototype.js functions) */
    var url = 'http://' + _host + (_version ? _version : '/tributes') + '/contribute';
    var params = '';
    var myAjax = new Ajax.Updater(
				  'fake-popup-details',
				  url, 
				  { method: 'get', parameters: params, insertion: show_fake_popup }
				  );

}

function display_tellafriend(_host, _version) {
    /* grab HTML for tell-a-friend page via AJAX (using prototype.js functions) */
    var url = 'http://' + _host + (_version ? _version : '/tributes') + '/tellafriend';
    var params = '';
    var myAjax = new Ajax.Updater(
				  'fake-popup-details',
				  url, 
				  { method: 'get', parameters: params, insertion: show_fake_popup }
				  );

}

function display_thanks(_host, _version) {
    /* grab HTML for tell-a-friend page via AJAX (using prototype.js functions) */
    var url = 'http://' + _host + (_version ? _version : '/tributes') + '/thanks';
    var params = '';
    var myAjax = new Ajax.Updater(
				  'fake-popup-details',
				  url, 
				  { method: 'get', parameters: params, insertion: show_fake_popup }
				  );

}

function show_fake_popup(element, html) {
    /* display the tribute HTML */
    element.innerHTML = html;   

    /* move the callout to the center */    
    $('quilt-fake-popup').style.left = fake_popup_display_left;

    /* add a style so the scrolling handler code knows that the callout is visible */
    Element.addClassName('quilt-fake-popup', 'onscreen');    
}

function hide_fake_popup() {
    /* move the fake popup back "offscreen" */
    $('quilt-fake-popup').style.left = fake_popup_hidden_left;

    /* tell the scrolling code it's okay to scroll again */
    Element.removeClassName('quilt-fake-popup', 'onscreen');    
}

function dump_position(_x, _y) {
    var _xpos = document.getElementById('xpos');
    if (_xpos) {
	_xpos.innerHTML = _x;
    }
    var _ypos = document.getElementById('ypos');    
    if (_ypos) {
	_ypos.innerHTML = _y;
    }
}

/* from http://snipplr.com/view.php?codeview&id=708 */
function getMouseX( e ) {
    if (!e) var e = window.event;
    if (e.pageX) {
	return e.pageX;
    }
    else if (e.clientX) {
	return ( e.clientX + ( document.documentElement.scrollLeft || document.body.scrollLeft ) );
    }
}
function getMouseY( e ) {
    if (!e) var e = window.event;
    if (e.pageY) {
	return e.pageY;
    }
    else if (e.clientY) {
	return ( e.clientY + ( document.documentElement.scrollTop || document.body.scrollTop ) );
    }
}

/* popup functions */
function open_quilt_popup(url) {
    var winName = 'quiltpopup';
    var height = 500;
    var width = 740;
    var features = 'status=0,toolbar=0,menubar=0,resizable=0,scrollbars=0,height='+height+',width='+width;
    window.open(url,winName,features);
}