﻿
function InitialiseAspNetAjax()
{
    Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequest);
}

$(document).ready(function(){
    InitialiseAjax();
});

function EndRequest(sender, args){
    InitialiseAjax();
}

var initialised = false;

function InitialiseAjax()
{
    if(!initialised)
    {

        initialised = true;
        //initSubMenu();
        formOps();
        stripe();
        dropList("dropList");
        dropList("dropList2");
        helpInit();
        flipInit();
        toggleDisplay();
        $('#pagePrint').show();
    }
}

// functionality shelved
    function initSubMenu() {
    //add expand graphic
    $('#SubNav ul ul > li.Current').parent().parent().addClass('Expand');

    //set up click
    $('#SubNav>ul>li:has(ul)')
        .children('a').click(function() {
        $(this).next().slideToggle('fast')
        .parent().toggleClass('Expand');
        return false
        });

    }

function toggleDisplay() {
// bind all anchors with class 'toggler'
// this will toggle the next div element, however the anchor should be nested in a p tag

$(".toggler")
.addClass('activeToggler')
.next().hide()
.end()

.bind("click", function(event){ 
	$(this).next().toggle(); 
	$(this).toggleClass('togglerOff');
	return false;
	});

}

// functinality shelved

function tempSubMenu() {
    //invisibilise submenus apart from native one
    $('#SubNav>ul>li:has(ul)')
        //add class to indicate drop down
        .addClass('hasChild')
        //hide nested submenus
        .children('ul').hide()
        //show current submenu
        .children('li').filter('.Current').parent().show()
        //change class to show open submenu graphic
        .parent().addClass('Expand')
        // go back to ul selection
        .end().end().end().end()
        //add click functionality
        .prev().click(function() {
        $(this).next().slideToggle('fast').parent().toggleClass('Expand');
        return false
        });
    }


function stripe() {
    $('.Stripe dd:odd, .Stripe tr:odd, .Stripe li:even').addClass('Odd');
    $('.Stripe tr li').removeClass('Odd');
}


function dropList(root) {
    // added option to show / hide first item - this defaults to true.
    // just add the class 'collapsed' to the ul and the first question will be hidden.
	var showFirst = true;
	if ( $("."+root).attr('class') ) {
		if ( $('.'+root).attr('class').search('collapsed') >=0) {
		showFirst = false;
		}
	}
    
    //if js is on, hide our content div's apart from the first child -
    $('.'+root+' li>div').hide();
    
    if (showFirst) {
    $('.'+root+' div:eq(0)').show().parent().addClass('Expand');
    }
    //wrap our H3 in A tags
    $('.'+root+' h3').wrap('<a class="dropper"></a>');

    //set first anchor to on
    $('.'+root+' a:eq(0)').addClass('dropOn');

    //add a plus / minus icon
   $('.'+root+' h3').addClass('Collapsed');
   
    //set href attribute and on click toggle display and anchor class
    $('.'+root+' a.dropper').attr({href:'#'}).click(function() {
        $(this).toggleClass('dropOn');
        //target sibling element - the following content div
        $(this).next().toggle();
        $(this).parent().toggleClass('Expand');
        return false;
        
    })
}

function flipInit() {
    /*if js is on, hide our div's apart from the first -*/
    $('.Flipper').hide();
}

function formOps() {
    var default_values = {};
    
    $('input.ToggleValue, textarea.ToggleValue').each(function(i) {
		var value = jQuery(this).val();
		default_values[jQuery(this).attr('name')] = value;
		var dv = jQuery(this).attr('defaultval');
		if( value === "" ){
			if(dv){
				jQuery(this).val(dv);
				$(this).css('color', '#666');
			}else{
				jQuery(this).val(value);
				$(this).css('color', '#666');
			}
		}
	}).focus(function() {
		var dv = jQuery(this).attr('defaultval');
		if( jQuery(this).val() === ( dv || default_values[jQuery(this).attr('name')] ) ){
			jQuery(this).val('');
			jQuery(this).css('color', '#000');
		}
	}).blur(function() {
		var dv = jQuery(this).attr('defaultval');
		if( jQuery(this).val() === "" ){	
			if(dv){
				jQuery(this).val(dv);
			}else{
				jQuery(this).val(default_values[jQuery(this).attr('name')]);
			}
			jQuery(this).css('color', '#666');
		}
	});
	
	$('a.Post, input.Post').click(function(){
		$('input.ToggleValue, textarea.ToggleValue').each(function(i){
			var dv = jQuery(this).attr('defaultval');
			if( jQuery(this).val() === dv ){
				jQuery(this).val('');
			}
		});
	});
}

function helpInit() {
    $('.HelpBox').addClass('PopOut');
    $('.HelpNav').removeClass('Hidden').click(function() {
    theTarget = $(this).attr('href');
    $(theTarget).toggleClass('PopOut');
    return false;
    });
}

//TODO
//	> add unobtrusive drop down
//		a) <select> drop down list
//		b) take on properties of the <select> drop down list through JS Object.
//		c) delete origanal <select> drop down list
	
ddl = [];

function regDropDownList(item, options){
	
	options.position = options.position || 'static';
	options.width = options.width || '200px';
	options.left = options.left || null;
	options.top = options.top || null;
	
	ddl.push(item);
	
	var $c = jQuery(item);
	var $bot = jQuery(item + ' .container .bottom');
	
	if(options.left){$c.css('left',options.left);}
	if(options.top){$c.css('top',options.top);}
	$c.css('position',options.position); // IE6 bug -> dissapears the menu bar
	jQuery(item + ' .container').css({
		width: options.width, 
		position: 'absolute',
		zIndex: '9999'
	});
	$c.css('display', 'block');
	
	$(item + ' div ul').css('display', 'none');
	
	// test for class 'selected'
	// if class found, enter value into drop down a.top
	var selectedText = jQuery(item + ' a.selected').text();
	if(selectedText){
		jQuery(item + ' a.top').text(selectedText);
		jQuery(item + ' a.top').css('font-size', '1.2em'); //ie6 minor hack (font size)
	}
	
	jQuery(item + ' .top').click(function(){
		for(var i=0;i<ddl.length;i++){
				if(item !== ddl[i])
					jQuery(ddl[i] + ' .container .bottom').hide();
		}
		$bot.toggle();
		return false;
	});
	
	jQuery($c, '.bottom a').click(function(e){
		$bot.toggle();
	});
	
	jQuery(document).click(function(){if($bot.css('display') === 'block'){$bot.css('display', 'none');}})
}

/*
// add this for each drop down
//	note: Add the below script in "Each Page" NOT in this JS file.
regDropDownList('[id]', {
	position: '[position]',
	width: '[width]px',
	left: '[left_position]px',
	top: '[top_position]px'}
);*/

/*
// add this html for each drop down
<div class="[id] ddwrapper">
	<div class="container">
		<a href="#" class="top border"><span>Select an Item</span></a>
		<ul>
			<li><a href="#" class="sel">item 1</a></li>
			<li><a href="#" class="sel">item 2</a></li>
		</ul>
	</div>
</div>*/