﻿$(function() {
    //Slider func
    $('#slides').slides({
        play: 10000,
        pause: 2500,
        hoverPause: true,

        animationStart: function(current) {
            $('.caption').animate({
                bottom: -90
            }, 100);
            if (window.console && console.log) {
                // example return of current slide number
                console.log('animationStart on slide: ', current);
            };
        },
        animationComplete: function(current) {
            $('.caption').animate({
                bottom: 50
            }, 200);
            if (window.console && console.log) {
                // example return of current slide number
                console.log('animationComplete on slide: ', current);
            };
        },
        slidesLoaded: function() {
            $('.caption').animate({
                bottom: 50
            }, 200);
        }
    });
});

// bbcode start
function bbcode_parser(str) {
    search = new Array(
    /\[b\](.*?)\[\/b\]/g,
    /\[i\](.*?)\[\/i\]/g,
    /\[img\](.*?)\[\/img\]/g,
    /\[url\="?(.*?)"?\](.*?)\[\/url\]/g,
    /\[quote](.*?)\[\/quote\]/g,
    /\[list\=(.*?)\](.*?)\[\/list\]/gi,
    /\[list\]([\s\S]*?)\[\/list\]/gi,
    /\[item\](.*?)\[\/item\]/g,
    /\[\*\]\s?(.*?)\n/g);

    replace = new Array(
      "<strong>$1</strong>",
      "<em>$1</em>",
      "<img src=\"$1\" alt=\"An image\">",
      "<a href=\"$1\">$2</a>",
      "<blockquote>$1</blockquote>",
      "<ol>$2</ol>",
      "<ul>$1</ul>",
      "<li>$1</li>",
      "<li>$1</li>");

    for (i = 0; i < search.length; i++) {
        str = str.replace(search[i], replace[i]);
    }

    return str;
}
// bbcode parser end

// JQUERY POPUP

$(init);
function init() {
    $('.siteBodyOuterContainer').prepend($('#arrowContent, .contactText'));
}

$(document).ready(function() {
    $("#arrowContent .description p").each(function() {
        var el = $(this);
        el.html(el.html().replace("\r", "").replace("\n\n", "</p><p>"));
    });

    $('a.poplight[href^=#]').click(function() {
        var popID = $(this).attr('rel');
        var popURL = $(this).attr('href');
        var query = popURL.split('?');
        var dim = query[1].split('&');
        var popWidth = dim[0].split('=')[1];

        $('#' + popID).fadeIn().css({ 'width': Number(popWidth) }).prepend('<a href="#" class="close"><img src="/KingstonSmith/Themes/Vivid/images/close_pop.png" class="btn_close" title="Close Window" alt="Close" /></a>');

        var popMargTop = ($('#' + popID).height() + 80) / 2;
        var popMargLeft = ($('#' + popID).width() + 80) / 2;

        $('#' + popID).css({
            'margin-top': -popMargTop,
            'margin-left': -popMargLeft
        });

        $('.siteBodyOuterContainer').append('<div id="fade"></div>');
        $('#fade').css({ 'filter': 'alpha(opacity=80)' }).fadeIn();

        $('#' + popID + " .content").html(bbcode_parser($('#' + popID + " .content").html()));

        return false;
    });

    $('a.close, #fade').live('click', function() {
        $('#fade , .popup_block').fadeOut(function() {
            $('#fade, a.close').remove();
        });
        return false;
    });
});
