/* Waterloo Javascript Effects */
/* Requires jQuery 1.4.3 */
/* By White Whale Web Services */

// Classes that match the following selector or the '.apply3d' class will be made 3d
/*var apply3d = '.container, .photo_feature,#wbar_panel,#wbutton,#newsite-notice,#emerg,#photo_credit_content,.divider,body#homepage #photo_background,body#homepage #homepage_features li,#multimedia,.gateway_internal #main,#faculty_links li a,#gallery_title,#gallery_main,#gallery_thumbs li,#gatewayheading,#topcontent,#main,.linklist li a';*/

var apply3d = '.container, .photo_feature,#wbar_panel,#wbutton,#newsite-notice,#photo_credit_content,.divider,body#homepage #photo_background,body#homepage #homepage_features li,#multimedia,.gateway_internal #main,#faculty_links li a,#gallery_title,#gallery_main,#gallery_thumbs li,#gatewayheading,#topcontent,#main,.linklist li a';

$(function() { // on DOM ready
	$('<div class="container"><div id="wbar_panel"/></div>').appendTo('#institutional_header');
	// 3D effect
	if(!!test_props(['transformProperty','WebkitTransform', 'MozTransform', 'OTransform','msTransform'])) { // if CSS transforms are supported
		var elements = $(apply3d+',.apply3d'); // make all the container parents 3D, plus any element opted-in with the 3d class
		elements.each(function() {
			var container = $(this).is('.container'),
				right = $('<div class="w3d_right w3d_face"><div class="w3d_color"></div>').appendTo(this),
				bottom = $('<div class="w3d_bottom w3d_face"><div class="w3d_color"></div>').appendTo(this),
				width = $(this).outerWidth(),
				bgimage = (container ? $(this).parent().css('background-image') : $(this).css('background-image')), // for containers, use the parent's background image
				bgcolor = $(this).css('background-color');
			if($(this).css('position')=='static') {
				$(this).css('position','relative');				
			}
			
			if(bgcolor != 'transparent' && bgcolor != 'rgba(0, 0, 0, 0)') {
				right.add(bottom).children().css({backgroundColor:bgcolor});
			}
			if(bgimage!='none') {
				right.add(bottom).children().css({backgroundImage:bgimage});
			}
		});
		var wbar_face = $('#wbar .w3d_right');
		wbar_face.removeAttr('style').append('<div id="wbutton_face"/>'); // special case for the W-bar
	}
	
	/*$('.w3d_right').each (function () {
		$(this).css('border', '1px red solid');
	});*/
	
	// W-bar panels
	var panel = $('<div id="panel_container"/>').appendTo('#wbar_panel'),
		background = $('#tagline,#homepage_features');
	$('#wbar_panel').hide();
	$('#wbar a').click(function(e) {
		e.preventDefault();
		var li = $(this).parent(),
			which = $(this).attr('id').replace('showpanel','panel');
		if(!li.hasClass('active')) { // if this tab is NOT already active
			$('#wbar_panel').fadeIn(300);
			li.addClass('active').siblings().removeClass('active');
			if(!$.browser.msie) background.stop().fadeTo(300,0.5); // don't do this in IE, due to IE bug that makes the elements non-clickable
			panel.show();
			var spinner = $('<div class="spinner"/>').css('opacity',0).insertBefore(panel).fadeTo(300,1);
			$.get('includes_new/includes/'+which+'.php'+'?'+Math.random(),function(data) {
				data = $(data);
				data.find('ul.listbox li:odd').addClass('odd');
				panel.attr('class',which).html(data);
				spinner.stop().fadeTo(300,0,function() { $(this).remove(); });
				
			});	
		} else { // otherwise, if it is active
			li.removeClass('active');
			if(!$.browser.msie) background.stop().fadeTo(300,1);
			$('#wbar_panel').fadeOut(300,function() { panel.empty(); });
		}
	});
	
	/* old function
	$('#officeslist_jump li a').live('click',function(e) { // offices & services jump links
		e.preventDefault();
		$('#officeslist').scrollTop(0);
		var offset = $($(this).attr('href')).position();
		if(offset) $('#officeslist').scrollTop(offset.top); // scroll to the offset
	});
	*/
	
	//IE7-compatible function
	$('#officeslist_jump li a').live('click',function(e) { // offices & services jump links
		e.preventDefault();
		var target = $(this).get(0).href; //was - $(this).attr('href');
		target = target.substring(target.indexOf ('#')); //IE7 will get the entire URL, so make sure we only get the anchor
    	var offset = $(target).position(); //gets pixel location of clicked section
		if (offset) { //if offset is not set, then section does exist, so do nothing; otherwise
			$('#officeslist').scrollTop(0);
			offset = $(target).position(); // position changes from previous line.
			if(offset && offset.top != 0) {
				$('#officeslist').scrollTop(offset.top); // scroll to the offset
			}
		}
	});
	
	
	if($('body').is('#homepage')) { // specific to the homepage:
		

		// Page sizing
		var content = $('#content>.resize'); 
		$(window).resize(function() {
			content.height(Math.min(750,Math.max($(window).height()-165,580)));
		}).resize();
	}
});

/**
 * Adapted from Modernizr v1.6
 * (under Modernizr's MIT license)
 */
function test_props( props, callback ) {
	var m = document.createElement('modernizr'),
	    m_style = m.style;
    for ( var i in props ) {
        if ( m_style[ props[i] ] !== undefined && ( !callback || callback( props[i], m ) ) ) {
            return true;
        }
    }
}

$.fn.extend({ // add plugins
	placeholder:function(options) { // bootstrap HTML5 placeholder attributes for browsers that don’t support it
		options = options || {};
		var s = {
			style:options.style || 'placeholder', // the class to add when the element is operating as a placeholder
			clear:options.clear || false // the elements which, when clicked, should wipe the placeholder
		};
		return this.each(function() { // with each matched element
			var self = $(this);
//			if(this.placeholder && 'placeholder' in document.createElement(this.tagName)) return; // if the browser supports placeholders for this element, abort
			if(self.data('placeholder')) { // if a placeholder has already been set on this element
				return; // abort to avoid double-binding
			}
			self.data('placeholder',true); // flag this element as having a placeholder, so we'll never double-bind
			var placeholder = self.attr('placeholder'),
				clear = function() { // to clear the placeholder
					if(self.val()==placeholder) { // if the text is the placeholder
						self.removeClass(s.style).val(''); // blank the text and remove the placeholder style
					}
				};
			self.focus(clear)
				.blur(function() {
					var val = self.val();
					if(!val||val==placeholder) { // if thereâ€™s no text, or the text is the placeholder
						self.addClass(s.style).val(placeholder); // set the text to the placeholder and add the style
					}
				}).blur(); // and do it now
			self.parents('form').submit(clear);
			$(s.clear).click(clear);
		});
	}
});

