/* this is the entry point for jquery
   execution starts when the dom is ready
*/

// help us walk the DOM for development
$.fn.tagName = function() {
    return this.get(0).tagName;
}

// get the element after the final '/' in the url
// this will allow us to do manipulations per page
function getFilename() {
	var file_name = document.location.href;
    var end = (file_name.indexOf("?") == -1) ? file_name.length : file_name.indexOf("?");
    return file_name.substring(file_name.lastIndexOf("/")+1, end);
}

// override the out-of-the-box defs for fading functions to work in IE
jQuery.fn.fadeIn = function(speed, callback) { 
    return this.animate({opacity: 'show'}, speed, function() { 
        if (jQuery.browser.msie)  
            this.style.removeAttribute('filter');  
        if (jQuery.isFunction(callback)) 
            callback();  
    }); 
}; 
 
jQuery.fn.fadeOut = function(speed, callback) { 
    return this.animate({opacity: 'hide'}, speed, function() { 
        if (jQuery.browser.msie)  
            this.style.removeAttribute('filter');  
        if (jQuery.isFunction(callback)) 
            callback();  
    }); 
}; 
 
jQuery.fn.fadeTo = function(speed,to,callback) { 
    return this.animate({opacity: to}, speed, function() { 
        if (to == 1 && jQuery.browser.msie)  
            this.style.removeAttribute('filter');  
        if (jQuery.isFunction(callback)) 
            callback();  
    }); 
};


$(document).ready(function() {

	// if we're logged into drupal, set the sitebox width 
	// to allow the management div to fit on the left
	if ( $('#drupal_left_management_div').length != 0 )	// if we have the div on the page... 
	{
		var width = $('#drupal_left_management_div').width();
		var winWidth = $(window).width();
		var siteboxWidth = winWidth - width - 30;
		$('#sitebox').css('width',siteboxWidth);
	}
	else
	{
		//jAlert('No management div... allowing full width for sitebox','Developer Message');
	}
	
	// we are hiding the search form radios, but we'll default the search selection to "search PSU"
	// instead of "current site"
	
	// get first radio and unselect it (current site)
	var rad_1 = $('#edit-location-0-wrapper').children(1).children(1);
	rad_1.removeAttr('checked');
	
	// get second radio and select it (all PSU)
	var rad_2 = $('#edit-location-1-wrapper').children(1).children(1);
	rad_2.attr('checked','checked');
	
	// fade effect for the site entry point (click here to begin)
	var page = getFilename();
	if (page == "home" || page == "")	// depends on what the user entry point is, but both are the homepage
	{
		// defining this function preloads both images (redundant preload on the inactive image)
		$('#splash-top-left').hover(
			
			// mouse IN definition
			function () {
				// on hovering over, find the element we want to fade *up*
				var fade = $('> div', this);
    
				// if the element is currently being animated (to a fadeOut)...
				if (fade.is(':animated')) {
					// ...take it's current opacity back up to 1
					fade.stop().fadeTo(500, 1);
				} else {
					// fade in quickly
					fade.fadeIn(500);
				}
			},
			
			// mouse OUT definition
			function () {
				// on hovering out, fade the element out
				var fade = $('> div', this);
				if (fade.is(':animated')) {
					fade.stop().fadeTo(250, 0);
				} else {
					// fade away slowly
					fade.fadeOut(250);
				}
			}
		);
	}
	
	// THIS IS NOT WORKING ... WILL COME BACK TO IT IF I HAVE TIME
	// add a link to the FSP logo leading to the homepage on all pages having
	// the logo in the left sidebar
	//$('.leftmenu').prepend('<a href="http://www.pdx.edu/fsp" style="width: 220px; height: 65px; display:block;">FSP Home</a>');
	//$('.leftmenu > .colright').css('padding-top','-65px');
	
	
	
	// image gallery pages use the jquery image gallery, so instantiate one
	// on the pages that the image lists appear (<ul class="gallery"> ...</ul>)
	
	// if there is an image gallery on the page ...
	if ( $('ul.galleria').addClass('fsp_gallery').length != 0)
	{	
		
		$('ul.fsp_gallery').galleria({
			history   : false, // activates the history object for bookmarking, back-button etc.
			clickNext : true, // helper for making the image clickable
			insert    : '#main_image', // the containing selector for our main image
			onImage   : function(image,caption,thumb) { // let's add some image effects for demonstration purposes
				
				// fade in the image & caption
				if(! ($.browser.mozilla && navigator.appVersion.indexOf("Win")!=-1) ) { // FF/Win fades large images terribly slow
					image.css('display','none').fadeIn(1000);
				}
				caption.css('display','none').fadeIn(1000);
				
				// fetch the thumbnail container
				var _li = thumb.parents('li');
				
				// fade out inactive thumbnail
				_li.siblings().children('img.selected').fadeTo(500,0.3);
				
				// fade in active thumbnail
				thumb.fadeTo('fast',1).addClass('selected');
				
				// add a title for the clickable image
				image.attr('title','Next image >>');
			},
			onThumb : function(thumb) { // thumbnail effects goes here
				
				// fetch the thumbnail container
				var _li = thumb.parents('li');
				
				// if thumbnail is active, fade all the way.
				var _fadeTo = _li.is('.active') ? '1' : '0.3';
				
				// fade in the thumbnail when finnished loading
				thumb.css({display:'none',opacity:_fadeTo}).fadeIn(1500);
				
				// hover effects
				thumb.hover(
					function() { thumb.fadeTo('fast',1); },
					function() { _li.not('.active').children('img').fadeTo('fast',0.3); } // don't fade out if the parent is active
				)
			}
		});
	}
});



