var ns4 = (document.layers) ? true:false;
var ie4 = (document.all) ? true:false;


function getElementPos(elem) {
	elem = (elem == null) ? 'slideshow_slide_container' : elem;
	
	var x = $(elem).offsetLeft;
	var y = $(elem).offsetTop;
	var parent = $(elem).offsetParent;
	while (parent) {
		x += parent.offsetLeft;
		y += parent.offsetTop;
		parent = parent.offsetParent;
	}
	return { x:x, y:y }
	
}

function getElementDimensions(elem) {
	elem = (elem == null) ? 'slideshow_slide_container' : elem;
	
	var y_height = $(elem).offsetHeight;
	var x_width = $(elem).offsetWidth;
	return { width:x_width, height:y_height }
}


/////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////
// MOUSE TRACK :: Track mouse coordinates and update hidden form inputs as well as global variables
var track_mouse_x = 0; // global variable for mouse 
var track_mouse_y = 0; // global variable
function mouse_track(e) {
	
	// mouse move
	if (!e) e = event;
	var mouse_x, mouse_y;
	
	if (e.pageX == null) {
		// IE case
		var d = (document.documentElement && document.documentElement.scrollLeft != null) ?	document.documentElement : document.body;
		mouse_x= e.clientX + d.scrollLeft;
		mouse_y= e.clientY + d.scrollTop;
	} else {
		// all other browsers
		mouse_x = e.pageX;
		mouse_y = e.pageY;
	}
	
	// set the global variables' values
	track_mouse_x = mouse_x;
	track_mouse_y = mouse_y;
	
	// apply mouse coordinates to the hidden form fields for debugging
	if( $('mouse_x') ) {
		var debug_mouse_x = $('mouse_x');
			debug_mouse_x.value = mouse_x;
	}
	if( $('mouse_y') ) {
		var debug_mouse_y = $('mouse_y');
			debug_mouse_y.value = mouse_y;	
	}
	
}


function mouse_compare(elem) {
	var elem = (elem == null) ? 'slideshow_slide_container' : elem;
	var mouse_x = track_mouse_x;
	var mouse_y = track_mouse_y;
	
	var object_coords = getElementPos(elem);
	var object_x = object_coords['x'];	// object left side
	var object_y = object_coords['y'];	// object right side
	
	var object_dimensions = getElementDimensions(elem);
	var object_height = object_dimensions['height'];
	var object_width = object_dimensions['width'];
	
	var object_x_2 = parseInt(object_x+object_width);	// right edge
	var object_y_2 = parseInt(object_y+object_height);	// bottom edge
	
	
	// apply object coordinates to the hidden form fields for debugging
	var debug_object_x = $('object_x');
		debug_object_x.value = object_x;
	var debug_object_y = $('object_y');
		debug_object_y.value = object_y;
	var debug_object_x_2 = $('object_x_2');
		debug_object_x_2.value = object_x_2;
	var debug_object_y_2 = $('object_y_2');
		debug_object_y_2.value = object_y_2;
		
		
	// compare mouse coordinates against object coordinates
	var mouse_over_x = false;
	var mouse_over_y = false;
	if( ( mouse_x >= object_x ) && ( mouse_x <= object_x_2 ) ) { // inside the slide hit area (left/right)
		mouse_over_x = true;
	}
	if( ( mouse_y >= object_y ) && ( mouse_y <= object_y_2 ) ) { // inside the slide hit area (top/bottom)
		mouse_over_y = true;
	}
	
	if(mouse_over_x == true && mouse_over_y == true) { // mouse is inside the hit area
		return true;
	} else {
		return false;
	}

}
function mouse_track_alert() {
	var text = mouse_compare();
	alert( text );
}


/////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////
// MOUSE TRACK HOVER :: 
function mouse_track_hover() {
	
	// GET SLIDESHOW POSITION & DIMENSIONS
	var elem = null;
	if( $('hover_container') ) { 
		elem = $('hover_container');
	}
	var mouse_over = mouse_compare(elem);
	
	if(mouse_over == true && $('slide_lock').value == 'false' ) {
		if( $('slide_open').value == 'false' ) {
			$('hover_wrap_expander').style.display = "block";
			$('hover_wrap_contracter').style.display = "none";
			if ($('hover_next_project_home') ) {
				$('hover_next_project_home').style.display = "none";
			}
			
		} else if( $('slide_open').value ) {
			
			$('hover_wrap_expander').style.display = "none";
			$('hover_wrap_contracter').style.display = "block";
			if ($('hover_next_project_home') ) {
				$('hover_next_project_home').style.display = "block";
			}
		}
		
	} else {
		$('hover_wrap_expander').style.display = "none";
		$('hover_wrap_contracter').style.display = "none";
		if ($('hover_next_project_home') ) {
			$('hover_next_project_home').style.display = "none";
		}
	}
	
}


