/**********************************************************************************************

  CSS on Sails Framework
  Title: Educacy
  Author: XHTMLized (http://www.xhtmlized.com/)
  Date: August 2010

***********************************************************************************************/

/**
 *
 * Copyright (c) 2007 Tom Deater (http://www.tomdeater.com)
 * Licensed under the MIT License:
 * http://www.opensource.org/licenses/mit-license.php
 *
 */

(function($) {
	/**
	 * equalizes the heights of all elements in a jQuery collection
	 * thanks to John Resig for optimizing this!
	 * usage: $("#col1, #col2, #col3").equalizeCols();
	 */

	$.fn.equalizeCols = function(){
		var height = 0,
			reset = $.browser.msie && $.browser.version < 7 ? "1%" : "auto";

		return this
			.css("height", reset)
			.each(function() {
				height = Math.max(height, this.offsetHeight);
			})
			.css("height", height)
			.each(function() {
				var h = this.offsetHeight;
				if (h > height) {
					$(this).css("height", height - (h - height));
				};
			});

	};

})(jQuery);

//rotator
function theRotator() {
	$('#sidebar .facts a').css({opacity: 0.0});
	$('#sidebar .facts a:first').css({opacity: 1.0});
	setInterval('rotate()',5000);
}
function rotate() {
	var current = ($('#sidebar .facts a.show')? $('#sidebar .facts a.show') : $('#sidebar .facts a:first'));
	var next = ((current.next().length) ? ((current.next().hasClass('show')) ? $('#sidebar .facts a:first') :current.next()) : $('#sidebar .facts a:first'));
	next.css({opacity: 0.0})
	.addClass('show')
	.animate({opacity: 1.0}, 1000);
	current.animate({opacity: 0.0}, 1000)
	.removeClass('show');
};

//RSS
/**
 * Plugin: jquery.zRSSFeed
 *
 * Version: 1.0.1
 * (c) Copyright 2010, Zazar Ltd
 *
 * Description: jQuery plugin for display of RSS feeds via Google Feed API
 *              (Based on original plugin jGFeed by jQuery HowTo)
 *
 * History:
 * 1.0.1 - Corrected issue with multiple instances
 *
 **/

(function($){
	var current = null;
	$.fn.rssfeed = function(url, options) {
		// Set pluign defaults
		var defaults = {
			limit: 10,
			header: false,
			title: true,
			titletag: 'strong',
			date: true,
			content: true,
			snippet: true,
			showerror: true,
			errormsg: '',
			key: null
		};
		var options = $.extend(defaults, options);
		// Functions
		return this.each(function(i, e) {
			var $e = $(e);
			// Add feed class to user div
			if (!$e.hasClass('rssFeed')) $e.addClass('rssFeed');
			// Check for valid url
			if(url == null) return false;
			// Create Google Feed API address
			var api = "http://ajax.googleapis.com/ajax/services/feed/load?v=1.0&callback=?&q=" + url;
			if (options.limit != null) api += "&num=" + options.limit;
			if (options.key != null) api += "&key=" + options.key;
			// Send request
			$.getJSON(api, function(data){
				// Check for error
				if (data.responseStatus == 200) {
					// Process the feeds
					_callback(e, data.responseData.feed, options);
				} else {
					// Handle error if required
					if (options.showerror)
						if (options.errormsg != '') {
							var msg = options.errormsg;
						} else {
							var msg = data.responseDetails;
						};
						$(e).html('<p class="error">'+ msg +'</p>');
				};
			});
		});
	};
	// Callback function to create HTML result
	var _callback = function(e, feeds, options) {
		if (!feeds) {
			return false;
		}
		var html = '';
		// Add header if required
		if (options.header)
			html += '<div class="rssHeader"><a href="'+feeds.link+'" title="'+ feeds.description +'">'+ feeds.title +'</a></div>';
		// Add feeds
		for (var i=0; i<feeds.entries.length; i++) {
			// Get individual feed
			var entry = feeds.entries[i];
			// Format published date
			var entryDate = new Date(entry.publishedDate);
			//var pubDate = entryDate.toLocaleDateString() + ' ' + entryDate.toLocaleTimeString();
			var pubDate = entryDate.toLocaleDateString();
			// Add feed row
			//if (options.date) html += '<p><'+ options.titletag +'>'+ pubDate +'</'+ options.titletag +'><br />'
			html += '<p>'
			if (options.title) {
				html += '<strong>'+ feeds.title +'</strong><br />'
			}
			if (options.content) {
				// Use feed snippet if available and optioned
				if (options.snippet && entry.contentSnippet != '') {
					var content = entry.contentSnippet;
				} else {
					var content = entry.content;
				}
				html += '<span class="content">'+ content +'</span></p>'
			}
		}
		html += '<p><a href="'+ entry.link +'" title="View this feed at '+ feeds.title +'">Read full story</a></p>'
		$(e).html(html);
	};
})(jQuery);

