var ts = new Date();

// Ads by RBC
var adInterval = 7;
var adNumber = 3;
var adCurrentIndex = 0;
var boxCounter = 0;
var boxID;

$(document).ready(
	function () {

		// Start rotating ads
		setInterval("nextAd()",adInterval * 1000);

    // if cookies set, read in cookie array, otherwise read in default array...
    // add a feed manually
    $("div#cell-middle").movableparts({
				id: 'box_1',
        title: 'Our Daily Bread',
        link: 'http://www.odb.org',
				feed_url: 'http://www.rbc.org/rss.ashx?id=50398',
				item_count: 1,
				show_date: true
    });
    
    $("div#cell-left").movableparts({
				id: 'box_2',
        title: 'My Utmost For His Highest',
        link: 'http://www.utmost.org',
				feed_url: 'http://www.rbc.org/rss.ashx?id=50400',
				item_count: 1,
				show_date: false
    });

    $("div#cell-middle").movableparts({
				id: 'box_3',
        title: 'Daily Strength',
        link: 'http://www.getmorestrength.org',
				feed_url: 'http://www.rbc.org/rss.ashx?id=50452',
				item_count: 1
    });

    $("div#cell-left").movableparts({
				id: 'box_4',
        title: 'Words to Live By',
        link: 'http://www.words.net',
				feed_url: 'http://www.rbc.org/rss.ashx?id=50458',
				item_count: 4,
				desc_length: 0,
				show_date: false
    });
    
		// RSS feed on a timer
    window.setTimeout(function() {
	    $("div#cell-right").movableparts({
					id: 'box_5',
	        title: 'Discover the Word',
	        link: 'http://www.discovertheword.org/',
					feed_url: 'http://www.rbc.org/rss.ashx?id=50454',
					item_count: 3,
					desc_length: 0,
					show_date: false
			});
    }, 2500);
    
    $("div#cell-right").movableparts({
				id: 'box_6',
        title: 'Been Thinking About Blog',
        link: 'http://www.beenthinking.org',
				feed_url: 'http://beenthinking.org/wp-json.php?callback=?',
				feed_type: 'json',
				item_count: 1,
				desc_length: 50,
				show_date: false
    });

		// Add Event Listeners
		$('div.collapse a').live('click', toggleBox);
		
		var els = ['#cell-left', '#cell-right', '#cell-middle'];
		var $els = $(els.toString());
	
		$els.sortable({
			items: 'div.movable',
			handle: 'div.movable h2',
			cursor: 'move',
			//cursorAt: { top: 2, left: 2 },
			//opacity: 0.8,
			//helper: 'clone',
			appendTo: 'body',
			//placeholder: 'clone',
			//placeholder: 'placeholder',
			connectWith: els,
			start: sortableStart,
			change: sortableChange,
			update: sortableUpdate
		});
	}
);

var getAd = function(ad_index,ad_url,ad_title,ad_image) {
	if(ad_index==0){
		var displayStatus = 'block';
	} else {
		var displayStatus = 'none';
	}
	return '<div id="ad_'+ad_index+'" style="display:'+displayStatus+';float:left"><a href="'+ad_url+'" title="'+ad_title+'"><img width="360" height="188" border="0" src="/uploadedImages/Site-Wide_Promotions/Resources_For_Everyone/'+ad_image+'" alt="'+ad_title+'" title="'+ad_title+'"/></a></div>';
};

var nextAd = function() {
	$('div#ad_'+adCurrentIndex).hide();
	adCurrentIndex = (adCurrentIndex + 1) % adNumber;
	$('div#ad_'+adCurrentIndex).fadeIn();
};

var toggleBox = function(e)
{
	var parentContainer = this.parentNode.parentNode.parentNode;
	var targetContent = $('div.module-wrapper', this.parentNode.parentNode.parentNode);

	if (targetContent.css('display') == 'none') {
		targetContent.slideDown(300);
		$(parentContainer).removeClass('box_collapsed');
		setCookie('state_'+$(this).attr('title'),'opened',365);
	} else {
		targetContent.slideUp(300);
		$(parentContainer).addClass('box_collapsed');
		setCookie('state_'+$(this).attr('title'),'closed',365);
	}
	this.blur();
	return false;
};

var sortableStart = function(e, ui){

};

var sortableChange = function(e, ui){

};

var sortableUpdate = function(e, ui){
	//console.log( $(this).sortable('serialize') );
	//function(e, ui){alert( $els.sortable('serialize')+' hello')}
};

function setCookie(name,value,days) {
  var expires = "";
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		expires = "; expires="+date.toGMTString();
	}
	document.cookie = name+"="+value+expires+"; path=/";
}

function getCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function deleteCookie(name) {
	setCookie(name,"",-1);
}