// VERTICALLY ALIGN FUNCTION
//if there are problems padding-top can be margin-top
jQuery.fn.vAlign = function(options) {
	var opts = jQuery.extend({},jQuery.fn.vAlign.defaults, options);
	return this.each(function(i){
	var ah = jQuery(this).height();
	var ph = jQuery(this).parent().height();
	var mh = (ph - ah) / 2;
	mh = mh + opt.offset;
	jQuery(this).css('padding-top', mh);
	});
};
jQuery.fn.vAlign.defaults = {
	"offset":0
};

jQuery.fn.homePage = function(options){
	var opts = jQuery.extend({},jQuery.fn.homePage.defaults, options);
	var $thisHomePage;
	var $parent;
	var	$prev;
	var $children;
	var panelIndex;
	var $child;
	var panelStyle;
	var panels = [];
	var i;
	return this.each(function(){
		$thisHomePage = jQuery(this);
		$sourceMarkup = $thisHomePage.clone();
		$parent = $thisHomePage.parent();
		$prev = $thisHomePage.prev();
		$children = $thisHomePage.children().detach();
		panelIndex = 0;
		$children.each(function(index,value){
			$child = jQuery(this);
			if($child.is("h1") || $child.is("h2")){
				panelIndex = panelIndex + 1;
				panelStyle = $child.attr('class');
				panels[panelIndex] = jQuery(document.createElement("div")).attr({'class':'hp-panel hp-panel-'+panelIndex+' '+panelStyle});
				$child.removeAttr('class').appendTo(panels[panelIndex]);
			} else {
				$child.appendTo(panels[panelIndex]);
			}
		});
		for (i = 0; i <= panels.length; i++){
			jQuery(panels[i]).wrapInner("<div class='inner-panel'></div>");
			$thisHomePage.addClass('hp-wrap').append(panels[i]);
		}
		if($prev.length){
			$thisHomePage.insertAfter($prev);
		} else {
			$thisHomePage.prependTo($parent);
		}
			return $sourceMarkup;
	});
};
jQuery.fn.homePage.defaults = {
	"option":null
};

jQuery.fn.sameHeight = function(options){
	var opts = jQuery.extend({},jQuery.fn.homePage.defaults, options);
	var $this;
	var $firstChild;
	var $thisChild;
	var $children;
	var $totalHeight;
	var height;
	var totalSiblingHeight;
	return this.each(function(){
		$this = jQuery(this);

		$panelOne = $this.find(".hp-panel-1");
		panelOneHeight = $panelOne.outerHeight();
		$siblingPanels = $this.find(".hp-panel").not(".hp-panel-1, :hidden");
		$(".panel-one-log").find("span").text(panelOneHeight);
//		if($siblingPanels.length){
//			return;
//		}
		totalSiblingHeight = 0;
		$siblingPanels.each(function(i){
			$thisSibling = $(this);
			thisSiblingHeight = $thisSibling.outerHeight();
			totalSiblingHeight = totalSiblingHeight + thisSiblingHeight;
		});
		$lastSibling = $siblingPanels.last();
		lastSiblingHeight = $lastSibling.outerHeight();
		$(".right-panels-log").find("span").text(totalSiblingHeight);
		if(panelOneHeight > totalSiblingHeight){
			finalHeight = (panelOneHeight - totalSiblingHeight) + lastSiblingHeight;
			$lastSibling.height(finalHeight)
		} else if(panelOneHeight < totalSiblingHeight){
			$panelOne.height(totalSiblingHeight);
		} else {
			return;
		}
	});
};
jQuery.fn.sameHeight.defaults = {
	"option":null
};


jQuery.fn.fetchBlogger = function(options){
    var opts = jQuery.extend({}, jQuery.fn.fetchBlogger.defaults, options);
    var $this;
    var feedData;
    var content;
	var $fetchedContent;
    var onSuccess = function(feedData){
		$fetchedContent = jQuery("<div></div>");
        jQuery(feedData.feed.entry).each(function(){
            thisEntry = this;
            $entryWrapper = jQuery("<div></div>").attr({
					"class": opts.prefix + "entry entry"
				});
//	console.log(thisEntry);
            $entryTitle = jQuery("<h3 class='entry-title'>" + thisEntry.title.$t + "</h3>");
            entryPubDate = jQuery("<p class='entry-date'>"+thisEntry.published.$t+"</p>");
            $entryContent = jQuery("<div class='entry-content'>" + thisEntry.content.$t + "</div>");
            $entryContent.prepend(entryPubDate);
			$entryTitle.prependTo($entryContent);
			$entryWrapper.append($entryContent);
			if(opts.cleanimg === true){
				$entryContent.find("img").removeAttr("style");
			};
			$entryWrapper.appendTo($fetchedContent);
        })
		return $fetchedContent;
    };
    return this.each(function(){
        $this = jQuery(this);
        jQuery.ajax({
            url: opts.url,
            dataType: "jsonp",
            data: ({
                alt: "json",
                prettyprint: true
            }),
            contentType: "application/x-www-form-urlencoded",
            type: "GET",
            success: function(feedData){
//				console.log(feedData);
                $fetchedContent = onSuccess(feedData);
//	console.log($fetchedContent);
				$fetchedContent.attr({
					"id":opts.prefix + "wrapper"
				});
                $this.html($fetchedContent);
            },
            error: function(xhr, textStatus, errorThrown){
                console.log("textStatus: '%s', errorThrown: '%s'", textStatus, errorThrown);
                console.log("xhr.status:%s", xhr.status);
            }
        });
    });
};
jQuery.fn.fetchBlogger.defaults = {
    "url": null,
    "prefix": "fetchBlogger-",
	"cleanimg": true
};