// JavaScript Document

function resizePage(){
    if(jQuery('#dsq-content-stub') && jQuery('#dsq-content-stub').css('display')!='none'){
        var itsPadding = 10;
    	var itsHeight = 236;
    	var contentHeight = jQuery("#contentMain").height();
    	if (contentHeight < itsHeight) {
    		jQuery("#contentMain").height(itsHeight-itsPadding);
    	} else {
		    jQuery("#contentMain").height(Math.ceil(contentHeight/itsHeight)*itsHeight-itsPadding);
    	}
    } else {
        resizePage();
    }
}

jQuery(document).ready(function(){
		//postTitle function
	jQuery(function(){
		jQuery('.postTitle a').each(function(){
			var title = jQuery(this);
			var newSize=21;
			var titleText=title.text();
			if(titleText.length>60) {
				step1 = titleText.substring(0,60);
				lastSpace = step1.lastIndexOf(" ");
				title.text(titleText.substring(0,lastSpace));
				title.append("...");
			}
			while(title.height()>25){
				title.css({fontSize:newSize+"px"});
				newSize--;
			}
			if(title.width()>410){
				while(title.width()>410){
					title.css({fontSize:newSize+"px"});
					newSize--;
				}
			}
			title.css({lineHeight:"26px"});
		});
	});

	//nav buttons
	jQuery("#mainNav a").mouseover(function(){
		var thisButton = jQuery(this).attr("id");
		jQuery("#"+thisButton+"Arrow").css({visibility:"visible"});
	}).mouseout(function(){
		var thisButton = jQuery(this).attr("id");
		if(jQuery("#"+thisButton+"Arrow").attr("title")!="active"){
			jQuery("#"+thisButton+"Arrow").css({visibility:"hidden"});
		}
	});
	
	//twitter function to adjust fontSize
	var tweetHeight = jQuery("#latestTweet").height();
	if(tweetHeight < 12) {
		jQuery("#latestTweet").css({fontSize:"15px", lineHeight:"16px", paddingTop:"0px" }).fadeIn(1500);
	} else if(tweetHeight < 23) {
		jQuery("#latestTweet").css({fontSize:"12px", lineHeight:"13px", paddingTop:"3px" }).fadeIn(1500);
	} else if(tweetHeight > 34) {
		jQuery("#latestTweet").css({fontSize:"10px", lineHeight:"12px", paddingTop:"0px" }).fadeIn(1500);
	} else {
		jQuery("#latestTweet").fadeIn(1500);	
	}
	
	//facebook and rss icons
	var iconArray = [];
		iconArray["facebookOver"] = "top";
		iconArray["facebookOut"] = "center -23px";
		iconArray["rssOver"] = "center -47px";
		iconArray["rssOut"] = "center -73px";
	
	jQuery(".icons").hover(function(){
		var thisIcon = jQuery(this).attr("title");	
		jQuery(this).css({ background:"url(wordpress/wp-content/themes/ensigo_sm/style/images/icons.png) "+iconArray[thisIcon+"Over"] });
	}, function(){
		var thisIcon = jQuery(this).attr("title");	
		jQuery(this).css({ background:"url(wordpress/wp-content/themes/ensigo_sm/style/images/icons.png) "+iconArray[thisIcon+"Out"] });			
	});
	
	//comment alternating backgrounds
	jQuery("#commentList li:even").addClass("commentbg");
	
	
	//categories
	var catArray = [];
		catArray[1]="news";
		catArray[3]="media";
		catArray[4]="fun";
		catArray[5]="art";
		catArray[6]="writing";
	
	for(c=1;c<7;c++) {
		jQuery(".cat-item-"+c+" a").prepend("<div class=\""+catArray[c]+"Box\"></div>");
		jQuery("cat-item-"+c).after("<br /><br class=\"clear\" />");
	}
	
	
	//img links
	jQuery(".content a img").each(function(){
  		jQuery(this).parent().css("background","none").attr("target","_blank");
  	});

	//target _blank on content links
	jQuery(".content a").each(function(){
		jQuery(this).attr("target","_blank");
	});
	
	//make sure content paper ends where it needs to end to attach to contentfooter
	var itsPadding = 10;
	var itsHeight = 236;
	var contentHeight = jQuery("#contentMain").height();
	if (contentHeight < itsHeight) {
		jQuery("#contentMain").height(itsHeight-itsPadding);
	} else {
	    if(typeof blogPost !== 'undefined'){
            // jQuery("#contentMain").height(Math.ceil(contentHeight/itsHeight)*itsHeight-itsPadding+itsHeight);
            resizePage();
		} else {
		    jQuery("#contentMain").height(Math.ceil(contentHeight/itsHeight)*itsHeight-itsPadding);
		}
	}
	
});
