/** Global modifications for the MassVOTE child theme of Widezine
Author: Adam Friedman
Date: 2011-06-12


**/



/**
Get text actual width with JQuery // Added by AMF 
Purpose: to do some hackish calculations to push the sidebar h4 stripe background
to the right so it is not laying behind the text. 
**/
jQuery.fn.textWidth = function(){
  var html_org = jQuery(this).html();
  var html_calc = '<span>' + html_org + '</span>'
  jQuery(this).html(html_calc);
  var width = jQuery(this).find('span:first').width();
  jQuery(this).html(html_org);
  return width;
};
 
 
/**
 * Widget header stripes push past the text
 *
 */
function adjustWidgetTitleStripes() {
	
	jQuery('#sidebar .widget h4 span, #footer .widget h4 span').each(function(k,v) {
		//console.log('elem '+v);
		w  = jQuery(v).textWidth() * 1;
		w  = w - 6; 
		h4 = jQuery(v).parent();
		
		// Adjust x-position for Safari and IE7+
		if( jQuery.browser.safari || (jQuery.browser.msie && jQuery.browser.version > 6 )) {
			w = w + 14; ///////
		}

		h4.css('background-position', w+'px center');	
	});	
}



jQuery(document).ready(function() {

	//console.log('ready....');
	
	
	// Graphic tease text - reduce font size for preposition words if the text width > n px  
	jQuery('.graphic_tease .title span').each(function(k,v) {
		
		w  = jQuery(v).textWidth() * 1;
	 	
		//console.log(w);
		
		if(w > 142) {
			 
			jQuery(['for','or','in','at','a','on','to']).each(function(k2,word) {
				//console.log(word);
				new_text = jQuery(v).html().toUpperCase().replace(' '+word.toUpperCase()+' ',' <span style="font-size:10px;">'+word+'</span> ');
				 
				jQuery(v).html(new_text);
				
				//console.log('results: '+w);
			});
		}
	});
	
	
	adjustWidgetTitleStripes();
	
	
	if(jQuery.browser.msie && jQuery.browser.version < 9) {
	
		// Transform the last three main navbar links into colored buttons
		// ** This is for IE only (since it doesnt support the CSS selector :nth-last-child)
		var n_nav_links = jQuery('#nav > ul > li').length;
		
		var mod = (n_nav_links - 2) * 1;
		
		jQuery('#nav > ul > li:nth-child(n+'+mod+')').each(function(k,v) {
			x = jQuery(v);
			
			// Forbid submenus
			x.children('ul').css({ display: 'none !important' });
			
			var bg_colors = ['green','orange','blue'];
		
			x.css({float:'right', background: 'url(/wp-content/themes/widezine-massvote/assets/btn_bg_'+bg_colors[k]+'.png) center 45% no-repeat' });
			
		}); // END nav buttons
		
		// Transform the ribbon labels for the graphic teases in the upper-right (IE <9)
		jQuery('.graphic_tease .title span:nth-child(n)').each(function(k,v) {
			x = jQuery(v);
			
			// these colors match those in the main CSS file
			var bg_colors = ['#393939', '#418419','#145A9C','#A60000'];
			
			x.css({backgroundColor: bg_colors[k] });
			
		}); // END graphic teases
		
	 
 
	} // END if browser is IE  
	
	
	// For lazy loading widgets, run again
	setTimeout('adjustWidgetTitleStripes();',2000);
});