/////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////
// MOUSE TRACK EXPANDER :: 
function mouse_track_showText() {
	var parent = null;
	var elem = 'slideshow_summary';
	var slide_width = 440;
	var slide_height = null;
	var elem = null;
	if( $('slideshow_summary') ) { 
		elem = $('slideshow_summary');
	}
	var mouse_over = mouse_compare(elem);
	
	if( (mouse_over == true) && ($('slide_open').value == 'true') && ($('slide_lock').value == 'false') ) {
		
		if( $('slideshow_id') ) {
			var parent = $('slideshow_id').value;
		}
		if( $('slide_width') ) {
			var slide_width = $('slide_width').value;
			var slide_width = parseInt(slide_width);
		}
		if( $('slide_height') ) {
			var slide_height = $('slide_height').value;
			var slide_height = parseInt(slide_height);
		}
		
		var mouse_x = track_mouse_x;
		var mouse_y = track_mouse_y;
		
		var object_coords = getElementPos(elem);
		var object_x = object_coords['x'];	// object left side
		var object_y = object_coords['y'];	// object right side
		
		
		var object_dimensions = getElementDimensions(elem);
		var object_height = object_dimensions['height'];
		var object_width = object_dimensions['width'];
		var object_x_2 = parseInt(object_x+object_width);	// right edge
		var object_y_2 = parseInt(object_y+object_height);	// bottom edge
		
			
		// compare mouse coordinates against object coordinates
		var mouse_over_x = false;
		var mouse_over_y = false;
		if( ( mouse_x >= (object_x+150) ) && ( mouse_x <= object_x_2 ) ) { // inside the slide hit area (left/right)
			mouse_over_x = true;
		}
		if ($('hover_next_project_home') ) { // homepage failsafe
			mouse_over_x = false;
			if( ( mouse_x >= (object_x+440) ) && ( mouse_x <= object_x_2 ) ) {
				mouse_over_x = true;
			}
		}
		if( ( mouse_y >= object_y ) && ( mouse_y <= object_y_2 ) ) { // inside the slide hit area (top/bottom)
			mouse_over_y = true;
		}
		
		if(mouse_over_x == true && mouse_over_y == true) { // mouse is inside the hit area
			slide_ExpandContract(parent, slide_height, slide_width, null);
		} else {
			return false;
		}
		
	}
	
}


/////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////
// ATTACH MOUSE EVENTS
function addEvent(element, ev, handler)  {
	var doHandler = function(e) {
		return handler(e||window.event);
	}
	if (element.addEventListener) { 
		element.addEventListener(ev, doHandler, false); 
	} else if (element.attachEvent) { 
		element.attachEvent("on" + ev, doHandler); 
	}
}
addEvent(window, "mousemove", mouse_track);
addEvent(window, "mousemove", mouse_track_hover);
addEvent(window, "mousedown", mouse_track_showText);

// IE failsafe...
if(browser == "Explorer") {
	$('body_container').onclick = function() {
		mouse_track_expander();
	}
	$('body_container').onmousemove = function() {
		mouse_track_hover();
	}
}








// PLAY VIDEO
function slideshow_play_video(id) {
	id = (id == null) ? null : id;
	if( id ) {
		$('load_layer_data').appendChild( $(id) );
		$(id).style.display = "block";
		new Effect.toggle('load_layer', 'appear', { duration: 0.25, afterFinish: function() { sendToActionScript(); } });
		new Effect.toggle('body_fog', 'appear', { duration: 0.25 });
		load_childLoaded = id;
		embed_obj = document.getElementsByClassName('body_qtObject_embed');
		for(i=0; i < embed_obj.length; i++) {
			$( embed_obj[i].id ).style.visibility = 'hidden';
		}
	}
	
	$('slide_lock').value='false';
}



// LAST SLIDE
var last_slide_background = null;
function slideshow_last_slide(url, action) {
	
	url = (url == null) ? null : url;
	action = (action == null) ? "show" : action;
	
	// SHOW
	if(action == 'show' && last_slide_background != true) {
		new Effect.Appear( $('hover_background'), {  from: 0.0, to: 1.0, duration: 0.25, afterFinish: function() { $('slide_lock').value='false'; } } ); // end Effect
		new Effect.Appear( $('hover_next_project'), {  from: 0.0, to: 1.0, duration: 0.25 } ); // end Effect
		last_slide_background = true;
		
	// HIDE
	} else if(action == 'hide' && last_slide_background == true) {
		new Effect.Appear( $('hover_background'), { from: 1.0, to: 0.0, duration: 0.25, afterFinish: function() { $('slide_lock').value='false'; } } ); // end Effect
		new Effect.Appear( $('hover_next_project'), { from: 1.0, to: 0.0, duration: 0.25 } ); // end Effect
		last_slide_background = null;
		
	// JUMP
	} else if(action == 'jump') {
		window.location = _URL + url;
		last_slide_background = null;
	} else {
		window.location = _URL + url;
		last_slide_background = null;
	}
	
}



