﻿/* ******************************************************************************************* */
/* jQuery Support */
/* ******************************************************************************************* */
$(document).ready(
    function() {
        var after_type_cast = {};
        var before_type_cast = {};
        var cached = false;

        jQuery.query = function(cast) {
            if (!cached) {
                // remove leading ? and trailing &
                var q = location.search.replace(/^\?/, '').replace(/\&$/, '').split('&');
                for (var i = q.length - 1; i >= 0; i--) {
                    var p = q[i].split('='), key = p[0], val = p[1];
                    before_type_cast[key] = val;
                    // convert floats
                    if (/^[0-9.]+$/.test(val))
                        val = parseFloat(val);
                    // convert booleans
                    if (/^(true|false)$/.test(val))
                        val = (val == 'true');
                    // ingnore empty values
                    if (val)
                        after_type_cast[key] = val;
                }
                cached = true;
            }
            return cast === false ? before_type_cast : after_type_cast;
        };
        if ($("input.clsSearchTextBox").html() != null) {
            if ($.query().q != null)
                $("input.clsSearchTextBox").val($.query().q);
            $("input.clsSearchTextBox").bind("onkeydown", function(e) {
                var key = 0;
                e = (window.event) ? event : e;
                key = (e.keyCode) ? e.keyCode : e.charCode;
                var obj = document.getElementById('ctl00_txtSearchBox');
                if (e.keyCode == 13) {
                    obj.focus();
                    var v = $("#ctl00_txtSearchBox");
                    window.location = "/search-results.aspx?q=" + v.val();
                }
                return false;
            });

            $("input.clsSearchButton").bind("click", function(e) {
                var v = $("input#ctl00_txtSearchBox");
                window.location = "/search-results.aspx?q=" + v.val();
                return false;
            });

        }
        $("a.ExpandDescriptions").bind("click", function(e) {
            $("div.EventSummary").toggle();
            return false;
        });


        // IE Headaches for the Main Nav
        var isFamilyStory = $(".HomePageStorySubItem1");

        if (isFamilyStory.html() != null) {
            jQuery.each(jQuery.browser, function(i) {
                if ($.browser.msie) {
                    $("div.Menu ul.level1 li.topLevel").bind("mouseenter", function() {
                        $(this).addClass("Selected");
                    }).bind("mouseleave", function() {
                        $(this).removeClass("Selected");
                    });
                }
            });


            $(".StoryButton").bind("click", function(e) {
                $(".FamilyStoryMainContainer").find(".HomePageStoryMainItem").hide();

                var HomePageStorySubItem = "";
                $(".StoryButton").removeClass("Button1Selected");
                $(".StoryButton").removeClass("Button2Selected");
                $(".StoryButton").removeClass("Button3Selected");
                $(".StoryButton").removeClass("Button4Selected");
                switch ($(this).attr("class")) {
                    case "Button2 StoryButton":
                        $(this).addClass("Button2Selected");
                        HomePageStorySubItem = ".HomePageStorySubItem2";
                        break;
                    case "Button3 StoryButton":
                        $(this).addClass("Button3Selected");
                        HomePageStorySubItem = ".HomePageStorySubItem3";
                        break;
                    case "Button4 StoryButton":
                        $(this).addClass("Button4Selected");
                        HomePageStorySubItem = ".HomePageStorySubItem4";
                        break;
                    default:
                        $(this).addClass("Button1Selected");
                        HomePageStorySubItem = ".HomePageStorySubItem1";
                        break;
                }

                $(".FamilyStoryMainContainer").find(".HomePageStoryMainItem .HomePageStoryItemContent").replaceWith($(HomePageStorySubItem).html());
                $(".FamilyStoryMainContainer").find(".HomePageStoryMainItem").fadeIn("slow");

                return false;
            });

            if (isFamilyStory.html() != null) {
                $(".FamilyStoryMainContainer").find(".HomePageStoryMainItem .HomePageStoryItemContent").replaceWith($(".HomePageStorySubItem1").html());
            }
        }

        //        $(".FAQ a.Question").bind("click", function(e) {
        //            var par = $(this).parent();
        //            $(par).find("div.Answer").toggle();
        //            return false;
        //        });
        //        $(".DMSSearch a.DMSAdvancedSearchLink").bind("click", function(e) {
        //            var par = $(this).parent();
        //            $(par).find("div.DMSAdvancedSearchOptions").toggle();
        //            return false;
        //        });

        $("a.photoGallery").bind("click", function(e) {
            var flashvars = {};
            flashvars.paramXMLPath = "http://www.unityhouston.org/App_Themes/UnityChurch/flash/param.xml";
            flashvars.xmlFilePath = $(this).attr("href");

            var params = {};

            params.mediaPlayerScale = "1";
            params.contentAreaBackgroundAlpha = "1";
            params.contentAreaBackgroundColor = "0x000000";
            params.galleryBackgroundColor = "0x000000";
            params.allowfullscreen = "true";

            var attributes = {};
            attributes.id = "photogallery";
            swfobject.embedSWF("/app_common/flash/slideshowpro.swf", "photogallery", "808px", "456px", "9.0.0", false, flashvars, params, attributes);
            tb_show('', '#TB_inline?width=808&height=486&inlineId=galleryContent&modal=true');
            return false;
        });

        //        $("a.calculator").bind("click", function(e) {
        //            var href = $(this).attr("href");
        //            var title = $(this).attr("title");
        //            tb_show(title, href);
        //            return false;
        //        });
        //        $("input#SearchResultState1").bind("click", function(e) {
        //            $("span.ResultItem").show();
        //        });
        //        $("input#SearchResultState2").bind("click", function(e) {
        //            $("span.ResultItem").hide();
        //        });
        //        $("a.CloseDialog").bind("click", function(e) {
        //            //var htmlData = '<div id="galleryContent" style="background-color: #000;"><div id="TB_caption">Photo Gallery</div><div id="TB_closeWindow"><a href="#" id="TB_closeWindowButton" title="Close" class="CloseDialog">close</a></div><div id="photogallery"></div></div>';
        //            //$("div#galleryContainer").html(htmlData);
        //        });

    }
);

/* ******************************************************************************************* */
/* General Support */
/* ******************************************************************************************* */
function OnChangeQuicklinks(dropdown)
{
    top.location.href = dropdown.value;
    return true;
}

function detectOnState(ele, className) {
    var onObject = document.getElementById(ele);
    if (navigator.appName.indexOf("Microsoft") != -1)
        onObject.className = className;
    else
        onObject.setAttribute("class", className);
}
function detectOffState(ele, className) {
    var OffObject = document.getElementById(ele);
    if (navigator.appName.indexOf("Microsoft") != -1)
        OffObject.className = className;
    else
        OffObject.setAttribute("class", className);
}