// JavaScript Document

$(document).ready(function () {
    $('body').addClass('hasJS');

    $(".quicklinks a.expand").click(function (e) {
        e.preventDefault();

        var _this = $(this).siblings('.moreLinks');
        if (_this.is(":hidden"))
            _this.fadeIn(200);
        else
            _this.fadeOut(400);
    }).trigger('click');

    $("a.fancybox").fancybox({
        'hideOnContentClick': true,
        'type': 'iframe',
        'autoDimensions': true,
        'scrolling': 'no',
        'transitionIn': 'elastic'
    });

    $("a[href*=modal.aspx]").fancybox({
        'hideOnContentClick': true,
        'type': 'iframe',
        'autoDimensions': true,
        'scrolling': 'no',
        'transitionIn': 'elastic',
        'titleShow': false
    });

    $("a.fancyboxR1").fancybox({
        'hideOnContentClick': true,
        'type': 'iframe',
        'autoDimensions': true,
        'scrolling': 'no',
        'transitionIn': 'elastic',
        'titleShow': false,
        'width': 450,
        'height': 200,
    });

    $("a.fancyboxR2").fancybox({
        'hideOnContentClick': true,
        'type': 'iframe',
        'autoDimensions': true,
        'scrolling': 'no',
        'transitionIn': 'elastic',
        'titleShow': false,
        'width': 450,
        'height': 300,
    });

    $("a.fancyboxR3").fancybox({
        'hideOnContentClick': true,
        'type': 'iframe',
        'autoDimensions': true,
        'scrolling': 'no',
        'transitionIn': 'elastic',
        'titleShow': false,
        'width': 450,
        'height': 380,
    });
    
    $("a[href*=.swf]:not(.whiteBG)").fancybox({
        'hideOnContentClick': true,
        'type': 'swf',
        'autoDimensions': false,
        'width': 800,
        'height': 600,
        'scrolling': 'no',
        'transitionIn': 'elastic',
        'titleShow': true,
        'swf': { wmode: 'transparent', allowscriptaccess: 'always', quality: 'high' },
        'onComplete': function () {
            $("#fancybox-inner").css('background-color', '#000000');
            $("#fancybox-outer").css('background-color', '#000000');
        }
    });

    $("a[href*=.swf].whiteBG").fancybox({
        'hideOnContentClick': false,
        'type': 'iframe',
        'autoDimensions': false,
        'scrolling': 'no',
        'transitionIn': 'elastic',
        'titleShow': true,
        'autoScale': true
    });

    $("a.fancybox4").fancybox({
        'scrolling': 'no',
        'titleShow': false
    });

    $("a.fancybox5").fancybox({
        'scrolling': 'no',
        'modal': true,
        'titleShow': false
    });

    $("a.fancybox2").fancybox({
        'hideOnContentClick': true,
        'type': 'iframe',
        'autoDimensions': true,
        'scrolling': 'auto',
        'transitionIn': 'elastic',
        'width': 600,
        'height': 525
    });

    $("a.fancybox3").fancybox({
        'hideOnContentClick': true,
        'type': 'iframe',
        'autoDimensions': true,
        'scrolling': 'auto',
        'transitionIn': 'elastic',
        'width': 820,
        'height': 525
    });

    $("a[href*='/videoplayer.aspx']").fancybox({
        'hideOnContentClick': true,
        'type': 'iframe',
        'autoDimensions': true,
        'scrolling': 'no',
        'transitionIn': 'none',
        'width': 820,
        'height': 565,
        'onComplete': function () {
            if (document.getElementById('fancybox-outer')) {
                document.getElementById('fancybox-outer').style.background = 'none';

                document.getElementById('fancy-bg-n').style.background = 'none';
                document.getElementById('fancy-bg-ne').style.background = 'none';
                document.getElementById('fancy-bg-e').style.background = 'none';
                document.getElementById('fancy-bg-se').style.background = 'none';
                document.getElementById('fancy-bg-s').style.background = 'none';
                document.getElementById('fancy-bg-sw').style.background = 'none';
                document.getElementById('fancy-bg-w').style.background = 'none';
                document.getElementById('fancy-bg-nw').style.background = 'none';
                document.getElementById('fancybox-close').style.right = '5px';
                document.getElementById('fancybox-close').style.top = '5px';

                $('.fancy-bg, #fancy-bg').attr("background", "none");
                $('.fancy-bg, #fancy-bg').attr("background", "none");
            }
        }
    });

    //$("#editProfile fieldset a").dppermissions();

    $("input[id*='hdnPostType']").datepicker({
        buttonImage: '/core/images/ui/calendar.gif',
        buttonImageOnly: true,
        changeMonth: true,
        changeYear: true,
        showOn: 'both',
        duration: '',
        showTime: true
    });

    //jquery bubble 
    $(".info a, .info img").hover(function () {
        $(this).next("em").animate({ opacity: "show", top: "-75" }, "slow");
    }, function () {
        $(this).next("em").animate({ opacity: "hide", top: "-85" }, "fast");
    });

    //jquery bubble 2
    $(".tags label").hover(function () {
        $(this).next("em").animate({ opacity: "show", top: "-75" }, "slow");
    }, function () {
        $(this).next("em").animate({ opacity: "hide", top: "-85" }, "fast");
    });

    //replace header fonts with Avant Garde
    //Cufon.replace(document.getElementById('h1').getElementsByTagName('a'));
    if (document.getElementById('contentWrapper')) { Cufon.replace(document.getElementById('contentWrapper').getElementsByTagName('h1')); }
    if (document.getElementById('relatedContent')) { Cufon.replace(document.getElementById('relatedContent').getElementsByTagName('h1')); }
    if (document.getElementById('contentWrapper')) { Cufon.replace(document.getElementById('contentWrapper').getElementsByTagName('h3')); }
    if (document.getElementById('relatedContent')) { Cufon.replace(document.getElementById('relatedContent').getElementsByTagName('h3')); }

    //set up css sprite animation
    //	$(".menu").children("li").each(function() {
    //	    var current = "selected";
    //	    var thisClass = $(this).attr("class");
    //	    
    //	    if (thisClass != current) {
    //	        //$(this).children("a").css({backgroundImage:"none"});
    //	    }
    //	});    

    attachNavEvents(".menu", "medishare");
    attachNavEvents(".menu", "manna");
    attachNavEvents(".menu", "healthychurch");
    attachNavEvents(".menu", "restore");

    //create div inside list item
    function attachNavEvents(parent, myClass) {
        $(parent + " #" + myClass).mouseover(function () {
            $(this).append('<div class="menu-' + myClass + '"></div>');
            $("div.menu-" + myClass).css({ display: "none" }).fadeIn(300);
        }).mouseout(function () {
            // fade out & destroy pseudo-link
            $("div.menu-" + myClass).fadeOut(300, function () {
                $(this).remove();
            });
        });
    }

    // Login and Search toggle 
    $('#login_menu div').css('opacity', 0);
    $('a.signin').toggle(
		function (event) {
		    $(this).addClass('menu-open'),
			$('#login_menu')
					.css('opacity', 1)
					.slideDown(600),
			$('#login_menu div')
					.css('opacity', 0)
					.animate({ opacity: "1" }, 1200)
		    return false;
		},
		function (event) {
		    $(this).removeClass('menu-open'),
	  		$('#login_menu div')
				.animate({ opacity: "0" }, 300),
			$('#login_menu')
	  			.slideUp(1200)
		    return false;
		}
	)

    $('#search_menu div').css('opacity', 0);
    $('a.search').toggle(
		function (event) {
		    $(this).addClass('menu-open'),
			$('#search_menu')
				.css('opacity', 1)
				.slideDown(300),
			$('#search_menu div')
				.css('opacity', 0)
				.animate({ opacity: "1" }, 600)
		    return false;
		},
		function (event) {
		    $(this).removeClass('menu-open'),
	  		$('#search_menu div')
				.animate({ opacity: "0" }, 300),
			$('#search_menu')
	  			.slideUp(600)
		    return false;
		}
	);



    // STAYWELL EKTRON LINKS: Replace relative links with absolute links

//    $('.staywell #relatedContent a[href^="/"]').each(function () {
//        var relLink = $(this).attr('href');
//        var absLink = ('http://ccm.wsol.com' + relLink);
//        $(this).attr('href', absLink);
//    });

    // MEMBERS NEWS TICKER
    $("#news").newsTicker();

    $("a[href*=/ajax.aspx?f=EXB]").click(function (e) {
        e.preventDefault();

        $.get($(this).attr('href'), function (data) {
            if (data.result == "OK") {
                window.location = "/Medi-Share-payment-portal.aspx"
            }
            else {
                $.fancybox("<p>We thank you for your interest on the Extra Blessings fund. We currently show that you have an amount due and would need to pay it before you can contribute to help other members.</p>", { "autoDimensions": false, "width": 450, "height": 100, "transitionIn": "none", "transitionOut": "none", "scrolling": "no" });
            }
        }, "json");
    });
});

