$(document).ready(function() {

    /*-----------------------------------------
    Preloading some stuff
    -----------------------------------------*/
    jQuery.preloadImages = function() {
        for (var i = 0; i < arguments.length; i++) {
            jQuery("<img>").attr("src", arguments[i]);
        }
    };
    jQuery.preloadImages(
        "images/logintab-left_on.png",
        "images/logintab-left_off.png",
        "images/logintab-right_on.png",
        "images/logintab-right_off.png",
        "images/logintab-middle_L.png",
        "images/logintab-middle_R.png"
    );

    /*-----------------------------------------
    Engaging Cufon for H1 and H2 tags
    -----------------------------------------*/
    Cufon.replace('.headlines h2');
    Cufon.replace('.headlines h1');
    Cufon.replace('.middle h1');
    Cufon.replace('.sub_homepage form .container #content .column h2');

    /*-----------------------------------------
    Popup modal for old browsers
    -----------------------------------------*/
    $("#outdatedbrowserlink").fancybox({
        'width': 763,
        'height': 694,
        'overlayShow': true,
        'autodimensions': false,
        'autoscale': false
    });

    /*-----------------------------------------
    Popup modal for online banking downtime
    -----------------------------------------*/
    $(".inlinepopuplink").fancybox({
        'modal': true,
        'width': 763,
        'height': 694,
        'overlayShow': true,
        'autodimensions': false,
        'autoscale': false
    });

    /*-----------------------------------------
    Popup modal for online Locations Page details
    -----------------------------------------*/
    $("a.lightbox").fancybox({
        'width': 763,
        'height': 694,
        'overlayShow': true,
        'autodimensions': false,
        'autoscale': false,
        'type': 'iframe'

    });

    /*-----------------------------------------
    Browser test for unsupported browser(s)
    -----------------------------------------*/
    // if (BrowserDetect.browser == "Firefox") {
    if ((BrowserDetect.browser == "Explorer") && (BrowserDetect.version < 7) && ($.cookie("CBank_BrowserNagTimeout") == null)) {
        //$("#outdatedbrowserlink").trigger('click');
        window.location = "http://" + window.location.host + "/upgrade-browser";
        var options = { path: '/', expires: 1 };
        $.cookie("CBank_BrowserNagTimeout", 'BrowserNagTimeout', options);
    }

    /*-----------------------------------------
    MLogin Window		
    -----------------------------------------*/
    //Login window tabs
    $(".homepage #login").tabs({
        select: function(event, ui) {
            var $tabs = $("#login").tabs();
            var selected = $tabs.tabs('option', 'selected');
            if (selected == "1") {
                $("#login ul").toggleClass("righttabactive lefttabactive");
            } else {
                $("#login ul").toggleClass("lefttabactive righttabactive");
            }
        }
    });

    /*-----------------------------------------
    Accordions
    -----------------------------------------*/
    $("#accordion").accordion({ autoHeight: false, navigation: true });
    $(".accordion").accordion({ autoHeight: false, navigation: true });
    $(".locationaccordion").accordion({ autoHeight: false, navigation: true });


    /*-----------------------------------------
    Various behaviors
    -----------------------------------------*/
    $("#tabs").tabs();

    $("#ctl00_Hero_ctl00_personalUsername").focus(function() {
        if ($(this).attr('value') == "User ID") {
            $(this).attr('value', '');
        }
    });
    $("#ctl00_Hero_ctl00_personalUsername").blur(function() {
        if (($(this).attr('value') == "")) {
            $(this).attr('value', 'User ID');
        }
    });

    $('#ctl00_Hero_ctl00_personalUsername').keypress(function(e) {
        if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13)) {
            $("#ctl00_Hero_ctl00_loginPersonal").click();
            return false;
        }
    });

    $('#ctl00_Hero_ctl00_personalUsername').keypress(function(e) {
        if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13)) {
            $("#ctl00_Hero_ctl00_personalPopup").click();
            return false;
        }
    });

    /*-----------------------------------------
    Marquee Slideshow
    -----------------------------------------*/
    //initiate cycle slideshow plugin
    $('#slideshow').cycle({
        pager: '#nav',
        timeout: 10000,
        speed: 0,

        // callback fn that creates a thumbnail to use as pager anchor 
        pagerAnchorBuilder: function(idx, slide) {
            return '<a href="#">&nbsp;</a>';
        }
    });

    //pause slideshow whenever user clicks on a slide
    $('.slideshowitem a').click(function() {
        $('#slideshow .slides').cycle('pause');
    });

    //initiate flowplayer plugin on all links on the page that link to a .mov file
    //$("a[href*=.mov]").flowplayer("/scripts/flowplayer/flowplayer-3.1.5.swf", {
    $("a[href*=.mov]").flowplayer("/scripts/flowplayer/flowplayer.commercial-3.2.1.swf", {
        key: '#$412c5428a8b4a4f70c0',
        clip: {
            scaling: 'scale'
        },
        plugins: {
            controls: {
                //hide-show controls on unhover-hover
                autoHide: 'always',

                //hide all controls and only show play,volume,mute,scrubber, & fullscreen
                all: false,
                play: true,
                volume: true,
                mute: true,
                scrubber: true,
                fullscreen: true,

                //controlbar styling
                timeColor: '#01DAFF',
                volumeSliderColor: '#000000',
                tooltipColor: '#5F747C',
                backgroundGradient: 'low',
                volumeSliderGradient: 'none',
                buttonOverColor: '#3b3b3b',
                tooltipTextColor: '#ffffff',
                sliderGradient: 'none',
                sliderColor: '#fafafa',
                borderRadius: '0',
                buttonColor: '#6b6b6b',
                backgroundColor: '#000000',
                progressColor: '#303030',
                bufferColor: '#4f4f4f',
                timeBgColor: '#555555',
                bufferGradient: 'none',
                durationColor: '#ffffff',
                progressGradient: 'medium',
                height: 24,
                opacity: 0.8

            }
        },

        // perform custom stuff before default click action
        onBeforeClick: function() {

            // unload previously loaded player 
            $f().unload();

            // get wrapper element as jQuery object 
            var wrap = $(this.getParent());

            //fade starting image out before loading video player
            wrap.find("img").fadeOut(1000);

            $("#slideshow #nav").fadeOut(1000);
            $("#slideshow .slideshowitem .video").width(411);
            $("#slideshow .slideshowitem .video").height(254);
            $("#slideshow .slideshowitem .video").css("margin-right", "10px");

            //animate flowplayer to full size of container element
            wrap.animate({ width: "100%", height: "100%" }, 1000, function() {

                // when animation finishes we load our player 
                $f(this).load();

            });

            //ignore default click action and do above stuff instead
            return false;
        },

        //when loading the flash video player do this stuff
        onLoad: function() {
            $("#slideshow").cycle('pause');
            //show "play" btn (in case this is the second video the user is playing)
            this.getPlugin("play").show();

        },

        // when playback finishes do this stuff 
        onFinish: function() {

            //hide the "replay video" btn
            this.getPlugin("play").hide();

            //fade out the entire player via container element
            $(this.getParent()).fadeOut(1000, function() {

                //swap player with original image
                $f().unload();

            });
        },

        // unload action resumes to original state         
        onUnload: function() {

            $("#slideshow #nav").fadeIn(1000);
            $("#slideshow .slideshowitem .video").width(421);
            $("#slideshow .slideshowitem .video").height(269);
            $("#slideshow .slideshowitem .video").css("margin-right", "0px");

            //fade in previous hidden container element
            $(this.getParent()).fadeIn(1000, function() {

                //start the slideshow again
                $("#slideshow").cycle('resume');

            });
        }

    });

    //unload the video if user clicks on the slideshow page nav
    $('#nav a').click(function() {
        $f().unload();
    });

    $("a").filter(function() {
        return this.hostname && this.hostname !== location.hostname
        && this.hostname != "www.snl.com"
        && this.hostname != "www.pcsbanking.net"
        && this.hostname != "www.onlinebanktours.com"
        && this.hostname != "tbe.taleo.net"
        && this.hostname != "columbiabank.webcashmgmt.com";
    }).click(function() {
        return confirm(offsiteNotice);
    });

    $("#popupcontent a").attr("target", "_blank");

    $("a").filter(function() {
        return this.hostname && this.hostname !== location.hostname && this.hostname == "www.snl.com";
    }).click(function() {
        return confirm(offsiteNoticeSNL);
    });
	
	$("#ctl00_Header_ctl01_search").keypress(function (e) {
		if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13)) {
			$('#ctl00_Header_ctl01_searchButton').click();
			return false;
		} else {
			return true;
		}
	}); 
});
