﻿/// <reference path="jquery-intellisense.js" />

if (typeof SiteBuilder == 'undefined') {
	SiteBuilder = {};
}

// Make <input type="file"> nicer, by changing it into some form of button that auto submits.
SiteBuilder.stylizeFileInputs = function() {
    // If we support stylized file inputs, mark the HTML so the CSS can act.
	$('html').addClass('sb-styled-file-input');

    // When the file input changes...
	$(':file').change(function() {
	    // ...find the submit button.
		var next = this.parentNode.nextSibling;

		while (typeof next.name == 'undefined') {
			next = next.nextSibling;
		}

		$('.sb-uploading').css('display', 'block');

        // Force a post-back to upload the file.
		__doPostBack(next.name, '');
	});
}

// Handler that detects the enter key being pressed and postsback on the given ID.
SiteBuilder.checkEnterKey = function(e, id) {
	var key = e.keyCode || e.which;

	if (key == 13) {
		e.preventDefault();
		__doPostBack(id, '');
	}
}

SiteBuilder.executeLink = function(href) {
    if (href.substring(0, 11) == 'javascript:') {
        href = href.substring(11);
    }
    
    eval(href);
}

// Create a Virtual Earth map on every sb-map-canvas element.
SiteBuilder.createVirtualEarthMap = function() {
    $('.sb-map-canvas').each(function(i) {
        // Find the data labels.
        var address = $(this).find('.sb-address-label').text();
        var orgName = $(this).find('.sb-org-name-label').text();

        // http://msdn.microsoft.com/en-us/library/bb429586.aspx
        var map = new VEMap(this.id);

        map.LoadMap();
        map.HideDashboard();

        // http://msdn.microsoft.com/en-us/library/bb429645.aspx
        // Lookup the address, and when found...
        map.Find(null, address, null, null, 0, 1, true, true, true, true, function() {
            // ...zoom in and set a map marker.
            map.SetZoomLevel(16);

            var shape = new VEShape(VEShapeType.Pushpin, map.GetCenter());

            shape.SetTitle(orgName);
            shape.SetDescription(address);

            map.AddShape(shape);
        });
    });
}

// On document.ready, create lightboxes for anchors marked as such.
$(function() {
    $('a[rel*=lightbox]').lightBox({
        imageLoading: '!!/_Serving/StaticFile/Scripts/Lightbox/lightbox-ico-loading.gif',
        imageBtnClose: '!!/_Serving/StaticFile/Scripts/Lightbox/lightbox-btn-close.gif',
        imageBtnPrev: '!!/_Serving/StaticFile/Scripts/Lightbox/lightbox-btn-prev.gif',
        imageBtnNext: '!!/_Serving/StaticFile/Scripts/Lightbox/lightbox-btn-next.gif',
        imageBlank: '!!/_Serving/StaticFile/Scripts/Lightbox/lightbox-blank.gif'
    });
});
