/*
	functions.js
	Description:	General functions and DOM.ready calls
	Client:			ALP NSW
*/

var RecaptchaOptions = {
    theme: 'clean'
};

$.fn.setupSlideshow = function () {
    $(this).bxSlider({
        alignment: 'horizontal',
        controls: false,
        speed: 500,
        pager: true,
        pager_short: false,
        auto: true,
        pause: 5000, /* default: 5000 */
        auto_direction: 'next',
        auto_hover: false,
        auto_controls: false,
        wrapper_class: 'overflow'
    });
};

//flash
$.fn.setupFlash = function() {
    $(this).each(function() {
        var flashURL = $(".flash-url", this).text();
        $(this).flashembed(flashURL);
    });
};

// Clear detault value on inputs
$.fn.clearText = function() {
    return this.focus(function() {
        if (this.value == this.defaultValue) { 
            this.value = "";
        }
    }).blur(function() {
        if (!this.value.length) {
            this.value = this.defaultValue;
        }
    });
};

function abuseReportButtons() {
    $(".ContentButton").mouseover(function() { $(this).addClass("ContentButtonOn"); });
    $(".ContentButton").mouseout(function() { $(this).removeClass("ContentButtonOn"); });
    $(".abuse-close").click(function(e) {
        e.preventDefault();
        $(".btnCancel").click();
    });
}

function kenticoFixes() {
    $("#plc_lt_zoneContent_SubContent_SubContent_lt_zoneLeft_CEMessageBoard_msgBoard_lblNoMessages").wrap("<li class='no-items'>");
}

function setBg(){
	$("#body-alp").css({height	:	$("#container").height()});
}

function setupLinks() {
    $("a[rel='external']").click(function() {
        window.open(this.href);
        return false;
    });

	if ($(".iframewindow").size() > 0) {

        $(".iframewindow").fancybox({
            'type': 'iframe',
            'transitionIn': 'fade',
            'transitionOut': 'fade',
            'width': 595,
            'height': 500,
            'padding': 0,
            'hideOnOverlayClick': false,
            'hideOnContentClick': false

        });
    }
    if ($(".electoratewindow").size() > 0) {

        $(".electoratewindow").fancybox({
            'type': 'iframe',
            'transitionIn': 'fade',
            'transitionOut': 'fade',
            'width': 595,
            'height': 330,
            'padding': 0,
            'hideOnOverlayClick': false,
            'hideOnContentClick': false
        });
    }
    if ($(".ajaxwindow").size() > 0) {
        $(".ajaxwindow").fancybox({
            'type': 'ajax',
            'titleShow': false,
            'padding': 20,
            'onComplete': afterPopup,
            'hideOnOverlayClick': false,
            'hideOnContentClick': false
        });
    }
    if ($(".modalwindow").size() > 0) {
        $(".modalwindow").fancybox({
            'modal': true,
            'titleShow': false,
            'padding': 0,
            'hideOnOverlayClick': false,
            'hideOnContentClick': false
        });
    }

    $("a.close-lightbox").live("click", function() {
        $.fancybox.close();
    });
}

function removeSeparator() {
    var m = $(".message-action-container");
    m.each(function() {
        $(this).children("a").last().addClass("last");
        $(this).children("a").first().addClass("first");
    });
}

function showPasswordField() {
    var m = $('.password-mask'),
        p = $('.password-field');
    m.show();
    m.focus(function() {
        m.hide();
        p.removeClass('hidden');
        p.focus();
    });
    p.blur(function() {
        if (p.val() == '') {
            m.show();
            p.addClass('hidden');
        }
    });

}

function updateElectorate(data) {
    $(".input-electorate").val(data);
    $(".input-electorate").focus();
}

function setupCufon() {
    Cufon.replace('#MainNav li a, .icon h2, .module-comments h2, .box-widget h3, .module-shortcuts h3, .module-connect h3, .idea-vote p', { fontFamily: 'DIN', hover: true });
    Cufon.replace('h1, .icon h2 span, .header-list-avatar h4, .header-list-popup h4, .read-more, .box-widget h3 span, .column-right .module-photo h2, .module-shortcuts h4, .box-widget h4.section-header-4, .module-connect h3 span ', { fontFamily: 'DIN BOLD', hover: true });
}

