$.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'
    });
};

$.fn.setupFlash = function() {
    $(this).each(function() {
        var flashURL = $(".flash-url", this).text();
        $(this).flashembed(flashURL);
    });
};

$.fn.equalHeight = function(options) {
    var tallest = 0;
    $(this).each(function() {
        if ($(this).outerHeight() > tallest) {
            tallest = $(this).outerHeight();
        }
    });
    $(this).height(tallest);
};

$.fn.equalInnerHeight = function(options) {
    var tallest = 0;
    $(this).each(function() {
        if ($(this).innerHeight() > tallest) {
            tallest = $(this).innerHeight();
        }
    });
    $(this).height(tallest);
};

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 setupLinks() {

    $("a[rel='external']").click(function() {
        window.open(this.href);
        return false;
    });


    if ($(".electoratewindow").size() > 0) {

        $(".electoratewindow").fancybox({
            'type': 'iframe',
            'transitionIn': 'fade',
            'transitionOut': 'fade',
            'width': 595,
            'height': 330,
            'padding': 0,
            'hideOnOverlayClick': false,
            'hideOnContentClick': false
        });
    }
    
    if ($(".iframewindow").size() > 0) {

        $(".iframewindow").fancybox({
            'type': 'iframe',
            'transitionIn': 'fade',
            'transitionOut': 'fade',
            'width': 595,
            'height': 500,
            '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({
            'type': 'iframe',
            'transitionIn': 'fade',
            'transitionOut': 'fade',
            'width': 595,
            'height': 500,
            'padding': 0,
            'hideOnOverlayClick': false,
            'hideOnContentClick': false
        });
    }

    $(".lightwindow:not([href=#])").colorbox({
        transition: "elastic",
        photo: true
    });

    $("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;
    });

}


function updateElectorate(data) {
    $(".input-electorate").val(data);
    $(".input-electorate").focus();
}

function getFeeds(mainDomain) {
    var numFeed = 3;
    
    $.jGFeed(
		mainDomain + 'CMSTemplates/ALPSite/BlogFeed.aspx',
		function(feeds) {
		    var blogOutput = "";
		    if (!feeds) {
		        // hide entries
		        $(".column-right .module-blog").hide();
		        return false;
		    }
		    
		    if (feeds.entries.length < numFeed) {
		        numFeed = feeds.entries.length;
		    }
		    for (var i = 0; i < numFeed; i++) {		        
		        var entry = feeds.entries[i];
		        var publishedDate = entry.publishedDate.substr(4, 13),
		            strMonth,
		            strDate;
		        objDate = new Date(publishedDate);
		        strMonth = objDate.getMonth() + 1;
		        strDate = objDate.getDate();

		        if (strMonth < 10) {
		            strMonth = "0" + strMonth;
		        }
		        if (strDate < 10) {
		            strDate = "0" + strDate;
		        }

		        blogOutput += "<li><span>" + strDate + "." + strMonth + "." + objDate.getFullYear() + "</span><a href='" + entry.link + "' title='" + entry.title + "' rel='external'>" + entry.title + "</a></li>\n";
		    }
		    $(".column-right .module-blog ul").html(blogOutput);
		},
		5);
		
		$.jGFeed(
		mainDomain + 'CMSTemplates/ALPSite/NewsFeed.aspx',
		function(feeds) {
		    var blogOutput = "", objDate;

		    if (!feeds) {
		        // hide entries
		        $(".column-right .module-latest-news").hide();
		        return false;
		    }
		    numFeed = 3;
		    if (feeds.entries.length < numFeed) {
		        numFeed = feeds.entries.length;
		    }
		    for (var i = 0; i < numFeed; i++) {		    
		        var entry = feeds.entries[i];
		        var publishedDate = entry.publishedDate.substr(4, 13),
		            strMonth,
		            strDate;
		        objDate = new Date(publishedDate);

		        strMonth = objDate.getMonth() + 1;
		        strDate = objDate.getDate();
		        if (strMonth < 10) {
		            strMonth = "0" + strMonth;
		        }
		        if (strDate < 10) {
		            strDate = "0" + strDate;
		        }

		        blogOutput += "<li><span>" + strDate + "." + strMonth + "." + objDate.getFullYear() + "</span><a href='" + entry.link + "' title='" + entry.title + "' rel='external'>" + entry.title + "</a></li>\n";
		    }
		    $(".column-right .module-latest-news ul").html(blogOutput);
		},
		5);
}

function callFeedRss(mainDomain) {
    getFeeds(mainDomain);
}

function abuseReportButtons() {
    $(".ContentButton").mouseover(function() { $(this).addClass("ContentButtonOn"); });
    $(".ContentButton").mouseout(function() { $(this).removeClass("ContentButtonOn"); });
    $(".abuse-close").click(function(e) {
        e.preventDefault();
        $(".btnCancel").click();
    });
}

function removeSeparator() {
    var m = $(".message-action-container");
    m.each(function() {
        $(this).children("a").last().addClass("last");
        $(this).children("a").first().addClass("first");
    });
}

$(function() {

    $('ul#MainNav').sooperfish({
        hoverClass: 'over',
        speedShow: 100
    });

    if ($('ul.module-slideshow li').length > 1) {
        $("ul.module-slideshow").setupSlideshow();
    }
    if ($('#tabs-latest').length) {
        $('#tabs-latest').tabs();
    }
    if ($('#tabs-video').length) {
        $('#tabs-video').tabs();
    }
    if ($('#tabs-photos').length) {
        $('#tabs-photos').tabs();
    }
    setupLinks();
    removeSeparator();
    $(".icon-print, .print").click(function(e) {
        e.preventDefault();
        window.print();
    });

    $('.clear-input').live("focus", function() {
        if (this.value == this.defaultValue) {
            this.value = "";
        }
    });
    $('.clear-input').live("blur", function() {
        if (!this.value.length) {
            this.value = this.defaultValue;
        }
    });

    $('.InlineAbuseLink, .link-report, .ce-event-email').click(function() { abuseReportButtons(); });
    showPasswordField();

    // add 'last-child' class to fix for IE6
    $('ul li:last-child').addClass('last-child');
    $('.ui-tabs .ui-tabs-nav li:first-child').addClass('first-child');
    $('.flash').setupFlash();

    $(".equal-height-1").equalHeight();
    $(".equal-height-2").equalHeight();
    $(".equal-height-social").equalHeight();
    $(".equal-height-address").equalInnerHeight();
    $(".PagerNumberArea span:empty").css('display', 'none');


    $('body').addClass('loaded');
});


