// A customised method for Facebox which makes a safe duplicate of any
// items (good for forms) and also changes the form action to match the
// location of the link which caused the click.
$.fn.facebox_loader = function(el, settings) {
  function clickHandler() {
	$.facebox.loading(true)
	var e = $(el).clone(true).makeIdentifiersUnique()
	if (e[0].nodeName == 'FORM' && this.href) {
	   e.attr('action', this.href)
	}
	$.facebox.reveal(e.show())
	var inputs = $('input[type!="hidden"]', e)
	if (inputs) {
	  inputs[0].focus()
	}
	return false;
  }
  return this.click(clickHandler)
}

$(function() {
    $('.login-link').facebox_loader('.login-form')
    $('.login-form').bind('submit', function() {
        document.cookie = 'testcookie=worked; path=/'
    })
})

// .makeIdentifiersUnique() function
jQuery.fn.extend({
	// Correct uniqueness for all id attributes and assoc labels and optionally
	// correct uniqueness for all name attributes.
	makeIdentifiersUnique: function(uniqueName, makeUnique) {
    // uniqueName: (optional) also make name attributes unique
    // makeUnique: (optional) returns unique identifier given an identifier (string)
		var descendantOrSelf = this.find("*").andSelf();
		var strUniqueSuffix = Math.floor(Math.random() * 1679616).toString(36); // 4 digit alphanum
		makeUnique = ("function" == typeof makeUnique ? makeUnique : function(id) {
			// Remove suffix of "_" 4-digit-alphanum (if it exists), then append new suffix
			return id.replace(/_[0-9a-z]{4}$/,"") + "_" + strUniqueSuffix;
		});

		descendantOrSelf.filter("[id]").each(function() {
            this.id = makeUnique(this.id);
		});

		descendantOrSelf.filter("label").each(function() {
            this.htmlFor = makeUnique(this.htmlFor);
		});

		// caution: do not split id & name uniqueness code b/c for <a>, id should equal name,
		// which won't be the case if the .random() function is called twice.

		if (uniqueName) {
			descendantOrSelf.filter("[name]").each(function() {
				try {
					if (jQuery.browser.msie) {
					        // Microsoft JScript allows the name to be changed at run time.
					        // HOWEVER!
					        // This does not cause the name in the programming model to change
					        // in the collection of elements, but it does change the name used
					        // for submitting elements. The NAME attribute cannot be set at run time
					        // on elements dynamically created with the createElement method.
					        // To create an element with a name attribute, include the attribute
					        // and value when using the createElement method.
					        var strHTML = this.outerHTML + "";
					        strHTML = strHTML.replace(new RegExp("name=" + this.name, "g"), "name=" + makeUnique(this.name));
					        jQuery(this).replaceWith(strHTML);
					} else {
					        this.name = makeUnique(this.name);
					}
				} catch (ex) {
					// ignore
				};
			});
		}

		return this;
	}
});

// make highlight boxes flash
$(function() {
    var speed = 500
    var flash_bgcolor = '#fff'
    var flash_color = '#000'
    var box = $('.greenbox')
    box.animate({backgroundColor: flash_bgcolor, color: flash_color}, speed).animate({backgroundColor: box.css('backgroundColor'), color: box.css('color')}, speed)
    var box = $('.yellowbox')
    box.animate({backgroundColor: flash_bgcolor, color: flash_color}, speed).animate({backgroundColor: box.css('backgroundColor'), color: box.css('color')}, speed)
    //var box = $('.redbox')
    //box.animate({backgroundColor: flash_bgcolor, color: flash_color}, speed).animate({backgroundColor: box.css('backgroundColor'), color: box.css('color')}, speed)

    // scroll down to the first error
    var error = $('li.error')
    if (error.length) setTimeout(function() {$.scrollTo(error, 500)}, 200)
})