$(document).ready(function() {
/*
    Replace H2 with dynamic text images
*/
    $('.head h2, .content h2').each(function(){
        $(this).html(
            '<img src="/images/dynatext/dynatext.php'+
            '?color=23404e'+
            '&amp;size=18'+
            '&amp;bg=cedce5'+
            '&amp;text='+escape($(this).html())+'" />'
        );
    });
    $('.content h3').each(function(){
        $(this).html(
            '<img src="/images/dynatext/dynatext.php'+
            '?color=23404e'+
            '&amp;size=14'+
            '&amp;bg=cedce5'+
            '&amp;text='+escape($(this).html())+'" />'
        );
    });
    $('.head-green h2').each(function(){
        $(this).html(
            '<img src="/images/dynatext/dynatext.php'+
            '?color=336600'+
            '&amp;size=18'+
            '&amp;bg=dae5cf'+
            '&amp;text='+escape($(this).html())+'" />'
        );
    });
    $('#nav-toplevel ul li a').each(function(){
    //  Nav dropdown
        $(this).html(
            '<img src="/images/dynatext/dynatext.php'+
            '?color=FFFFFF'+
            '&amp;size=13'+
            '&amp;bg=3875a3'+
            '&amp;text='+escape($(this).html())+'" />'
        );
    });
    
/*
    Expand categories
*/
    /*
    $('ul.categories a.parent').live('click',function(){
        var is_level2 = $(this).parent().parent().hasClass('categories-2');
        if ($(this).parent().hasClass('open')) {
            $(this).parent().removeClass('open');
        }
        else {
            $(this).parent().siblings().removeClass('open');
            $(this).parent('li').addClass('open');
        }
        return false;
    });
    */
    
//  Show categories by letter
    $('ul.categories li.letter a').live('click',function(){
        $('ul.categories li.letter a').removeClass('selected');
        $(this).addClass('selected');
        var letter = $(this).text().toLowerCase();
        $('ul.categories li.alphabetical ul').hide();
        $('ul.categories ul.alpha-'+letter).show();
        return false;
    });


/*
    Expand feeds
*/
    $('#feeds .clickable').click(function(){
        var feed = $(this).parent().parent();
        if (feed.hasClass('expanded')) {
            feed.removeClass('expanded');
        }
        else {
            feed.addClass('expanded');
        }
        return false;
    });
    
/*
    Events calendar
*/
    $('.calendar .month a').live(
        'click',
        function() {
            //  load next / prev month via ajax
            $(this).parent().parent().load(
                $(this).attr('href')
            );
            return false;
        }
    );
    
/*
    Show/hide login form
*/
    $('#menu-login-link').click(function(){
        $('#login-button').toggle();
        $('#login-form').toggle();
        return false;
    });
    
/*
    Management search tabs
*/
    $('.managers .tabs a').click(function(){
        var id = $(this).attr('id').substr(13);
    //  Activate tab
        $('.managers .tabs span').removeClass('active');
        $(this).parent('span').addClass('active');
    //  Show dropdown
        $('#manager-search-left-property span').hide();
        $('#category-'+id).show();
    //  Set form value
        $('#manager-search-type').val(id);
        return false;
    });
    
/*
    Supplier search tabs
*/
    $('.suppliers .tabs a').click(function(){
        var id = $(this).attr('id').substr(14);
    //  Load categories
        $('ul.categories').html('<li><img src="/images/loading.gif" /></li>');
        $('ul.categories').load(
            '/ajax/categories_'+id+'/'
        );
    //  Activate tab
        $('.suppliers .tabs a').removeClass('active');
        $(this).addClass('active');
    //  Set form value
        $('#suppliers-search-type').val(id);
        return false;
    });
    
/*
    Reload captcha
*/
    $('.reloadcaptcha').click(function(){
        var rand = Math.random();
        $('img.captcha').attr('src', '/images/dynatext/captcha.php?'+rand);
        return false;
    });

/*
    Show / hide categories
*/
    var alphabetical = false;
    $('a.plus').click(function(){
        if ($(this).hasClass('ondemand')) {
        //  Load content on demand
            $(this).removeClass('ondemand');
            if (alphabetical) {
                var id = 'alphabetical';
            }
            else {
                if ($(this).hasClass('projects')) {
                    var id = 'projects';
                }
                else {
                    var id = 'listings';
                }
            }
            $('ul.categories').html('<li><img src="/images/loading.gif" /></li>');
            $('ul.categories').load('/ajax/categories_'+id+'/');
        }
    //  Show / hide
        if ($(this).hasClass('minus')) {
            $(this).removeClass('minus');
        }
        else {
            $(this).addClass('minus');
        }
        $('ul.categories').slideToggle();
        return false;
    });
    $('h3.plusminus').click(function(){
        $(this).siblings('a.plus').trigger('click');
        return false;
    });
//  Switch alphabetical / list
    $('a.alphaswitch').click(function(){
        alphabetical = !alphabetical;
        if (alphabetical) {
            $(this).text('switch to tree view');
        }
        else {
            $(this).text('switch to alphabetical view');
        }
        
        if ($('a.plus').hasClass('ondemand')) {
            $('a.plus').trigger('click');
        }
        else {
            if (alphabetical) {
                var id = 'alphabetical';
            }
            else {
                if ($('a.plus').hasClass('projects')) {
                    var id = 'projects';
                }
                else {
                    var id = 'listings';
                }
            }
            $('ul.categories').html('<li><img src="/images/loading.gif" /></li>');
            $('ul.categories').load('/ajax/categories_'+id+'/');
        }
        return false;
    });
    
    
/*
    Example in search box
*/
    if ($('#fld-keywords').val()=='') {
        show_search_example();
    }
    $('#fld-keywords').blur(function(){
        if ($('#fld-keywords').val()=='' || $('#fld-keywords').val()==search_example) {
            show_search_example();
        }
    });
    $('#fld-keywords').focus(function(){
        if ($('#fld-keywords').val()==search_example) {
            $('#fld-keywords').val('');
        }
    });
    
/*
**  Fly-out in user control panel (admin)
*/
    var cat_open = 'open';
    var flyout_is_dirty = false;
	$("li.profile-flyout a").click(function(){
    //  First, check if flyout has unsaved changes
        if (flyout_is_dirty) {
            if (!confirm(
                'Are you sure you want to close this tab?\n'+
                'Your unsaved changes will be lost!\n\n'+
                'Tip:\n'+
                'To save your changes before proceeding to the\n'+
                'next tab, click the "Save" button at the bottom\n'+
                'of the page.'
            )) { return false; }
            flyout_is_dirty = false;
        }
	
        //  show/hide selected flyout
        if ($(this).hasClass(cat_open)) {
        //  hide selected
            $(this).removeClass(cat_open);
            $(this).removeClass('loading');
            $(this).parent().find('div.flyout').slideUp('fast');
        }
        else {
        //  hide all others
            $('a.'+cat_open).each(
                function() {
                    $(this).removeClass(cat_open);
                    $(this).removeClass('loading');
                    $(this).parent().find('div.flyout').slideUp(
                        'fast',
                        function() {
                            $(this).html('Loading...');
                        }
                    );
                }
            );
        //  load / show selected
            $(this).addClass(cat_open);
            $(this).addClass('loading');
            var link = $(this);
            var section = $(this).attr('id').substr(7);
            $(this).parent().find('div.flyout').load(
                '/profile/section/'+section+'/',
                null,
                function(responseText, textStatus, XMLHttpRequest) {
                //  Show
                    var flyout = $(this);
                    flyout.slideDown('fast');
                    link.removeClass('loading');
                //  Intercept submit button
                    $('form').unbind();
                    $('form').attr('action','/profile/section/'+section+'/');
                    $('form').unbind('submit');
            		$('form').bind('submit', function() {
            			$(this).ajaxSubmit({
            				target: flyout
            			});
            			flyout_is_dirty = false;
            			//flyout.html('<img src="/images/loading.gif" />');
            			return false;
            		});
                }
            );
        }
        return false;
	});
	
/*
    Mouseover event details
*/
	var event_div = null;
	$('.calendar .event a').live('mouseenter',function(event) {        
        if (event_div==null) {
        //  Create div
            event_div = $('<div id="event-hover" class="content" />')
                        .css('top', event.pageY + 2)
                        .css('left', event.pageX + 2);
            $('body').append(event_div);
        }
        $(event_div).stop(true, true)
                    .css('top', event.pageY + 2)
                    .css('left', event.pageX + 2)
                    .html('<p><img src="/images/loading.gif" /><br />Loading event details...</p>')
                    .fadeIn();
    //  Load event details
        $(event_div).load('/ajax'+$(this).attr('href')+'&d='+$(this).text());
	});
	$('#event-hover, .calendar .event a').live('mouseleave',function(event) {
        if (event_div!==null) {
            $(event_div).stop(true, true).delay(300).fadeOut();
        }
	});
	$('#event-hover').live('mouseenter', function(event){
	   if (event_div!==null) {
	       $(event_div).stop(true, true).fadeIn();
	   }
	});
	
	
/*
    Warn user when closing a control panel fly-out with unsaved changes
*/
    $("li.profile-flyout input").live('keypress',function(){ flyout_is_dirty = true; });
    $("li.profile-flyout select").live('change',function(){ flyout_is_dirty = true; });
    
});

//  Random search example
var search_examples = Array(
    'ashphalt paving',
    'window replacement',
    'parking manager',
    'elevator repair',
    'fire detection',
    'waste management',
    'video surveillance'
);
var search_example = '';
function show_search_example() {
    search_example = 'e.g. ';
    var r = Math.floor(Math.random()*search_examples.length);
    search_example += search_examples[r];
    $('#fld-keywords').val(search_example);
}

