$.fn.vAlign = function() {
  return this.each(function(i){
    var ah = $(this).height();
    var ph = $(this).parent().height();
    var mh = (ph - ah) / 2;
    $(this).css('padding-top', mh);
  });
};

function getFlashMovie(movieName) {
    return ($.browser.msie && $.browser.version < 8) ? window[movieName] : document[movieName];
}

function home_buttonNextCallback(carousel, button, enabled) {
    if (!enabled) {
        var movie = getFlashMovie("video");
        
        if (movie != undefined)
            movie.pauseVideo();
    }
};

function home_buttonPrevCallback(carousel, button, enabled) {
    if (!enabled) {
        var movie = getFlashMovie("video");

        if (movie != undefined)
            movie.pauseVideo();
    }
};


function gallery_initCallback(carousel) {
    $('#mycarousel li img').bind('click', function() {
        carousel.scroll($.jcarousel.intval($(this).parent().attr('jcarouselindex'))-2);
        return false;
    });

    jQuery('.carousel-next').bind('click', function() {
        carousel.next();
        return false;
    });

    jQuery('.carousel-prev').bind('click', function() {
        carousel.prev();
        return false;
    });
        
};

function gallery_itemVisibleInCallback(carousel, item, i, state, evt) {
    var firstItem = carousel.first;
    var secondItem = carousel.first + 1;
    var thirdItem = carousel.first + 2;
    var fourthItem = carousel.first + 3;
    var fifthItem = carousel.first + 4;

    //selected item
    $("#mycarousel li[jcarouselindex='" + thirdItem + "'] img")
        .animate({
            marginLeft: '-8px',
            marginRight: '-8px'
        }, 0)
        .parent().addClass('selected');

  var selectedText = $("#mycarousel li[jcarouselindex='" + thirdItem + "'] img").attr('class');
    
    if (state == "init") {
        $("#carouselBG li").hide();
        $("#carouselBG li." + selectedText).show();        
    }
    else {
        $("#carouselBG li")
        .not("#carouselBG li." + selectedText)
        .fadeOut(1000);

        $("#carouselBG li." + selectedText)
        .fadeIn(1000);
    }
    
  $("#mycarousel li[jcarouselindex='" + firstItem + "'] img,#mycarousel li[jcarouselindex='" + secondItem + "'] img,#mycarousel li[jcarouselindex='" + fourthItem + "'] img,#mycarousel li[jcarouselindex='" + fifthItem + "'] img")
        .animate({
            marginLeft: '0px',
            marginRight: '0px'
        }, 0)
        .parent().removeClass('selected');
  
}

$(document).ready(function () {
    $('#globalNav li, div.box, div.bigText, input.search, .prospectus div.order, .form div.button input').hover(function () {
        $(this).addClass('hover');
    }, function () {
        $(this).removeClass('hover');
    });

    $('.jcarousel-prev img,.jcarousel-next img').live('mouseover mouseout', function (event) {
        if (event.type == 'mouseover') {
            $(this).animate({ opacity: '0' }, 300).animate({ opacity: '1' }, 300);
        }
    });

    $('div.box p a span').vAlign();

    //input boxes
    $('#searchText').click(function (event) {
        $(this).attr('value', '');
    });

    var school = $('meta[name=school]').attr("content");

    if (school == "Trinity Music" || school == "Laban Dance" || school == "Trinity Laban" || school == "Blackheath halls") {

        if (school == "Trinity Music")
            school = "music";
        else if (school == "Laban Dance")
            school = "dance";
        else if (school == "Trinity Laban" || school == "Blackheath halls")
            school = "home";

        $('#main').addClass(school);

        if (school != "home")
            $('#logo img').attr('src', '/images/logo-' + school + '.jpg');
    }

    $popupPanel = $('div.popupPanel');
    $popup = $('#column-c').find('.popup');
    $popupPanel.append($popup);
    $popupPanel.find('div.popup').addClass('hide');
    $popupPanel.find('div.popup').addClass('hasJs');
    $popupPanel.find('h3 a').click(function(){
        $popup = $(this).parent().parent().find('div.popup');
        $popup.removeClass('hide');
        if($popup.find('h3').length <= 0) {
            $popup.find('> div').prepend('<h3>' + $(this).html() + '</h3>');
        }
        if($popup.find('a.close').length <= 0) {
            $popup.find('> div').prepend('<a href="#" class="close">Close</a>');
            $popup.find('> div').find('a.close').click(function() {
                $popup.addClass('hide');
                return false;
            });
        }
        return false;
    });
  
});
