//alert('fancify loaded');

//var masthead_logo = 'default';
var category_heading;
var section_heading;
var article_heading;
 

// 	ONLOAD SHOW CONTENT (after everthing has loaded)  *	*	*	*	*	*	*	*	*	*	*	*
	function onload_show_content() {

		// alert('ONLOAD: show content ');
	    $('#loading').fadeOut();
		$('#content').css('visibility','visible').hide();
		$('#content, #strip:has(.strip), #banner:has(.banner)').fadeIn(3000);
		

	};   


// 	DOCUMENT READY FUNCTION *	*	*	*	*	*	*	*	*	*	*	*
	$(document).ready(function(){
	
	    
		/* CENTER CONTENT 
		//margin_to_center = $(window).width();     
		//alert(margin_to_center);
		//margin_to_center = ( $(window).width() - ( $('#content').width() + 16 ) )/2 );
		window_width = $(window).width();
		content_width = 1006;
		margin_to_center = ((window_width - content_width)/2);
		
		alert(window_width + ' ' + margin_to_center);
		
		$('#content').css('margin-left',margin_to_center + 'px');
		
		*/ 
	   	
	
		// RELOCATE ARTICLE RESOURCES
		$('#sidebar #article_widget').html( $('#article .widget') );    
		$('#strip').html( $('#article .strip') ); 
		$('#banner').html( $('#article .banner') );
		
	
	
		// Add VERTICAL BAR to .nav items (except the last one)
		$('.nav li a:not(.nav li:last-child a)').livequery(function() {
			$(this).after('<span>&nbsp;|&nbsp;</span>'); 
		});
	
	
		// SPIFFY DESIGN
		replace_headings();           
		make_reflections(); // old reflections
		
		// make reflections (JS)
		$('.js-reflect').livequery(function(){
			$(this).reflect();  // default values  {height:75, opacity:.4} 
		}); 
		
		// strip corers and reflections

		$('.strip img').livequery(function(){
			$(this).reflect( {opacity:.3, height:40} );
		});
		

		// TOOL TIPS FOR DEFINITIONS
		$('em[data-tooltip]').livequery(function(){
			$(this).addClass('tooltip');
			tooltip();
		});
	                    
	    
		// SET WIDGET BACKGROUND (needed for browsers that don't understand border-radius) 
		$('.widget:not(#dashboard.default .widget)').livequery(function() {
			$(this).each(function() {
				var widget_height = $(this).children('.content').height();
				$(this).children('.bg').css('height',widget_height+'px');
			
				// CHANGE postioning of ul.links if category is short (to keep it at the bottom of the box)
				if (widget_height < 175) { // trial and error value
					// alert('short widget. change ul.link position');
					$(this).children('.content').children('.links').css('position','absolute');
				}

			});
		});
	
	
		// *** FANCIFY BASIC RENDERING ***

		    // Relocate #site_nav
			// $('#dashboard #site_nav_placeholder').replaceWith( $('#site_nav') );
			// temporarily diabled becasue didn't like it at the bottom
		
			/* TEMP DASHBOARD NAV, HIDE CURRENT LOCATION
			$('#dashboard_nav').livequery(function(){
				//alert(this_category)
				$(this).find('li.' + this_category).hide();
			});
			*/
	
    	
			 
 

	});            



