$(document).ready(setup);
function setup()
{
	
    $('#header-search').example('Search');
    $('#help-text').example('For example:\nI am setting up a new business and my partner is here on a green card \nI am being sued over a real estate deal \nI am being audited and I need a good tax attorney', {className: 'gray'});

    $('#confirm').jqm({overlay: 88, modal: true, trigger: false, toTop: true});

    // trigger a confirm whenever links of class alert are pressed.
    $('a[href^="mailto"]').click(function() {
        confirm('About to visit: '+this.href+' !',this.href);
        return false;
    });

    var appanim = 0;
    $('#legal-ease').click(function(){
        if(appanim == 0){
            if($('#pop-up-wrapper').is(':hidden')){
                //                $('#appointment .content').show();
                //                $('#appointment .thankyou').hide();
                appanim = 1;
                if ( $.browser.msie && /6.0/.test(navigator.userAgent) ) {
                    $('#content select').css('visibility', 'hidden');
                }
                $('#pop-up-wrapper').animate({
                    height: 198
                }, function(){
                    appanim = 0;
                });
            }
            else{
                appanim = 1;
                if ( $.browser.msie && /6.0/.test(navigator.userAgent) ) {
                    $('#content select').css('visibility', '');
                }
                $('#pop-up-wrapper').animate({
                    height: 0
                }, function(){
                    $(this).hide();
                    appanim = 0;
                });
            }
        }
        return false;
    });

    $('#pop-up-close').click(function(){
        appanim = 1;
        if ( $.browser.msie && /6.0/.test(navigator.userAgent) ) {
            $('#content select').css('visibility', '');;
        }
        $('#pop-up-wrapper').animate({
            height: 0
        }, function(){
            $(this).hide();
            appanim = 0;
        });
        return false;
    });


    $('#main-nav ul:first').children().hoverIntent(
    function(){
        if ( $.browser.msie && /6.0/.test(navigator.userAgent) ) {
            $('#content select').css('visibility', 'hidden');
        }
        $('ul:first', this).css('left', '3px');
        $('a:first', this).css('color', '#FCA712');
    },
    function(){
        if ( $.browser.msie && /6.0/.test(navigator.userAgent) ) {
            $('#content select').css('visibility', '');
        }
        $('ul:first', this).css('left', '-999em');
        $('a:first', this).css('color', '');
    }
    );


    $('#main-nav ul').children().each(
    function(){

        var liCount = $('ul', this).children().size();

        if(liCount > 10){

            var thisLi = this;

            var count = parseInt(liCount / 3);
            var remainder = liCount - (count * 3);

            var col1 = count - 1;

            if(remainder > 0){
                col1 += 1;
            }

            var col2 = count + col1;

            if(remainder > 1){
                col2 += 1;
            }


            $('ul', this).children().each(
            function(i){
                $(this).replaceWith($(this).html());
            });

            $('ul', this).children().each(
            function(i){
                if(i <= col1){
                    $(this).addClass('c1');
                }

                else if(i > col1 && i <= col2){
                    $(this).addClass('c2');
                }

                else if(i > col2){
                    $(this).addClass('c3');
                }
            });

            $('li', this).removeClass('c1').removeClass('c2').removeClass('c3');

            $('.c1', this).wrapAll('<td class="col first"></td>');
            $('.c2', this).wrapAll('<td class="col"></td>');
            $('.c3', this).wrapAll('<td class="col"></td>');

            $('a', this).removeClass('c1').removeClass('c2').removeClass('c3');

            $('td.col', this).wrapAll('<li><table><tr></tr></table></li>');

            // remove nested ul as this causes layout issues
            $('ul', $('td.col', this)).each(function() {
                $('a', this).insertBefore(this).css('margin-left', '20px');
            });
            $('ul', $('td.col', this)).remove();
        };
    }
    );

};

function confirm(msg,callback) {
    $('#confirm')
    .jqmShow()
    .find(':submit:visible').unbind('click')
    .click(function(){
        if(this.value == 'Continue')
        (typeof callback == 'string') ?
        window.location.href = callback :
        callback();
        $('#confirm').jqmHide();
    });
}