//Changed by Brian Heinz 12/20/2011 moved Complex Content code to window.load to eliminate race condition with CSS
$(window).load(function(){
// Accordion Panel
    $("div.more").each(function () {
        $(this).css("height", $(this).height() + "px");
        if (!$(this).hasClass("active")) {
            $(this).hide();
        }
    });

    $(".accordion h3").css("cursor", "pointer");
    $("#sideContent2 .accordion h2").css("cursor", "pointer");
    $(".readMore").css("cursor", "pointer");
    $('<a class="toggleSprite"> </a>').prependTo('#mainContent .accordion h3');

    //$('<div class="toggleSprite"> &raquo; </div>').appendTo('#sideContent2 .accordion h2');

    $(".accordion h3").hover(
		function () {
		    $(this).css({ color: "#4ab6d6" }); //mouseover
		},
		function () {
		    $(this).css({ color: "#666666" }); // mouseout
		}
	);

    $("#mainContent .accordion h3").click(function () {
        $(this).siblings("div.more").slideToggle(1000);
        $(this).children("a.toggleSprite").toggleClass("close");
    });

    $("#sideContent2 .accordion h2").click(function () {
        $(this).siblings("div.more").slideToggle(500);
        $(this).toggleClass("selected");
    });

    $(".readMore").click(function () {
        $(this).siblings("div.more").slideToggle(1000);
        $(this).children("span.toggleSprite").toggleClass("close");
    });

    // Tab Switchers
    $(function () {
        var mainTabContainers = $('#mainContent div.tabContainer > div');
        $('#mainContent div.tabs ul.tabNavigation a').click(function () {
            mainTabContainers.hide().filter(this.hash).show();
            $('#mainContent div.tabs ul.tabNavigation a').removeClass('selected');
            $(this).addClass('selected');
            return false;
        }).filter(':first').click();

        var relatedTabContainers = $('#relatedContent div.tabContainer > div');
        $('#relatedContent div.tabs ul.tabNavigation a').click(function () {
            relatedTabContainers.hide().filter(this.hash).show();
            $('#relatedContent div.tabs ul.tabNavigation a').removeClass('selected');
            $(this).addClass('selected');
            return false;
        }).filter(':first').click();
    });

});