$(document).ready(function() {

	//
	theRotator();

	//
	$('#rss1').rssfeed('http://feeds.feedburner.com/CBP', {limit: 1});
	$('#rss2').rssfeed('http://californiawatch.org/feed', {limit: 1});
	$('#rss3').rssfeed('http://educatedguess.org/feed/', {limit: 1});
	//
	$('#rss4').rssfeed('http://californiawatch.org/feed', {limit: 1, title: false});
	$('#rss5').rssfeed('http://feeds.feedburner.com/TheQuickAndTheEd', {limit: 1, title: false});
	$('#rss6').rssfeed('http://feeds.feedburner.com/CBP', {limit: 1, title: false});
	$('#rss7').rssfeed('http://feeds.feedburner.com/EducationWeekNewsAndInformationAboutEducationIssues', {limit: 1, title: false});
	$('#rss8').rssfeed('http://feeds.nytimes.com/nyt/rss/Education', {limit: 1, title: false});
	$('#rss9').rssfeed('http://feeds.stateline.org/StatelineorgRss-Education', {limit: 1, title: false});


	//
	$('a[rel=_blank]').click(function(){
		this.target = "_blank";
	});

	//
	Cufon.replace('#header .nav-top li a, #navigation li a, #header .accordion .accordion-content .text h3, #content .articles div h4, #content .news dt, .sidebar .links li a', { fontFamily: 'Neutra2Text-Demi', hover: true });

	//
	$('#header .accordion div .text h3').click(function(){
		maxWidth = 770;
		minWidth = 96;
		if($(this).parent().parent().hasClass('active')){
			return false;
		}
		else {
			$('#header .accordion > div').animate({'width': minWidth+'px'}, { queue:false, duration: 400 }).removeClass('active');
			$(this).parent().parent().animate({'width': maxWidth+'px'}, { queue:false, duration: 400 }).addClass('active');

			if ($(this).parent().parent(':first-child').hasClass('active')){
				$('#header .accordion  div.accordion-content2').animate({'left': '770px'}, { queue:false, duration: 400 });

				$('#header .accordion  div.accordion-content1 img').animate({'left': '0px'}, { queue:false, duration: 400 });
			}
			else {
				$('#header .accordion  div.accordion-content2').animate({'left': '96px'}, { queue:false, duration: 400 });

				$('#header .accordion  div.accordion-content1 img').animate({'left': '-90px'}, { queue:false, duration: 400 });
			}
			if ($(this).parent().parent(':nth-child(2)').hasClass('active')){
				$('#header .accordion  div.accordion-content2 img').animate({'left': '0px'}, { queue:false, duration: 400 });
			}
			else {
				$('#header .accordion  div.accordion-content2 img').animate({'left': '-360px'}, { queue:false, duration: 400 });
			}
			if ($(this).parent().parent(':nth-child(3)').hasClass('active')){
				$('#header .accordion  div.accordion-content3 img').animate({'left': '0px'}, { queue:false, duration: 400 });
			}
			else {
				$('#header .accordion  div.accordion-content3 img').animate({'left': '-260px'}, { queue:false, duration: 400 });
			}

			if ($(this).parent().parent().hasClass('accordion-content3')){
				$('#header .accordion .accordion-content2 img').animate({'left': '-360px'});
			}
		}
	});

	//
	$('input[value="Search"], input[value="Search the Blog"]').each(function() {
		var defaultValue = this.value;
		$(this).focus(function() {
			if(this.value == defaultValue) {
				this.value = '';
			}
		});
		$(this).blur(function() {
			if(this.value == '') {
				this.value = defaultValue;
			}
		});
	});

	//
	$('.wrapper-tab').hide();
	$('#sidebar .menu li ul li a').each(function() {
		var wrapperTabMenu = $(this).attr('href');
		if($(this).parent().hasClass('active')) {
			$(wrapperTabMenu).show();
		}
		$(this).click(function(){
			$('#sidebar .menu li ul li').removeClass('active');
			$('.wrapper-tab').hide();
			$(this).parent().addClass('active');
			$(wrapperTabMenu).fadeIn('normal');
			return false;
		})
	});


	//
	if (($.browser.mozilla)){

		//
		$('html').addClass('firefox');

	}

	//
	if (($.browser.msie)  && ($.browser.version <= "8.0") ){

		//
		$('#content .articles > div:nth-child(2)').css({'padding-top': '5px'});
		$('#content .articles > div:nth-child(2) h4').css({'border-top': 'none'});
		$('#content .news dd:nth-child(2)').css({'border-top': 'none'});

		//
		$('#navigation li.active:nth-child(4) a').css({'background-position': '68% 0'});

		//
		$('#content .sponsor:nth-child(2) dd img').css({'top': '16px'});
		$('#content .sponsor:nth-child(3) dd img').css({'top': '-9px'});
		$('#content .sponsor:nth-child(4) dd img').css({'top': '26px'});
		$('#content .sponsor:nth-child(5) dd img').css({'top': '-4px'});
		$('#content .sponsor:nth-child(6) dd img').css({'top': '24px'});
		$('#content .sponsor:nth-child(7) dd img').css({'top': '33px'});
		$('#content .sponsor:nth-child(8) dd img').css({'top': '22px'});

	}

	//
	if (($.browser.msie) && ($.browser.version == "8.0")){

		//
		$('html').addClass('ie8');

	}

	//
	if (($.browser.msie) && ($.browser.version == "7.0")){

		//
		$('html').addClass('ie7');

		$('#content .facts-list dd').after('<br />')
		$('#content .facts-list').after('<br /><br />')

		//
		$('input[type="text"]').focus(function() {
			$(this).addClass('focus');
		});
		$('input[type="text"]').blur(function() {
			$(this).removeClass('focus');
		});

	}

});

$(window).load(function() {

	$('#content .articles > div:first-child, #content .articles > div:nth-child(2)').equalizeCols();
	$('#content .articles > div:nth-child(3), #content .articles > div:nth-child(4)').equalizeCols();
	$('#content .articles > div:nth-child(5), #content .articles > div:nth-child(6)').equalizeCols();
/*
	$('#content .sponsor dd img').each( function(){
		var top = 100;
		var margin0 = $(this).height();
		var margin1 = margin0/2;
		var height0 = $(this).parent().parent().height();
		var height1 = height0/2;
		//console.log( margin1 );
		$(this).css({'top':+height1+'px', 'margin-top':'-'+margin1+'px'})
	});
*/
});
