$(window).load(function() {
    if ($('#flash-card-container').length > 0) {
        var flashCardAttributes = new Array();
        $('#flash-card-container').addClass("loaded");
        $('#flash-card-container .flash-card:first-child').addClass('front-card');
        var i = 1;
        $('#flash-card-container .flash-card').each(function() {
            if (i == 1 || i == 2 || i == 4) {
                jQuery.data($(this).get(0), "header-align", "card-header-1");
                $(this).find('h2').addClass('card-header-1');
            } else if (i == 3 || i == 5) {
                jQuery.data($(this).get(0), "header-align", "card-header-2");
                $(this).find('h2').addClass('card-header-2');
            } else if (i == 6 || i == 7) {
                jQuery.data($(this).get(0), "header-align", "card-header-3");
                $(this).find('h2').addClass('card-header-3');
            }
            flashCardAttributes["flash-card-" + i] = new Object();
            flashCardAttributes["flash-card-" + i].pos = {
                top: $(this).position().top,
                left: $(this).position().left
            };
            flashCardAttributes["flash-card-" + i].grown = {
                top: $(this).position().top - 10,
                left: $(this).position().left - 10
            };
            flashCardAttributes["flash-card-" + i].zIndex = $(this).css('zIndex');
            i++;
        });
        $('#flash-card-container .flash-card .card-content').css({ "opacity":"0", "-ms-filter":"progid:DXImageTransform.Microsoft.Alpha(Opacity=0)", "filter":"alpha(opacity=0)"});
        if (jQuery.support.opacity) {
            $('#flash-card-container .flash-card:not(.front-card) .card-content').css("opacity", "0");
        } else {
            $('#flash-card-container .flash-card:not(.front-card) .card-content').hide();
        }
        function eatMe() {
            if (!$(this).hasClass('front-card')) {
                $(this).dequeue().animate({
                    top: flashCardAttributes[$(this).attr("id")].pos.top - 5
                },
                200);
            }
        }
        function drinkMe() {
            if (!$(this).hasClass('front-card')) {
                $(this).dequeue().animate(flashCardAttributes[$(this).attr("id")].pos, 200);
            }
        }
        function killLink(event) {
            event.preventDefault();
        }
        $('#flash-card-container .flash-card a').click(killLink);
        $('#flash-card-container .front-card a').unbind('click', killLink);
        function cardSwapper(event) {
            $('#flash-card-container .flash-card').unbind('mouseenter', eatMe);
            $('#flash-card-container .flash-card').unbind('mouseleave', drinkMe);
            $('#flash-card-container .flash-card').dequeue();
            if (!$(this).hasClass('front-card')) {
                var clickedCard = $(this);
                var clickedCardAtts = flashCardAttributes[clickedCard.attr('id')];
                var currentFrontCard = $(this).siblings('.front-card');
                var currentFrontCardAtts = flashCardAttributes[currentFrontCard.attr('id')];
                $.blockUI.defaults.message = null;
                $.blockUI.defaults.overlayCSS.opacity = 0;
                $.blockUI.defaults.overlayCSS.cursor = 'pointer';
                var clickedHeaderClass = jQuery.data($(clickedCard).get(0), "header-align");
                var currentFrontHeaderClass = jQuery.data($(currentFrontCard).get(0), "header-align");
                $(clickedCard).find('h2').removeClass().addClass(currentFrontHeaderClass);
                $(currentFrontCard).find('h2').removeClass().addClass(clickedHeaderClass);
                jQuery.data($(clickedCard).get(0), "header-align", currentFrontHeaderClass);
                jQuery.data($(currentFrontCard).get(0), "header-align", clickedHeaderClass);
                if (jQuery.support.opacity) {
                    $('.card-content').stop().css("opacity", 0);
                } else {
                    $('.card-content').stop().hide();
                }
                flashCardAttributes[clickedCard.attr('id')] = currentFrontCardAtts;
                flashCardAttributes[currentFrontCard.attr('id')] = clickedCardAtts;
                if ($(currentFrontCard).hasClass('concord-card')) {
                    $(currentFrontCard).find('h3.active').each(function() {
                        jQuery.data($(this).get(0), "toggle-state", "open");
                    }).click();
                }
                if (jQuery.support.opacity) {
                    clickedCard.css('opacity', .7);
                }
                clickedCard.css('zIndex', flashCardAttributes[clickedCard.attr('id')].zIndex).animate(flashCardAttributes[clickedCard.attr('id')].pos, 600,
                function() {
                    $(clickedCard).find('h3.toggle').each(function() {
                        if (jQuery.data($(this).get(0), "toggle-state") == "open") {
                            $(this).click();
                            jQuery.data($(this).get(0), "toggle-state", "");
                        }
                    });
                    if (jQuery.support.opacity) {
                        $(this).animate({
                            opacity: 1
                        },
                        250);
                        $('#flash-card-container .front-card').children('.card-content').fadeTo(300, 1);
                    } else {
                        $('#flash-card-container .front-card').children('.card-content').show();
                    }
                });
                if (jQuery.support.opacity) {
                    currentFrontCard.css('opacity', .7);
                }
                currentFrontCard.css('zIndex', flashCardAttributes[currentFrontCard.attr("id")].zIndex).animate(flashCardAttributes[currentFrontCard.attr('id')].pos, 600,
                function() {
                    if (jQuery.support.opacity) {
                        $(this).animate({
                            opacity: 1
                        },
                        250);
                    }
                    
                    $('#flash-card-container .flash-card').mouseenter(eatMe);
                    $('#flash-card-container .flash-card').mouseleave(drinkMe);
                });
                $(currentFrontCard).removeClass('front-card');
                $(clickedCard).addClass('front-card');
                $('#flash-card-container .flash-card a').click(killLink);
                $('#flash-card-container .front-card a').unbind('click', killLink);
            } else {
                if ($(this).children('.fc-link').length == 1) {
                    window.location = $(this).children('.fc-link').attr('href');
                }
            }
        }
        $('body.home #content').css('background', 'none');
        if (document.getElementById("homepage")) {
            $('.flash-card').animate(flashCardAttributes[$('.front-card').attr("id")].pos, 0,
            function() {
                $('#flash-card-container').css('display', 'none');
                $('#flash-card-container').css('left', '0');
            });
            if (jQuery.support.opacity) {
                $('#flash-card-container:hidden').fadeIn(1500,
                function() {
                    $('.flash-card').each(function() {
                        $(this).animate(flashCardAttributes[$(this).attr("id")].pos, 1700,
                        function() {
                            $('#flash-card-container .flash-card').mouseenter(eatMe);
                            $('#flash-card-container .flash-card').mouseleave(drinkMe);
                            $('#flash-card-container .flash-card').click(cardSwapper);
                        });
                    });
                });
            }
            else {
                $('#flash-card-container').css('display', 'block');
                $('.flash-card').each(function() {
                    $(this).animate(flashCardAttributes[$(this).attr("id")].pos, 1700,
                    function() {
                        $('#flash-card-container .flash-card').mouseenter(eatMe);
                        $('#flash-card-container .flash-card').mouseleave(drinkMe);
                        $('#flash-card-container .flash-card').click(cardSwapper);
                    });
                });
            }
        }
        else {
            $('#flash-card-container').css('left', '0');
            $('#flash-card-container .flash-card').mouseenter(eatMe);
            $('#flash-card-container .flash-card').mouseleave(drinkMe);
            $('#flash-card-container .flash-card').click(cardSwapper);
        }
    }




});
$(document).ready(function(){
	$('.flash-card').mouseover(
		function(){
			if( $(this).hasClass('front-card') ){
				$(this).children('.card-content').css({"opacity":"1.0", "-ms-filter":"progid:DXImageTransform.Microsoft.Alpha(Opacity=100)", "filter":"alpha(opacity=100)","-ms-filter":"progid:DXImageTransform.Microsoft.gradient(startColorstr=#7fffffff,endColorstr=#7fffffff)", "filter":"progid:DXImageTransform.Microsoft.gradient(startColorstr=#7fffffff,endColorstr=#7fffffff)"});
            }
		});
    $('.flash-card').mouseout(
		function(){
			if( $(this).hasClass('front-card') ){
				$(this).children('.card-content').css({"opacity":"0", "-ms-filter":"progid:DXImageTransform.Microsoft.gradient(startColorstr=#00ffffff,endColorstr=#00ffffff)", "filter":"progid:DXImageTransform.Microsoft.gradient(startColorstr=#00ffffff,endColorstr=#00ffffff)","-ms-filter":"progid:DXImageTransform.Microsoft.Alpha(Opacity=0)", "filter":"alpha(opacity=0)"});
			}
		});
});
