////////////////////////////////////////////////////////////////////////////////////////////////////////////
// saythetime.js
// Copyright 2012 Provenio Software Corporation. All rights reserved.
// http://www.saythetime.com/
////////////////////////////////////////////////////////////////////////////////////////////////////////////

// Google Analytics
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-216609-7']);
_gaq.push(['_trackPageview']);

var addthis_offset_top = 25, addthis_offset_left = -200;

(function() {

    $(document).ready(function() {

        var i = 0;
        var flipping = function() {
            $('.flipper > span').eq(i).fadeOut();
            i = (i + 1) % 6;
            $('.flipper > span').eq(i).fadeIn();
            window.setTimeout(flipping, 2750);
        }

        if ($('.flipper').length) {
            window.setTimeout(flipping, 2750);
        }

        // Slimbox
        var flickrimages = $("a[href^='http://www.flickr.com/photos/'] > img:first-child[src]");
        if (flickrimages.length) {

            $.ajax({ url: '/js/slimbox2.js', dataType: 'script', cache: true, complete: function() {
                flickrimages.parent().slimbox({}, function(el) {
                    return [el.firstChild.src.replace(/_[mts]\.(\w+)$/, ".$1"), (el.title || el.firstChild.alt) + '<br /><a href="' + el.href + '">Flickr page</a>'];
                });
            }
            });
        }

        $("#contact").click(function() {

            var cf = $("#contact-form");

            if (cf.length === 0) {

                cf = $('<div id="contact-form" title="Contact Us"></div>');
                $('body').append(cf);

                cf.load('/contact-us.htm', function() {

                    $.getScript('http://www.google.com/recaptcha/api/js/recaptcha_ajax.js', function() {
                        Recaptcha.create("6LcPKwcAAAAAAL3U5-uyrOClADIWpUdfj6_rQcrV", "captcha", { theme: "white", callback: Recaptcha.focus_response_field });
                    });

                    cf.dialog({
                        autoOpen: false,
                        height: 600,
                        width: 500,
                        modal: true,
                        buttons: {
                            "Submit": function() {
                                var name = cf.find("#name"), email = cf.find("#email"), comment = cf.find("#comment"), captcha = cf.find("#recaptcha_response_field");
                                var x = /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i;

                                if (name.val().length < 3) {
                                    name.focus();
                                    return;
                                }

                                if (email.val().length < 10 || !x.test(email.val())) {
                                    email.focus();
                                    return;
                                }

                                if (comment.val().length < 10) {
                                    comment.focus();
                                    return;
                                }

                                if (captcha.val().length === 0) {
                                    captcha.focus();
                                    return;
                                }

                                $.post('/contact-us', cf.find("form").serialize(), function(result) {
                                    if (!result.success) {
                                        Recaptcha.reload();
                                        captcha.focus();
                                    }
                                    else {
                                        cf.dialog("close");
                                        alert("Your message has been sent. Thank you!");
                                    }
                                }, 'json');
                            }
                        },
                        close: function() { }
                    });

                    cf.dialog("open");
                });

            }
            else {
                $("#contact-form").find('input, textarea').val('');
                Recaptcha.reload();
                cf.dialog("open");
            }

            return false;
        });

        if ($("#features").length) {
            $(window).bind('hashchange', function(e) {
                var hash = window.location.hash.match('\\w.+\\w');

                $('#features .sh').removeClass('sh hi').addClass('hi');

                if (hash) {
                    $(window.location.hash).addClass('sh');
                    $('.' + hash).addClass('sh');
                }

            });

            $(window).trigger('hashchange');
        }

        $("#accordion").accordion({ autoHeight: false, animated: false, collapsible: true, active: false });

        if ($("#btn-buy").length) {

            $("#dialog").dialog({
                autoOpen: false, resizable: false,
                width: 440, modal: true, closeOnEscape: false,
                buttons: {
                    'Yes. Mail me a Backup CD.': function() { window.location = storeURL + "&backupcd=1"; },
                    'No. I don\'t need a CD.': function() { window.location = storeURL; }
                },
                close: function(event, ui) { window.location = storeURL; }
            });

            $("#btn-buy").click(function() {
                $("#dialog").dialog("open");
                return false;
            });
        }

        // Adwords tracking
        $("#btn-download").one('click', null, function() {
            $.getScript("http://www.googleadservices.com/pagead/conversion/1072694267/?label=KGTMCKe5mAEQ-4fA_wM&amp;guid=ON&amp;script=0");
        });

        (function() {
            var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
            ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
            (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(ga);
        })();

        // Facebook
        if ($('.fb-like').length) {
            (function(d, s, id) { var js, fjs = d.getElementsByTagName(s)[0]; if (d.getElementById(id)) return; js = d.createElement(s); js.id = id; js.src = "//connect.facebook.net/en_US/all.js#xfbml=1&appId=188252131239355"; fjs.parentNode.insertBefore(js, fjs); } (document, 'script', 'facebook-jssdk'));
        }

        // Share widget
        if ($('.addthis_toolbox').length) {
            $.ajax({ url: 'http://s7.addthis.com/js/250/addthis_widget.js#pubid=provenio', dataType: 'script', cache: true, complete: function() { } });
        }
    });

} ());
