 /*-----------------------------------------------
'	Company: Community Engine (www.communityengine.com)
'	Copyright (c) 2009, All rights reserved.
'	Date Created: April 2009
'
'	Last Modified Date: 17th December 2009
'	Last Modified By: James // james.cooke@communityengine.com.au
'
'	DO NOT MODIFY THIS DOCUMENT WITHOUT
'	NOTIFYING THE AUTHOR FIRST
'
------------------------------------------------*/

$(function() {

	$(".nav-horizontal ul li:not(.nav-horizontal li li)").append("<span class=\"nav-right\"></span>");
	$(".valueclear").valueClear();

	$(".nav-horizontal ul").supersubs({
		maxWidth: 40,
		extraWidth: 1
	}).superfish({
		hoverClass: "hover",
		pathClass: "hover",
		animation: { opacity: 'show', height: 'show' },
		delay: 1400,
		autoArrows: false
	});

	$('a[rel="external"]').live("click", function() {
		window.open(this.href);
		return false;
	});

	setupSlider();
	$(".lightwindow:not([href=#])").colorbox({
		transition: "elastic",
		photo: true
	});

	$(".palettes a, .switchstyles a").live("click", function() {
		changeStyle($(this).attr("rel"));
	});

	var cookiePalette = $.cookies.get('palette');

	if (cookiePalette) {
		changeStyle(cookiePalette);
	} else {
		var syleName = "palette-1";
	}

	$('.module-gallery li').equalRow().addLast();

	getFeeds();

	hideEmpty();

});

function hideEmpty() {
	$(".container").each(function(i) {
		$(this).children("h2, .cmscontent").each(function(c) {
			if ($(this).html().trim().length == 0) {
				$(this).remove();
			} else {
			if ($(this).html().trim() == "<br>" || $(this).html().trim() == "<br />") {
					$(this).remove();
				}
			}
		});
		if ($(this).children().length == 0) {
			$(this).hide();
		}
	});
}

$.fn.valueClear = function() {
	return this.focus(function() {
		if( this.value == this.defaultValue ) {
			this.value = "";
		}
	}).blur(function() {
		if( !this.value.length ) {
			this.value = this.defaultValue;
		}
	});
};

function changeStyle(styleName){
	$('link[@rel*=style][title]').each(function(i){
		this.disabled = true;
		if($(this).attr('title') == styleName){
			this.disabled = false;
			$(".palettes .on").removeClass("on");
			$("."+styleName).addClass("on");
		}
	});
	if($("#colorbox:visible").size()){
		$.fn.colorbox.close(); // Close colorbox lightwindow only if it is open
	}
	$.cookies.set("palette", styleName, { hoursToLive: 100});
}

function setupSlider(){
	$('.slider:not(.right-column .slider)').anythingSlider({
        easing: "easeInOutExpo",        // Anything other than "linear" or "swing" requires the easing plugin
        autoPlay: true,                 // This turns off the entire FUNCTIONALY, not just if it starts running or not.
        delay: 6000,                    // How long between slide transitions in AutoPlay mode
        startStopped: false,            // If autoPlay is on, this can force it to start stopped
        animationTime: 1000,             // How long the slide transition takes
        hashTags: false,                 // Should links change the hashtag in the URL?
        buildNavigation: true,          // If true, builds and list of anchor links to link to each slide
		pauseOnHover: true,             // If true, and autoPlay is enabled, the show will pause on hover
		startText: "play",             // Start text
        stopText: "stop"               // Stop text
    });
    $('.right-column .slider').anythingSlider({
        easing: "easeInOutExpo",        // Anything other than "linear" or "swing" requires the easing plugin
        autoPlay: true,                 // This turns off the entire FUNCTIONALY, not just if it starts running or not.
        delay: 9000,                    // How long between slide transitions in AutoPlay mode
        startStopped: false,            // If autoPlay is on, this can force it to start stopped
        animationTime: 1000,             // How long the slide transition takes
        hashTags: false,                 // Should links change the hashtag in the URL?
        buildNavigation: false,          // If true, builds and list of anchor links to link to each slide
		pauseOnHover: true,             // If true, and autoPlay is enabled, the show will pause on hover
		startText: "play",             // Start text
        stopText: "stop"               // Stop text
    });
}

$.fn.equalRow = function(){

	var maxHeight = 144;
	var counter = 1;
	var grid = 3;
	
	$(this).each(function(){
		if(counter > 3){
			counter = 0;
			maxHeight = 144;
		}
		if($(this).height() > maxHeight){
			maxHeight = $(this).height();
			$(this).css({'height': maxHeight});
		} else{
			$(this).css({'height': maxHeight});
		}
		counter++;
	});
	return this;
}

$.fn.addLast = function(){
	var counter = 1;
	
	$(this).each(function(){
		if(counter == 3){
			counter = 0;
			if(!$(this).hasClass('last')){
				$(this).addClass('last');
			}
		}
		counter++;
	});
	
	return this;
}

$.fn.equalCols = function(){
	var sortNumber = function(a,b){return b - a;};
	var heights = [];
	$(this).each(function(){
		heights.push($(this).height());
	});
	heights.sort(sortNumber);
	var maxHeight = heights[0];
	return this.each(function(){
		$(this).css({'height': maxHeight});
	});
};

function getFeeds() {

	$.jGFeed(
		'http://www.alp.org.au/CMSTemplates/ALPSite/BlogFeed.aspx',
		function(feeds) {
			var blogOutput = "";
			if (!feeds) {
				// hide entries
				$(".right-column .module-blog").hide();
				return false;
			}
			for (var i = 0; i < feeds.entries.length; i++) {
				var entry = feeds.entries[i];
				var publishedDate = entry.publishedDate.substr(0,17);
				blogOutput += "<li><a href='" + entry.link + "' title='" + entry.title + "' rel='external'><strong>" + entry.title + "</strong></a>" + publishedDate + "</li>\n";
			}
			$(".right-column .module-blog ul").html(blogOutput);
		},
		5);

		$.jGFeed(
		'http://www.alp.org.au/CMSTemplates/ALPSite/NewsFeed.aspx',
		function(feeds) {
			var blogOutput = "";
			if (!feeds) {
				// hide entries
				$(".right-column .news-blog").hide();
				return false;
			}
			for (var i = 0; i < feeds.entries.length; i++) {
				var entry = feeds.entries[i];
				var publishedDate = entry.publishedDate.substr(0, 17);
				blogOutput += "<li><a href='" + entry.link + "' title='" + entry.title + "' rel='external'><strong>" + entry.title + "</strong></a>" + publishedDate + "</li>\n";
			}
			$(".right-column .module-news ul").html(blogOutput);
		},
		5);
}
