/*******************************************************************************

	CSS on Sails Framework
	Title: McFinn Club
	Author: XHTMLized (http://www.xhtmlized.com/)
	Date: October 2011

*******************************************************************************/

/* Sound */



$(document).ready(function() {
	FPEngine.init();
});

var FPEngine = {
    init: function () {
        this.singalong();
        this.clickSounds();
        this.bgplaceholder();
        this.hometabs();
        this.mapOverlays();
        $('body').removeClass('nojs').addClass('js');
    },

    /*
    * Focus background placeholder
    */
    bgplaceholder: function () {
        subscribeemail = $('#subscribeemail');
        if (subscribeemail.val() != '') {
            subscribeemail.addClass('focus');
        }
        subscribeemail.focus(function () {
            subscribeemail.addClass('focus');
        }).blur(function () {
            if (subscribeemail.val() == '') {
                subscribeemail.removeClass('focus');
            }
        })
    },

    /*
    * Home Tabs
    */
    hometabs: function () {
        tabnav = $('#characters .tabs')
        tablinks = $('#characters .tabs a');
        panels = $('#characters .panels .panel');
        panelswrap = $('#characters .panels');
        panels.not('.panel-active').hide();
        /* Click activation */
        tablinks.click(function (e) {
            $('.active', tabnav).removeClass('active');
            $('.panel-active', panelswrap).removeClass('panel-active').stop(true, true).fadeOut(300);
            $($(this).attr('href')).addClass('panel-active').stop(true, true).fadeIn(300);
            $('#header .main-message-active').removeClass('main-message-active').stop(true, true).fadeOut(300);
            $('#header .' + $(this).attr('href').substring(1)).addClass('main-message-active').stop(true, true).fadeIn(300);
            $(this).parent().addClass('active');

            if ($(this).attr('href') == '#char_gem') {
                setTimeout(function () {
                    $('#char_gem .gem-old').fadeOut('slow', function () {
                        $('#char_gem .character:first').fadeIn('slow')
                    });
                }, '6000');
            }

            e.preventDefault();
        });
        /* EasterEggs */
        eastertriggers = $('#characters .widgettrigger');
        widgets = $('#characters .widget');
        widgets.hide();
        eastertriggers.click(function (e) {
            e.preventDefault();
            $('#char_mcfinn .contents').fadeOut(300);
            widgets.stop(true, true).fadeOut(300);
            $($(this).attr('href')).stop(true, true).fadeIn(300);
        });
    },

    /*
    * Map Overlays
    */
    mapOverlays: function () {
        // enable on the map page
        if ($('#page_map').length) {
            if ($.browser.msie) {
                if (parseInt($.browser.version, 10) > 8) {
                    map = $('#map');
                }
            }
            else {
                map = $('#map');
            }
            backtomap = $('#returnmap');
            overlays = $('.mapoverlay', map);
            mapindex = $('#map-index', map)
            overlaytriggers = $('.pointer', mapindex);
            backtomap.hide();
            overlays.hide();
            overlaytriggers.click(function (e) {
                e.preventDefault();
                mapindex.stop(true, true).fadeOut(500);
                $($(this).attr('href')).stop(true, true).fadeIn(500);
                backtomap.stop(true, true).fadeIn(500);
                $('#header .main-message-active').removeClass('main-message-active').stop(true, true).fadeOut(300);
                $('#header .' + $(this).attr('href').substring(1)).addClass('main-message-active').stop(true, true).fadeIn(300);
            });
            backtomap.click(function (e) {
                e.preventDefault();
                overlays.fadeOut(500);
                mapindex.stop(true, true).fadeIn(500);
                backtomap.fadeOut(500);
                $('#header .main-message-active').removeClass('main-message-active').stop(true, true).fadeOut(300);
                $('#header .first-message').addClass('main-message-active').stop(true, true).fadeIn(300);
            });
        }
    },

    /*
    * Sing-a-Long
    */
    singalong: function () {
        // enable on the map page
        if ($('#page_sing').length) {
            // detect iOS
            var isiOS = (navigator.userAgent.match(/iPad/i) != null) || (navigator.userAgent.match(/iPod/i) != null) || (navigator.userAgent.match(/iPhone/i) != null);

            var audios = new Array();
            if (typeof $.fn.mediaelementplayer != 'undefined') {
                $('#currentsong audio').mediaelementplayer();
            };
            songlinks = $('#singalong .songs a');
            songpanels = $('#currentsong .panel');
            //songpanels.css('left','-999em');
            $('div#audio2').css('left', '-999em');
            $('div#audio3').css('left', '-999em');
            songlinks.click(function (e) {
                e.preventDefault();

                if ($(this).hasClass('active')) { // Click on active link
                    targetPanel = $($(this).attr('href'));
                    targetAudio = $('audio', targetPanel);
                    if ($(this).hasClass('active-paused')) {
                        $(this).removeClass('active-paused');
                        if (isiOS) {
                            targetAudio[0].play();
                        } else {
                            targetAudio[0].player.play();
                        }
                    } else {
                        $(this).addClass('active-paused');
                        if (isiOS) {
                            targetAudio[0].pause();
                        } else {
                            targetAudio[0].player.pause();
                        }
                    }
                } else { // Click on new link
                    targetPanel = $($(this).attr('href'));
                    // remove previous active link
                    songlinks.removeClass('active');
                    // move out from view the panels
                    songpanels.not(targetPanel).css('left', '-999em');
                    // let the active panel show up
                    $($(this).attr('href')).stop(true, true).css('left', '0');
                    // setup new active link
                    $(this).addClass('active');
                    // Stop all players
                    $('#currentsong audio').each(function () {
                        if (isiOS) {
                            $(this)[0].pause();
                        } else {
                            $(this)[0].player.pause();
                        }
                    });
                    targetAudio = $('audio', targetPanel);
                    // Play current audio
                    if (isiOS) {
                        targetAudio[0].play();
                    } else {
                        targetAudio[0].player.play();
                    }
                }

            });

        }
    },

    /*
    * Sounds
    */
    clickSounds: function () {
        if (typeof soundManager != 'undefined') {
            soundManager.url = '_ui/swf/';
            soundManager.debugMode = true;
            soundManager.preferFlash = false;
            soundManager.onready(function () {
                // Homepage -> Hello chars
                $('#characters .tabs a').click(function () {
                    soundManager.stopAll();
                    soundManager.play($(this).parent().attr('id'), $(this).attr('rel'));
                });
                // Main navigation
                $('#navigation a').click(function (e) {
                    if ($(this).attr('rel')) {
                        currentlink = $(this);
                        e.preventDefault();
                        soundManager.stopAll();
                        var navSoundObject = soundManager.createSound({
                            id: $(this).parent().attr('id'),
                            url: $(this).attr('rel')
                        });
                        navSoundObject.play({
                            onfinish: function () {
                                window.open(currentlink.attr('href'), '_self');
                            }
                        });
                    }
                })

                // Second navigation
                $('#navigation2 a').click(function (e) {
                    if ($(this).attr('rel')) {
                        currentlink = $(this);
                        e.preventDefault();
                        soundManager.stopAll();
                        var navSoundObject = soundManager.createSound({
                            id: $(this).parent().attr('id'),
                            url: $(this).attr('rel')
                        });
                        navSoundObject.play({
                            onfinish: function () {
                                window.open(currentlink.attr('href'), '_self');
                            }
                        });
                    }
                })

                $('#map-index a').click(function (e) {
                    soundManager.play($(this).parent().attr('id'), { url: $(this).attr('rel'), onfinish: function () { soundManager.reboot(); } });
                })
            });
        }
    }
}


