$(function() {

	$('span.mt-enclosure-image img').each(function(){
		// Remove the link, if there is one
		$(this).unwrap('a');
		
		// Set some variables
		var image = $(this),
			parent_span = image.parent('span'),
			img_width = image.width(),
			width_adjusted = img_width + 14,
			applied_style = image.attr('style'),
			caption = image.attr('alt');
		
		// Wrap the parent span in a div & set it's width based on the image width
		parent_span.wrap('<div class="content-image"></div>');
		var parent_div = parent_span.parent('div');
		
		// Insert the caption from the Alt text
		parent_div.css('width', width_adjusted).append('<p>' + caption + '</p><div class="clear"></div>');
		parent_div.wrap('<div>');
				
		var behavior = parent_div.parent('div');
		behavior.attr({ style: applied_style });
		
		// Remove the image style attribute
		image.removeAttr('style');
	
	});
	
	$("p.searcharchives a").toggle(function(){
		//mimick checkbox
		$(this).addClass("checked");
		
		//add hidden fields
		$("#search form").prepend('<input name="site" type="hidden" value="default_collection" /><input name="client" type="hidden" value="default_frontend" /><input name="output" type="hidden" value="xml_no_dtd" /><input name="sitesearch" type="hidden" value="www.usc.edu/uscnews/stories" />');
		$('#search form input[name="IncludeBlogs"], #search form input[name="limit"]').remove();
		
		//change form action
		$("#search form").attr("action","http://uscnews.usc.edu/search/index.php");
		$("#search form input#searchbox").attr("name","q");
		$("#search form input#searchbox").attr("value","SEARCH NEWS ARCHIVE");
		return false;
	},
	function(){
		 
		//mimick empty checkbox
		$(this).removeClass("checked");
		
		//remove hidden fields
		$('#search form input[name="site"], #search form input[name="client"], #search form input[name="output"], #search form input[name="sitesearch"]').remove();
		$("#search form").prepend('<input type="hidden" name="IncludeBlogs" value="71" /><input type="hidden" name="limit" value="10" />');
		
		//change form action back
		$("#search form").attr("action","http://uscnews.usc.edu/mt/mt-search.cgi");
		$("#search form input#searchbox").attr("name","search");
		$("#search form input#searchbox").attr("value","SEARCH USC NEWS");
		return false;
	});//
		
	
	//hiding stuff onLoad
	//$('li#forjournalists ul').hide();
	
	$('li#related-stories, li#latest-stories, li#inthenews_from88').after('<div class="gap"></div>');
	
	$('#share ul').hide();
	
	$('#share a.action').click(function(){
		$('#share ul').slideToggle('slow');
		return false;
	});//

	//RSS feeds footer menu
	$('.feeds h3 a').mouseover(function(){
		$('#menu-rss').slideToggle();
		$(this).css('border','1px solid #999');
		return false;
	});//
	
	$('.feeds h3 a').click(function(){return false;	});
	
	$('body, html').click(function(){
		$('#menu-rss').hide();
		$('.feeds h3 a').css('border','1px solid #ededed');
	});//
	
	/* Clears out the search box */
	$("#search input").focus( function() {if ($(this).val()=="SEARCH USC NEWS") {$(this).val("");}}).blur( function() { if ($(this).val()=="") {$(this).val("SEARCH USC NEWS"); }});
	$("#search input").focus( function() {if ($(this).val()=="SEARCH NEWS ARCHIVE") {$(this).val("");}}).blur( function() {	if ($(this).val()=="") {$(this).val("SEARCH NEWS ARCHIVE");}});

	//USC in the News
	$('#news-usc li p').expander({
		slicePoint: 150, 
		widow: 2,
		expandText: '[show more]',
		expandPrefix: '... ',
		expandEffect: 'fadeIn',
		expandSpeed: '100',
		userCollapse: 'true',
		userCollapseText: '[show less]',
		userCollapsePrefix: ' '
	});//
	
	//load youtube movie	
	$("#youtube #play a").each(function(){
		var movieid = $('#youtube #play p.title a').attr("title");
		$('#youtube img').hide();
		$(this).before('<object width="255" height="155"><param name="movie" value="http://www.youtube.com/v/'+movieid+'"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/'+movieid+'" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="255" height="155"></embed></object>');
		return false;
	});//

}); // End loaded jQuery

//Equal hieght columns
matchColumns=function(){ 
     var divs,contDivs,maxHeight,divHeight,d; 
     divs=document.getElementsByTagName('div'); 
     contDivs=[]; 
     maxHeight=0; 
     for(var i=0;i<divs.length;i++){ 
          if(/\bstretch\b/.test(divs[i].className)){ 
                d=divs[i]; 
                contDivs[contDivs.length]=d; 
                if(d.offsetHeight){ 
                     divHeight=d.offsetHeight; 					
                }
                else if(d.style.pixelHeight){ 
                     divHeight=d.style.pixelHeight;					 
                }
                maxHeight=Math.max(maxHeight,divHeight); 
          }
     }
     for(var i=0;i<contDivs.length;i++){ 
          contDivs[i].style.height=maxHeight + "px"; 
     }
}

// Runs the script when page loads 
window.onload=function(){ 
     if(document.getElementsByTagName){ 
          matchColumns();			 
     } 
}