function panelAccordion() {
    $(".membership-details").hide();
    $(".membership-details:first").show();
    $('input[name="Category"]').click(function () {
        $(".membership-details").hide();
        $(this).siblings(".membership-details").show();
    });
}

$(function () {

    setupCufon();

    //group search does not search on default value of "Search for a group"
    $(".form-group-search a.button").click(function () {
        if ($(".form-group-search input.input-search").val() == "Search for a group") {
            $(".form-group-search .member-group-error-message").show();
            return false;
        } else {
            $(".form-group-search .member-group-error-message").hide();
        }
    });

    //member search does not search on default value of "Search for a group"
    $(".form-find-users a.button").click(function () {
        if ($(".form-find-users input.input-search").val() == "Search for members") {
            $(".form-find-users .member-group-error-message").show();
            return false;
        } else {
            $(".form-find-users .member-group-error-message").hide();
        }
    });



    //when rollover the news avatar module inside the header banner, a hidden larger div pops up (fades in) with more details about the article. roll out, popup disappears.
    $(".module-promo-tiles li").hover(
      function () {
          $(".header-list-popup", this).fadeIn(500);
      },
      function () {
          $(".header-list-popup", this).fadeOut(50);
      }
    );

    //primary nav horizontal rollovers
    if ($('ul#MainNav').length > 0) {
        $('ul#MainNav').sooperfish({
            delay: 300,
            animationShow: { opacity: 'show' },
            speedShow: 100,
            animationHide: { opacity: 'hide' },
            speedHide: 100,
            dualColumn: 15
        });
    }

    setupLinks();
    $(".flash").setupFlash();
    removeSeparator();
    $('.InlineAbuseLink, .link-report, .ce-event-email').click(function () { abuseReportButtons(); });

    if ($('.module-panel-carousel').length > 0) {
        if ($('.module-panel-carousel').css("display") != "none") {

            $('.module-panel-carousel ul').bxSlider({
                displaySlideQty: 3,
                moveSlideQty: 3,
                auto: true,
                infiniteLoop: true,
                nextImage: '/resources/images/modules/carousel/panel/forward.png',
                prevImage: '/resources/images/modules/carousel/panel/back.png',
                pause: 7000
            });
        }
    }

    showPasswordField();
    $(".icon-print, .print").click(function (e) {
        e.preventDefault();
        window.print();
    });
    $(window).resize(setBg);

    /* tooltip */
    if ($('.tooltip').length > 0) {
        $('.tooltip').tipsy({
            gravity: 'n',
            fade: true
        });
    }

    $('a.tooltip').click(function () {
        return false;
    });

    $(".list-shortcuts li a").hover(
      function () {
          $(this).parent().addClass("active");
      },
      function () {
          $(this).parent().removeClass("active");
      }
    );

    // add 'last-child' class to fix for IE6
    $('ul li:last-child').addClass('last-child');

    $("button.button").each(function () {
        $(this).html($(this).html().replace(' ', "\u00a0"));
    });


    $('.button-radio-local').click(function () {
        $('.module-search-hidden').fadeIn(700);
    });

    $("a.select-all").click(function () {
        $(this).closest("li").find("select").each(function () {
            $("option", this).attr("selected", "select");
        });
        return false;
    });
    $("a.deselect-all").click(function () {
        $(this).closest("li").find("select").each(function () {
            $("option", this).attr("selected", "");
        });
        return false;
    });

    if ($('.module-slideshow li').length > 1) {
        $(".module-slideshow ul").setupSlideshow();
    }

    $(".clear-input").clearText();

    kenticoFixes();

    panelAccordion();

    // fix IE 6 displaying issue
    $(".thumb img").css('zoom', '1');

    document.body.className += ' loaded';

});


