/* common.js */

// Cookie Functions
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

// URI functions
function replaceUriSegment(position, str) {
	var parts = location.href.split('/');
	parts[position+2] = str;
	location.href = parts.join('/');
}

// Pop-up functions
function popUp(uri, x, y, mode) {
	if(!x) { x=800; }
	if(!y) { y=600; }

	if(!mode || mode == 'dialog') {
		var opts = 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=yes,width='+x+',height='+y+',bgcolor=#cccccc';
	} else if(mode == 'normal') {
		var opts = 'toolbar=yes,location=yes,directories=yes,status=yes,menubar=yes,scrollbars=yes,resizable=yes,width='+x+',height='+y+',bgcolor=#cccccc';
	}

	if(window.open(uri,'HR Central Window',opts)) {
		return false;
	} else {
		return true;
	}
}


// Page Initialization (for body onload event)
$(document).ready(function() {
	/*
	Begin Form initialization
	*/

	// Check if forms.js is loaded
	var scripts = document.getElementsByTagName('script');
	if(scripts.length>0) {
		for(var i=0; i<scripts.length; i++) {
			if(scripts[i].src.toLowerCase().indexOf('forms.js')>=0) {
				// Create the onsubmit event
				$('form').submit(function(event){
					return validateForm($(this).id);
				});
			}
		}
	}

	/*
	End Form initialization
	*/

	/*
	Begin External Link initialization
	*/
	var links = document.getElementsByTagName('a');
	var domain = window.location.hostname;
	for(var i=1; i<links.length; i++) {
		if(links[i].href.toLowerCase().indexOf('javascript:')==-1 && !links[i].href.match(domain)) {
			links[i].target = "_blank";
			links[i].title += " (New Window)";
		}
	}
	/*
	End External Link initialization
	*/


	/*
	Begin Table Row Link initialization
	*/
	$('tr').each(function(){
		var anchors = $(this).find('a');
		if(anchors.length==1) {
			$(this).click(function(){window.location.href=this.getElementsByTagName('a')[0].href; return false;});
			$(this).addClass('clickable');
		}
	});
	/*
	End Table Row Link initialization
	*/

	/*
	Begin Form Field Empty initialization
	*/
	$('#search_form input').focus(function(){
		if($(this).val() == 'Search people and courses') {
			$(this).val('');
		}
	});

	$('#search_form input').blur(function(){
		if($(this).val().length<1) {
			$('#search_string').val('Search people and courses');
		}
	});
	/*
	End Form Field Empty initialization
	*/


	/*
	Begin Tee Select initialization
	*/
	var urlParts = document.URL.split('/');
	urlParts[6] = '';
	var newUrl = urlParts.join('/');
	$('select#tee_select').change(function(){
		window.location.href=newUrl + $(this).val();
	});
	/*
	End Tee Select initialization
	*/

	/*
	Begin a.close initialization
	*/
	$('a.close').click(function(event){
		event.preventDefault();
		$(this).parent().slideUp('slow');
	});
	/*
	End a.close initialization
	*/

	/*
	Begin Row Totals initialization
	*/
	$('tr td input').change(function(){
		var total = 0;
		$(this).parent().parent().find('td input').each(function(){total += $(this).val() *1});
		$(this).parent().parent().find('td:last').html(total);
	});
	/*
	End  Row Totals initialization
	*/

	/*
	Begin tr.row_hover initialization
	*/
	$('.scorecard_display tr').hover(function(){$(this).addClass('row_hover');}, function(){$(this).removeClass('row_hover');});
	/*
	End tr.row_hover initialization
	*/

	/*
	quick links initialization
	*/
	$('form.login_inline').hide();
	$('a.quick-login').click(function(event){
		event.preventDefault();
		$('form.login_inline').slideToggle('slow');
	});

	/*
	ul.slideshow-list initialization
	*/
	$('ul.slideshow-nav li:first-child').addClass('current');
	$('ul.slideshow-list li').hide();
	$('ul.slideshow-list li:first-child').show();

	// The slideshow-nav
	$('ul.slideshow-nav li a').click(function(event){
		event.preventDefault();
		$('ul.slideshow-nav li').removeClass('current');
		$(this).parent().addClass('current');
		$('ul.slideshow-list li').hide();
		$($(this).attr('href')).show();
	});

	// The slideshow-list
	$('ul.slideshow-list a.button_grey').click(function(event){
		event.preventDefault();
		$('ul.slideshow-list li').hide();
		$('ul.slideshow-nav li').removeClass('current');
		var context = $(this).attr('href');
		$(context).addClass('current');
		$(context).show();
		var contextIndex = $('ul.slideshow-list li').index($(context));
		$('ul.slideshow-nav li').eq(contextIndex).addClass('current');
	});

	// Begin Toolkit initialization
	$('.practice_log .toolkit').hide();
	$('.practice_log li, #course_list li').hover(function(){$(this).find('.toolkit').show(); $(this).addClass('hover');}, function(){$(this).find('.toolkit').hide(); $(this).removeClass('hover');})
	// End Toolkit initialization
	
	// Begin Date Field initialization
	if($.datepicker) {
		$.datepicker.setDefaults({
			dateFormat: 'yy-mm-dd',
			maxDate: '+0m +0w',
			nextText: '>>',
			prevText: '<<'
		});
		$('#practice_date, #dateplayed').datepicker();
	}
	// End Date Field initialization
	
	// Begin accordian initialization
	$('.accordian').next().hide();
	$('.accordian').addClass('clickable');
	$('.accordian').click(function(event){
		event.preventDefault();
		$(this).next().slideToggle();
		$(this).hasClass('accordian_close') ? $(this).removeClass('accordian_close') : $(this).addClass('accordian_close');
	});
	// End accordian initialization

});
