// Pacific Union College
// Written by Nic Hubbard
// nic@zedsaid.com

$(document).ready(function() {
						   
	// ----- Global -----
	// Lets start tracking files
	$("a[href*='.pdf'], a[href*='.doc'], a[href*='.xls'], a[href*='.ppt']").click(function() {
		pageTracker._trackPageview('/doc/' + $(this).attr('href'));
	});
	$("a[href*='.mov']").click(function() {
		pageTracker._trackPageview('/video/' + $(this).attr('href'));
	});
	$("a[href*='.zip']").click(function() {
		pageTracker._trackPageview('/file/' + $(this).attr('href'));
	});
	$("a[href*='.mp3']").click(function() {
		pageTracker._trackPageview('/audio/' + $(this).attr('href'));
	});
	
	var proto = location.protocol;
	var site = location.host;
	var host_url = proto + '//' + site;
	
	
	// ----- Get rid of IE6!! -----
	if (typeof worst_browser_ever !== 'undefined') {
		if (worst_browser_ever) {
			$('body').prepend('<div id="noIE" style="display:none">Pacific Union College is degrading support for Internet Explorer 6.  Please consider downloading a modern browser. <a target="blank" href="http://www.apple.com/safari/">Download Safari</a> - <a target="blank" href="http://www.mozilla.com/firefox/">Download Firefox</a></div>');
			$('#noIE').slideDown('slow');
		}
	}
	
	
	// ----- Transportation Reg -----
	// Make sure they enter student ID
	$('#form_email_60737_submit').click(function() {
		// Check to see if they are paying with SID
		if ($('#q60737_q11').val() == '0') {
			// Zero means the first item, which is charge to student acct.
			if ($('#q60737_q4').val() == '') {
				alert('You must enter your student ID number if you are paying with your student account.');
				// Set a nice color
				$('#q60737_q4').css('background-color', '#ca582b');
				return false;
			}
			
		}
	});
	
	if ($('#form_email_60737_submit').length > 0) {
		var matrix_hour_format = $("#q60737\\:q10value\\[h\\], #q60737\\:q12value\\[h\\]");
		convertMatrixTime(matrix_hour_format);
	}
	
	// ----- Student Activity Request form
	if ($('#form_email_63789').length > 0) {
		var matrix_hour_format = $("#q63792\\:q10value\\[h\\], #q63792\\:q11value\\[h\\], #q63795\\:q6value\\[h\\], #q63795\\:q7value\\[h\\]");
		convertMatrixTime(matrix_hour_format);
	}
	
	
	// ----- Online Giving -----
	// Auto select the fund based on a query string
	//var fund_type;
	if (typeof fund_type !== 'undefined') {
		// We want to check and see what find to select
		$('#q30305_q4').children('option').each(function() { 
			var selection = $(this).val().replace(/ /gi, '').toLowerCase();
			selection = selection.replace(/-/gi, '');
			if (selection == fund_type) { 
				$(this).attr('selected', true);
			} 
		});//end each
	}
	
	
	// ----- Parents Weekend Registration ----
    $('#q57722_q12_0').click(function() {
		var val = $('#q57722_q17');
		if ($('#q57722_q12_0:checked').length > 0)		 {
			val.removeAttr('readonly');
		} else {
			val.attr('readonly', 'readonly');
			val.val('');
		}
	});//end click
	
    $('#q57722_q13_0').click(function() {
		var val = $('#q57722_q18');
		if ($('#q57722_q13_0:checked').length > 0)		 {
			val.removeAttr('readonly');
		} else {
			val.attr('readonly', 'readonly');
			val.val('');
		}
	});//end click
	
    $('#q57722_q14_0').click(function() {
		if ($('#q57722_q14_0:checked').length > 0)		 {
			$('#q57722_q19, #q57722_q20').removeAttr('readonly');
		} else {
			$('#q57722_q19, #q57722_q20').attr('readonly', 'readonly');
			$('#q57722_q19, #q57722_q20').val('');
			
		}
	});//end click
	
	
	// ----- Alumni: Give PUC -----
	$('#teamTabs li').click(function() {
		$('#teamTabs li').removeClass('selected');
		$(this).addClass('selected');
		$('.teamTab').hide();
		var id = $(this).attr('id');
		if (id == 'teamTab1') {
			$('#team1').show();
		} else if (id == 'teamTab2') {
			$('#team2').show();
		} else if (id == 'teamTab3') {
			$('#team3').show();
		} else if (id == 'teamTab4') {
			$('#team4').show();
		}//end else if
		return false;
	});//end click
	
	
	// ----- Current Students: Tabs -----
	$('#pucTabs li').click(function() {
		$('#pucTabs li').removeClass('selected');
		$(this).addClass('selected');
		$('.pucTab').hide();
		var id = $(this).attr('id');
		if (id == 'pucTab1') {
			$('#pucTabHolder1').show();
		} else if (id == 'pucTab2') {
			$('#pucTabHolder2').show();
		} else if (id == 'pucTab3') {
			$('#pucTabHolder3').show();
		} else if (id == 'pucTab4') {
			$('#pucTabHolder4').show();
		}//end else if
		return false;
	});//end click
	
	
	// ----- Top Level Pages -----
	// Truncate Summary
	if ($('#bannerTopItems .eventItem').length > 0) {
		$('#bannerTopItems .eventItem p').truncate(138); 
		$('#bannerTopItems .eventItem p').show();
	}
	

	// ----- Homepage -----
	// Homepage News Cycle
	if ($('#s4').length > 0) {
		$('#s4').before('<div id="nav" class="nav">').cycle({
			fx: 'fade',
			speed: 1000,
			timeout: 4000,
			pager: '#nav',
			pause: 1
		});
	}
	
	// Truncate News Summary because it can be too long
	if ($(".homenewsText").length > 0) {
		$(".homenewsText").truncate(160);
	}
	
	// Truncate News Summary
	if ($("span.deptNewsSum").length > 0) {
		$("span.deptNewsSum").truncate(60); 
		$("span.deptNewsSum").show();
	}
	
	// Convert ISO Date to a readable date
	if ($("span.ISODate").length > 0) {
		$("span.ISODate").each(function() {
			$(this).text(getTFID($(this).text()))
		});
	}
	
	// Add scrollers to homepage calendar div
	if ($('#home_contentright .scroll').length > 0) {
		$('#home_contentright .scroll').jScrollPane({
			showArrows: true,
			scrollbarWidth: 7
		});
	}
	
	// Show homepage image caption
	$("#homepage_image").mouseover(function() {
		$("#homepageCaption").show();
	});
	
	// Hide homepage image caption
	$("#homepage_image").mouseout(function() {
		$("#homepageCaption").hide();
	});
	
	
	// ----- Top Level Pages -----
	// Cycle Banners
	if ($('ul#acaBannerFeat').length > 0) {
		$('ul#acaBannerFeat').cycle({ 
			fx:      'fade', 
			speed:    3000, 
			timeout:  5000,
			pause: 1
		});
	}
	
	// ----- Portals -----
	// Don't let news summary be too long
	if ($('#features').length > 0) {
		$('#features .eventItem p').truncate(70); 
		$('#features .eventItem h4').truncate(30); 
		$('#features .eventItem p, #features .eventItem h4').show();
	}
	
	//Add scrollers to div
	if ($('#announceHub .scroll').length > 0) {
		$('#announceHub .scroll').jScrollPane({
			showArrows:true,
			scrollbarWidth:7
		});
	}
	
	// Add Scroller to our new Current Students 2.0 Calendar
	if ($('#currentStudentsCal .scroll').length > 0) {
		// Remove any days that don't have a following child that is an event
		$('li.calTitle').each(function() {
			if ($(this).next().attr('class') !== 'calItem') {
				$(this).hide();
			}
		});
		$('#currentStudentsCal .scroll').jScrollPane({
			showArrows:true,
			scrollbarWidth:7
		});
	}

	//Clue Tip
	if ($('.cluetip').length > 0) {
		$('.cluetip').cluetip({
			activation: 'click',
			positionBy: 'bottomTop',
			sticky: true,
			width: '500px',
			showTitle: false,
			closePosition: 'bottom',
			closeText: '<p class="close"></p>'
		});
	}
	
	//Cycle image plugin
	if ($('#eventsHub').length > 0) {
		$('#eventsHub').cycle({
			fx:     'fade',
			speed:  1000,
			timeout: 2000,
			pause:   1,
			prev: '#prev',
			next: '#next'
		});
	}
	
	
	//Code for accordion
	if ($('#portalBodyRight').length > 0) {
		$('#portalBodyRight').accordion({
			header: 'h3', 
			autoheight: false,
			alwaysOpen: false
		});
	}
	
	//Add scrollers to div
	if ($('#portalEvents .scroll').length > 0) {
		$('#portalEvents .scroll').jScrollPane({
			showArrows:true,
			scrollbarWidth:7
		}); 
	}
	
	// --- Portals 2.0 ----
	if ($('#currentStudentsCal').length > 0) {
		// Add scroller
		$('#gallerySlideshow').jScrollPane({
			showArrows:true,
			scrollbarWidth:7
		}); 
		
		$('div.galleryImage a').click(function() {
			var obj = $(this);
			var img_id = $(this).attr('rel');
			obj.parent('div.galleryImage').addClass('galleryClicked').append('<img src="/__data/assets/image/0017/52910/loading-portals.gif" />');
			$.ajax({
				type: 'GET',
				url: current_student.gallery_url+'?gallery='+img_id,
				success: function(data) {
					$('#gallerySlideshow').html(data);
					// Re-initialize Thickbox
					tb_init('a.thickbox');
					// Add a scroller
					$('#gallerySlideshow').jScrollPane({
						showArrows:true,
						scrollbarWidth:7
					}); 
				}//end success
			});//end ajax	
			return false;
	   });//end click
	}
	
	// ----- Top Header Image Rotator -----
	var header_image = (window.arr_header === undefined) ? true : false;
	var admin = ($('#adminLoggedInUser').length > 0) ? true : false;
	if (!header_image) {
		// Lets write out our images
		var rand = Math.random();
		rand = rand * (arr_header.length - 1);
		rand = Math.ceil(rand);
		// Make sure we have a URL
		if (arr_header[rand] !== undefined) {
			$('#section_title').before('<img src="' + arr_header[rand] + '" alt="Page Header Image" />');
		}
			
	}
	
	// ----- News: Calendar -----
	// Add new class and link
	$('.calendarEvent').click(function() {
		// Find our child link							   
		var cal_link = $(this).children('a').attr('href');
		document.location = cal_link;
	});
	$('.calendarEvent').mouseover(function() {
		$(this).addClass('calOver').removeClass('calendarEvent');							   
	});
	// Even though this won't really be the class, we need to use it, since binding is not "live".
	$('.calendarEvent').mouseout(function() {
		$(this).addClass('calendarEvent').removeClass('calOver');							   
	});
	
	// Rewrite links to use "Printable" Calendar
	if ($('.printCal').length > 0) {
		var new_cal_path = document.location.href.split('?');
		var print_cal = $('.printCal').attr('href');
		if (isset(new_cal_path[1])) {
			print_cal = print_cal + '?' + new_cal_path[1];
			$('.printCal').attr('href', print_cal);
			
		}//end if
		
	}//end if
	// Show "through" if we have an end date
	$('.through_day').each(function() {
		if ($(this).text() != '') {
			$(this).parent().show();	
		}
	});
	
	
	// ----- Business Department -----
	// Calculations for quarter
	$('#quarter').keyup(function() {
		var message = 'You have not entered a number.  Please enter a number'; 
		if (isNaN($(this).val())) {
			alert(message);
			$(this).val('');
		} else {
			if ($('#quarter').val() == '') {
				var quarter = 0;
				$('#semester').val('0');
			} else {
				var quarter = parseInt($(this).val()) / 1.5;
				$('#semester').val(quarter.toFixed(2));
			}// End else
			
		}// End else
	
	}); // End keyup
	// Calculations for semester
	$('#semester').keyup(function() {
		var message = 'You have not entered a number.  Please enter a number'; 
		if (isNaN($(this).val())) {
			alert(message);
			$(this).val('');
		} else {
			if ($('#semester').val() == '') {
				var semester = 0;
				$('#quarter').val('0');
			} else {
				var semester = parseInt($(this).val()) * 1.5;
				$('#quarter').val(semester.toFixed(2));
			}// End else
			
		}// End else
		
	});// End keyup
	
});// End ready


