/* JQuery Functions */

/* Get Current Year */
$(document).ready(function() {
	var currentYear = (new Date).getFullYear();
	$(".fullYear").text( (new Date).getFullYear() );
});

/* Drop Shadows */
//
// Add a class of "ds" to any div you want to have a drop shadow
// Written by James Player 2009
//

(function($) {
    $.fn.drop_shadow = function(options) {
        
        var opts = $.extend({}, $.fn.drop_shadow.defaults, options);
        var i = 0;
        
        $(this).each(function() {
        
            var path_to_images = opts.path_to_images;
            var shadow_size = new Array();
            shadow_size["top"] = opts.shadow_size_top;
            shadow_size["right"] = opts.shadow_size_right;
            shadow_size["bottom"] = opts.shadow_size_bottom;
            shadow_size["left"] = opts.shadow_size_left;
            var corner_side_length = opts.corner_size;
            var border_width = new Array();
            var offset = new Array();
            var start_point = new Array();
            
            // Are we dealing with a self closing tag?
            var self_closer = false;
            if(/^(?:area|br|col|embed|hr|img|input|link|meta|param)$/i.test(this.tagName)) {
                self_closer = true;
            }
            
            for(var key in shadow_size){
                // Get border widths
                border_width[key] = $(this).css("border-"+key+"-width").replace("px", "");
                if(!isInteger(border_width[key])){
                    border_width[key] = 0;
                }
                
                // Get offset (absolute positions of shadows)
                offset[key] = parseInt(shadow_size[key]) + parseInt(border_width[key]);
                
                // Get start point for side images
                start_point[key] = corner_side_length - offset[key];
                
                offset[key] = parseInt("-" + offset[key]);
            }
            
            // Get side height and width
            var box_width = $(this).outerWidth();
            var box_height = $(this).outerHeight();
            
            var side_length = new Array();
            side_length["width"] = box_width - start_point["right"] - start_point["left"] - border_width["left"] - border_width["right"];
            side_length["height"] = box_height - start_point["top"] - start_point["bottom"] - border_width["top"] - border_width["bottom"];
            
            if(self_closer){
            
                // adjust positions of drop shadow images to cater for margins on the self closing tag
                var margin = new Array;
                margin['top'] = $(this).css("margin-top").replace("px", "");
                margin['right'] = $(this).css("margin-right").replace("px", "");
                margin['bottom'] = $(this).css("margin-bottom").replace("px", "");
                margin['left'] = $(this).css("margin-left").replace("px", "");
                
                offset['top'] = parseInt(offset['top']) + parseInt(margin['top']);
                offset['right'] = parseInt(offset['right']) + parseInt(margin['right']);
                offset['bottom'] = parseInt(offset['bottom']) + parseInt(margin['bottom']);
                offset['left'] = parseInt(offset['left']) + parseInt(margin['left']);
                
                start_point['top'] = parseInt(start_point['top']) + parseInt(margin['top']);
                start_point['right'] = parseInt(start_point['right']) + parseInt(margin['right']);
                start_point['bottom'] = parseInt(start_point['bottom']) + parseInt(margin['bottom']);
                start_point['left'] = parseInt(start_point['left']) + parseInt(margin['left']);
                
                $(this)
                    .wrap("<div style='position: relative; display: inline-block; zoom: 1; *display: inline;'></div>")
                    .siblings("img.shadow")
                    .remove()
                    .end()
                    .after("<img src='" + path_to_images + "/ds-top-left.png' class='shadow' style='width: "+corner_side_length+"px; height: "+corner_side_length+"px; position: absolute; top: "+offset["top"]+"px; left: "+offset["left"]+"px; margin: 0; padding: 0;' />")
                    .after("<img src='" + path_to_images + "/ds-top.png' class='shadow' style='height: "+shadow_size["top"]+"px; width: "+side_length["width"]+"px; position: absolute; top: "+offset["top"]+"px; left: "+start_point["left"]+"px; margin: 0; padding: 0;' />")
                    .after("<img src='" + path_to_images + "/ds-top-right.png' class='shadow' style='width: "+corner_side_length+"px; height: "+corner_side_length+"px; position: absolute; top: "+offset["top"]+"px; right: "+offset["right"]+"px; margin: 0; padding: 0;' />")
                    .after("<img src='" + path_to_images + "/ds-right.png' class='shadow' style='width: "+shadow_size["right"]+"px; height: "+side_length["height"]+"px; position: absolute; top: "+start_point["top"]+"px; right: "+offset["right"]+"px; margin: 0; padding: 0;' />")
                    .after("<img src='" + path_to_images + "/ds-bottom-right.png' class='shadow' style='width: "+corner_side_length+"px; height: "+corner_side_length+"px; position: absolute; bottom: "+offset["bottom"]+"px; right: "+offset["right"]+"px; margin: 0; padding: 0;' />")
                    .after("<img src='" + path_to_images + "/ds-bottom.png' class='shadow' style='height: "+shadow_size["bottom"]+"px; width: "+side_length["width"]+"px; position: absolute; left: "+start_point["left"]+"px; bottom: "+offset["bottom"]+"px; margin: 0; padding: 0;' />")
                    .after("<img src='" + path_to_images + "/ds-bottom-left.png' class='shadow' style='width: "+corner_side_length+"px; height: "+corner_side_length+"px; position: absolute; bottom: "+offset["bottom"]+"px; left: "+offset["left"]+"px; margin: 0; padding: 0;' />")
                    .after("<img src='" + path_to_images + "/ds-left.png' class='shadow' style='width: "+shadow_size["left"]+"px; height: "+side_length["height"]+"px; position: absolute; top: "+start_point["top"]+"px; left: "+offset["left"]+"px; margin: 0; padding: 0;' />");
                    
            } else {
        
                $(this)
                    .css("position", "relative")
                    .children("img.shadow")
                    .remove()
                    .end()
                    .append("<img src='" + path_to_images + "/ds-top-left.png' class='shadow' style='width: "+corner_side_length+"px; height: "+corner_side_length+"px; position: absolute; top: "+offset["top"]+"px; left: "+offset["left"]+"px; margin: 0; padding: 0;' />")
                    .append("<img src='" + path_to_images + "/ds-top.png' class='shadow' style='height: "+shadow_size["top"]+"px; width: "+side_length["width"]+"px; position: absolute; top: "+offset["top"]+"px; left: "+start_point["left"]+"px; margin: 0; padding: 0;' />")
                    .append("<img src='" + path_to_images + "/ds-top-right.png' class='shadow' style='width: "+corner_side_length+"px; height: "+corner_side_length+"px; position: absolute; top: "+offset["top"]+"px; right: "+offset["right"]+"px; margin: 0; padding: 0;' />")
                    .append("<img src='" + path_to_images + "/ds-right.png' class='shadow' style='width: "+shadow_size["right"]+"px; height: "+side_length["height"]+"px; position: absolute; top: "+start_point["top"]+"px; right: "+offset["right"]+"px; margin: 0; padding: 0;' />")
                    .append("<img src='" + path_to_images + "/ds-bottom-right.png' class='shadow' style='width: "+corner_side_length+"px; height: "+corner_side_length+"px; position: absolute; bottom: "+offset["bottom"]+"px; right: "+offset["right"]+"px; margin: 0; padding: 0;' />")
                    .append("<img src='" + path_to_images + "/ds-bottom.png' class='shadow' style='height: "+shadow_size["bottom"]+"px; width: "+side_length["width"]+"px; position: absolute; left: "+start_point["left"]+"px; bottom: "+offset["bottom"]+"px; margin: 0; padding: 0;' />")
                    .append("<img src='" + path_to_images + "/ds-bottom-left.png' class='shadow' style='width: "+corner_side_length+"px; height: "+corner_side_length+"px; position: absolute; bottom: "+offset["bottom"]+"px; left: "+offset["left"]+"px; margin: 0; padding: 0;' />")
                    .append("<img src='" + path_to_images + "/ds-left.png' class='shadow' style='width: "+shadow_size["left"]+"px; height: "+side_length["height"]+"px; position: absolute; top: "+start_point["top"]+"px; left: "+offset["left"]+"px; margin: 0; padding: 0;' />");
                    
            }
            i++;
        });
        
    }
    
    $.fn.drop_shadow.defaults = {
        path_to_images: "/PublishingImages",   // String. Path to images folder where your drop shadow images will be kept
        shadow_size_top: 5,         // Integer: Height of the top shadow image
        shadow_size_right: 7,       // Integer: Width of the right shadow image
        shadow_size_bottom: 9,      // Integer: Height of the bottom shadow image
        shadow_size_left: 7,        // Integer: Width of the left shadow image
        corner_size: 25             // Integer: Height or width of the corner squares (since they're squares height and width should be the same!)
    };
    
    function isInteger(s) { // THANKS http://surf11.com/entry/157/javascript-isinteger-function
        return (s.toString().search(/^-?[0-9]+$/) == 0);
    }

    
})(jQuery);

