//GA//
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-25423891-1']);
_gaq.push(['_trackPageview']);
_gaq.push(['_trackPageLoadTime']);

(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';
    var s = document.getElementsByTagName('script')[0];
    s.parentNode.insertBefore(ga, s);
})();
//GA//
//FB//
(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_GB/all.js#xfbml=1";
  fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
//FB//


Cufon.replace('.cufon');

$(document).ready(function() {

    function preload(arrayOfImages) {
        $(arrayOfImages).each(function() {
            $('<img/>')[0].src = this;
        });
    }

    preload([
        'images/solar-panels-quote.png',
        'images/solar-panels-quote-submit-on.png',
    ]);

    $('#postcode')
        .focus(
        function() {
            if ($(this).val() == $(this).attr('rel')) {
                $(this).val('');
            }
            $(this).toggleClass('empty-value', false);
        }).blur(
        function() {
            if ($(this).val() == '' || $(this).val() == $(this).attr('rel')) {
                $(this).val($(this).attr('rel'));
                $(this).toggleClass('empty-value', true);
            }
        }).blur();

    $('.tabs a').click(function(e) {

        e.preventDefault();

        $('.tabs div.selected').removeClass('selected');
        $(this).parent().addClass('selected');

        var selector = $(this).attr('href').replace('#', '.');
        $('.information .tab-content').hide();
        $(selector, '.information').show();
    })

    $('.postcode .submit').hover(function() {
        $(this).addClass('submit-hover');
    }, function() {
        $(this).removeClass('submit-hover');
    })

    $('.navigation a')
        .hover(function(e) {
            var bg = $(this).css('background-image').replace('-off', '-on');
            $(this).css('background-image', bg);
        }, function(e) {
            var bg = $(this).css('background-image').replace('-on', '-off');
            $(this).css('background-image', bg);
        });

    $('.benefits .section-link')
        .click(function(e) {
            e.preventDefault();
        });

    $('.benefits .section')
        .hover(
        function(e) {
            $(this).addClass('section-hover');
        },
        function(e) {
            $(this).removeClass('section-hover');
        }).click(function(e) {
            e.preventDefault();
            window.location.href = $(this).find('.section-link').attr('href');
        });

    $.validator.addMethod("requirePostCode", function(value, element, param) {
        return /^[0-9]{4,4}$/.test(value);
    }, 'Please enter your postcode');

    $.validator.addMethod(
        "regex",
        function(value, element, regexp) {
            var check = false;
            var re = new RegExp(regexp);
            return this.optional(element) || re.test(value);
        },
        "Please check your input."
    );


    $('.quote form').validate({
        onkeyup: false,
        rules: {
            name: {
                required: true,
                regex: /^[^0-9]{2,}$/
            },
            address: {
                required: true,
                regex: /^.{2,}$/
            },
            phone: {
                required: true,
                regex: /^0[^a-zA-Z]{7,}$/
            },
            email: {
                required: true,
                email: true
            },
            postcode: {
                required: true,
                regex: /^[0-9]{4,4}$/
            },
            type: { required: true },
            timeframe: { required: true }
        },
        errorPlacement: function(error, element) {
            if (element.is(":radio"))
                error.insertBefore(element.parents('li').parents('li').find('label:first'));
            else
                error.insertBefore(element.parents('li').find('label'));
        },
        messages: {
            postcode: 'Please enter your postcode',
            name: 'Please enter your name',
            address: 'Please enter your suburb/street',
            phone: 'Please enter your phone number',
            email: 'Please enter a valid email address',
            type: 'Please choose one of the options',
            timeframe: 'Please choose one of the options'
        }
    });

    $('.postcode form').validate({
        rules: {
            postcode: { requirePostCode: true }
        },
        errorPlacement: function(error, element) {
            error.insertBefore($(':first-child', element.parent()));
        },
        messages: {
            postcode: 'Please enter your postcode'
        }
    });

    $('#quote-mask').width($(window).width());
    //$('#quote-mask').height($(window).height());
});
