/*
	Copyright DTDigital         :: www.dtdigital.com.au ::
	Unauthorised modification / use is a criminal offence, and
	will be prosecuted to the fullest extent permitted by law.
	All Rights Reserved
*/

// jQuery functions that run on "dom ready"

$(function () {
    // Makes links open in new window when they have rel=external
    $("a[rel=external]").attr("target", "_blank");
    // Use below method if using thickbox, matches class rather than rel
    $("a.external-link").attr("target", "_blank");

    HiddenFields();

});

// clear input on focus
$(".clear-value").focus(function () {
    if ($(this).val() == this.defaultValue) {
        $(this).val("");
    }
});

// if field is empty afterward, add text again
$(".clear-value").blur(function () {
    if ($(this).val() == "") {
        $(this).val(this.defaultValue);
    }
});


// go to previous page
$('a.back').click(function () {
    parent.history.back();
    
    return false;
});

function HiddenFields() {
	
	$('.hidden-fields-container').hide();

	$('input:radio').each(function() {

	    if ($(this).is(':checked')) {
	        var currentSelection = $(this).attr('title');
	        $('.hidden-fields-container').hide();
	        $('.fields-container_' + currentSelection).toggle();
	        //$('.btn_' + currentSelection).toggle();
	    }

	    $(this).click(function() {
	        var currentSelection = $(this).attr('title');
	        $('.hidden-fields-container').hide();
	        $('.fields-container_' + currentSelection).toggle();
	        //$('.btn_' + currentSelection).toggle();
	    });
	});

	$('.location-prompt > input:text').keydown(function(event) {
	    if (event.keyCode == 13) {
	        if ($('input[type="radio"]:checked').attr('title') == 'postcode') {
	            $('input.btn_postcode').click();
	        }
	        else {
	            $('input.btn_suburb').click();
	        }
	    }
	});
}

var searchURLStart;
function FixSearchSubmit(searchURL) {
    searchURLStart = searchURL;
    $('.searchbox input:text').keypress(function (e) {
        console.log(e.which);
        if (e.which == 13) {
            e.stopPropagation();
            LaunchSearchQuery();
        }
    });

    $('.searchbox input:text').focusin(function (e) {
        hasEnterBeenPressed = false;
    });
        
    $('.searchbox input:text').focusout(function (e) {
        if (hasEnterBeenPressed)
        {
            LaunchSearchQuery();
        }
    });

    $('.searchbox input:text').keypress(function (e) {
        hasEnterBeenPressed = e.keyCode == 13;
    });

    $('#search > form').submit(function () {
        
        if ($('.searchbox input:text').val() == 'Product Search') {
            return false;
        }
    });
}

function LaunchSearchQuery()
{
    var query = $('.searchbox input:text').val();
    console.log(searchURLStart + '/' + query);
    if (query.length > 0) {
        //location.href = searchURLStart + query;
    }
}

// site menu rollout toggle	
function toggle(a) {
	var b=document.getElementById(a);
	if(b.style.display=="block") {
		b.style.display="none"
	} else {
		b.style.display="block"
	}
}
// site menu toggle class on the button
$(".menu-btn").click(function(e) {
	$(e.target).toggleClass("menu-active");
});