/* Hide and show Regulatory Service Tabs 
$(document).ready(function() {
	$(".green #tabContainer").hide();
	
	$(".benefitsButton").click(function () {
      $("#tabContainer").show();
    });
});*/


/* Regulatory Service Tabs */
$(document).ready(function () {

    //Default Action
    $(".tab_content").hide(); //Hide all content
    if ($("ul.tabs li a[href='" + window.location.hash + "']").length>0) {
        $("ul.tabs li a").removeClass("active"); //Remove any "active" class
        $("ul.tabs li a[href='" + window.location.hash + "']").parent("li").addClass("active");

    } else {
        $("ul.tabs li:first").addClass("active").show(); //Activate first tab
        $(".tab_content:first").show(); //Show first tab content
    }

    //On Click Event
    $("ul.tabs li").click(function () {
        $("ul.tabs li").removeClass("active"); //Remove any "active" class
        $("ul.tabs li a").removeClass("current"); //Remove any "active" class
        $(this).addClass("active"); //Add "active" class to selected tab
        $(".tab_content").hide(); //Hide all tab content
        var activeTab = $(this).find("a").attr("href"); //Find the rel attribute value to identify the active tab + content
        $(activeTab).show(); //Fade in the active content
        return false;
    });

    //On Click Event
    $("ul.servedBy li a").click(function () {
        $("ul.tabs li").removeClass("active"); //Remove any "active" class
        $("ul.tabs li a").removeClass("current"); //Remove any "active" class

        $(".tab_content").hide(); //Hide all tab content
        var activeTab = $(this).attr("href"); //Find the rel attribute value to identify the active tab + content

        $("ul.tabs li a[href='" + activeTab + "']").parent('li').addClass("active");
        $("ul.tabs li a[href='" + activeTab + "']").addClass("current");

        $(activeTab).show(); //Fade in the active content
        return false;
    });

    $.scrollTo('0px');
});


