﻿/*--------------------------
-------- SETTINGS --------
--------------------------*/

// POPUP WINDOWS
var popupFadeTime = 750;

// IMAGE GALLERY
var animateFirstImage = false;          // Should first image just be displayed, or animated in?
var animationSpeed = 400;               // Speed of the chosen animation/transition effect (in milliseconds)
var animationEffect = 'slide-hori';     // Chosen transition effect. Options are 'slide-hori', 'slide-vert', 'resize', 'fade', 'none' and false
var enableKeyboard = true;              // Enable or disable image navigation using keyboard arrows
var cycleSlides = true;                 // If set to false, you can't go from the last image to the first, and vice versa
var slideChangeSpeed = 7000;            // The time to show each slide before moving on to the next one (in milliseconds)
var pauseTimeout = 240000;              // Time to pause on a single image after a thumbnail is clicked before resuming the slideshow (in milliseconds)
var newsletterExtraMove = 10;           // Number of extra pixels to move newsletter form before sliding button up with it
var newsletterSlideTime = 500;          // Time (in milliseconds) taken to slide the newsletter form when opening/closing it
var screenLockOpacity = 0.6;            // Maximum opacity of screen lock overlay (0.0 - 1.0)

/*----------------------------
------ END SETTINGS --------
----------------------------*/

$j = jQuery.noConflict();

// Start of code (use caution when editing beyond this point)
var unpauseTimer, newsletterForm, newsletterFormButton, popup;
var newsletterAnimating = false;
var fadeInPopup = false;

function onStop() { unpauseTimer = setTimeout(doStart, pauseTimeout); }
function onStart() { clearTimeout(unpauseTimer); }
function doStart() { $j('.ad-slideshow-start').click(); }
$j(function () {
    var galleries = $j('.ad-gallery').adGallery({
        description_wrapper: $j('.ad-description-holder'),
        animate_first_image: animateFirstImage,
        animation_speed: animationSpeed,
        effect: animationEffect,
        enable_keyboard_move: enableKeyboard,
        cycle: cycleSlides,
        slideshow: {
            autostart: true,
            speed: slideChangeSpeed,
            onStart: onStart,
            onStop: onStop
        }
    });
});

