﻿// Slider and Countdowns
$(document).ready(function () {

    // Strict
    "use strict";

    // Variables
    var wrapper, container, overlay, background, slider, animating, theme, request, urlString;

    // DOM Elements
    wrapper = $('#PageWrap');
    container = $('#PageContainer');
    slider = $('#FeatureSlider');

    // Default Background
    theme = wrapper.css('background-image');
    theme = theme.substring(5, theme.length - 2);

    // Colliding Animations
    animating = false;

    // URL String (Local vs Live)
    urlString = window.location.href.indexOf('internal.idstudios.net') !== -1 ? 'http://internal.idstudios.net/mainevent.com.au' : 'http://mainevent.com.au';

    // Remove Cache String
    function removeCache(string) {
        return string.replace("cache/2000x2500/", "");
    }

    // Compare Backgrounds
    function backgroundChanged(backgroundSource) {
        return removeCache(wrapper.css('background-image')).indexOf(backgroundSource) === -1;
    }

    // Get Background
    function imageString() {
        var hiddenInputVal = slider.find('ul').eq(parseInt(slider.getCurrentSlide(), 10) + 1).find('.EventMediaBG').val();
        hiddenInputVal = hiddenInputVal === "" ? 'generic.jpg' : hiddenInputVal;
        return urlString + '/content/background/' + hiddenInputVal;
    }

    // Change Background
    function highlightCallback() {
        if ($('#FeatureSlider ul.Hidden').length > 0) {
            $('#FeatureSlider ul.Hidden').show();
        }
        if (!animating) {
            background = imageString();
            if (backgroundChanged(background)) {
                getThumbnail(background);
            }
        }
        removeVideo();
    }

    // Background Swapper
    function swapBackground(source) {
        if (removeCache(source) === background) {
            animating = true;
            background = 'url("' + source + '")';
            wrapper.append('<div id="FauxBackground"></div>');
            container.addClass('Animating');
            overlay = $('#FauxBackground');
            overlay.css({ 'background': background, 'background-position': '50% 0%' }).animate({ 'opacity': 1 }, 1000, function () {
                wrapper.css({ 'background': background, 'background-position': '50% 0%' });
                container.removeClass('Animating');
                overlay.remove();
                animating = false;
                if (backgroundChanged(imageString())) {
                    highlightCallback();
                }
            });
        }
    }

    // Background Preloader
    function preloadImage(source) {
        var img = new Image();
        img.onload = function () {
            swapBackground(this.src);
            img = null;
        };
        img.src = source;
    }

    // Get Thumbnail
    function getThumbnail(src) {
        if (src === undefined) {
            preloadImage(theme);
        } else {
            if (request !== null && request !== undefined) {
                request.abort();
            }
            request = $.ajax({
                url: "home/includes/featured-events-image.aspx",
                data: { thumbSrc: src.substring(src.lastIndexOf('/') + 1, src.lastIndexOf('.') + 4) },
                success: function (data) {
                    preloadImage(urlString + data.replace("/mainevent.com.au", ""));
                },
                error: function () {
                    preloadImage(theme);
                }
            });
        }
    }

    // Remove Video
    function removeVideo() {
        $('.Featurette .VideoPlayer').remove();
        $('.Featurette .PlayVideo').show();
    }

    // Social Feed Toggle
    $('.SocialFeedToggle').click(function (event) {

        // Stop
        event.preventDefault();

        // Change Classes
        $('.SocialFeedToggle').removeClass('Active');
        $(this).addClass('Active');

        // Toggle Visibility
        $('.SocialFeedBox').hide();
        if ($(this).attr('id') === 'Twitter') {
            $('.SocialFeedBox.Twitter').show();
        } else {
            $('.SocialFeedBox.Facebook').show();
        }

    });

    // Slider Featured Event Click
    $('.FeaturedEventTrigger').click(function (event) {
    	event.preventDefault();
    	slider.stopShow();
        slider.goToSlide(parseInt($(this).attr('rel'), 10));
    });

    // Run Slider
    if (slider.length > 0) {
        slider.bxSlider({ prevText: 'Previous', nextText: 'Next', onBeforeSlide: highlightCallback, auto: true, pause: 8000 });
    }

	// Stop Show
    $('.bx-prev, .bx-next').live('mousedown', function () {
    	slider.stopShow();
	});

    // Slider Play Video Button
    $('.FeaturedPod .PlayVideo').click(function () {
    	slider.stopShow();
        var videoButton, videoPlayer;
        videoButton = $(this);
        videoButton.after('<div class="VideoPlayer"></div>');
        videoPlayer = $('.Featurette .VideoPlayer');
        $.ajax({
            url: "home/includes/featured-events-video.aspx",
            data: { videoUrl: $(this).attr('name') },
            success: function (data) {
                videoPlayer.html(data);
                videoButton.hide();
            },
            error: function () {
                removeVideo();
                videoPlayer.remove();
                videoButton.show();
            }
        });
    });

    // Run Countdown
    if ($('.EventCountDown').length > 0) {
        $('.EventCountDown').countDown();
    }

    // Clear Search Field
    if ($('.SearchText').length > 0) {
        $('.SearchText').focus(function () {
            if ($(this).val() === $(this)[0].defaultValue) {
                $(this).val('');
            }
        }).blur(function () {
            if ($(this).val() === '') {
                $(this).val($(this)[0].defaultValue);
            }
        });
    }

});
