jQuery('input.text').concrete({
    addHighlight: function () {
        if (this.val().length === 0) {
            this.addClass('highlight');
        }
    },
    
    removeHighlight: function () {
        this.removeClass('highlight');
    },
    
    onmatch: function () {
        this.addHighlight();
    },
    
    onfocusin: function () {
        this.removeHighlight();
    },
    
    onfocusout: function () {
        this.addHighlight();
    }
});

jQuery('#Navigation ul.parent').concrete({
    Sticky: null,
    Timeout: null,
    
    onmatch: function () {
        this.setSticky(jQuery('#Navigation ul.parent > li.section, #Navigation ul.parent > li.current'));
    },
    
    onmouseleave: function () {
        var that = this;
        
        this.setTimeout(window.setTimeout(function () {
            that.find('> .section').removeClass('section');
            that.find('> .current').removeClass('current');
            
            if (that.getSticky().find('ul')) {
                that.getSticky().addClass('section');
            } else {
                that.getSticky().addClass('current');
            }
        }, 300));
    },
    
    onmouseenter: function () {
        window.clearTimeout(this.getTimeout());
        this.setTimeout(null);
    }
});

jQuery('#Navigation ul.parent li').concrete({
    showSubnav: function () {
        this.addClass('section');
    },
    
    hideSubnav: function () {
        var mySticky = this.getSticky();
        this.removeClass('section');
        
        if (mySticky.find('ul')) {
            mySticky.addClass('section');
        } else {
            mySticky.addClass('current');
        }
    },
    
    hideAllActive: function () {
        this.parent().find('> li.section').removeClass('section');
        this.parent().find('> li.current').removeClass('current');
    },
    
    onmouseover: function () {
        this.hideAllActive();
        
        if (!this.find('ul')) {
            this.addClass('current');
        } else {
            return this.showSubnav();
        }
    }
});

jQuery('#Navigation ul.parent li ul li').concrete({
    onmouseover: function (e) {
        e.stopPropagation();
    }
});

jQuery('.required').concrete({
    onmatch: function () {
        this.parent().addClass('field-required');
    }
});

function main () {
	var cycle = function () {
        jQuery('#HeadImage').cycle &&
		jQuery('#HeadImage').cycle({timeout: 8000, speed: 5000});
    };
    
    var img_loaded = function (obj) {
        return ((typeof obj.width) !== 'undefined');
    };
	
	if (img_loaded($('#HeadImage img').get(0)) === false) {
		var maybeImage = $('#HeadImage img');
		
		maybeImage && 
        jQuery(maybeImage.get(0)).load(function () {
            cycle();
        });
    } else {
        cycle();
    }
}

jQuery(main);