

function SupplySameAsAbove(elem){
	if ( $(elem).attr('checked') ){
		$(".same-as-above").hide();
	}else{
		$(".same-as-above").show();
	}
}

/**
 * Interface functionality for Aurora website v2
 */
$(function () {

	/** Fancybox Stylesheet */
	(function() {
		var s = document.createElement('link');
		s.href = appPath + '_static/js/libs/fancybox/jquery.fancybox-1.3.4.css';
		s.rel = 'stylesheet';
		document.getElementsByTagName('head')[0].appendChild(s);
	})();

	$('.gallery a').fancybox({
		padding: 3,
		titleShow: false,
		transitionIn: 'none',
		transitionOut: 'none'
	});

	/** Video Player Stylesheet */
	(function() {
		var s = document.createElement('link');
		s.href = appPath + '_static/js/libs/mediaelement/mediaelementplayer.css';
		s.rel = 'stylesheet';
		document.getElementsByTagName('head')[0].appendChild(s);
	})();

	/** http://mediaelementjs.com/ */
	var options = {
	    plugins: ['flash', 'silverlight'],
	    pluginPath: appPath + '_static/js/libs/mediaelement/',
	    flashName: 'flashmediaelement.swf',
	    silverlightName: 'silverlightmediaelement.xap',
	    enablePluginDebug: false,
	    defaultVideoWidth: 520,
	    loop: false,
	    mode: 'shim'
	};
	$('video, audio').mediaelementplayer(options);



    $('.ie6 #sidenav li').hover(function () {
        $(this).addClass('sidenav-hover');
    }, function () {
        $(this).removeClass('sidenav-hover');
    });


    if (typeof wireUpDateField != 'undefined') {
		var datefield = null;
		
		$(".datefield").each(function(i) {
			$(this).click(function(e) {
				datefield = $(this).parent().children('.datefield-icon')[0];
			});
		});
		
        $(".datefield").datepicker({
            dateFormat: 'dd/mm/yy',
			showOn: "both",
			buttonImage: "/_static/images/calendar/icon-calendar.gif",
			buttonImageOnly: true
        });
    }

    $('.banner-slide, .box-ad').each(function () {
        var button = $(this).find('.button');
        $(this).click(function () {
            if (typeof pageTracker != 'undefined') {
                pageTracker._trackPageview(button.attr('href'));
            }

            if (button.attr('rel') == 'external' || button.attr('rel') == 'pdf') {
                // Open in new window
                window.open(button.attr('href'));
            } else {
                // Open in same window
                window.location = button.attr('href');
            }
        });
    });

    /**
    * Image map tooltips
    */
    $('.image-map-tooltips').each(function () {
        $(this).find('area').each(function () {
            $(this).attr('title', $($(this).attr('href')).text()).click(function (e) {
                e.preventDefault();
                return false;
            });
        }).tooltip({
            cursor: true
        });
    });

    $('.tooltip').tooltip({ cursor: false });

    if ($('#page').hasClass('home')) {
        var section = window.location.pathname;
        section = section.replace(appPath, '');
        section = section.replace('default.asp', '');
        section = section + 'default';
        $('#search').after($('<div id="make-this-my-homepage"><a href="' + appPath + '?set_home_route=' + section + '">Make this my home page</a></div>'));
    }

    /**
    * Track PDFs push GA event on all anchors with a REL="pdf" attribute
    * Also open in new window
    */
    $('a[rel="pdf"]').click(function (e) {
        if (typeof pageTracker != 'undefined') {
            pageTracker._trackPageview($(this).attr('href'));
        }
        window.open($(this).attr('href'));
        e.preventDefault();
        return false;
    });

    /**
    * Open links with REL="external" in a new window
    
    $('a[rel="external"]').click(function (e) {
        window.open($(this).attr('href'));
        e.preventDefault();
        return false;
    });
	*/
    if ($('html').hasClass('ie6')) {
        $('#sidenav li').hover(function () {
            $(this).addClass('sidenav-hover');
        },
		function () {
		    $(this).removeClass('sidenav-hover');
		});
    }

    if ($('#banner').length) {
        BannerRotation();
    };

    /**
    * Clear label from search field
    */
    $('#search-query').bind({
        focus: function () {
            $(this).prev('label').hide();
        },
        blur: function () {
            if ($(this).val() == '') {
                $(this).prev('label').show();
            }
        }
    });

    if ($('#search-query').val() != '') {
        $('#search-query').prev('label').hide();
    }

    /**
    * Set column height to equal heights
    * TODO: height is thrown out because the 'PT Sans Narrow' font
    * is loading after the script is called, hence size is thrown out
    */
    $('.row-equal').each(function () {
        var cols = $(this).find('.col');
        var maxHeight = 0;

        cols.each(function () {
            var height = $(this).find('.box-content').height();

            if (height > maxHeight) {
                maxHeight = height;
            }

        }).find('.box-content').css('height', maxHeight);
		
        cols.each(function () {
		
            /* Move buttons to bottom of box */
            $(this).find('.box-content').find('.button-bottom').css({
                bottom: 10,
                position: 'absolute'
            });
        })
		
    });

    /**
	* Forms forms/supply-interruptions-lodgement-request/default.asp
	* same as above action
    */
	$("#StreetSameAsPostal").click(function(){
		SupplySameAsAbove( $("#StreetSameAsPostal") );
	});
	
	SupplySameAsAbove($("#StreetSameAsPostal"));

    /**
	* Forms forms/electricity-connection-form/default.asp
	* same as above action
    */
	$("#MailingSameAsConnection").click(function(){
		SupplySameAsAbove( $("#MailingSameAsConnection") );
	});
	
	SupplySameAsAbove($("#MailingSameAsConnection"));
	
	$CorrespondenceType = $(".CorrespondenceType");
	$CorrespondenceType.click(function(){
		if ( $(this).val() == 'ClaimLetter' )
		{
			$(".claim-details").show();
		}
		else
		{
			$(".claim-details").hide();
		}
	});
	if ( $CorrespondenceType.prop("checked") )
	{
		if ( $CorrespondenceType.val() == 'ClaimLetter' )
		{
			$(".claim-details").show();
		}
		else
		{
			$(".claim-details").hide();
		}
	}
	
	$ConcessionCardNumber = $("#ConcessionCardNumber");
	$ConcessionCard = $(".ConcessionCard");
	$ConcessionCard.click(function(){
		if ( $(this).val() == 'Yes' )
		{
			$(".no-concession").show();
			if ( $ConcessionCardNumber.val() == 'Not required' )
			{
				$ConcessionCardNumber.val('');
			}
		}
		else
		{
			$(".no-concession").hide();
		}
	});
	if ( $ConcessionCard.prop("checked") )
	{
		if ( $ConcessionCard.val() == 'Yes' )
		{
			$(".no-concession").show();
			if ( $ConcessionCardNumber.val() == 'Not required' )
			{
				$ConcessionCardNumber.val('');
			}
		}
		else
		{
			$(".no-concession").hide();
		}
	}
	
    /*$('.ie7 .button').each(function () {
        $('<span class="button-right"></span>').appendTo($(this));
    });*/

});

