﻿/****************************************************************************************	
	[GLOBAL JQUERY FUNCTIONS]
*****************************************************************************************/

// Sensis web search in the navbar
$(function() {
    $('#navbar_sensisSearchForm').keypress(function(e) {
        if (e.which == 13 && e.target.type != 'textarea') {
            window.location.href = 'http://www.sensis.com.au/search.do?partnerid=ssn106&find=' + $('#navbar_searchField').val();
            return false;
        }
    });
});

// menu
$(document).ready(function() {
    $("#nav-search ul").superfish({ delay: 400, animation: { height: "show" }, speed: "fast", autoArrows: false });
});

// homepage hero
$(function() {
    if ($("div#promo").length > 0) {
        var heroImage = $("div#promo ul li p.hero")[0].innerHTML; // grab the <a>
        $($("div#promo ul li")[0]).addClass("active");
        $("div#promo").prepend($(heroImage).attr("id", "hero"));

        $("div#promo ul li").each(function(i) {
            $(this).mouseover(function() {
                $("div#promo ul li").removeClass("active");
                var currentImage = $("div#promo a#hero");
                heroImage = $(this).find("p.hero")[0].innerHTML;
                if (currentImage.outerHTML != heroImage) {
                    $("div#promo a#hero").remove();
                    $("div#promo").prepend($(heroImage).attr("id", "hero"));
                }
                $(this).addClass("active");
            });
        });
    }
});

// contact form
$(function() {
    var $div = $("#main-content #content fieldset div#contact-existing-cust");
    var isExistingCustomer = ($("#main-content #content fieldset .radio-existing-cust input:radio[value='yes']:checked").length > 0);
    if (isExistingCustomer)
        $div.show();
    else
        $div.hide();
    $("#main-content #content fieldset .radio-existing-cust input:radio").click(function() {
        var v = $(this).val();
        if (v == "yes") {
            $div.fadeIn("slow");
        }
        else {
            $div.fadeOut("slow");
        }
    });
});

// Enable Forms to be Submitted via ENTER key
var AreaSelector = "#content, .search";
var ButtonSelector = "input[type='submit'],input[type='image'],button";
$(document).ready(function() {
    jQuery.each($(AreaSelector), function() {
        $(this).keypress(function(e) {
            if (e.which == 13 && e.target.type != 'textarea') {
                var arrItems = $(this).find(ButtonSelector);
                if (arrItems.length > 0) {
                    $(this).find(ButtonSelector)[0].click();
                }
                return false;
            }
        });
    });
});

// Clear text input values
var swap_text_boxes = [];
$(document).ready(function() {
    jQuery.each($("input[type='text'].autoclear"), function() {
        swap_text_boxes[$(this).attr('id')] = $(this).attr('value');
        $(this).bind('focus', function() {
            if ($(this).val() == swap_text_boxes[$(this).attr('id')]) {
                $(this).val('');
            }
        });
        $(this).bind('blur', function() {
            if ($(this).val() == '') {
                $(this).val(swap_text_boxes[$(this).attr('id')]);
            }
        }); 
    });
});

// Insert WAI-ARIA Landmark Roles
$(document).ready(function() {
    // HEADER
    $("#header").attr("role", "banner");
    // Search
    $(".search").attr("role", "search");
    // NAVIGATION
    $("#nav,#subnav").attr("role", "navigation");
    // MAIN CONTENT
    $("#content").attr("role", "main");
    // ASIDE
    $("#aside").attr("role", "complementary");
    // FOOTER
    $("#footer").attr("role", "contentinfo");
});


