(function ($, window, undefined) {    
    $(document).on('click', 'a[href="#"]', function (e) {
        //prevent shooting to top of the page
        e.preventDefault();
    }).on('focus', 'input[type!="submit"]', function (e) {
        //place the focused class on the element focus
        $(this).removeClass('invalid').addClass('focused');
    }).on('blur', 'input', function (e) {
        //remove focused class and validate if necessary
        $(this).removeClass('focused');
        app.validateField(this);
    }).on('submit', 'form', function (e) {
        //validate a form on submitm prevent submission
        //if it is invalid
        return app.validateForm(this);
    }).on('click', 'span.dialog_close', function (e) {
        $(this).parent().hide();
    }).on('mouseenter', '.tooltipTrigger', function(e) {
        $('#' + this.rel).show();
    }).on('mouseout', '.tooltipTrigger', function(e) {
        $('#' + this.rel).hide();
    })

    //doc ready
    $(function () {
        //setup buttons
        $('a.button, button, input[type="submit"], input[type="checkbox"]').buttonize();
        //setup buttonsets
        $('fieldset > input[type="radio"]').parent().buttonset();
        //fix any badly formatted errors from cake
        $('div.cakeError').each(function (i) {
            var $e = $(this),
                $o = $e.parent().find('.validateOutput'),
                txt = $e.text();
            $o.html('<span class="icon msg_error"></span>');//<span class="validation_error tooltip">' + txt + '</span>');
	    $o.find('span.icon').qtip({
		style: {
		    classes: app.qtip.style
		},
		content: {
		    text: txt
		},
		position: {
		    my: 'left center',
		    at: 'right center'
		}
	    });
            $e.remove();
        });
        //setup tabs
        $('div').withData('tabify').tabify();
        //fade out flash
        $('div.flash').hover(function () { $(this).stop(true).css('opacity', 1); })
            .delay(2500).fadeOut();
        //setup pretty print
        prettyPrint();
        //setup tooltips
	$('*[title]').qtip({
	    style: {
		classes: app.qtip.style
	    },
	    position: {
		my: 'bottom center',
		at: 'top center'
	    }
	});
        //highlight nav menu item of current page
        if (location.href.indexOf(location.host + '/products') > -1) {
            $('#nav > li > a.products').addClass('current');
        } else if (location.href.indexOf(location.host + '/services') > -1) {
            $('#nav > li > a.services').addClass('current');
        } else if (location.href.indexOf(location.host + '/pages/about') > -1) {
            $('#nav > li > a.abouts').addClass('current');
        } else {
            $('#nav > li > a.home').addClass('current');
        }

    });
})(jQuery, window);