function OpenLogin() {
    $('a.signin').show().addClass('menu-open');
    $('#login_menu').css('opacity', 1).slideDown(0);
    $('#login_menu div').css('opacity', 0).animate({ opacity: "1" }, 1);
}

function redirectParernt(url) {
    document.location = url;
}

var CurrentPost = "";
var CurrentContainer = "";

function ShowPostComment(ShowButton, HideButton, Container) {
    var m_ShowButton = document.getElementById(ShowButton);
    var m_HideButton = document.getElementById(HideButton);

    if ((CurrentContainer != Container) && CurrentContainer != "") {
        $("#" + CurrentContainer).slideUp(100);
    }

    if ((CurrentPost == ShowButton) && CurrentPost != "") {
        $("#" + Container).slideUp(100);
        CurrentPost = "";
        CurrentContainer = "";
        return;
    }

    CurrentPost = ShowButton;
    CurrentContainer = Container;

    $("#" + ShowButton).show();
    $("#" + HideButton).hide();
    $("#" + Container).slideDown(100);
}


// --------------------- SEARCH BOX --------------------------
// CLEAR ELEMENT TEXT
function ClearElementText(text, elementId) {
    if ($('#' + elementId).val() == text)
        $('#' + elementId).val("");
}

// RE-ENTER ELEMENT TEXT ON BLUR
function SetElementText(text, elementId) {
    if ($('#' + elementId).val() == "")
        $('#' + elementId).val(text);
}

function selectClass(button, wrapper) {
    if (wrapper != "") {
        $(wrapper).removeClass("selected");
    }
    $(button).siblings("a").removeClass("selected");
    $(button).addClass("selected");    
}

//Jquery Accordion Plugin
//Created 4/6/2010 by Steven Stanley
//Last Modifed 4/6/2010 by Steven Stanley
//usage example 1: $('.accordion').accordion();
//usage example 2: $('.accordion').accordion({ Speed: '1000', ActiveClass: 'active' Indicator: 'a.toggleSprite', IndicatorClass: 'close' });

//<div class="accordion">
//  <div>Handle...</div>
//  <div>Content...</div>
//</div>

(function($) {
    
    $.fn.accordion = function(options) {
        //Default Properties
        var defaults = {
            Speed: '500',
            ActiveClass: 'active',
            Indicator: '',
            IndicatorClass: ''
        },

        //merge passed in items with defaults (passed in items take precidence)
		options = $.extend(defaults, options);

        //process each accordion found
        $(this).each(function() {
            //set the height of the wrapper to the height of the content.
            $(this).children(":nth-child(2):not(." + options.ActiveClass + ")");

            //Hide all none active elements
            $(this).children(":nth-child(2):not(." + options.ActiveClass + ")").hide();

            //get all handlers
            var elm = $(this).children(":nth-child(1)");

            //unbind click event if it already exists
            //then rebind bind toggle event.
            //(namespaced for uniqueness)
            elm.unbind("click.accordion").bind("click.accordion", function(e) {
                //change class and toggle state
                $(this).next().toggleClass(options.ActiveClass).slideToggle(options.Speed);

                //update indicator
                if (options.Indicator != '' && options.IndicatorClass != '') {
                    $(this).children(options.Indicator).toggleClass(options.IndicatorClass);
                }
            });
        });
    }
})(jQuery);

(function($) {
    
    $.fn.dppermissions = function(options) {
        //Default Properties
        var defaults = {
            Speed: '500',
            ActiveClass: 'active',
            Class: 'permissions',
            Indicator: '',
            IndicatorClass: ''
        },

        //merge passed in items with defaults (passed in items take precidence)
	options = $.extend(defaults, options);

        //process each accordion found
       
	$(this).each(function () {
	    $(this).next("div." + options.Class).hide();
	    $(this).bind("click",function(e){
	    	e.preventDefault();
		$(this).next("div." + options.Class).slideToggle(options.Speed);
	    });
	});
    }
})(jQuery); 