/**
* Checks to see if a variable is set
*
* @param string		obj		The variable we check
*
* @version $Revision: 0.1
*/
function isset(obj)
{
    // Check to see if a variable or array item is set
    if (typeof(obj) == 'undefined') {
		return false;
	} else {
		return true;
	}

}// End isset


// Converts select menu for hour format in a Custom Form
function convertMatrixTime(obj) {
	// Run through the select
	var time_am = 1;
	var time_pm = 1;
	var item_num = 0;
	obj.children('option').each(function() {
		var text = $(this).text();
		var val = parseInt($(this).val());
		if (item_num === 1) {
			$(this).text('12 am');
		}//end if
		if (item_num === 13) {
			$(this).text('12 pm');
		}//end if
		
		if (text !== '') {
			// Rewrite
			if (item_num < 13 && item_num !== 1) {
				$(this).text(time_am+' am');
				// Increment
				time_am++;
			}//end if
			
			if (item_num >= 13 && item_num !== 13) {
				$(this).text(time_pm+' pm');
				// Increment
				time_pm++;
			}//end if
		
		}//end if
		
		// Increment
		item_num++;
		
		// Reset our vars
		if (item_num >= 25) {
			time_am = 1;
			time_pm = 1;
			item_num = 0;
		}
		
	});//end each
	
}//convertTime