/* Show and Hide Checklist Items */
$(document).ready(function() {

	var showText="show text";
	var hideText="hide text";
	
	var is_visible = false;
	
	$('.toggle').next().append('<a href="#" class="toggleLink">'+showText+'</a>');
	
	$('.toggle').hide();
	
	$('a.toggleLink').click(function() {
	
		is_visible = !is_visible;
		
		if ($(this).text()==showText) {
			$(this).text(hideText).addClass('checkActive');
			$(this).parent().prev('.toggle').slideDown('fast');
		}
		else {
			$(this).text(showText).removeClass('checkActive');
			$(this).parent().prev('.toggle').slideUp('fast');
		}
		return false;
	});

	$('.showAll').click(function() {
		$('.toggle').toggle();
	  	// switch visibility
		is_visible = !is_visible;
		
		$(this).text($(this).text() == 'Show me all the detail for each checklist item' ? 'Hide the detail and just show me the summaries' : 'Show me all the detail for each checklist item');
		$('.toggleLink').html( (!is_visible) ? showText : hideText);
		
	});
});



$(document).ready(function(){
/* Print Function */
$("#printSheet").click( function() {
   $('#infoSheet').jqprint();
   return false;
});

/* Remove Spaces from subheading */
try {
$('#pageSubHeading').html($('#pageSubHeading').html().replace(/&[^;]+;/g, ''))
} catch(err){
	return false;
	}

/* Search page default error */
$(function() {
    var foundin = $('span.ms-descriptiontext:contains("or the query came from advanced search")');
    if (foundin)
    	$('span.ms-descriptiontext').hide();
});

});


