﻿/*global _gat, GBrowserIsCompatible, GEvent, GLatLng, GMap2, GMarker, GUnload, lightbox, window*//*jslint bitwise: true, browser: true, eqeqeq: true, immed: true, newcap: false, nomen: false, onevar: true, plusplus: true, regexp: true, undef: true, white: true, indent: 4 */

// Make all external links open in a new window
document.addEvent('ready', function () {
    var i, link, links = document.links;
    for (i = 0; (link = links[i]); i += 1) {
        if (!(/mailto:/).test(link.href) && link.hostname.search(location.hostname) === -1 && (/http(s)?:/).test(link.href)) {
            link.target = '_blank';
            link.title = 'Link opens in new window';
        }
    }
});

// Blog archive functionality
document.addEvent('ready', function() {
    if (document.get('blog-archive')) {
        var click = function(event) {
            if (this.hasClass('active')) {
                this.removeClass('active');
                this.parentNode.getElementsByTagName('ul')[0].addClass('hide');
            } else {
                this.addClass('active');
                this.parentNode.getElementsByTagName('ul')[0].removeClass('hide');
            }
            event.preventDefault();
        }, i, link, links = document.get('#blog-archive strong') || [];
        for (i = 0; (link = links[i]); i += 1) {
            link.addEvent('click', click);
        }
    }
});

// Setup social network and bookmarking for blog pages
document.addEvent('ready', function() {
    if (document.get('social')) {
        var description = encodeURIComponent(document.get('meta[name="description"]')[0].getProperty("content")),
            title = encodeURIComponent(document.title),
            url = encodeURIComponent(location.href);
        document.get('social').appendChild(document.set('span', { 'class': 'social' }, [
            document.set('a', { 'class': 'buzz', 'href': 'http://buzz.yahoo.com/buzz?guid=' + url + '&headline=' + title + '&summary=' + description, 'target': '_blank', 'title': 'Buzz up!' }),
            document.set('a', { 'class': 'delicious', 'href': 'http://delicious.com/save?jump=yes&url=' + url + '&title=' + title, 'target': '_blank', 'title': 'Bookmark this on Delicious' }),
            document.set('a', { 'class': 'digg', 'href': 'http://digg.com/submit?url=' + url + '&title=' + title + '&bodytext=' + description + '&media=news&topic=tech_news&thumbnails=0', 'target': '_blank', 'title': 'Digg This' }),
            document.set('a', { 'class': 'facebook', 'href': 'http://www.facebook.com/share.php?u=' + url + '&t=' + title, 'target': '_blank', 'title': 'Share on Facebook' }),
            document.set('a', { 'class': 'google', 'href': 'http://www.google.com/bookmarks/mark?op=add&bkmk=' + url + '&title=' + title, 'target': '_blank', 'title': 'Add to Google Bookmarks' }),
            document.set('a', { 'class': 'stumbleupon', 'href': 'http://www.stumbleupon.com/submit?url=' + url + '&title=' + title, 'target': '_blank', 'title': 'Thumb it up on Stumbleupon' }),
            document.set('a', { 'class': 'twitter', 'href': 'http://twitter.com/?status=' + title + '%20' + url, 'target': '_blank', 'title': 'Tweet This' })
        ]));
    }
});

// Avvo.com Answers iFrame
document.addEvent('ready', function () {
    var div = document.get('avvo_answers');
    if (div) {
        div.appendChild(document.set('iframe', { scrolling: 'auto', src: 'http://www.avvo.com/attorneys/answer_syndication_list/' + div.className + '.html' }));
    }
});

document.addEvent('ready', function () {
    if (document.get('#contact')) {
        if (GBrowserIsCompatible()) {
            var map = new GMap2(document.getElementById("map")), text = '<span class="gmap-title">Pivotal Law Group, PLLC</span><br /><span class="gmap-address">600 University St Ste 1730<br />Seattle WA 98101-3150</span><br /><span class="gmap-directions">Get directions to our office</span><form action="http://maps.google.com/maps" target="_blank"><label for="saddr">Enter your address:</label><br /><input id="saddr" name="saddr" size="20" type="text" /><input name="daddr" type="hidden" value="600 University St Ste 1730, Seattle, WA 98101-3150 @47.610091,-122.331455" /><input type="submit" value="GO" class="gmap-submit" /></form>', marker;
            map.setCenter(new GLatLng(47.610091, -122.331455), 15);
            map.setUIToDefault();
            marker = new GMarker(new GLatLng(47.610091, -122.331455));
            map.addOverlay(marker);
            marker.openInfoWindowHtml(text);
            GEvent.addListener(marker, 'click', function () {
                marker.openInfoWindowHtml(text);
            });
            window.addEvent('unload', GUnload);
        }
    }
});

document.addEvent('ready', function () {
    if (document.get('.gallery')) {
        lightbox.initialize(1, 80, 8, true, 5000);
    }
});

// Manually fire DomContentLoaded event
document.ready();

// Google Anayltics
(function () {
    var ga = document.createElement('script');
    ga.async = true;
    ga.src = ('https:' === document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    ga.type = 'text/javascript';
    (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(ga);
}());
