﻿/*jslint bitwise: true, browser: true, eqeqeq: true, immed: true, newcap: true, nomen: true, onevar: true, plusplus: true, regexp: false, undef: true, white: true, indent: 4 */
/*global analogue, animate, lightbox*/

// Fix png images for IE6
if (analogue.msie && analogue.msie.version === 6) {
    document.addEvent('ready', function () {
        var element, elements = document.getElementsByTagName('img'), i;
        for (i = 0; (element = elements[i]); i += 1) {
            if (element.src && (/adt\.png$/.test(element.src))) {
                element.style.zoom = '1';
                element.runtimeStyle.filter = 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src=' + element.src + ', sizingMethod=scale)';
                element.src = '/assets/img/clear.gif';
            }
        }
    });    
}

// 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';
        }
    }
});

// Initialize the contact form and associated links
document.addEvent('ready', function () {
    var click = function (event) {
        if (document.get('name')) {
            document.location.href = '#contact';
            document.get('name').focus();
            event.preventDefault();
        };
    };
    document.body.insertBefore(document.set('div', {'id': 'overlay'}), document.get('container').nextSibling);
    document.get('#nav li.contact a')[0].addEvent('click', click);
    document.get('a.contact')[0].addEvent('click', click);
    document.get('inquiry').addEvent('submit', function (event) {
        var content = this.serialize(), form = this, request = new XMLHttpRequest();
        content += ('&ajax=' + new Date().getTime());
        if (form.getElementsByClassName('isInvalid').length > 0) {
            return false;
        }
        request.onreadystatechange = function () {
            if (request.readyState === 4 && request.status === 200) {
                form.removeClass('loading');
                form.innerHTML = request.responseText;
            } else {
                form.innerHTML = '<h5><b>Contact</b> HomeStar Security Today!</h5><p>Submitting your information... please wait!</p>';
                form.addClass('loading');
            }
        };
        request.open(this.method.toUpperCase(), this.action, true);
        request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8'); 
        request.send(content);
        window._gaq.push(['_trackEvent', 'Contact Form', 'Submitted', document.location.pathname]);
        event.preventDefault();
    });
});

// Initialize the equimpment list scroller
document.addEvent('ready', function () {
    if (document.get('equipment')) {
        var divs = document.get('#scroller div') || [], equipment = document.get('equipment'), scroller = document.get('scroller'), totalHeight = (279 * divs.length);
        equipment.insertBefore(document.set('a', {id: 'scrollUp', href: '#', onclick: function (event) {
            var callback = arguments.callee, scroll, target = this;
            if (scroller.offsetTop >= 0) {
                this.style.visibility = 'hidden';
                return event.preventDefault();
            }
            document.get('scrollDown').style.visibility = 'visible';
            this.removeEvent('click', callback).style.visibility = 'visible';
            scroll = animate(scroller.style, 'top', animate.easeOut, scroller.offsetTop, (scroller.offsetTop + 279), 1.2, 'px');
            scroll.start();
            scroll.onfinish = function () {
                target.addEvent('click', callback);
                if (scroller.offsetTop >= 0) {
                    document.get('scrollUp').style.visibility = 'hidden';
                }
            };
            event.preventDefault();
        }, style: 'visibility:hidden;', title: 'Scroll Up…'}, 'PREVIOUS'), equipment.firstChild);
        equipment.appendChild(document.set('a', {id: 'scrollDown', href: '#', onclick: function (event) {
            var callback = arguments.callee, scroll, target = this;
            if (((scroller.offsetTop * -1) + 279) >= totalHeight) {
                this.style.visibility = 'hidden';
                return event.preventDefault();
            }
            document.get('scrollUp').style.visibility = 'visible';
            this.removeEvent('click', callback).style.visibility = 'visible';
            scroll = animate(scroller.style, 'top', animate.easeOut, scroller.offsetTop, scroller.offsetTop - 279, 1.2, 'px');
            scroll.start();
            scroll.onfinish = function () {
                target.addEvent('click', callback);
                if (((scroller.offsetTop * -1) + 279) >= totalHeight) {
                    document.get('scrollDown').style.visibility = 'hidden';
                }
            };
            event.preventDefault();
        }, title: 'Scroll Down…'}, 'NEXT'));
    }
});

// Manually fire DomContentLoaded event
document.ready();

// AddThis
(function (at, s) {
    window.addthis_config = {
        services_exclude: 'email,print',
        username: 'ra-4f0b585711bbea92',
        ui_cobrand: 'HomeStar Security'
    };
    at.type = 'text/javascript';
    at.async = true;
    at.src = ('https:' == document.location.protocol ? 'https:' : 'http:') + '//s7.addthis.com/js/250/addthis_widget.js#pubid=ra-4f0b585711bbea92';
    s.parentNode.insertBefore(at, s);
}(document.createElement('script'), document.getElementsByTagName('script')[0]));

// Google Anayltics
(function(ga, s) {
    ga.type = 'text/javascript';
    ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    s.parentNode.insertBefore(ga, s);
}(document.createElement('script'), document.getElementsByTagName('script')[0]));
