/**
 * Planning/reporting components jQuery theme customisations
 */

/**
 * DOM ready functions
 */
$(document).ready(function () {
	var bHasErrorClass = false;// true if error class applied to banner

	/**
	 * keyboard shortcuts
	 */
	// modifier key flags
	var isCtrl = false;
	var isShift = false;

	$(document).ready(function() {
		$(document).keydown(function(e) {
		    if(e.which == 17) {
		        isCtrl = true;
		    }
		    if(e.which == 16) {
		        isShift = true;
		    }

		    /**
		     * Shortcut key combinations
		     */

		    // ctrl + l (logout)
		    if((e.which == 76) && isCtrl) {
				//e.preventDefault();
		    	window.location='/dios/?coreAction=logout';// logout
		    }
		    // e (edit plan/report) - TODO
		    /*if (e.which == 69 || e.which == 101) {
		    	window.location=
		    }*/
		});

		$(document).keyup(function(e) {
		    if (e.which == 17) {// clear ctrl flag
		        isCtrl = false;
		    }
		    if(e.which == 16) {// clear shift flag
		        isShift = false;
		    }
		});
	});


	// setup jquery ui buttons
	$('.nav,.action,.button,:button,:submit')
		.button()
		.removeClass('nav action info');
		//.click(function() {
			/*$('.nav,.action,.button,:button,:submit')
				.attr('disabled', 'disabled');// disable all buttons*/
		//});

	$('.ui-button-text').css({'font-weight' : 'bold'});

	// make some elements draggable
	/*$('#diosContent').addClass('ui-overlay');
	$('#progress-box').addClass('ui-widget-shadow');ui-widget ui-widget-content*/

	$('#progress-box')
		.draggable({ containment: '#diosContent', handle: 'h3.title' });// restrict dragging to within #diosContent div
	$('#progress-box>h3').css({'cursor': 'move'});// add movable cursor to title

	// tooltips
	$('#pageOrganisationName').tipsy({
		gravity: 'w',
		fade: true,
		html: true,
		'delayIn': 0,
		'delayOut': 1000
	});

	// plan & report banner interaction
	$('.plan-banner,.report-banner')
		.click(function() {
			$(this).slideUp('slow');
		})
		.addClass('cursor-link')
		.hover(
			// mouseover
			function() {
				if ($(this).hasClass('ui-state-error')) {
					bHasErrorClass = true;// remember to re-apply the error class
					$(this).removeClass('ui-state-error');
				}
				$(this).removeClass('ui-state-highlight');
				$(this).addClass('ui-state-hover');
				//$(this).css('border-style', 'dashed');
			},
			// mouseout
			function() {
				if (bHasErrorClass) {// re-apply error class
					$(this).addClass('ui-state-error');
					bHasErrorClass = false;
				}
				$(this).removeClass('ui-state-hover');
				$(this).addClass('ui-state-highlight');
			});
	// apply other theme classes

	// headings
	$('h1,div.title,.item_label')
		.addClass('ui-widget-header');
	$('h3:not(.plan-img-current h3,.plan-img-archived h3,.plan-img-submitted h3,.plan-img-rejected h3,.plan-img-approved h3)')// exclude plan icon elements
		.filter(function() {// special h3 elements to ignore
			return ($(this).parents(':not[id^=planIcon]').length < 1 && $(this).parents(':not[id^=reportIcon]').length < 1);// plan and report icon text
		})
		.addClass('ui-widget-header');// exclude plan icon elements
	// result table captions
	$('table.ind-result>caption').css({'text-align' : 'left'});// left-align result table caption text
	// hilight
	$('.inline-hilight').addClass('ui-state-error ui-corner-all');
	$('.instructionsspecial,.bar,#progress-box,p.indicator12monthonly,p.program12monthonly,p.focusArea12MonthOnly,#planMessage,#reportMessage,.homeMessage').addClass('ui-state-highlight ui-corner-all');
	$('p.indicator12monthonly,p.program12monthonly,p.focusArea12MonthOnly').css({'border-width':'3px'});

	// required control (qcodo)
	$('.required')
		.addClass('ui-state-error');

	// remove some headings
	$('#planMessage,#reportMessage,.homeMessage').removeClass('ui-widget-header');

	// error/alert
	$('.error,.warning,#validationErrorCollaborations')
		.addClass('ui-state-error')
		.css('padding', '.2em');

	// hide validation error message if empty (qcodo puts the element on the page even when there are no validation errors)
	if ($('#validationErrorCollaborations').is(':empty'))
		$('#validationErrorCollaborations').removeClass('ui-state-error');

	// give some elements rounded edges (css3 browsers only)
	$('#prevStrategicDirection,p.textbox2,.timestamps,li,.textbox,.previousContextTable,.activitiesApproachesPreviousVersion,#resultTables,#resultTableInstructions,.warning').addClass('ui-corner-all');
	$('.instructionsspecial h3')
		.addClass('ui-corner-all')
		.css('padding-left', '.5em');

	/*$('div[id^=indActApp]')
		.addClass('ui-corner-all');*/
});

/**
 * jQuery theme customisations
 */
