/** required on all pages **/
$(document).ready(function(){
							
	//show login							
	$("a.bt-login").click(function() {
		showLogin("a.bt-login");
	});			
	
	//hide login
	$("#page-overlay").click(function() {
	    hideLogin("a.bt-login");
	    hideUserMenu("a.bt-usermenu");
	});

	//show user menu
	$("a.bt-usermenu").click(function() {
	    showUserMenu("a.bt-usermenu");
	});
	
});



/*********************************************** FUNCTIONS  ***********************************************/


/** homepage hero slides **/
var currentHomeFeature = 1;
var transitionDelay = 700;
var slideDuration = 1000;
var showHideDuration = 150;
var homeFeatureCount;
var intervalVar;
var isFirstInitClick = true;

$(document).ready(function() {
    init();
});

function init() {
    slideDuration = $('#slideDuration').val();
    slideDuration = parseInt(slideDuration);
    if (!slideDuration) {
        slideDuration = 10;
    } else {
        slideDuration = slideDuration * 1000;
    };

//    // clear interval on hover
//    $("div.hero-slides").hover(function() {
//        clearInterval(intervalVar);
//    }, function() {
//            startSlideshow();
//    });

    // feature navigation
    $("ul.slide-nav > li > a").click(function() {
        if (isFirstInitClick == true) { // slide starts at the first click
            isFirstInitClick = false;
            startSlideshow();
        }
        else {
            clearInterval(intervalVar); // stop slideshow
        }
        
        this.blur();
        var currentId = this.href.split("#");
        currentId = parseInt(currentId[1]);

        fadeHomeFeatures(currentHomeFeature, currentId);
        updateCurrent(currentId);
        //startSlideshow(); // resume slideshow
        return false;
    });

    // auto slideshow
    homeFeatureCount = $(".slide").length;


    $('#tab-1').click();
    //startSlideshow();
};

function startSlideshow() {
	if (homeFeatureCount > 1) {
		intervalVar = setInterval("advanceSlideshow()", slideDuration);
	}
}

function advanceSlideshow() 
{	
	// determine what next slide is
	if (currentHomeFeature == homeFeatureCount) {
		nextSlide = 1;
	} else {
		nextSlide = currentHomeFeature + 1;
	}
	
	fadeHomeFeatures(currentHomeFeature, nextSlide);
	updateCurrent(nextSlide);	
}

function fadeHomeFeatures(oldNum, newNum) {
	$("#side-tab-" + oldNum).fadeOut(transitionDelay);
	$("#side-tab-" + newNum).fadeIn(transitionDelay)
}

function updateCurrent(newNum) {		
	$("#currentCount").text(newNum.toString());
	currentHomeFeature = newNum;
	
	$(".slide-text").hide();
	$(".slide").hide();
	$("#slide-tab-" + newNum).show();
	$("#slide-tab-" + newNum).find(".slide-text").fadeIn("slow");	
	
	$(".hero-link").removeClass("on");		
	$("ul.slide-nav li a#tab-" + newNum).addClass("on");	
}

/*
function showSlide(slide)
{
	$(".hero-link").removeClass("on");
	$(".slide").hide();
	$(".slide-text").hide();
	
	var strTab = "";
	var strSlide = "";
	
	if (slide == "")
	{
		strTab = "#tab-01";
		strSlide = "#slide-tab-01";
	}
	else
	{
		strTab = "#" + slide;
		strSlide = "#slide-" + slide;
	}
	
	$(strTab).addClass("on");
	$(strSlide).show();
	$(strSlide).find(".slide-text").fadeIn("slow");	
}*/


/** homepage spotlight tabs switcher **/
function switchHomeTab(h2, tab) 
{
    //buttons
	$(".tabs").find("h2").removeClass("on");	
    $(".tabs").find(h2).addClass("on");    

    //tabs
    $(".tab").hide();
    $(tab).fadeIn("slow");	
}


/** form tooltips **/
function tooltip(target_items, name){
	$(target_items).each(function(i) {
		$("body").append("<div class='" + name + "' id='" + name + i + "'>" + $(this).attr('title') + "<div class='tooltip-bottom'></div>");
		var my_tooltip = $("#" + name + i);

		$(this).removeAttr("title").mouseover(function() {
			my_tooltip.show();
		}).mousemove(function(kmouse) {
			my_tooltip.css({ left: kmouse.pageX + 25, top: kmouse.pageY + 1 });
		}).mouseout(function() {
			my_tooltip.hide();
		});
	});
}


