(function($) {
	/* User agent identification */
	var ua_str = navigator.userAgent;
	
	var ua = {
	    iphone: ua_str.match(/(iPhone|iPod|iPad)/),
	    blackberry: ua_str.match(/BlackBerry/),
	    android: ua_str.match(/Android/)
	};
	
	var ua_type = {
	    mobile: ua.iphone || ua.blackberry || ua.android
	};
	
	/* Highlighter */
	function highlight() {
		var hash = window.location.hash;
		
		$(hash).each(function() {
			var color = $(this).css('background-color');
			var $element = $(this).css('background', '#ffef89');
			
			$element.animate({
				backgroundColor: color}, 1500, function() {
				});
		});
	}
	
	$(document).ready(function() {
		/* Highlighter */
		$(window).bind('hashchange', function() {
			highlight();
		});
		highlight();
		
		/* Schedule */
		$('.schedule-button').each(function() {
			var $button = $(this);
			var $link = $('h3 a.schedule-link', $button);
			var $schedule_full = $('.schedule-full', $button);
			
			var title = $link.attr('rel');
			var url = $link.attr('href');
			
			$schedule_full.dialog({
                draggable: true,
                modal: true,
                autoOpen: false,
                resizable: false,
                title: title + ' Schedule',
                width: 820,
                height: 700
			});

			$link.click(function(event) {
				event.preventDefault();
				
				if (ua_type.mobile) {
					window.location = $link.attr('href');
				}
				else {
					$schedule_full.dialog('open');
				}
			});
			
			$('.schedule-mini table.schedule', $button).click(function(event) {
				$link.click();
			});
		});
		
		/* Schedule Session */
		$('.schedulesession').each(function() {
			var $schedulesession = $(this);
			var $link = $('a.schedule-class-desc-link', $schedulesession);
			var $class_desc = $('.schedule-class-desc', $schedulesession);
			
			var title = $link.attr('rel');
			
			$class_desc.dialog({
                draggable: true,
                modal: true,
                autoOpen: false,
                resizable: false,
                title: title,
                width: 700,
                height: 500
			});

			$link.click(function(event) {
				event.preventDefault();
				
				if (ua_type.mobile) {
					window.location = $link.attr('href');
				}
				else {
					$class_desc.dialog('open');
				}				
			});
		});
			
		/* Profiles */
		$('.profile-overview').each(function() {
			var $profile_overview = $(this);
			var $profile_full = $('.instructor-profile-full', $profile_overview);
			var $profile_link = $('.profile-link', $profile_overview);
			
			$profile_full.dialog({
                draggable: true,
                modal: true,
                autoOpen: false,
                resizable: false,
                title: $profile_link.filter(':first').attr('title'),
                width: 700,
                height: 500
			});
			
			$profile_link.click(function() {
				if (ua_type.mobile) {
					window.location = $profile_link.attr('href');
				}
				else {
					$profile_full.dialog('open');
				}
				
				return false;
			});
		});

		/* Newsletter form */
		$('.newsletter-form').each(function() {
			var $form = $(this);
			var $text = $('input[type="text"]', $form);
			
			$text.val('Email Address');
			$text.css('color', '#c7c7c7');
			
			$text.focus(function() {
				$(this).val('');
				$text.css('color', '');
			});
		});
		
		/* Fragment edit */
		/*$('.fragment > a.fragment-edit').each(function() {
			var $link = $(this);
			
			$link.parent().attr('title', 'Middle click to edit').bind('mousedown', function(event) {
				if (event.which == 2) {
					document.location.href = $link.attr('href');
				}
			});
		});
		*/
		/* List edit */
		/*$('.content-items-list > a.content-items-list-edit').each(function() {
			var $link = $(this);
			
			$link.parent().attr('title', 'Middle click to edit').bind('mousedown', function(event) {
				if (event.which == 2) {
					document.location.href = $link.attr('href');
				}
			});
		});*/
		
		
		/* Menu hovering */
		$('.menu .clickable').hover(function() {
			$(this).addClass('hover');
			
			if ($(this).hasClass('menu-list')) {
				$(this).addClass('menu-list-hover');
			}
		}, function() {
			$(this).removeClass('hover').removeClass('menu-list-hover');
		});
		
		/* Find class menu item */
		$('.location-selection').each(function() {
			var $loc_sel = $(this);	
			var $prov_select = $('select[name="province"]', $loc_sel);
			var $classes = $('.location-selection-classes', $loc_sel);
			var $hidden = $('.hidden', $loc_sel);
		
			var inactive_selected = function() {
				$classes.html('Select a province to show available class locations');
				$classes.addClass('inactive-location-selection');
			};
		
			inactive_selected();
		
			$prov_select.change(function() {
				$('.location-selection-province', $classes).appendTo($hidden);
			
				if ($prov_select.val() == 'NA') {
					inactive_selected();
				}
				else {
					$classes.html('');
					$('.location-selection-' + $prov_select.val(), $hidden).appendTo($classes);
					$classes.removeClass('inactive-location-selection');
				}
			});
			
			$prov_select.change();
		});
		
		var $find_class = $('.find-class-popup');
		var $loc_sel_hidden = $('body > .hidden .location-selection');
		
		if ($find_class.length > 0) {
			$loc_sel_hidden.dialog({
                draggable: true,
                modal: true,
                autoOpen: false,
                resizable: false,
                title: 'Find a Fit 4 Two Class',
                width: 700,
                height: 500
			});
			
			$find_class.each(function() {
				var $item = $(this);
				
				$item.attr('onclick', '');
			
				$item.click(function() {
					if ($item.hasClass('find-class-popup-province')) {
						var $ps = $('select[name="province"]', $loc_sel_hidden);
						$ps.val($item.attr('title'));
						$ps.change();
					}
					
					if (ua_type.mobile) {
						window.location = $item.attr('href');
					}
					else {
						$loc_sel_hidden.dialog("open");
					}
                
					return false;
				});
			});
		}
	});
})(jQuery);