// 	REPLACE HEADINGS *	*	*	*	*	*	*	*	*	*	*	*
	function replace_headings() {
	
		// CREATE replaceAll() method
		String.prototype.replaceAll=function(s1, s2) {return this.split(s1).join(s2)}
	
		// HIDE FIRST TO PREFENT FLASH
		$('#masthead h1, #category h2, #sidebar h3, #sidebar h4').livequery(function() {
			// DON'T INCLUDE #article h4 BECAUSE IT WILL NOT ALWAYS BE REPLACED. 
			$(this).hide().addClass('graphic');
		});
		
		// DASHBOARD DEFAULT /(dashboard) HEADINGS ARE SET ON /dashboard/scripts
	
		// REPLACE MASTHEAD HEADINGS (h1) WITH LOGO GRAPHIC 
		$('#masthead h1').livequery(function() {
			$(this)
				.empty()
				//.css('background-image','url(/content/graphics/headings/' + masthead_logo + '.png)')
				.css('background-image','url(/content/graphics/headings/default.png)')
				.show();
		});

		// REPLACE CATEGORY HEADING (h2) WITH GRAPHIC 
		$('#category #heading h2').livequery(function() {
			var heading = $(this).text().toLowerCase().replaceAll(' ','_'); 
			$(this)
				.empty()
				.css('background-image','url(/content/' + category + '/graphics/headings/' + heading + '.png)')
				.show(); 
		});
	
		// REPLACE SECTION-WIDGET HEADINGS (h3) WITH GRAPHIC
		$('#sidebar .section.widget h3').livequery(function() {
			var heading = $(this).text().toLowerCase().replace(' ','_').replace('.',''); 
			$(this)
				.empty()
				.css('background-image','url(/content/' + category + '/' + section + '/graphics/headings/' + heading + '.png)')
				.show();
		});
	
		// REPLACE ARTICLE-WIDGET HEADINGS (h4) WITH GRAPHIC
		$('#sidebar .article.widget h4').livequery(function() {
			var heading = $(this).text().toLowerCase().replace(' & ','+').replaceAll(' ','_').replaceAll('.',''); 
			// common graphic - category/graphcis/headings
			if ( $(this).hasClass('.common.graphic')) { 
				$(this)
					.empty()
					.css('background-image','url(/content/' + category + '/graphics/headings/' + heading + '.png)')
					.show();
			}
			// default overide (party, marcello_diaz/help_fund) section/graphics/headings
			else if (this_article == 'default') {
				$(this)
					.empty()
					.css('background-image','url(/content/' + category + '/' + section + '/graphics/headings/' + heading + '.png)')
					.show();
			} 
			// normal article/graphics/headings
			else {
				$(this)
					.empty()
					.css('background-image','url(/content/' + category + '/' + section + '/' + article + '/graphics/headings/' + heading + '.png)')
					.show();
			}
		});


		// REPLACE ARTICLE-CONTENT HEADINGS (h4) WITH GRAPHIC
		$('#article h4.graphic').livequery(function() {
			// HARDCODE .graphcic in HTML IF IT'S TO BE REPLACED
			var heading = $(this).text().toLowerCase().replace(' ','_').replace('.',''); 
			$(this)
				.empty()
				.css('background-image','url(/content/' + category + '/' + section + '/' + article + '/graphics/headings/' + heading + '.png)')
				.show();
		});
	 

	}

    

// 	MAKE REFLECTIONS *	*	*	*	*	*	*	*	*	*	*	*
	function make_reflections() {

		$('.reflect').livequery(function() {
	     
			// NOTE, .reflect must be placed in the desired location of the document flow
			// you can't move it on document.ready because text will reflow.
		
			// NOTE: to prevent FLASH of image before applying reflection, 
			// .reflect img must be set to visibility:hidden (on fancy.css) 
			// (if set with js, we'll get the FLASH)
			// if reflection fails, set visibilty back to visible.
			// After setting img_type + img_src, Set img to display:none; to prevent it from taking space
		 
	
			var img_elem = $(this).find('img');
			var img_src = img_elem.attr('src');
			var img_credit = $(this).find('p.credit'); 	
	        var img_type = img_elem.attr('type'); ;
			var reflection;

			// DEFINE IMAGE TYPE (img_type)
			if (img_type == null) { 
		        if ((img_elem.attr('width') == 390) && (img_elem.attr('height') == 530 )) img_type = 'ad';				
		        else if (img_elem.attr('width') == 390) img_type = 'photo';  
				else if (img_elem.attr('width') == 610) img_type = 'poster';  
				else if (img_elem.attr('width') == 930) img_type = 'spread'; 
				else if (img_elem.attr('width') == 290) img_type = 'widget';
			} 	
			// alert('img_type = ' + img_type);                                            

			// MAKE REFLECTION
			if (img_type != null) {
		
				reflection = new Image();
				reflection.src = img_src.replace('.jpg','-r.png');
				$(this)
					// .prependTo('#article .article')
					.addClass(img_type)  
					.html(
						'<div class="'+ img_type +' corners" style="background:url('+ img_src +');"></div>' +
						'<div class="reflection corners" style="background:url('+ reflection.src +');"></div>'
					)
					.append(img_credit);
		
				// LATER...
					// add code that checks for image + reflection to be completely loaded 
					// on_complete() {  fadeIn() }

				$(this).hide().fadeIn(3000);

		   	} 
			else {
				// FAIL-SAFE: if no img_type, show plain
				img_elem.css('visibility','visible');  
			}    

		});	


	}   



