// Functions for use in PDE
// Please just define functions in here, and apply them in the 
// "pde-selectors.js" file. With meaningful names, this should 
// make it easier to understand what's going on...

// Make a link open in a new window
jQuery.fn.openInNewWindow = function () {
    this.each(function () {
        this.target = '_blank';
    });

    return this;
};

// prefill an input box from a named cookie
jQuery.fn.preFillValue = function (cookie_name) {
    return this.each(function() {
        var cookie_val = $.cookie(cookie_name);
        if (cookie_val && cookie_val != this.defaultValue) {
            $(this).val(cookie_val);
        }
    });
};

// If we've still got the default values when a submit occurs, remove
// them.
jQuery.fn.clearDefaultValuesOnSubmit = function () {
    return this.each(function() {
        $(this).submit(function() {
            $('input:text', this).each(function() {
                if (this.value === this.defaultValue) {
                    this.value = '';
                }
            });
        });
    });
}

// select the text in an input box on focus
jQuery.fn.selectOnFocus = function () {
    return this.each(function() {
        $(this).focus(function () {
            this.select();
        });
    });
}

// Copy the label of a text box into the box when empty
jQuery.fn.hint = function (clearOnFocus) {
  // Parameter clearOnFocus: should the hint be cleared on focus or not?
  // reason: we may select the whole box on focus in another handler
  return this.each(function (){
    // get jQuery version of 'this'
    var t = jQuery(this); 
    // get the hint from the label once since it won't change
    var label = t.siblings('label[@for=' + t.attr('id') + ']')
    // only apply logic if the label has a value
    if (label && label.text()) {
      // get the hint text from the label value 
      var hint = label.text(); 

      // hide the label. this is because the label is not on the wireframes.
      label.hide();

      // on blur, set value to the label text if blank
      t.blur(function (){
        if (t.val() == '') {
          t.val(hint);
        }
      });
      // on focus, set value to blank if current value 
      // matches title attr
      t.focus(function () {
          if (clearOnFocus && t.val() == hint) {
              t.val('');
          } 
      });
      // now change all inputs to hint
      t.blur();
    }
  });
};

// turn nested ULs of radio buttons into a collapsable tree
jQuery.fn.toggleNestedLists = function (selected_id) {
    $(this).find('li:has(ul)').each( function () { 
        var t = $(this); 
        t.children('ul').hide(); 
        t.click(function() {
            $(this).children('ul').show(); 
            $(this).siblings('li').children('ul').hide(); 
        });
    });
    $(this).find('#' + selected_id).each(function () {
        	$(this).parents('ul').show();
          	$(this).parent().children('ul').show();
    });

};

// public method for url encoding
jQuery.fn.encode_utf8 = function (string) {
	string = string.replace(/\r\n/g,"\n");
	var utftext = "";

	for (var n = 0; n < string.length; n++) {

		var c = string.charCodeAt(n);

		if (c < 128) {
			utftext += String.fromCharCode(c);
		}
		else if((c > 127) && (c < 2048)) {
			utftext += String.fromCharCode((c >> 6) | 192);
			utftext += String.fromCharCode((c & 63) | 128);
		}
		else {
			utftext += String.fromCharCode((c >> 12) | 224);
			utftext += String.fromCharCode(((c >> 6) & 63) | 128);
			utftext += String.fromCharCode((c & 63) | 128);
		}

	}

	return utftext;
};

// public method for url decoding
jQuery.fn.decode_utf8 = function (utftext) {
	var string = "";
	var i = 0;
	var c = c1 = c2 = 0;

	while ( i < utftext.length ) {

		c = utftext.charCodeAt(i);

		if (c < 128) {
			string += String.fromCharCode(c);
			i++;
		}
		else if( ((c > 191) && (c < 224) ) ||
				( (c >= 65471) && (c < 65504) ) ) { 
			c2 = utftext.charCodeAt(i+1);
			string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
			i += 2;
		}
		else {
			c2 = utftext.charCodeAt(i+1);
			c3 = utftext.charCodeAt(i+2);
			string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
			i += 3;
		}

	}

	return string;
};

jQuery.fn.resize_org_logo = function ( max_height_arg, max_width_arg ){

		this.each(function(){

		var org_image = this;

		if (!org_image){
			return;
		}

		var max_height = parseInt(max_height_arg);
		var max_width = parseInt(max_width_arg);

		var image_obj = new Image();
		image_obj.src = org_image.src;

		var image_height = image_obj.height;
		var image_width = image_obj.width;

		if (isNaN(parseInt(max_height + max_width)) || !org_image || ( image_height > 0 && image_height <= max_height && image_width > 0 && image_width <= max_width ) || image_height == 0 || image_width == 0) {
			return;
		}

		var bigger_dim = ( image_height / max_height ) >= ( image_width / max_width ) ? 'height' : 'width';

		var resize_ratio;
		var new_height;
		var new_width;

		if (bigger_dim == 'height') {
			resize_ratio = parseFloat(max_height / image_height);
			var aspect_ratio = parseFloat(image_height / image_width);
			new_height = parseInt(image_height * resize_ratio);
			new_width = parseInt(new_height / aspect_ratio);
		}
		else{
			resize_ratio = parseFloat(max_width / image_width);
			var aspect_ratio = parseFloat(image_width / image_height );
			new_width = parseInt(image_width * resize_ratio);
			new_height = parseInt(new_width / aspect_ratio);
		}

		org_image.height = new_height;
		org_image.width = new_width;
	
	});
}

// public method to set change_CAPTCHA flag to true and submit form
jQuery.fn.change_captcha = function ( ){
        $("#change_CAPTCHA").val(1);
		this.submit();
}
