﻿
var count_class_title = ".character-limit";
function InitializeInputs() {
    var inputs = jQuery(":input[characterlimit]:not([counted])");
    for (index = 0; index < inputs.length; index++) {
        jQuery(inputs[index]).attr("counted", "true").siblings(count_class_title)
        .html(GenerateMaxDiv(RemainingChars(jQuery(inputs[index])), jQuery(inputs[index]).attr("characterlimit")));
    }

    jQuery(inputs).keyup(function (event) {
        var remaining_chars = RemainingChars(jQuery(this));
        if (remaining_chars < 0) {
            remaining_chars = jQuery(this).attr("characterlimit") - 1;
            (count_class_title, jQuery(this).parent()).val(jQuery(this).val().substring(0, remaining_chars));
        }
        jQuery(this).siblings(count_class_title).text(GenerateMaxDiv(remaining_chars, jQuery(this).attr("characterlimit")));
        return;
    });

    function RemainingChars(div) {
        var length = jQuery(div).val().length;
        var limit = jQuery(div).attr("characterlimit");
        if (length < limit)
            return limit - length;
        else return 0;
    }

    function GenerateMaxDiv(remaining_chars, max_chars) {
        return remaining_chars + " / " + max_chars;
    };
};

function slideY(slidePositionY) {
    $("html, body").animate({ scrollTop: slidePositionY }, "slow");
};

function slideToDivY(divId, offset) {
    $("html, body").animate({ scrollTop: $("#" + divId).offset().top - offset }, "slow");
};

function slideToDivWithMinimumHeight(divIds, offset) {
    var divIdsElements = divIds.length;
    var minimumHeightDivId = divIds[0];
    var minimumHeight = $("#" + divIds[0]).offset().top;
    for (i = 1; i < divIdsElements; i++) {
        if ($("#" + divIds[i]).offset().top < minimumHeight) {
            minimumHeightDivId = divIds[i];
            minimumHeight = $("#" + divIds[i]).offset().top;
        }
    }
    slideToDivY(minimumHeightDivId, offset);
}

(function ($) {
    //Custom Tabs
    $.fn.tabIt = function (callback) {
        var holder = $(this);
        var tabs = holder.children("ul");
        holder.children("div").hide();
        holder.children("div:first").show();
        tabs.children("li:first").addClass("activeTab").show();
        tabs.children("li").click(function (e) {
            tabs.children("li").removeClass("activeTab");
            holder.children("div").hide();
            $(this).addClass("activeTab");
            var activeTab = $(this).find("a").attr("href");
            $(activeTab).show();
            if (callback != undefined)
                callback();
            return false;
        });
    };

    //Twitter
    $.fn.getLatestTweets = function (account, take, languageShort) {
        var host = $(this);
        $.ajax({
            type: 'POST',
            url: '/Services/TwitterService.asmx/GetTweets',
            data: "{ twitterAccount:'" + account + "',takeTweets:'" + take + "',languageShort:'" + languageShort + "'}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            async: false,
            success: function (tweetData) {
                $.each(tweetData.d, function (i, tweet) {
                    host.append("<div class='" + host.selector.replace('#', '') + "-item' style='display:none;'>" + tweet.Text + "<span>" + tweet.Date + "</span></div>");
                })
            }
        });
        return host;
    };
})(jQuery);

function slideContent(sliderId, contentId) {
    $(sliderId).slider({
        animate: true,
        orientation: "vertical",
        value: 100,
        change: handleSliderChange,
        slide: handleSliderSlide
    });

    function handleSliderSlide(e, ui) {
        var maxScroll = $(contentId).attr("scrollHeight") - $(contentId).height();
        $(contentId).attr({ scrollTop: maxScroll - ui.value * (maxScroll / 100) });
    }

    function handleSliderChange(e, ui) {
        var maxScroll = $(contentId).attr("scrollHeight") - $(contentId).height();
        $(contentId).animate({ scrollTop: maxScroll - ui.value * (maxScroll / 100) }, 1000);
    }
};

//Player Scripts

function loadPlayerAndSetVideo(playerName, informationsDiv) {
    var videoCode = $(informationsDiv).children(".youtube-id").val();
    if (document[playerName] == undefined) {
        var holder = $("#" + playerName);
        var width = holder.css("width").replace('px', '');
        var height = holder.css("height").replace('px', '');
        var flashvars = {
            videoCode: videoCode,
            width: width,
            height: height,
            controlsBackground: colorToHex(holder.css("background-color")),
            colorText: colorToHex(holder.css("color")),
            colorOver: colorToHex(holder.css("border-top-color"))
        };
        var params = {
            scale: "exactFit",
            wmode: 'transparent',
            allowscriptaccess: 'always',
            allowfullscreen: 'false'
        };
        var attributes = {};
        swfobject.embedSWF("/snippets/flash/Player.swf", playerName, width, height, "9.0.0", "expressInstall.swf", flashvars, params, attributes);
    } else {
        document[playerName].ChangeVideo(videoCode);
    }
};

function colorToHex(color) {
    if (color.substr(0, 1) === '#')
        return color.replace('#', '0x');
    var digits = /(.*?)rgb\((\d+), (\d+), (\d+)\)/.exec(color);
    if (digits) {
        var red = parseInt(digits[2]);
        var green = parseInt(digits[3]);
        var blue = parseInt(digits[4]);
        var rgb = blue | (green << 8) | (red << 16);
        var hex = rgb.toString(16);
    }
    return "0x" + ((hex === "0") ? "000000" : hex);
};

function changeBannerSize(div, width, height) {
    $(div).width(width).height(height);
};

//expanding baner
$(document).ready(function () {
    function setExpandingBanner() {
        var expanded = false;
        $(".banner-type-four-placeholder").height(100).click(function (event) {
            if (!expanded) {
                expanded = true;
                var bannerDiv = $(this).animate({ height: '+=400' }, 500);
                event.preventDefault();
                $('<a class="super-banner-close"></a>').insertAfter(bannerDiv);
                $('.super-banner-close').click(function (event) {
                    bannerDiv.animate({ height: '-=400' }, 500);
                    expanded = false;
                    $(this).remove();
                });
            }
        });
    };
    setExpandingBanner();
});
