
// 'stacks' is the Stacks global object.
// All of the other Stacks related Javascript will 
// be attatched to it.
var stacks = {};


// this call to jQuery gives us access to the globaal
// jQuery object. 
// 'noConflict' removes the '$' variable.
// 'true' removes the 'jQuery' variable.
// removing these globals reduces conflicts with other 
// jQuery versions that might be running on this page.
stacks.jQuery = jQuery.noConflict(true);

// Javascript for stacks_in_3_page7
// ---------------------------------------------------------------------

// Each stack has its own object with its own namespace.  The name of
// that object is the same as the stack's id.
stacks.stacks_in_3_page7 = {};

// A closure is defined and assigned to the stack's object.  The object
// is also passed in as 'stack' which gives you a shorthand for referring
// to this object from elsewhere.
stacks.stacks_in_3_page7 = (function(stack) {

	// When jQuery is used it will be available as $ and jQuery but only
	// inside the closure.
	var jQuery = stacks.jQuery;
	var $ = jQuery;
	

//-- RSS JS Stack v1.5 by Joe Workman --//

//---------------------------
// Start Common RSS Code
//---------------------------
formatString = function(str) {
	str = str.replace(/<[^>]+>/ig,'');
	str=' '+str;
	return $.trim(str);
}

enrichString = function(str) {
	str = str.replace(/((ftp|https?):\/\/([-\w\.]+)+(:\d+)?(\/([\w/_\.]*(\?\S+)?)?)?)/gm,'<a href="$1" target="_blank">$1</a>');
	str = str.replace(/([^\w])\@([\w\-]+)/gm,'$1@<a href="http://twitter.com/$2" target="_blank">$2</a>');
	str = str.replace(/([^\w])\#([\w\-]+)/gm,'$1<a href="http://twitter.com/search?q=%23$2" target="_blank">#$2</a>');
	return $.trim(str);
}

parse_date = function(str) {
    if (str.match(/^\d+\-\d+\-\d+T/)) {
        str = str.replace(/T.+$/,'');
    }
	var d = new Date(str);
	var m = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'June', 'July', 'Aug', 'Sept', 'Oct', 'Nov', 'Dec'];
	if (d.getUTCDate()) {
		return d.getUTCDate() + ' ' + m[d.getUTCMonth()] + ' ' + d.getFullYear();
    }
    return str;
};

find_link = function(obj) {
    var default_string = '#';
    if (obj == null || obj == undefined) {
        return default_string;
    }	
    else if (typeof obj.origLink == 'string') {
        return obj.origLink;
    }
    else if ($.isArray(obj.link)) {
        var mylink = obj.link[0].href;
        $.each(obj.link,function(index, value){
            if (value.rel === 'alternate') {
                mylink = value.href;
            }
		})
        return mylink;
    }
    else if (typeof obj.link == 'object') {
        return obj.link.href;
    }
    else if (typeof obj.link == 'string') {
        return obj.link;
    }
    else if (typeof obj.enclosure == 'object' && typeof obj.enclosure.url == 'string') {
        return obj.enclosure.url;
    }
    return default_string;
};

find_title = function(obj) {
    var default_string = 'No Items in RSS Feed';
    if (obj == null || obj == undefined) {
        return default_string;
    }	
    else if (typeof obj.title.content == 'string') {
        return formatString(obj.title.content);
    }
    else if (typeof obj.title == 'string') {
        return formatString(obj.title);
    }
    return default_string;
};

find_date = function(obj) {
    var default_string = 'Date Unknown';
    if (obj == null || obj == undefined) {
        return default_string;
    }	
    else if (typeof obj.pubDate == 'string') {
		return parse_date(obj.pubDate);
    }
    else if (typeof obj.date == 'string') {
		return parse_date(obj.date);
    }
    else if (typeof obj.published == 'string') {
		return parse_date(obj.published);
    }
    else if (typeof obj.updated == 'string') {
		return parse_date(obj.updated);
    }
    return default_string;
};

find_descr = function(obj) {
    var default_string = 'RSS Feed Invalid. No Description Found.';
    if (obj == null || obj == undefined) {
        return default_string;
    }	
    else if (typeof obj.description == 'string') {
		return formatString(obj.description);
    }
    else if (typeof obj.encoded == 'string') {
		return formatString(obj.encoded);
    }
    else if (typeof obj.content == 'object' && typeof obj.content.content == 'string') {
		return formatString(obj.content.content);
    }
    return default_string;
};

find_author = function(obj) {
    var default_string = 'Unknown Author';
    if (obj == null || obj == undefined) {
        return default_string;
    }	
    else if (typeof obj.creator == 'string') {
		return obj.creator;
    }
    else if ($.isArray(obj.author)) {
        return obj.author[0];
    }
    else if (typeof obj.author == 'object' && typeof obj.author.email == 'string') {
		return obj.author.email;
    }
    else if (typeof obj.author == 'string') {
		return obj.author;
    }
    return default_string;
};

$(document).ready(function() {
	/* Forming the query: */
	var feed = "www.mattbrouwer.com/mbwp2/feed/";
	feed = feed.replace(/feed:\/\//,'http://'); // Replace feed:// with http://
	var query = 'select * from feed where url="' + feed + '" LIMIT 3';

	/* Forming the URL to YQL: */
	var url = "http://query.yahooapis.com/v1/public/yql?q="+encodeURIComponent(query)+"&format=json&callback=?";

	$.getJSON(url,function(data){
		if (data.query == null || data.query == undefined || data.query.results == null || data.query.results == undefined) {
			// Invalid or Empty RSS Feed - Add Blank/Default Entries
			add_feed_item();
	 	}
	 	else if ($.isArray(data.query.results.item || data.query.results.entry) ) {  //item exists in RSS and entry in ATOM feeds
			$.each(data.query.results.item || data.query.results.entry,function(){
	       		//Normal RSS Feed
	       		add_feed_item(this);
			})
		}
		else {
		    // RSS Feed with only one item in it
			add_feed_item(data.query.results.item || data.query.results.entry || null);
		}
		post_process_feed(this);
	});
});
//---------------------------
// End Common RSS Code
//---------------------------

find_content = function(obj) {
    var default_string = 'RSS Feed Invalid. No Content Found.';
    if (obj == null || obj == undefined) {
        return default_string;
    }	
    else if (typeof obj.encoded == 'string') {
		return obj.encoded;
    }
    else if (typeof obj.content == 'object' && typeof obj.content.content == 'string') {
		return obj.content.content;
    }
    else if (typeof obj.description == 'string') {
		return obj.description;
    }
    return default_string;
};
function add_feed_item(obj) {
	$('#rss-container-stacks_in_3_page7').append(
		'<div class="rss-fc-feed">' + 
			'<h3 class="rss-fc-title">'+ find_title(obj) + '</h3>' +
		  	'<p class="rss-fc-date">'+ find_date(obj) +  '</p>' +
		  	'<div class="rss-fc-content">' + find_content(obj) + '</div>' +
		'</div>'
	);
	return;    	        
};
function post_process_feed(obj) {
	return;    	        
};
//-- End RSS JS Stack --//

	return stack;
})(stacks.stacks_in_3_page7);


// Javascript for stacks_in_30_page7
// ---------------------------------------------------------------------

// Each stack has its own object with its own namespace.  The name of
// that object is the same as the stack's id.
stacks.stacks_in_30_page7 = {};

// A closure is defined and assigned to the stack's object.  The object
// is also passed in as 'stack' which gives you a shorthand for referring
// to this object from elsewhere.
stacks.stacks_in_30_page7 = (function(stack) {

	// When jQuery is used it will be available as $ and jQuery but only
	// inside the closure.
	var jQuery = stacks.jQuery;
	var $ = jQuery;
	
jQuery(document).ready(function(e){
	
	jQuery('#signUpstacks_in_30_page7wrapper button#cancelBut').click(function(e) {
		if (fade=='true') {
			jQuery('#signUpstacks_in_30_page7wrapper').stop().fadeOut(200, function() {
				jQuery(this).hide;
				jQuery('a.showSU').show();
				});
			
			e.preventDefault();
		}
		
		//jQuery('#bottomLinks').fadeIn(400);
	});
	
	jQuery('a.showSU').click(function(e) {
		//jQuery('#bottomLinks').hide();
		if (fade=='true') {
			jQuery('#signUpstacks_in_30_page7wrapper #contact_form').show();
			jQuery('#signUpstacks_in_30_page7wrapper').stop().fadeIn(200);
			jQuery('a.showSU').css('display', 'none');
		}
		e.preventDefault();
		//return false;
	});
	
	

	jQuery("#signUpstacks_in_30_page7wrapper button#submitBut").click(function(e) {
		var firstname=jQuery('#signUpstacks_in_30_page7wrapper input#firstname').val();
		var lastname=jQuery('#signUpstacks_in_30_page7wrapper input#lastname').val();
		var email=jQuery('#signUpstacks_in_30_page7wrapper input#email').val();
		if ((firstname=='') || (lastname=='') || (email=='')) {
			alert('All fields are required');
			return false;
		}
		var list="news";
		var dataString = 'lastname='+ lastname + '&email=' + email + '&firstname=' + firstname + '&cmd=subscribe&list=' + list +'&resptype=simple';  
		jQuery.ajax({  
			type: "POST",  
			url: "http://www.mattbrouwer.com/lm/lm.php",  
			data: dataString,  
			success: function() {
				jQuery('#signUpstacks_in_30_page7wrapper #contact_form').stop().fadeOut(500);  
				jQuery('#signUpstacks_in_30_page7wrapper #message').stop()
				.fadeIn(500).delay(5000).fadeOut(500, function() {
					jQuery('#signUpstacks_in_30_page7wrapper').stop().fadeOut(500);
					//jQuery('#bottomLinks').fadeIn(400);
				});
				
				
			}  
		});
	
	return false;
	});
	
	
	
	var layout='horizontal';
	if (layout=='vertical') {
			jQuery('#signUpstacks_in_30_page7wrapper').css({
				'position': 'absolute',
				'top': '-5000px',
				'display': 'block'
			});
		var theMaxLabelWidth=0;
		jQuery('#signUpstacks_in_30_page7wrapper #myForm label').each(function() {
			var theTmpInputWidth=jQuery(this).outerWidth();
			if (theTmpInputWidth>theMaxLabelWidth) {
				theMaxLabelWidth=theTmpInputWidth;
			}
		});
		jQuery('#signUpstacks_in_30_page7wrapper').css({
			'position': 'static',
			'top': 'auto',
			'display': 'none'
		});
		
		
		var margLeft=theMaxLabelWidth+6+'px';
		jQuery('#signUpstacks_in_30_page7wrapper #myForm input').css('margin-bottom', '6px').after('<br>');
		
		jQuery('#signUpstacks_in_30_page7wrapper button#submitBut').css({
			'margin-left': margLeft,
			'margin-top': '10px'
			});
		jQuery('#signUpstacks_in_30_page7wrapper label').css({
			'display': 'inline-block',
			'width': theMaxLabelWidth
		});
		jQuery('#signUpstacks_in_30_page7wrapper input').css({
			'margin-right': '0'
		});
	}
	else {
		jQuery('#signUpstacks_in_30_page7wrapper label').css({
			'display': 'inline'
		});
	}
	var fade='true';
		if (fade=='false') {
			jQuery('#signUpstacks_in_30_page7wrapper').css('display', 'block');
		}
	var bg_color = $('#stacks_in_30_page7').css('background-color'); 
	if (bg_color) { 
		$('#stacks_in_30_page7').css({'background-color': 'transparent'});     
		$('#stacks_in_30_page7 form').css({'background-color': bg_color });   
	}
	var theColor = $('#stacks_in_30_page7').css('color'); 
	if (theColor) { 
		$('#stacks_in_30_page7').css({'color': 'transparent'});     
		$('#stacks_in_30_page7 form').css({'color': theColor });   
	}
	var move='false';
	if (move=='true') {
		jQuery('#SUMove').append(jQuery('#signUpstacks_in_30_page7wrapper'));
	}
	
    return;
});
	return stack;
})(stacks.stacks_in_30_page7);



