﻿/*------------------------------------------------------------------------------------------
    Ingenium Designs, LLC Common Javascript Library

    Author: Adam M. Dille

    June 2011
------------------------------------------------------------------------------------------*/

// Namespaces
var Ingenium = Ingenium || {};
Ingenium.CommonPage = {};
Ingenium.Mapping = {};
Ingenium.Libraries = {};

// Global Variables
var _gaq = _gaq || [];   // Used by Google Analytics

// Page Load
$(function () {
    Ingenium.CommonPage.InitializeInnerLabelInputs();
    Ingenium.CommonPage.InitializeHintLabelInputs();
});

window.log = function () {
    log.history = log.history || [];
    log.history.push(arguments);
    arguments.callee = arguments.callee.caller;
    if (this.console) console.log(Array.prototype.slice.call(arguments));
};
(function (b) { function c() { } for (var d = "assert,count,debug,dir,dirxml,error,exception,group,groupCollapsed,groupEnd,info,log,markTimeline,profile,profileEnd,time,timeEnd,trace,warn".split(","), a; a = d.pop(); ) b[a] = b[a] || c })(window.console = window.console || {});

// ---------------------------- Common Page Related Functions -------------------------------
Ingenium.CommonPage.InitializeHintLabelInputs = function () {
    // On focus out, if the value is empty, put the hint back in the field
    $('div.hint_label :input').keyup(function () {
        if ($.trim($(this).val()).length > 0) {
            $(this).siblings('label').hide();
        }
        else {
            $(this).siblings('label').show();
        }
    });

    $('div.hint_label :input').keyup();
};

Ingenium.CommonPage.InitializeInnerLabelInputs = function () {
    $('div.inner_label input').keyup(function () {
        if ($.trim($(this).val()).length > 0) {
            $(this).siblings('label').children('span').hide();
        }
        else {
            $(this).siblings('label').children('span').show();
        }
    });

    $('div.inner_label label').each(function () {
        $(this)
            .html('<span>' + $(this).html() + '</span>')
            .siblings('input').keyup();
    });
};

Ingenium.CommonPage.AddAsyncScriptTag = function (url) {
    $('<script />', {
        type: 'text/javascript',
        async: true,
        src: url
    }).appendTo("head");
};

// ---------------------------- Mapping Related Functions -------------------------------
Ingenium.Mapping.GeocodeAddress = function (address, callback) {
    if (Ingenium.Libraries.NoGoogleMaps())
        return;

    var geocoder = new google.maps.Geocoder();

    geocoder.geocode({ 'address': address }, function (results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            callback(results[0].geometry.location.lat(), results[0].geometry.location.lng());
        } else {
            alert("Geocode was not successful for the following reason: " + status);
        }
    });
};

Ingenium.Mapping.MapLocation = function (inLatitude, inLongitude, inMapSelector, inMapOptions) {
    if (Ingenium.Libraries.NoGoogleMaps())
        return;

    var mapDIV = $(inMapSelector);

    if (mapDIV != undefined && mapDIV.length) {
        var location = new google.maps.LatLng(inLatitude, inLongitude);

        inMapOptions = Ingenium.Mapping.SetMapOptions(inMapOptions);
        inMapOptions.center = location;

        var map = new google.maps.Map(mapDIV.get(0), inMapOptions);

        var marker = new google.maps.Marker({
            position: location,
            map: map
        });
    }
};

Ingenium.Mapping.MapLocations = function (inLocations, inMapSelector, inMapOptions) {
    if (Ingenium.Libraries.NoGoogleMaps())
        return;

    var mapDIV = $(inMapSelector);

    if (mapDIV != undefined && mapDIV.length && inLocations != undefined && inLocations.length) {
        inMapOptions = Ingenium.Mapping.SetMapOptions(inMapOptions);

        if (inLocations.length == 1) {
            Ingenium.Mapping.MapLocation(inLocations[0].lat, inLocations[0].lng, inMapSelector, inMapOptions);
        }
        else {
            var map = new google.maps.Map(mapDIV.get(0), inMapOptions);

            var bounds = new google.maps.LatLngBounds();

            for (var ndx in inLocations) {
                var location = new google.maps.LatLng(inLocations[ndx].lat, inLocations[ndx].lng);

                bounds.extend(location);

                var marker = new google.maps.Marker({
                    position: location,
                    map: map
                });
            }

            map.fitBounds(bounds);
        }
    }
};

Ingenium.Mapping.SetMapOptions = function (inMapOptions) {
    var inMapOptions = inMapOptions || {};

    inMapOptions.zoom = inMapOptions.zoom || 14;
    inMapOptions.mapTypeId = inMapOptions.mapTypeId || google.maps.MapTypeId.ROADMAP;
    inMapOptions.streetViewControl = inMapOptions.streetViewControl || false;

    return inMapOptions;
};

// ---------------------------- Third Party Library Loading Functions -------------------------------

Ingenium.Libraries.NoJQuery = function () {
    if (typeof jQuery == 'undefined') {
        alert('JQuery is not currently loaded');
        return true;
    }
};

Ingenium.Libraries.LoadGoogleAnalytics = function (inAccount, inDomainName, inAllowLinker) {

    if (inAccount != undefined) {
        _gaq.push(['_setAccount', inAccount]);
    }
    else {
        alert('You must pass an Account ID to Ingenium.Libraries.LoadGoogleAnalytics');
        return false;
    }

    if (inDomainName != undefined) {
        _gaq.push(['_setDomainName', inDomainName]);
    }

    if (inAllowLinker != undefined) {
        _gaq.push(['_setAllowLinker', inAllowLinker]);
    }

    _gaq.push(['_trackPageview']);

    var src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';

    Ingenium.CommonPage.AddAsyncScriptTag(src);
};

Ingenium.Libraries.LoadGoogleMaps = function (callback) {
    var src = ('https:' == document.location.protocol ? 'https://maps-api-ssl.google.com' : 'http://maps.google.com') + '/maps/api/js?v=3&sensor=false&callback=' + callback;

    Ingenium.CommonPage.AddAsyncScriptTag(src);
};

Ingenium.Libraries.NoGoogleMaps = function () {
    if (typeof google == 'undefined' || typeof google.maps == 'undefined') {
        alert('Google Maps API is not currently loaded');
        return true;
    }
};

Ingenium.Libraries.ImportFacebook = function (appID) {
    if (appID == undefined) {
        alert('You must pass an App ID to Ingenium.Libraries.ImportFacebook');
        return false;
    }

    if (Ingenium.Libraries.NoJQuery()) {
        return false;
    }

    $('body').append('<div id="fb-root"></div>');

    $('html').attr('xmlns:fb', 'http://www.facebook.com/2008/fbml');

    window.fbAsyncInit = function () {
        FB.init({
            appId: appID,
            status: true,
            cookie: true,
            xfbml: true
        });
    };

    Ingenium.CommonPage.AddAsyncScriptTag(document.location.protocol + '//connect.facebook.net/en_US/all.js');
};

Ingenium.Libraries.ImportDigg = function () {
    Ingenium.CommonPage.AddAsyncScriptTag('http://widgets.digg.com/buttons.js');
};

Ingenium.Libraries.ImportTwitter = function () {
    Ingenium.CommonPage.AddAsyncScriptTag('http://platform.twitter.com/widgets.js');
};

Ingenium.Libraries.ImportLinkedIn = function () {
    Ingenium.CommonPage.AddAsyncScriptTag('http://platform.linkedin.com/in.js');
};
