/**
 * South McKeel Website ( $Id: mck.js 1 2011-06-27 18:38:06Z brandon $ )
 *
 * Global javascript
 *
 * @author			Brandon Davie <brandondavie@mckeelacademy.com>
 * @copyright		Copyright (c) 2010 The Schools of McKeel Academy
 * @link				http://mckeelschools.com/
 * @version			$Rev: 1 $
 * @package			sma
 * @subpackage	js
 */

window.mckjs = Class.extend( {
	vars: [],
	
	init: function()
	{
		$( document ).ready(
			function()
			{
				/* Image rollovers... */
				$( '.rollover' ).each( function()
				{
					mck.setupRollover( $( this ) );
				} );
				
				/* Admin tools */
				$( '.adminToolsWrap' ).each(
					function()
					{
						mck.setupAdminTools( this );
					}
				);
				
				/* Navigation... */
				$( '.nav li a' ).hover(
					function()
					{
						/* Find the image... */
						var theParent = $( this ).parent();
						if ( theParent.hasClass( 'active' ) )
						{
							return;
						}						
						
						if ( theParent.children( 'img' ) )
						{
							var curImg = theParent.children( 'img' );
							var curSrc = theParent.children( 'img' ).attr( 'src' );
							if ( curSrc.match( 'disabled' ) )
							{
								curImg.attr( 'src', curSrc.replace( '_disabled', '' ) );
							}
						}
					},
					function()
					{
						/* Find the image... */
						var theParent = $( this ).parent();
						if ( theParent.hasClass( 'active' ) )
						{
							return;
						}
						
						if ( theParent.children( 'img' ) )
						{
							var curImg = theParent.children( 'img' );
							var curSrc = theParent.children( 'img' ).attr( 'src' );
							if ( ! curSrc.match( 'disabled' ) )
							{
								curImg.attr( 'src', curSrc.replace( '.png', '_disabled.png' ) );
							}
						}
					}
				);
				
				/* Generic deletion confirmations.. */
				$( '.delConfirm' ).each(
					function()
					{
						$( this ).click(
							function()
							{
								return confirm( 'Are you sure you want to delete this ' + $( this ).attr( 'class' ).replace( 'delConfirm ', '' ) + '?' );
							}
						);
					}
				);
				
				/* Google Maps */
				if ( $( '#map_canvas' ) && $( '#map_canvas' ).attr( 'id' ) == 'map_canvas' )
				{
					if ( GBrowserIsCompatible() )
					{
						var map = new GMap2( document.getElementById( 'map_canvas' ) );
						map.setMapType(G_HYBRID_MAP);
						map.setCenter( new GLatLng( 28.011898, -81.914341 ), 13 );
						map.addOverlay( new GMarker( new GLatLng( 28.011898, -81.914341 ), {title: "South McKeel Academy"} ) );
						map.setZoom( 17 );
						map.setUIToDefault();
					}
				}
				
				/* Search keywords */
				$( '#ftMckSearchKeywords' ).focus(
					function()
					{
						if ( $( this ).attr( 'value' ) == 'Enter search terms' )
						{
							$( this ).attr( 'value', '' );
							$( this ).css( { color: '#000000' } );
						}
					}
				);
				
				$( '#ftMckSearchKeywords' ).blur(
					function()
					{
						if ( $( this ).attr( 'value' ) == '' )
						{
							$( this ).attr( 'value', 'Enter search terms' );
							$( this ).css( { color: '#bdbdbd' } );
						}
					}
				);
			}
		);
	},
	
	setupRollover: function( img )
	{
		var rollSrc = img.attr( 'src' );
		if ( ! rollSrc.match( /_over.png/ ) )
		{
			/* Preload... */
			$( '<img>' ).attr( 'src', rollSrc.replace( '.png', '_over.png' ) );
		}

		/* Setup events */
		$( img ).hover(
			function()
			{
				var rollSrc = $( this ).attr( 'src' );
				if ( ! rollSrc.match( /_over.png/ ) )
				{
					$( this ).attr( 'src', rollSrc.replace( '.png', '_over.png' ) );
				}
			},
			function()
			{
				var rollSrc = $( this ).attr( 'src' );
				if ( rollSrc.match( /_over.png/ ) )
				{
					$( this ).attr( 'src', rollSrc.replace( '_over.png', '.png' ) );
				}
			}
		);
	},
	
	setupAdminTools: function( wrap )
	{
		$( wrap ).hover(
			function()
			{
				$( this ).children( '.adminTools' ).css( { opacity: 1.0, filter: 'alpha( opacity = 100 )' } );
			},
			function()
			{
				$( this ).children( '.adminTools' ).css( { opacity: 0.3, filter: 'alpha( opacity = 30 )' } );
			}
		);
	},
	
	getCookie: function( cookieName )
	{
		/* Attach prefix */
		cookieName = this.vars[ 'cookiePrefix' ] + cookieName;
		var theCookie = document.cookie.match( cookieName + '=(.*?)(;|$)' );
		
		if ( theCookie )
		{
			return unescape( theCookie[ 1 ] );
		}
		else
		{
			return null;
		}
	},
	
	setCookie: function( cookieName, cookieValue, expires )
	{
		/* Attach prefix */
		cookieName = this.vars[ 'cookiePrefix' ] + cookieName;
		
		try {
			cookieValue = String( cookieValue.toString() );
			
			if ( ! expires )
			{
				var theDate = new Date();
				theDate = new Date( theDate.getTime() + ( ( 100 * 24 * 60 * 60 ) * 1000 ) );
				expires = theDate.toGMTString();
			}
			
			var theCookie = cookieName + "=" + escape( cookieValue ) + '; expires=' + expires;
			document.cookie = theCookie;
		}
		catch ( e )
		{
			return false;
		}
		
		return true; 
	}
} );

mck = new mckjs();
