

// Facebox customisations
$.facebox.settings.loadingImage = '/Includes/Images/loading.gif';
$.facebox.settings.closeImage = '/Includes/Images/closelabel.png';
$.facebox.settings.opacity = 0.64;

// Put global methods in "transact" namespace.
var transACT = transACT || {};

/**
* Simulate placeholders in browsers without support for the HTML5 attribute.
*/
(function ($)
{
    // Don't need to do anything on modern browsers.
    if (Modernizr.input.placeholder)
    {
        var nothing = function ()
        {
        };
        transACT.removePlaceholderAction = nothing;
        transACT.addPlaceholderAction = nothing;
        transACT.initalisePlaceholders = nothing;
        transACT.removePlaceholderActionNoCaretChange = nothing;
        return
    }

    /**
    * Remove placeholder text from the bound 'this' element.
    *
    * Useful for jQuery event callbacks.
    */
    transACT.removePlaceholderAction = function ()
    {
        var $this = $(this);
        if ($this.val() === $this.attr('placeholder'))
        {
            $this.val('')
                    .removeClass('with-placeholder').caret(0, 0);
        }
    };

        /**
    * Remove placeholder text from the bound 'this' element.
    *
    * Useful for jQuery event callbacks.
    */
    transACT.removePlaceholderActionNoCaretChange = function ()
    {
        var $this = $(this);
        if ($this.val() === $this.attr('placeholder'))
        {
            $this.val('')
                    .removeClass('with-placeholder');
        }
    };

    /**
    * Add placeholder text from the bound 'this' element.
    *
    * Useful for jQuery event callbacks.
    */
    transACT.addPlaceholderAction = function ()
    {
        var $this = $(this);
        if ($.trim($this.val()) === '')
        {
            $this.val($this.attr('placeholder'))
                    .addClass('with-placeholder');
        }
    };

    /**
    * Initialise placeholder emulation. This is called on page load, but needs to be called again if
    * new input fields are added to the page.
    */
    transACT.initalisePlaceholders = function ()
    {
        $('input[placeholder]')
                .each(transACT.addPlaceholderAction)
                .focus(transACT.removePlaceholderAction)
                .blur(transACT.addPlaceholderAction);
    };

})(jQuery);

jQuery(function ($) {
    var undefined;

    $('a[rel*=facebox]').facebox();

    transACT.initalisePlaceholders();

    $('.locality-button').click(function (event) {
        event.preventDefault();
    });

    $('.state-choice-locality').click(function (event) {
        saveStateInSession($(this));
        event.preventDefault();

        // if needed, strip out any language in the URL before reloading.
        var newLocation = location.pathname.toLowerCase();

        if (newLocation.indexOf("/en-act") == 0) {
            newLocation = newLocation.replace("en-act", "");
        }
        else if (newLocation.indexOf("/en-vic") == 0) {
            newLocation = newLocation.replace("en-vic", "");
        }
        else if (newLocation.indexOf("/en-qld") == 0) {
            newLocation = newLocation.replace("en-qld", "");
        }
        else if (newLocation.indexOf("/en-sa") == 0) {
            newLocation = newLocation.replace("en-sa", "");
        }
        else if (newLocation.indexOf("/en-nsw") == 0) {
            newLocation = newLocation.replace("en-nsw", "");
        }
        else if (newLocation.indexOf("/en-tas") == 0) {
            newLocation = newLocation.replace("en-tas", "");
        }
        else if (newLocation.indexOf("/en-nt") == 0) {
            newLocation = newLocation.replace("en-nt", "");
        }
        else if (newLocation.indexOf("/en-wa") == 0) {
            newLocation = newLocation.replace("en-wa", "");
        }
        else if (newLocation == "/en" || newLocation.indexOf("/en/") == 0) {
            newLocation = newLocation.replace("en", "");
        }

        location.pathname = newLocation.replace("\/\/", "/");
    });

    /**
    * EPG page waypoints: Make buttons and times sticky while scrolling
    */
    (function () {

        // Table time header
        $.waypoints.settings.scrollThrottle = 30;
        $('table.epg').waypoint(function (event, direction) {
            $(this).toggleClass('sticky', direction === "down");
        });


        // Next/previous time buttons
        $('.header:has(.time-navigation)').waypoint(function (event, direction) {
            $('.time-navigation').toggleClass('sticky', direction === 'down');
        });

        // When the buttons reach the bottom of the epg table, set their z-index to sit in the background.
        // We can't use this z-index statically, as the buttons aren't clickable when they have a negative z-index.
        var BUTTON_HEIGHT = 172;
        $('table.epg + *').waypoint(function (event, direction) {
            $('.time-navigation').css('z-index', direction === 'down' ? -2 : 0);
        }, { offset: BUTTON_HEIGHT });


    })();
});

// Add toggle to the terms and condition boxes
jQuery(function ($) {
    $('h2', '.more-info').click(function () {
        $('div', $(this).parent()).slideToggle();
        if ($(this).parent().hasClass('down')) {
            $(this).parent().removeClass('down');
        } else {
            $(this).parent().addClass('down');
        }
    });

    $('div', '.more-info').css('display', 'none');
});

function saveCookie(c_name, value, exdays) {
    $.cookie(c_name, value, { path: "/", expires: exdays});
}
 
var clickedState = '';

function saveStateInSession($item)
{
    clickedState = getStateClick($item);
    var dataParameters = "{ state: '" + clickedState + "' }";

    /* Call server-side method to save the state */
    $.ajax(
            {
                type: "POST",
                url: "/Controls/Tools.aspx/StoreClickedState",
                data: dataParameters,
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                async: false
            }
        );
}

/**
* Get state for a given state link element
*
* States are a class prefixed by 'state-click-'
*/
function getStateClick($item)
{
    var classes = $item.attr('class').split(' ');

    for (var i = 0; i < classes.length; i++) {
        if (classes[i].indexOf('state-click') == 0) {
            return classes[i].substr('state-click'.length+1);
        }
    };
    throw { msg: "State not found.", obj: $item };
}

// jQuery UI Tabs
$(function ()
{
    // Tabs
    $('#tabs').tabs();
    //hover states on the static widgets
    $('#dialog_link, ul#icons li').hover(
					function () { $(this).addClass('ui-state-hover'); },
					function () { $(this).removeClass('ui-state-hover'); }
				);
});

// Scroll effect with local anchors - jQuery
$(document).ready(function () {
    $(".scroll").click(function (event) {
        event.preventDefault();//prevent the default action for the click event
        var full_url = this.href;//get the full url - like mysitecom/index.htm#home
        var parts = full_url.split("#");//split the url by # and get the anchor target name - home in mysitecom/index.htm#home
        var trgt = parts[1];
        var target_offset = $("#" + trgt).offset(); //get the top offset of the target anchor
        var target_top = target_offset.top;
        $('html, body').animate({ scrollTop: target_top }, 500);//goto that anchor by setting the body scroll top to anchor top
    });
    
});
// NIVO slider
$(function () {
    $('#slider').nivoSlider({
        controlNav: false, // 1,2,3... navigation
        pauseTime: 5000 // How long each slide will show
    });
});

