﻿/// <reference path="jquery-1.6.2.min.js" />
/// <reference path="jquery-ui-1.8.15.min.js" />
/// <reference path="jquery.corner.js" />

// init namespace
var bs = bs | { };


// ie doesn't do indexOf
if (!Array.prototype.indexOf) {
    Array.prototype.indexOf = function (obj) {
        for (var i = 0; i < this.length; i++) {
            if (this[i] == obj) {
                return i;
            }
        }
        return -1;
    };
}

// round a number to .0 or .5
function roundToHalf(value) {
    var converted = parseFloat(value); // Make sure we have a number 
    var decimal = (converted - parseInt(converted, 10));
    decimal = Math.round(decimal * 10);
    if (decimal == 5) { return (parseInt(converted, 10) + 0.5); }
    if ((decimal < 3) || (decimal > 7)) {
        return Math.round(converted);
    } else {
        return (parseInt(converted, 10) + 0.5);
    }
}
function roundNumber(orig, decimalPlaces) {
    return Math.round(orig * Math.pow(10, decimalPlaces)) / Math.pow(10, decimalPlaces);
}
 

// site wide
$(document).ready(function () {
    // round corners on the site
    $(".pagerounded").corner("round 10px").parent().corner("round 14px");
    $(".rounded").corner();

    // pearl gallery hover
    $('.pearlGalleryItem').hover(
    function() {
        $(this).addClass("pearlGalleryItemHighlight");
        var focus = $(".pearlGalleryItemFocus");
        if (focus.length > 0) {
            focus.html($("a:first", this).attr("title"));
        }
    },
    function() {
        $(this).removeClass("pearlGalleryItemHighlight");
        var focus = $(".pearlGalleryItemFocus");
        if (focus.length > 0) {
            focus.html('');
        }
    });

    // search autocomplete
    var searchBox = $('#searchText');
    if (searchBox.length != 0) {
        searchBox
            .autocomplete({
                minLength: 3,
                delay: 50,
                source: function (request, response) {
                    $.post('/Search/AutoComplete', { term: request.term }, function (data, textStatus) {
                        if (textStatus == 'success') {
                            response(data);
                        }
                    }, "json");
                },
                selectFirst: true,
                select: function (event, ui) {
                    location.href = '/Venue/Show/' + ui.item.ID;
                }
            })
            .data("autocomplete")._renderItem = function (ul, item) {
                return $("<li></li>")
				        .data("item.autocomplete", item)
				        .append("<a>" + item.MatchText + "</a>")
				        .appendTo(ul);
            };
    }
});



