/*----------------------
[custom JS styles]

Project: If main site
Last change: 15.01.10
Assigned to: Felicity Evans
-----------------------*/



$(document).ready(function() {

    /* fonts */
    Cufon.replace('.tagCloudWrapper h3, #centreCol h1, h2, #wrapper .content ul.homeFeature h3, ul.toolBox h3, #wrapper .content .loginForm legend span,.callout h3,#leftCol .btn, .cta,.mainNav li a', { hover: true, hoverables: { a: true} });

    //Accordian
    $('ul.accordian > li:first-child').addClass('expanded');
    $('ul.accordian .accTitle').click(function() {
        $('.accContent').not($(this).next('.accContent')).slideUp('fast');
        $(this).next('.accContent').slideDown('fast');
        $('.accordian li').not($(this).parent()).removeClass('expanded');
        $(this).parent().addClass('expanded');
        return false;
    });

    //Dashboard - add bookmark
    $(".show-form,.cancel").not("p.cancel a").click(function() {
        $(".bookmarks .intForm").slideToggle();
        $(".show-form").parent('p').slideToggle();
        return false;
    });

    //Graphs accordian
    $('.viewDetail .more').click(function() {
        $('.testDetail').not($(this).parent().next('.testDetail')).slideUp('fast');
        $(this).parent().next('.testDetail').slideToggle('fast');
        $('.more').not($(this)).removeClass('expanded');
        $(this).toggleClass('expanded');
        return false;
    });

    //Colorbox with star rating system
    $(".colorboxCareer").click(function() {
        var popContents = $(this).attr('href') + ' #centreCol';
        $.fn.colorbox({
            title: $(this).attr('title'),
            href: popContents,
            open: true,
            width: 600,
            maxHeight: 500,
            onComplete: function() {
                $(".colorboxInPage").click(function() {
                    var thispopContents = $(this).attr('href') + ' #centreCol';
                    return false;
                });
            }
        });
        return false;
    });

    $('#wrapper .content table.careerTable .add-small').parent('p').after('<p style="display:none;"><a href="#" class="remove-small">Remove from my career shortlist</a></p>');
    $('#wrapper .content table.careerTable .add-small').click(function() {
        $(this).parent('p').fadeOut('fast', function() {
            $(this).next('p').fadeIn('fast');
        });
        return false;
    });
    $('#wrapper .content table.careerTable .remove-small').click(function() {
        $(this).parent('p').fadeOut('fast', function() {
            $(this).prev('p').fadeIn('fast');
        });
        return false;
    });

    // Interactive table
    $("table.interactive tbody th:last-child,table.interactive tbody li:last-child ").addClass("last-child")
    $("table.interactive tbody tr.title").addClass("contracted");
    $("table.interactive tbody tr.title").nextAll("tr").addClass("jsHide");
    $("table.interactive thead").next("tbody").children("tr").removeClass("jsHide contracted");
    $("table.interactive tbody tr.title th").not('table.interactive tbody tr.title th.noBubble').click(function() {
        $(this).parent().toggleClass("contracted");
        $(this).parent().nextAll("tr").toggleClass("jsHide");
        return false;
        event.stopPropagation();
    });
    $('table.interactive ul.detailList li:even').addClass('even');

    /* (Proof of concept) */
    $("tr.intForm").hide();
    $("table.interactive .remove input").click(function() {
        $(this).parents("tr").prev("tr").prev("tr").show();
        return false;
    });

    // Edit notes
    $("table.interactive .note-view input").click(function() {
        $(this).closest('td').addClass('edit')
    });
    $("table.interactive .note-edit .cancel").click(function() {
        $(this).closest('td').removeClass('edit')
    });

    //Colorbox for map
    $(".colorboxMap").click(function() {
        var popContents = $(this).attr('href') + ' #centreCol';
        $.fn.colorbox({
            title: $(this).attr('title'),
            href: popContents,
            open: true,
            width: 732,
            maxHeight: 630,
            onComplete: function() {
                $('#centreCol').addClass('map');
                $(".colorboxInPage").click(function() {
                    var thispopContents = $(this).attr('href') + ' #centreCol';
                    return false;
                });
            }
        });
        return false;
    });

    //Home page logged out view - login form	
    if ($('#wrapper .loginBoxInputs input').val() != "") {
        $('#wrapper .loginBoxInputs input').prev('label').hide();
    }
    $('#wrapper .loginBoxInputs input').focus(function() {
        if ($(this).val() == "") {
            $(this).prev('label').hide();
        }
    });
    $('#wrapper .loginBoxInputs input').blur(function() {
        if ($('#wrapper .loginBoxInputs input').val() == "") {
            $('#wrapper .loginBoxInputs input').prev('label').show();
        } else {
            $('#wrapper .loginBoxInputs input').prev('label').hide();
        }
    });

    $('.dashboardFeature .callout ul li:last-child').addClass('last-child');

    // Select everything after the 4th child element
    var extendedContent = $('.bookmarks .calloutContent ul').children().slice(5);

    var a = $("<a></a>");

    a.click(function(event) {
        $(extendedContent).toggle();
        if ($(event.target).hasClass('expand')) {
            $(event.target).removeClass('expand').addClass('collapse');
            a.html('Hide some links');
        }
        else {
            $(event.target).removeClass('collapse').addClass('expand');
            a.html('Show more links');
        }
        return false;
    });

    a.attr('href', '#');
    a.addClass('expand');
    a.html('Show all links');

    $(extendedContent).hide();
    $(extendedContent).parent().append(a);

    if ($('.profileWidget').height() >= $('.tagCloudWrapper').height()) {

        $('.tagCloudWrapper').height($('.profileWidget').height() - 32);

    } else if ($('.tagCloudWrapper').height() >= $('.profileWidget').height()) {

        var difference = $('.tagCloudWrapper').height() - $('.profileWidget').height();
        difference = difference + 34;
        $('.profileWidget .calloutTitle').height($('.profileWidget .calloutTitle').height() + difference);
    }

});        //end document ready
 
$(window).load(function(){
	
	$('.auto-submit-star').rating({
		callback: function(value, link) {
			var clientIdPrefix = this.id.substring(0, this.id.lastIndexOf("_") + 1);
			document.getElementById(clientIdPrefix + "btnRate").click();
		}
	});

});

