﻿window.$ = jQuery;

var SG = function() {
    return {
        nullGuid: '00000000-0000-0000-0000-000000000000'
    };
} ();


SG._sendRequest = function(o) {
    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        url: o.url,
        data: o.data,
        success: o.success || SG._ajaxSuccess,
        error: o.error || SG._ajaxError
    });
};

SG._ajaxError = function(x, s, e) {
    if ($.browser.msie) {
        alert('AJAX Error');
    }
    else {
        console.log('AJAX Error');
    }
};

SG._ajaxSuccess = function(d, s) {
};

/*
    Click handler events are passed a single parameter which is a jQuery object for the element that was clicked to trigger 
    the action.  If the function returns a true the default behavior of the element will be stopped while returning false will 
    allow the default behavior to continue.  As an example returning true will prevent a hyperlink with a url of # from triggering
    a screen refresh, returning true will also prevent a checkbox from being checked, a drop down list from expanding, etc.  
    
    clickHandlerClass1: function($e) {
        return true; // Prevents the default behavior of the element that was clicked.
    },
    clickHandlerClass2: function($e) {
        return false; // Allows the default behavior of the element that was clicked.
    }
*/
SG.clickHandler = {
    activate: function() {
        $(document).bind('click', function(e) {//master click listener
            var pd = false;
            e = e || window.event;
            if (e.target.className && e.target.className.length > 0 && SG.clickHandler.events != null) {
                try {
                    var classNames = e.target.className.split(' ');
                    $.each(classNames, function(i, o) {
                        if (SG.clickHandler.events[o]) {
                            pd = SG.clickHandler.events[o]($(e.target));
                        }
                    });
                } catch (ex) {
                    alert(ex);
                }
            }

            for (var propName in SG.clickHandler.globalEvents) {
                if ($(e.target).hasClass(propName) && !$(e.target).hasClass(propName + 'Abort')) {
                    SG.clickHandler.globalEvents[propName]($(e.target));
                } else if ($(e.target).parents('.' + propName).length > 0 && !$(e.target).parents('.' + propName + 'Abort').length > 0) {
                    SG.clickHandler.globalEvents[propName]($(e.target));
                }
            }

            if (pd)
                e.preventDefault();
        })
    },
    events: null,
    globalEvents: null
};

SG.showLoader = function() { $('.loader').show(); };
SG.hideLoader = function() { $('.loader').hide(); };
SG.unblockIE = function(el, opts) {
    $('.blockUI').remove();
};

SG.IsUndefinedOrNull = function(e) { return (e === undefined || e === null) };

SG.getCookie = function(name) {
    name = name + "=";
    var carray = document.cookie.split(';');
    for (var i = 0; i < carray.length; i++) {
        var c = carray[i];
        while (c.charAt(0) == ' ') c = c.substring(1, c.length);
        if (c.indexOf(name) == 0) return c.substring(name.length, c.length);
    }
    return null;
};

SG.browserSupportsVirtualEarthMaps = function() {
    // Check if browser meets Virtual Earth Map minimum requirements
    if (($.browser.msie && !!$.browser.versionNumber && $.browser.versionNumber >= 6.0) ||
        ($.browser.mozilla && !!$.browser.versionNumber && $.browser.versionNumber >= 2.0) ||
        ($.browser.safari && !!$.browser.versionNumber && $.browser.versionNumber >= 412))
        return true;
    return false;
};

SG.browserSupportsGoogleMaps = function() {
    // Check if browser meets Google Maps minimum requirements
    if (($.browser.msie && !!$.browser.versionNumber && $.browser.versionNumber >= 6.0) ||
        ($.browser.mozilla && !!$.browser.versionNumber && $.browser.versionNumber >= 2.0) ||
        ($.browser.safari && !!$.browser.versionNumber && $.browser.versionNumber >= 525))
        return true;
    return false;
};

SG.showMessage = function(msg) {
    if (!!msg && msg.toString().length > 0) {
        var $message = $('.MessageBox').find('.message').html(msg).end();
        $.blockUI({ message: $message, width: '700px', fadeOut: 0 });
        $message = null;
    }
}

SG.courseFeaturesToolTipContent = [
            "IntelliGreen&reg; Pro (Beta) is a dynamically-enhanced version of IntelliGreen&reg; that adds the ability to display major tiers, contours, false fronts and mounds from your angle of approach to the green. With IntelliGreen Pro, you’ll always know what’s ahead, no matter where you are on the hole... even if you can’t see the green!<br /><br /><i>Devices:</i><br />SG5, SG4, SG3.5, SG3, SG2.5<br /><br /><a href=\'/support/course%20features/intelligreenproinfo.aspx\'>Learn More...</a>",
            "SkyGolf’s patented IntelliGreen&reg; technology provides you with an eagle’s view of each green from anywhere on the golf course. The graphic rotates to provide front carry, back yardages, and green depth from your approach angle.<br /><br /><i>Devices:</i><br />SG5, SG4, SG3.5, SG3, SG2.5, SG2<br /><br /><a href=\'/support/course%20features/intelligreeninfo.aspx\'>Learn More...</a>",
            "SkyCaddie mappers walk every course recording up to 40 targets per hole, giving golfers the most reliable and complete maps in the game.<br /><br /><i>Devices:</i><br />SG5, SG4, SG3.5, SG3, SG2.5, SG2<br /><br /><a href=\'/support/course%20features/truegroundinfo.aspx\'>Learn More...</a>",
            "Courses mapped and submitted by SkyCaddie members that generally include distances to the front, center, and back of each green.<br /><br /><i>Devices:</i><br />SG5, SG4, SG3.5, SG3, SG2.5, SG2, SG1<br /><br /><a href=\'/support/course%20features/membermappedcourseinfo.aspx\'>Learn More...</a>",
            "SkyCaddie’s HoleVue&trade; (Beta) displays a graphical image of a hole using SkyGolf’s reliable TrueGround&trade; mapping process. HoleVue availability is extremely limited during the Beta testing period. Upon a successful Beta test, this feature will be gradually included on more courses.<br /><br /><i>Devices:</i><br />SG5, SG3.5<br /><br /><a href=\'/support/course%20features/fullgraphicholevueinfo.aspx\'>Learn More...</a>",
            "SkyCaddie’s exclusive QuickVue&trade; provides a virtual preview of the safest path from the tee to green. At a single glance, golfers know the exact green shape with IntelliGreen&reg;, the total distance from tee to the green, the shape of the golf hole, the distance and direction to the perfect fairway position and the distance remaining from that position for the approach shot to the green.<br /><br /><i>Devices:</i><br />SG5, SG4, SG3.5, SG3<br /><br /><a href=\'/support/course%20features/virtualholeinfo.aspx\'>Learn More...</a>"
    ];

String.prototype.trim = function() {
    return this.replace(/^\s+|\s+$/g, "").toString();
};
String.prototype.ltrim = function() {
    return this.replace(/^\s+/, "").toString();
};
String.prototype.rtrim = function() {
    return this.replace(/\s+$/, "").toString();
};