/*
	functions.js
	Author: 		James Cooke
	Description:	General functions and DOM.ready calls
	Client:			ALP Victoria
*/

var RecaptchaOptions = {
    theme: 'clean'
};

//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 mycarousel_initCallback(carousel) {
    $('.arrow-forward').bind('click', function() {
        carousel.next();
        return false;
    });

    $('#carousel-back').bind('click', function() {
        carousel.prev();
        return false;
    });
}

function listAltItem() {
    var myList = $(".column-main .list-avatar, .column-main .list-alt-bg, .column-main .branches-result");
    
    if (myList.hasClass("list-split-half")) {
        myList.children("li:nth-child(4n+1), li:nth-child(4n+2)").addClass("odd");
        myList.children("li:nth-child(1), li:nth-child(2)").addClass("dotted-top");
    }
    else {
        myList.children("li:nth-child(odd)").addClass("odd");
    }
}


function removeSeparator() {
    var m = $(".message-action-container");
    m.each(function() {
        $(this).children("a").last().addClass("last");
        $(this).children("a").first().addClass("first");
    });
}

function removeDottedLine() {
    var totalLi = 0,
        listUL = $(".list-split-half"),
        emptyLi = '<li></li>';  
    
    listUL.each(function() {
        totalLi = $(this).find('li').length;
        if (totalLi % 2 == "1") { //odd number of li
            $("<li>").insertAfter($(this).find("li:last-child"));
        }
    });
}

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() {

    try {
        Typekit.load();
    }
    catch (e) {
        $('html').addClass('no-typekit');
    }


    //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
    $('ul#MainNav').sooperfish({
        delay: 300,
        animationShow: { opacity: 'show' },
        speedShow: 100,
        animationHide: { opacity: 'hide' },
        speedHide: 100,
        dualColumn: 15
    });

    //primary nav onstate for
    //$('.connect').addClass('on');

    // Background image
    setBg();
    setupLinks();
    $(".flash").setupFlash();
    removeDottedLine(); /* must be placed before listAltItem() */
    removeSeparator();
    listAltItem();
    $('.InlineAbuseLink, .link-report, .ce-event-email').click(function() { abuseReportButtons(); });

    if ($('.module-panel-carousel').css("display") != "none") {
        $('.module-panel-carousel ul').jcarousel({
            wrap: 'circular',
            scroll: 3,
            initCallback: mycarousel_initCallback,
            buttonNextHTML: null,
            buttonPrevHTML: null,
            auto: 3
        });
    }

    showPasswordField();
    $(".icon-print, .print").click(function(e) {
        e.preventDefault();
        window.print();
    });
    $(window).resize(setBg);

    /* tooltip */
    $('.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");
      }
    );

    $("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;
    });

    $(".clear-input").clearText();

    kenticoFixes();
    
    $(".arrow-forward-inner, .arrow-forward").hover(
      function() {
          $(".arrow-forward").css("background-position","left -352px");
      },
      function() {
          $(".arrow-forward").css("background-position", "left top");
      }
    );
    
    // fix IE 6 displaying issue
    $(".thumb img").css('zoom', '1');

    document.body.className += ' loaded';

});