/* Active Navigation */
$(document).ready(function(){
$(function(){
   var path = location.pathname.substring(1);
   if ( path )
     $('.ERWIN_Nav a[href$="' + path + '"]').parent().addClass('activate');
 });
});

/* Empty Elements */
$(document).ready(function(){
$("span.registerError:empty").css('display', 'none');
$("span.registerSuccess:empty").css('display', 'none');
$("span.homeError:empty").css('display', 'none');
});

/* Information Sheet Dialog Title */
$(document).ready(function(){
$("#dialogTitleSpan").attr('title', 'ERWIN Information Sheet');
});

/* Select Widths IE 
$(function ()
{
	$("[id$='LocalAuthority']").addClass('LASelect');
    // Set the width via CSS.
    $('select.LASelect').ieSelectWidth
    ({
        containerClassName : 'select-container',
        overlayClassName : 'select-overlay'
    });
});*/

/* Checklist Items on Edit Form */
$(document).ready(function(){
  $('.infoSheetAdmin td div table.ms-formtable select option:nth-child(odd)').addClass('alternateopt');
});

/* Tooltips */
$(".searchAdvice[title]").tooltip();


/* ERWIN Logo */

$(document).ready(function () {
    try {
        $("img[src*='erwin_logo.jpg']").hide();
    } catch (err) { }
});


/*Robet's JS*/
	d        = document;
	stregexp = new RegExp;

	function funNarrowDD(str, IDofTargetDDendsWith)
	{
		var theID = $("input[id$='" + IDofTargetDDendsWith + "']").attr('id');
		selObj = d.getElementById(theID);
	
		// if the length of str is 0, bypass everything else
		if (str.length == 0)
		{
			buildOptions(namesArray, IDofTargetDDendsWith);
			remvdArray.length = 0;
		}
		else
		{
			// clear tempArray
			tempArray.length = 0;
			
			// set up temporary array
			for (i=0;i<selObj.options.length;i++)
			{
				tempArray[tempArray.length] = selObj.options[i].value;
			}
			
			// make case-insensitive regexp
			stregexp = new RegExp(str,"i");
			
			// remove appropriate item(s)
			if (lastVal.length < str.length)
			{
				for (j=selObj.options.length-1;j>-1;j--)
				{
					if (selObj.options[j].value.match(stregexp) == null)
					{
						// remove the item
						tempArray.splice(j,1);
					}
				}
			}
			
			// add appropriate item(s)
			else
			{
				// if a removed item matches the new pattern, add it to the list of names
				for (k=remvdArray.length-1;k>-1;k--)
				{
					tempName = remvdArray[k];
					
					if (tempName.match(stregexp) != null)
					{
						tempArray[tempArray.length] = tempName;
					}
				}
				
				// sort the names array
				tempArray.sort();
			}
			
			// build the new select list
			buildOptions(tempArray, IDofTargetDDendsWith);
		}
		
		// remember the last value on which we narrowed
		lastVal = str;
		
		// write the number of matches
		writeMatches(IDofTargetDDendsWith);
	}
	
	function buildOptions(arrayName, IDofTargetDDendsWith)
	{
		var theID = $("input[id$='" + IDofTargetDDendsWith + "']").attr('id');
		selObj = d.getElementById(theID);
	
		// clear the select list
		selObj.options.length = 0;
		
		// build the select list
		for (l=0;l<arrayName.length;l++)
		{
			selObj.options[l] = new Option(arrayName[l])
		}
		
		// remember which items were removed
		buildRemvd();
	}
	
	function buildRemvd()
	{
		// clear the removed items array
		remvdArray.length = 0;
		
		// build the removed items array
		for (m=namesArray.length-1;m>-1;m--)
		{
			if (namesArray[m].match(stregexp) == null)
			{
				// remember which item was removed
				remvdArray[remvdArray.length] = namesArray[m];
			}
		}
	}
	
	function writeMatches(IDofTargetDDendsWith)
	{
		var theID = $("input[id$='" + IDofTargetDDendsWith + "']").attr('id');
		selObj = d.getElementById(theID);
	
		if (selObj.options.length == 1)
		{
			d.getElementById("matches").innerHTML = "1 match";
		}
		else
		{
			d.getElementById("matches").innerHTML = selObj.options.length + " matches";
		}
	}


