﻿
$(document).ready(function () {

    var _gaq = _gaq || [];
    _gaq.push(['_setAccount', 'UA-12551491-2']);
    _gaq.push(['_trackPageview']);

    (function () {
        var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
        ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
        var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
    })();


    $('a[rel="external"]').click(function () {
        window.open($(this).attr('href'));
        return false;
    });


    if ($("#main-rotator")[0]) {
        $('#main-rotator').cycle({
            fx: 'fade',
            timeout: 6000,
            delay: -2000,
            cleartypeNoBg: true,
            cssBefore: { zIndex: 1 },
            cssAfter: { zIndex: 0 },
            pager: '#rotator-pager'
        });
    }

    $("#do-search").click(function () {
        var query = $("#search-query").val().trim();
        if (query.length > 0) {
            window.location.href = "/search.aspx?q=" + query;
        }
        return false;
    });


    $("a.video-dialog").click(function () {
        $('#main-rotator').cycle('pause');
        $.fancybox({
            //            'padding': 0,
            'autoScale': false,
            'transitionIn': 'none',
            'transitionOut': 'none',
            'title': this.title,
            'titlePosition': 'inside',
            'width': 480,
            'height': 390,
            'href': this.href.replace(new RegExp("watch\\?v=", "i"), 'v/') + '&autoplay=1',
            'type': 'swf',
            'swf': {
                'wmode': 'transparent',
                'allowfullscreen': 'true'
            },
            'onCleanup': function () {
                $('#main-rotator').cycle('resume');
            }
        });
        return false;

    });


    $('a[rel="video-dialog"]').click(function () {
        $('#main-rotator').cycle('pause');
        $.fancybox({
            //            'padding': 0,
            'autoScale': false,
            'transitionIn': 'none',
            'transitionOut': 'none',
            'title': this.title,
            'titlePosition': 'inside',
            'width': 480,
            'height': 390,
            'href': this.href.replace(new RegExp("watch\\?v=", "i"), 'v/') + '&autoplay=1',
            'type': 'swf',
            'swf': {
                'wmode': 'transparent',
                'allowfullscreen': 'true'
            },
            'onCleanup': function () {
                $('#main-rotator').cycle('resume');
            }
        });
        return false;

    });


    try {
        $('#tag-line-rotator').cycle({
            fx: 'fade',
            timeout: 6000,
            delay: -2000
        });
    }
    catch (err) {
    }


    function megaHoverOver() {
        $(this).find(".sub").stop().fadeTo('fast', 1).show();

        //Calculate width of all ul's
        (function ($) {
            jQuery.fn.calcSubWidth = function () {
                rowWidth = 0;
                //Calculate row
                $(this).find("ul").each(function () {
                    rowWidth += $(this).width();
                });
            };
        })(jQuery);

        if ($(this).find(".row").length > 0) { //If row exists...
            var biggestRow = 0;
            //Calculate each row
            $(this).find(".row").each(function () {
                $(this).calcSubWidth();
                //Find biggest row
                if (rowWidth > biggestRow) {
                    biggestRow = rowWidth;
                }
            });
            //Set width
            $(this).find(".sub").css({ 'width': biggestRow });
            $(this).find(".row:last").css({ 'margin': '0' });

        } else { //If row does not exist...

            $(this).calcSubWidth();
            //Set Width
            $(this).find(".sub").css({ 'width': rowWidth });

        }
    }

    function megaHoverOut() {
        $(this).find(".sub").stop().fadeTo('fast', 0, function () {
            $(this).hide();
        });
    }


    var config = {
        sensitivity: 2, // number = sensitivity threshold (must be 1 or higher)    
        interval: 100, // number = milliseconds for onMouseOver polling interval    
        over: megaHoverOver, // function = onMouseOver callback (REQUIRED)    
        timeout: 500, // number = milliseconds delay before onMouseOut    
        out: megaHoverOut // function = onMouseOut callback (REQUIRED)    
    };

    $("ul#topnav li .sub").css({ 'opacity': '0' });
    try {
        $("ul#topnav li").hoverIntent(config);
    }
    catch (err) {
    }


    $(window).unload(function () {
        //alert('executing...');
        $.ajax({
            url: "/scripts/updateSession.ashx"
        })

    });



});


function contactProgram(c,s) {
    window.location = "mailto:" + c + "@cfsbny.org?subject=" + s;
}




