var $J = jQuery.noConflict();

jQuery(document).ready(function() {


    //I'm looking to scroller
    if ($J('.scroller ul').length > 0) {
        temp = $J('.scroller ul li').length * 165;
        $J('.scroller ul').css('width', temp + "px");
    }

    // Quick Links
    if ($J("#quick-links").length > 0) {
        $J("#quick-links").change(function() {
            if ($J('#quick-links option:selected').val() != "")
                location.href = $J('#quick-links option:selected').val();
        });
    }

    // Sliding banners
    if ($J("#slider-container").length > 0) {
        $J("#slider").cycle({ timeout: '10000', fx: 'scrollHorz', next: '.next', prev: '.prev', pager: '.pager', pause: true });
        $J("#slider-container").hover(
			    function() { $J('.next,.prev', this).show() },
			    function() { $J('.next,.prev', this).hide() }
		    );
    }

    // Collapsable footer links
    $J(".nav-sitemap .column > ul > li > span").each(function() {
        el = $J(this).parent('li');
        if ($J('ul', el).length > 0)
            $J(this).addClass('plus');
    });

    $J(".nav-sitemap .column > ul > li > span").click(function() {
        el = $J(this).parent('li');
        $J('ul', el).toggle();
        $J(this).toggleClass('plus');
        $J(this).toggleClass('minus');
        return false;
    });

    // Replace REL attribute on Input fields
    $J('input[rel!=""]').each(function() {
        if ($J(this).val() == '') {
            $J(this).val($J(this).attr('rel'));
        }
        $J(this).focus(function() {
            if ($J(this).val() == $J(this).attr("rel")) {
                $J(this).val('');
            }
        });
        $J(this).blur(function() {
            if ($J(this).val() == "") {
                $J(this).val($J(this).attr("rel"));
            }
        });
    });

    waterMark();

    //search
    $J("#search-link").click(function() {
        doSearch();
        return false;
    });

    $J("input.input-search").bind({
        keyup: function(event) {
            var code = (event.keyCode ? event.keyCode : event.which);
            if (code == 13)
                return $J("#search-link").click();
        }
    });

    //add class to ATM Location
   
    var wrapHeight = 100;
    $J(".atm-location-wrap").each(function() {
        if ($J(this).find(".atm-location").height() > wrapHeight) {
            wrapHeight = $J(this).find(".atm-location").height();
        }
        $J(this).height(wrapHeight);
    });
});


function doSearch() {
    var searchTerm = $J("input.input-search").val();

    if (searchTerm.length == 0) {
        return;
    }

    if (searchTerm != $J("input.input-search").attr("title")) {
        var url = encodeURI($J("#search-link").attr("href").concat(searchTerm));
        window.location = url;
    }
}

function waterMark() {
    // clear hint on focus
    $J(".watermark").bind({
        focusin: function() {
            var formField = $J(this),
				formFieldVal = formField.val(),
				formFieldTitle = formField.attr("title");
            if (formFieldVal == formFieldTitle) {
                formField.val("");
            }
        },
        focusout: function() { // if empty string re-add hint
            var formField = $J(this),
				formFieldVal = formField.val(),
				formFieldTitle = formField.attr("title");

            if (formFieldVal == "") {
                formField.val(formFieldTitle);
            }
        }
    }).each(function(index, el) {
        if ($J(el).val() === "") {
            $J(el).val($J(el).attr("title"));
        }
    });
}

function activateImage(index) {
    var images = $J('.gallery-overlay .image img');
    $J(images).removeClass('active');
    $J(images[index]).addClass('active');
}

// return index of active class
function getActiveClass(searchParam) {
    var a = $J(searchParam);
    var l = a.length;
    for (i = l; i--; ) {
        if ($J(a[i]).hasClass('active')) {
            return i;
        };
    }
    return false;
}

// remove a class name from all items
function removeFromAll(searchParam, c) {
    var a = $J(searchParam);
    var l = a.length;
    for (i = l; i--; ) {
        $J(a[i]).removeClass(c);
    }
}

// Insert SWF into page
function insertVideo(width, height, flashPlayerVersion) {
    allVideos = $J('.insertVideoPlayer');
    for (i = 0; i < allVideos.length; i++) {
        myVideoContainer = $J(allVideos)[i];
        videoUrl = $J(myVideoContainer).attr('rel');
        videoId = $J(myVideoContainer).attr('id');
        insertHtml = "<script type='text/javascript'>"
                   + " var so = new SWFObject('/assets/video/player/videoPlayer.swf', 'mpl', '" + width + "', '" + height + "', '" + flashPlayerVersion + "');"
                   + " so.addParam('allowfullscreen', 'false'); "
                   + " so.addParam('allowscriptaccess', 'always'); "
                   + " so.addParam('wmode', 'opaque'); "
                   + " so.addVariable('fileName', '" + videoUrl + "'); "
				   + " so.addVariable('skinPath', '/assets/video/player/SteelOverAll.swf'); "
                   + " so.addVariable('autostart', 'false'); "
                   + " so.write('" + videoId + "'); "
                + " </script>";
        $J(myVideoContainer).after(insertHtml);
    }
}