$j(document).ready(function () {

    var sampleAppGallery = $j("#sample-app-gallery");
    if (sampleAppGallery.length > 0) $j("#sample-app-gallery").featureCarousel({});

    // Generic Popup

    var screenLock = $j('<div id="screen-lock" style="position: absolute; left: 0; top: 0; background: black; width: 100%; height: 50px; z-index: 988; display:none;"></div>');
    $j('body').append(screenLock);
    $j(screenLock).height($j(document).height());

    popup = $j('.popup');

    if (fadeInPopup) {
        $j('body').append(popup);
        var horPos = ($j(window).width() / 2) - ($j(popup).outerWidth() / 2);
        var verPos = ($j(window).height() / 2) - ($j(popup).outerHeight() / 2);
        $j(popup).css('top', verPos + "px");
        $j(popup).css('left', horPos + "px");
        $j(popup).show();
        $j(screenLock).fadeTo(popupFadeTime, screenLockOpacity);
        $j(popup).fadeIn(popupFadeTime);
    }

    $j('.popup .close-popup-button').click(function () {
        screenLock.fadeOut(popupFadeTime);
        popup.fadeOut(popupFadeTime);
        return false;
    });
    $j(screenLock).click(function () {
        screenLock.fadeOut(popupFadeTime);
        popup.fadeOut(popupFadeTime);
    });

    //Contact Form
    if ($j('.contact-form').length > 0) {

        $j('.contact-form .form_tag label').each(function (i) {
            var lblText = $j(this).text();
            if (lblText.indexOf("*") != -1) {
                $j(this).text(lblText.substr(0, lblText.length - 1));
                $j('<span class="contact-asterisk contact-field-required">Required</span>').insertBefore(this);
            }
            else $j('<span class="contact-asterisk contact-field-optional">Optional</span>').insertBefore(this);
        });

        if ($j('.contact-form .RadCaptcha_Default span:visible').length > 0) $j('#contact-failure-message').show();

        $j('.contact-form .submit-button').click(function () {
            var failure = false;
            $j('.contact-form .form_req:visible').each(function () {
                if ($j(this).css('visibility') != "hidden") failure = true;
            });
            $j('.contact-form .form_answer .RadInput').each(function () {
                if ($j(this).next().hasClass('form_req') && $j(this).find('.riTextBox').hasClass('riEmpty')) failure = true;
            });
            if (failure) $j('#contact-failure-message').show();
        });

        $j('.RadCaptcha').parent('.form_answer').prev('.form_tag').hide().remove();
    }

    // Webinars

    $j('.webinar-list tr:last-child').addClass('last');

    // Blog archive

    $j('.blog-archive .rtMinus').parent().each(function () {
        $j(this).remove();
    });

    // Blog Category List

    var radTreeView = $j('.RadTreeView_Default');
    radTreeView.each(function () {
        if (!$j(this).hasClass('blog-archive')) {
            $j(this).find('.rtUL .rtUL a').each(function () {
                $j(this).html("- " + $j(this).html())
            });
            $j(this).find('.rtUL .rtUL .rtUL a').each(function () {
                $j(this).html("-- " + $j(this).html())
            });
            $j(this).find('.rtUL .rtUL .rtUL .rtUL a').each(function () {
                $j(this).html("--- " + $j(this).html())
            });
        }
    });

    // Blog comments popup

    var popupHolder = $j('.popup-dialog-holder');

    if ($j(popupHolder).length > 0) {
        $j('#__SCROLLPOSITIONX').val(0);
        $j('#__SCROLLPOSITIONY').val(0);
        $j(popupHolder).fadeIn(popupFadeTime);
        return "#top";
    }

    $j('.popup-cancel').click(function (e) {
        $j('.sf_postNewComment .sf_commentsValidator').hide();
        $j(popupHolder).fadeOut(popupFadeTime);
    });

    var commentLinkHolder = $j('#comment-links');
    var postLink = $j('.post-comment-link');
    if ($j('.blog-post').length > 0) {
        $j('.sf_socialBookmarks').after(commentLinkHolder);
        $j('.post-comment-link').prependTo(commentLinkHolder);
    }
    else if (postLink.length > 0) postLink.hide();

    // Code block styling
    $j('code, pre').each(function () {
        if (!($j(this).parent().hasClass('code-block'))) {
            $j(this).wrap('<div class="code-block" />');
            $j(this).parent().wrap('<div class="code-block-wrapper" />');
            $j(this).parent().before('<div class="code-block-header">' +
                                    '<h3>Code Block</h3>' +
                                    '<span class="code-block-tools">' +
                                        '<a href="#" title="Select All" class="select-code first last">Select All</a>' +
                                    '</span>' +
                                '</div>');
        }
    });

    $j('.code-block-tools .select-code').click(function () {
        $j(this).closest('.code-block-wrapper').find('pre, code').each(function () {
            selectElementText(this);
        });
        return false;
    });

    // Youtube embedded video resizing

    if ($j('.video-post object').length > 0) {
        $j('.video-post object').each(function () {
            $j(this).wrap('<p class="youtube" />');
            var newWidth = $j(this).parent().width();
            //if ($j(this).attr('width') > newWidth) {
            var embed = $j(this).find('embed');
            $j(this).attr('height', newWidth / 1.2);
            $j(this).attr('width', newWidth);
            embed.attr('height', newWidth / 1.2);
            embed.attr('width', newWidth);
            //}
        });
    }
    else if ($j('.video-post embed').length > 0) {
        $j('.video-post embed').each(function () {
            $j(this).wrap('<p class="youtube" />');
            var ratio = $j(this).width() / $j(this).height();
            $j(this).attr('width', '100%');
            var newHeight = $j(this).width() / ratio;
            $j(this).attr('height', newHeight + 'px');
        });
    }

    // Blog Lightbox

    $j('img.lightbox').each(function () {
        if ($j(this).parent('a').length < 1) {
            var $lbLink = '<a href="' + $j(this).attr('src') + '" title="' + $j(this).attr('alt') + '" rel="lightbox" />';
            $j(this).wrap($lbLink);
        }
    });

    $j('a[rel*=lightbox]').lightBox({
        imageLoading: '/images/lightbox/lightbox-ico-loading.gif',
        imageBtnPrev: '/images/lightbox/lightbox-btn-prev.gif',
        imageBtnNext: '/images/lightbox/lightbox-btn-next.gif',
        imageBtnClose: '/images/lightbox/lightbox-btn-close.gif',
        imageBlank: '/images/lightbox/lightbox-blank.gif'
    });

    // NEWSLETTER FORM

    newsletterForm = $j('#newsletter-form');
    newsletterFormButton = $j('#newsletter-form-button');
    var newsletterMoveString = "-" + newsletterForm.outerHeight() + "px";
    var newsletterButtonMoveString = "-" + (newsletterForm.outerHeight() - newsletterExtraMove) + "px";

    newsletterFormButton.click(function () {
        if (!newsletterAnimating) {
            newsletterAnimating = true;
            if ($j(this).hasClass('open')) {
                closeNewsletterForm();
            }
            else {
                $j(this).addClass('open');
                $j(this).stop().animate({ marginTop: newsletterButtonMoveString }, newsletterSlideTime);
                newsletterForm.stop().animate({ marginTop: newsletterMoveString }, newsletterSlideTime, newsletterFormOpened);
            }
        }
        return false;
    });

    $j('#newsletter-form .btn').click(function () {
        window.scrollTo = function () { };
    });


    $j('#close-newsletter-form').click(function () {
        if (!newsletterAnimating) closeNewsletterForm();
        return false;
    });

});

function selectCode(item) {
    $j(item).closest('.code-block-wrapper').find('pre, code').each(function () {
        selectElementText(this);
    });
}

function selectElementText(el, win) {
    win = win || window;
    var doc = win.document, sel, range;
    if (win.getSelection && doc.createRange) {
        sel = win.getSelection();
        range = doc.createRange();
        range.selectNodeContents(el);
        sel.removeAllRanges();
        sel.addRange(range);
        return range;
    } else if (doc.body.createTextRange) {
        range = doc.body.createTextRange();
        range.moveToElementText(el);
        range.select();
        return range;
    }
}

function searchKeyPress(sender, eventArgs) {
    if (eventArgs.get_keyCode() == 13) {
        $j($j.get('#header .sf_searchSubmit')).click();
        eventArgs.set_cancel(true);
    }
}

function closeNewsletterForm() {
    newsletterFormButton.stop().animate({ marginTop: '0' }, newsletterSlideTime);
    newsletterForm.stop().animate({ marginTop: '0' }, newsletterSlideTime, newsletterFormClosed);
}

function newsletterFormOpened() {
    newsletterAnimating = false;
}

function newsletterFormClosed() {
    newsletterAnimating = false;
    newsletterFormButton.removeClass('open');
}

function openPopup() {
    $j('#__SCROLLPOSITIONX').val(0);
    $j('#__SCROLLPOSITIONY').val(0);
    fadeInPopup = true;   
}