/** used to show login box **/
function showLogin(button) 
{
	var loginButton = $(button);
	var loginBox = $(".login-box");
	var loginContent = $(".login-content");
	var loginLoading = $(".login-loading");
	var pageOverlay = $("#page-overlay");
	
	if (!loginBox.is(":visible"))
	{
		//open
	    //pageOverlay.show();
	    pageOverlay.hide();
		
		loginButton.toggleClass("on");
		loginLoading.show();
		loginBox.slideToggle("slow");

		setTimeout("$(\".login-loading\").hide();", 700);
		setTimeout("$(\".login-content\").fadeIn(\"slow\");", 750);
		setTimeout("$(\"#ctl00_SiteLogin1_loginControl_UserName\").focus();", 800);
	}
	else
	{
		//close
		hideLogin(button);
	}
}

/** used to hide login box **/
function hideLogin(button)
{
	var loginButton = $(button);
	var loginBox = $(".login-box");
	var loginContent = $(".login-content");
	var loginLoading = $(".login-loading");
	var pageOverlay = $("#page-overlay");
	
	loginLoading.hide();
	pageOverlay.hide();
	loginContent.fadeOut("slow");
	loginBox.slideToggle("slow");
	loginButton.toggleClass("on");
}




/** used to clear default form values **/
function clearDefault(field, value)
{
	if (field.value == value)
		field.value = "";
}


/** used for font-size functions **/
var currentSize = "";
var smallSize = "80%";
var largeSize = "115%";
function reduceTextSize(DivName)
{
	var size;
	size = ( currentSize == "" || currentSize == "100%" || currentSize == smallSize )? smallSize : "100%";	
	
	var divObj = new getObj(DivName);
	divObj.style.fontSize = size;
	currentSize = size;
}
function enlargeTextSize(DivName) 
{
	var size;
	size = ( currentSize == "" || currentSize == "100%" || currentSize == largeSize )? largeSize : "100%";
	
	var divObj = new getObj(DivName);
	divObj.style.fontSize = size;
	currentSize = size;
}
function getObj(name) 
{
	if (document.getElementById) {
		this.obj = document.getElementById(name);
		if (this.obj) this.style = document.getElementById(name).style;
	} else if (document.all) {
		this.obj = document.all[name];
		if (this.obj) this.style = document.all[name].style;
	} else if (document.layers) {
		if (document.layers[name]) {
			this.obj = document.layers[name];
			if (this.obj) this.style = document.layers[name];
	   } else {
			this.obj = eval("document.layers." + name + ".layers[name]");
			if (this.obj) this.style = eval("document.layers." + name + ".layers[name]");
	   }
	}
}

function redirectTo(id) {
    window.location = $('#' + id).val();
};

function ValidateCheckBoxes(chkClientID) {
    var containerId = chkClientID;
    var containerRef = document.getElementById(containerId);
    var inputRefArray = containerRef.getElementsByTagName('input');
    for (var i = 0; i < inputRefArray.length; i++) {
        var inputRef = inputRefArray[i];
        if (inputRef.type.substr(0, 8) == 'checkbox') {
            if (inputRef.checked == true) {
                return true;
            }
        }
    }
    return false;
}

function ValidateAddress(source, args) {
    var defaultValue = 'Address 1';
    var strText = args.Value;
    if (strText.length == 0 || strText == defaultValue) {
        args.IsValid = false;
    }
    else {
        args.IsValid = true;
    }
    return;
}

function ValidateSuburb(source, args) {
    var defaultValue = 'Suburb';
    var strText = args.Value;
    if (strText.length == 0 || strText == defaultValue) {
        args.IsValid = false;
    }
    else {
        args.IsValid = true;
    }
    return;
}

function ValidateCity(source, args) {
    var defaultValue = 'City';
    var strText = args.Value;
    if (strText.length == 0 || strText == defaultValue) {
        args.IsValid = false;
    }
    else {
        args.IsValid = true;
    }
    return;
}
