jQuery.noConflict(); // use jQuery instead of $ from here on 
var $j = jQuery;
$j(document).ready(function() {

    // Addthis
    $j('div#breadcrumb').prepend('<a href="http://www.addthis.com/bookmark.php?v=250" id="add-this" onmouseover="return addthis_open(this, \'\', \'[URL]\', \'[TITLE]\')" onmouseout="addthis_close()" onclick="return addthis_sendto()"><img src="http://s7.addthis.com/static/btn/lg-bookmark-en.gif" width="125" height="16" alt="Bookmark and Share" style="border:0"/></a><script type="text/javascript" src="http://s7.addthis.com/js/250/addthis_widget.js?pub=sumac"></script>');

    // Form backgrounds
    if ($j('#search-box').length) {
        $j('#search-box fieldset input').addClass('search').removeClass('textbox');
        if ($j('input.username').length > 0) {
            InputBackgrounds($j('input.username'), 'bgUsername.gif');
            InputBackgrounds($j('input.password'), 'bgPassword.gif');
        }
        InputBackgrounds($j('input.search'), 'bgSearch.gif');
    }

    $j('input.username').blur(function() {
        $j(this).removeClass('focus');
        InputBackgrounds($j(this), 'bgUsername.gif');
    });
    $j('input.password').blur(function() {
        $j(this).removeClass('focus');
        InputBackgrounds($j(this), 'bgPassword.gif');
    });
    $j('input.search').blur(function() {
        $j(this).removeClass('focus');
        InputBackgrounds($j(this), 'bgSearch.gif');
    });
    /*IE :focus fix*/
    $j('input.username').focus(function() {
        $j(this).addClass('focus');
    });
    $j('input.password').focus(function() {
        $j(this).addClass('focus');
    });
    $j('input.search').focus(function() {
        $j(this).addClass('focus');
    });

    // Flat forms
    if ($j('#add-user').length) {
        if ($j('#add-user div.alert').css('display') == 'none') {
            $j('#add-user div.form.flat input[type="text"]').labelswitch();
        }
    }
    if ($j('#search-users').length) {
        if (!$j('#search-users div.alert').length) {
            $j('#search-users div.form.flat input[type="text"]').labelswitch();
        }
    }

    // Form input return handling
    $j('input.search').keypress(function(e) {
        if (e.which == 13) {
            $j('input#SearchInput_btnSearch').click();
            return false;
        }
    });
    $j('input.password').keypress(function(e) {
        if (e.which == 13) {
            $j('input#ucLogIn_btnLogIn').click();
            return false;
        }
    });
    $j('input#txtSearchTerms, select#ddlBorough').keypress(function(e) {
        if (e.which == 13) {
            $j('input#btnSearchDirectory').click();
            return false;
        }
    });

    // Scrolling 
    if (window.location.pathname == "/directory.aspx" && window.location.search.length > 0) {
        ScrollTo($j('div#results'));
    }
    if (window.location.pathname == "/organisation.aspx" && window.location.hash.length > 0) {
        $j('a[name=' + window.location.hash.replace('#', '') + ']').parent().addClass('emphasis');
        ScrollTo($j('a[name=' + window.location.hash.replace('#', '') + ']'));
    }


    // Equal Heights
    var highest = 0;
    $j('div.equal').each(function() {
        if ($j(this).height() > highest) {
            highest = $j(this).height();
        }
    });
    $j('div.equal').height(highest + 15);

    // SlideShow
    if ($j('ul.slideshow').length > 0) {
        $j('ul.slideshow').each(function() {
            $j(this).innerfade({
                speed: 'slow',
                timeout: 2000,
                type: 'sequence',
                containerheight: 250
            });
        });
    }

    // FlashVideo
    if ($j('#video-player').length > 0) {
        var flashvars = {};
        flashvars.videoSource = $j('.media a.src').attr('rel');

        if (window.location.search.indexOf('play=true') > 0) {
            flashvars.setAutoPlay = 'true';
        } else {
            flashvars.setAutoPlay = 'false';
        }

        var params = {};
        params.scale = "noscale";
        params.quality = "high";
        params.wmode = "transparent";
        params.allowfullscreen = "true";
        params.allowScriptAccess = "sameDomain";
        params.align = "middle";
        params.bgcolor = "#000000";

        var attributes = {};
        attributes.id = "video-player";
        attributes.name = "video-player";

        swfobject.embedSWF("flash/minimal-white-01.swf", "video-player", "587", "320", "9.0.45", "flash/expressInstall.swf", flashvars, params, attributes);
    }

    // FlashMap
    if ($j('#flash-map').length > 0) {
        var flashvars = {};
        flashvars.BoroughPage = '';

        var params = {};
        params.scale = "noscale";
        params.quality = "high";
        params.wmode = "transparent";
        params.allowfullscreen = "true";
        params.allowScriptAccess = "sameDomain";
        params.align = "middle";
        params.bgcolor = "#000000";

        var attributes = {};
        attributes.id = "video-player";
        attributes.name = "video-player";

        swfobject.embedSWF("flash/sel-map.swf", "flash-map", "582", "466", "9.0.45", "flash/expressInstall.swf", flashvars, params, attributes);
    }
    
    // HexMap
    if($j('div#hexmapbox').length > 0){
        $j('div#hexmapbox').append('<a id="hoverbox" href="#">hover</a>')
        var ext = '.png';
            if ($j.browser.msie && $j.browser.version.substr(0,1)<7) { ext = '.gif'; }
        var map;
        var $hoverbox = $j('a#hoverbox');
        $hoverbox.hide();
        var boxX = 0;
        var boxY = 0;
        var bgImg = '';
        $j('.modal').hide();
        $j('body').mouseover(function(e) {
            if ($j(e.target).is(':not(map#hexmap area, a#hoverbox)')) {
             $hoverbox.hide();
            }
        }); 
        $hoverbox.mouseover(function(){
            $hoverbox.attr('style','left: '+boxX+ 'px; top: ' + boxY + 'px;background-image: url('+ bgImg +');')
        });
        $hoverbox.click(function(){    
            $j('div#modal').remove();
            modal($j($hoverbox.attr('href')).html());
            return false;
        })
        $j('div#modal a.close').live('click',function(){                
            $j('div#modal').remove();
            return false;
        });
        $j(document).keyup(function(e){
            if (e.keyCode == 27) {
               $j('div#modal').remove();
            }
        });

         $j('map#hexmap area').mouseover(function(e){
             var coords = $j(this).attr('rel').split(',');
             boxX = coords[0];
             boxY = coords[1];
             bgImg = pathprefix + 'images/hex-' + $j(this).attr('href').replace('#','') + ext;
             $hoverbox
                 .show()
                 .attr('style','left: '+boxX+ 'px; top: ' + boxY+'px;background-image: url('+ bgImg +');')
                 .attr('href',$j(this).attr('href'))
                 .attr('title',$j(this).attr('title'));
             $hoverbox.show();    
         }).mouseout(function(){
             $hoverbox.hide();
         });
     }
});
function modal(content){            
    $j('div#hexmapbox').append('<div id="modal" style="margin-left: -202px;">' +
        '<a href="#" class="close">close</a>' +
        '<div class="content">' + 
            content +
        '</div>' +
        '<div class="footer">&#160;</div>' +
    '</div>');            
    $j('div#modal').css('marginTop',-$j('div#modal').height()/2);
}
 
function InputBackgrounds($t, imgName) {
   if($t.attr('value').length > 0) {
      $t.css('background-image','none');      
   } else if(imgName.length == 0) {
   } else {
      $t.css('background-image','url('+pathprefix+'images/'+ imgName + ')');
   }
}
function ScrollTo($t) {
    var y = $t.offset().top;
    if($j('html,body').scrollTop() < y) {
        $j('html,body').stop().animate({scrollTop: y-20}, 1000, 'swing');
    }
}