/**
 * Homepage banner rotation
 * Adds transition and navigation to homepage banner
 */
function BannerRotation() {
	var self = this;

	var delay = 8000; 				// Delay (ms) between transitions
	var duration = 850; 			// Transition duration (ms)
	var transition = true;

	var banner = $('#banner');
	var slides = banner.find('.banner-slide');
	var nav = $('<ul id="banner-nav"></ul>');

	var timer, links, progress;

	/**
	 * If there are 1 or less slides, we don't need to transition
	 */
	if (!slides.length || slides.length <= 1) {
		return false;
	}

	/**
	 * Banner transition
	 * @param int num The slide number to switch to
	 */
	this.swap = function(num) {
		// Make sure there's no timer duplication
		clearTimeout(timer);

		if (progress) {
			return false;
		}

		progress = true;

		// Update active nav link
		links.removeClass('banner-nav-active');
		$(links[num - 1]).addClass('banner-nav-active');

		// Transition?
		if (!transition) {
			slides.removeClass('banner-slide-active');
			$(slides[num - 1]).addClass('banner-slide-active');
			progress = false;
		} else {
			// Fade new slide in
			$(slides[num - 1]).addClass('banner-slide-next').fadeIn(duration, function() {
				// Hide old banner
				banner.find('.banner-slide-active').hide().removeClass('banner-slide-active');
				$(slides[num - 1]).removeClass('banner-slide-next').addClass('banner-slide-active');
				progress = false;
			}).find('.banner-slide-content').hide().fadeIn(1500);
		}

		// Work out what the next slide is
		var next = (typeof slides[num] == 'undefined' ? 1 : num + 1);

		timer = setTimeout(function(){this.swap(next)}, delay + duration);
	}

	/**
	 * Initialises the banner and starts transition timer
	 */
	this.init = function() {

		slides.first().addClass('banner-slide-active');

		for (var i = 1; i <= slides.length; i++) {
			var id = $(slides[i-1]).attr('id');
			$('<li><a class="' + (i == 1 ? 'banner-nav-active' : '') + '" href="#' + id + '">' + i + '</a></li>').appendTo(nav);
		}

		links = nav.find('a');

		links.click(function(e) {
			clearTimeout(timer);

			if ($(this).hasClass('banner-nav-active')) {
				e.preventDefault();
				return false;
			}

			var num = parseInt($(this).html());

			self.swap(num);

			e.preventDefault();
			return false;
		});

		nav.hide().appendTo(banner).fadeIn(400);

		timer = setTimeout(function(){this.swap(2)}, delay + duration);
	}();
}