function addTooltips() {
	// add tooltips (tipsy)
	// nw | n | ne | w | e | sw | s | se
	$('.tooltip-nw').tipsy({gravity: 'nw', fade: true, html: true, opacity: .95});
	$('.tooltip-n').tipsy({gravity: 'n', fade: true, html: true, opacity: .95});
	$('.tooltip-ne').tipsy({gravity: 'ne', fade: true, html: true, opacity: .95});
	$('.tooltip-w').tipsy({gravity: 'w', fade: true, html: true, opacity: .95});
	$('.tooltip-e').tipsy({gravity: 'e', fade: true, html: true, opacity: .95});
	$('.tooltip-sw').tipsy({gravity: 'sw', fade: true, html: true, opacity: .95});
	$('.tooltip-s').tipsy({gravity: 's', fade: true, html: true, opacity: .95});
	$('.tooltip-se').tipsy({gravity: 'se', fade: true, html: true, opacity: .95});
	// autoNS | autoWE
	$('.tooltip-auto-ns').tipsy({gravity: $.fn.tipsy.autoNS, fade: true, html: true, opacity: .95});
	$('.tooltip-auto-we').tipsy({gravity: $.fn.tipsy.autoWE, fade: true, html: true, opacity: .95});
}// function addTooltips

function customiseTheme_phcris() {
	// javascript to customise phcris theme
	$('a.nav,a.action').css('color', '#28574e');// adjust link colour

	// text drop-shadows - TODO: apply based on color attribute
	/*$(\"h1[color~=ff0000],div.title[color~=ff0000],.item_label[color~=ff0000]\").css({'text-shadow': '0px 0px 2px #333333'});
	$(\"h1[color~=ffffff],div.title[color~=ffffff],.item_label[color~=ffffff]\").css({'text-shadow': '0px 0px 2px #333333'});
	$(\"h1[color~=363636],div.title[color~=363636],.item_label[color~=363636]\").css({'text-shadow': '0px 0px 2px #ffffff'});
	$(\"h1[color~=000000],div.title[color~=000000],.item_label[color~=000000]\").css({'text-shadow': '0px 0px 2px #ffffff'});*/
	$('h1,div.title,.item_label').css({'text-shadow': '0px 0px 2px #666666'});
	/*$('h3:not(.plan-img-current h3,.plan-img-archived h3,.plan-img-submitted h3,.plan-img-rejected h3,.plan-img-approved h3,[id^=accordion] h3)')// exclude plan icon elements
		.filter(function() {// exclude planIcon & reportIcon class h3's
			return ($(this).parents(':not[id^=planIcon]').length < 1 && $(this).parents(':not[id^=reportIcon]').length < 1);
		})
		.filter(function() {// exclude bar class h3's
			return !$(this).hasClass('bar');
		})
		.css({'text-shadow': '0px 0px 2px #666666', 'padding-left': '.25em'});*/

	//$('h3 > a').css({'text-shadow': '0px 0px 2px #ffffff'});

	/*$('h3:not([id^=accordion] h3)')
		.filter(function() {// exclude planIcon & reportIcon class h3's
			return ($(this).parents(':not[id^=planIcon]').length < 1 && $(this).parents(':not[id^=reportIcon]').length < 1);
		})
		.filter(function() {// apply only to h3's of class bar
			return $(this).hasClass('bar');
		})
		.css({'text-shadow': '0px 0px 2px #ffffff'});*/
}// function customiseTheme_phcris

// sticky footer for form edit controls
function stickyFooter() {
	/**
	 * floXPos_Orig and floYPos_Orig must be defined on document ready,
	 * they are the initial x and y (left & top) position of the #formButtonControls
	 * element, in pixels
	 */
	var floEffectiveWindowHeight = ($(window).height()+$(window).scrollTop());
	var floEffectiveBoxBottom = (floYPos_Orig+($('#formButtonControls').height()/2));
	if (floEffectiveWindowHeight < floEffectiveBoxBottom) {
		// controls box has disappeared off the bottom of the document - 'stick' hover controls to the bottom of the document
		$('#formButtonControls').fadeOut('fast');// hide inline controls
		$('#formButtonControlsHover').css({
				'position': 'absolute',
				'top': ($(window).scrollTop()+$(window).height()-50+'px')
			})
			.fadeIn('fast');
	} else {// put controls box back in original position in the document
		$('#formButtonControls').fadeIn('fast');// show inline controls
		$('#formButtonControlsHover').fadeOut('fast');
	}
}// function stickyFooter

// sticky header for form edit controls
function stickyHeader() {
	/**
	 * floXPos_header_Orig and floYPos_header_Orig must be defined on document ready,
	 * they are the initial x and y (left & top) position of the #inlineControls
	 * element, in pixels
	 */
	var floEffectiveWindowHeight = ($(window).height()+$(window).scrollTop());
	//var floEffectiveBoxBottom = (floYPos_header_Orig+($('#inlineControls').height()/2));
	var floEffectiveBoxBottom = (floYPos_header_Orig+($('#inlineControls').height()));
	//if (floEffectiveWindowHeight < floEffectiveBoxBottom) {
	if (document.documentElement.scrollTop > floYPos_header_Orig || self.pageYOffset > floYPos_header_Orig) {
		// controls box has disappeared off the bottom of the document - 'stick' hover controls to the bottom of the document
		$('#inlineControls').fadeOut('fast');// hide inline controls
		$('#formIndicatorDetailsSticky').css({
				'position': 'absolute',
				//'top': ($(window).scrollTop()+$(window).height()-50+'px')// stick to bottom of viewport
				'top': ($(window).scrollTop()+'px')// stick to top of viewport
			})
			.fadeIn('fast');// show sticky controls
	} else {// put controls box back in original position in the document
		$('#inlineControls').fadeIn('fast');// show inline controls
		$('#formIndicatorDetailsSticky').fadeOut('fast');// hide sticky controls
	}
}// function stickyHeader


