jQuery(document).ready(function($) {
    InitDefault();
    $('.movies a').click(function( e ) {
        /* Track downloads of SWFs with Google Analytics event tracking */
        _gaq.push(['_trackEvent', 'Videos', 'Play', $(e.target).attr('href')]);
        window.open('/content/movie.html?movie=' + $(e.target).attr('href') + '&title=' + $(e.target).html(), 'movie', 'width=805,height=605,resizable=1,scrollbars=1');
        return false;
    });

    /* Track downloads of PDFs with Google Analytics event tracking */
    $('a[href$=pdf]').click(function(e) {
        _gaq.push(['_trackEvent', 'Downloads', 'PDF', $(e.target).attr('href')]);
        return true;
    });

    /* Contact Us form validation */
    $('#contactus form').validate({
        submitHandler: function(form) {
            /* Track contact us form submissions with Google Analytics event tracking */
            _gaq.push(['_trackEvent', 'Contact Us', 'Submitted']);
            form.submit();
        },
        invalidHandler: function(e, validator) {
            $.each(validator.errorMap, function(key, value) {
                _gaq.push(['_trackEvent', 'Contact Us', 'Error', value]);
            });
            validator.showErrors();
        },
        rules: {
            company_name: 'required',
            contact_name: 'required',
            email_address: {
                required: true,
                email: true
            },
            phone_number: 'required'
        },
        messages: {
            company_name: 'Please enter your company name',
            contact_name: 'Please enter your contact name',
            email_address: 'Please enter your email address',
            phone_number: 'Please enter your phone number'
        }
    });
    $('#contactus form input[name=notspam]').val('true');

    var cbs = {
        t: 'trial',
        c: 'callback',
        m: 'moreinfo'
    };

    function hashChange() {
        var hash = window.location.hash.substring(1) || 't';

        for (var char in cbs) {
            var els = $('#contactus input[name=' + cbs[char] + ']');
            els.attr('checked', false);
        }

        for (var i = 0; i < hash.length; i++) {
            var char = hash.split('')[i];
            if (char in cbs) {
                var els = $('#contactus input[name=' + cbs[char] + ']');
                if (els.length == 0) {
                    break;
                } else {
                    els.attr('checked', 'checked');
                }
            } else {
                break;
            }
        }


        // partner products click throughs
        $('.partner-products a.' + hash + ':first').click();
    }

    $('a.trial, a.callback, a.moreinfo').click(function() {
        setTimeout(hashChange, 1);
    });

    $('#smxsaasplatform .product-information a').click(function(e) {
        if (e.target) {
            var target = $(e.target);
            var links = target.is('a') ? target : target.parents('.partner-products a');
            if (links.length > 0) {
                var ref = links[0].className;
                var toShow = $('a[name=' + ref +']').parents('.partner-point:hidden');
                if (toShow.length > 0) {
                    $('#smxsaasplatform .selected-product').removeClass('selected-product');
                    $('.partner-point:visible').fadeOut('fast', function() {
                        $('a.' + ref + ' > div').addClass('selected-product');
                        toShow.fadeIn();
                    });
                }
            }
            var top = $('.main-content');
            if (top.length > 0) {
                try {
                    top[0].scrollIntoView();
                } catch(e) {}

            }
        }
        return false;
    });

    $('#hiddencostlink').click(function(e) {
        if (e.target) {
            if ($('#hiddencost').is(':hidden')) {
                $(e.target).html('Close');
                $('#hiddencost').fadeIn();
            }
            else {
                $(e.target).html('Uncover the hidden costs of spam');
                $('#hiddencost').fadeOut();
            }
        }
        return false;
    });

    hashChange();
});
