//Code for homepage video
function getVideoCode(){
	var videoCode = '';
	videoCode += '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="180" height="150">';
	videoCode += '<param name="movie" value="http://advancedmethod.http.internapcdn.net/advancedmethod_vitalstream_com/rsi/blue_bannerembed.swf" />';
	videoCode += '<param name="quality" value="high">';
	videoCode += '<param name="allowscriptaccess" value="always">';
	videoCode += '<param name="FlashVars" value="URL=http://yourrsivideo.com/rsi/interface.aspx%3f%26intro=%26settingspath=http://yourrsivideo.com/rsi/CentennialRoofing/settings.xml&WinParams=toolbar=0,location=0,scrollbars=0,width=780,height=550">';
	videoCode += '<embed src="http://advancedmethod.http.internapcdn.net/advancedmethod_vitalstream_com/rsi/blue_bannerembed.swf" FlashVars="URL=http://yourrsivideo.com/rsi/interface.aspx%3f%26intro=%26settingspath=http://yourrsivideo.com/rsi/CentennialRoofing/settings.xml&WinParams=toolbar=0,location=0,scrollbars=0,width=780,height=550" quality="high" allowscriptaccess="always" pluginspage="http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash" type="application/x-shockwave-flash" width="180" height="150"></embed>';
	videoCode += '</object>';
	return videoCode;
}
		


function playInfoVideo(){
	swfobject.embedSWF( TEMPLATE_URL + "/swf/videoplayer.swf", "video_player_container", "426", "240", "8.0.0", null, {
		allowFullScreen : true,
		videoLink : /*TEMPLATE_URL + */'http://google.com/asdvasdasdfvsadfvdfv.flv',
		videoVolume : 75,
		autoplay : true
	});
}

$j = jQuery;
$j(window).load(function(){
	$j('#side_accordion').accordion({
		event: 'mouseover',
		header: 'h3.head',
		selectedClass: 'selected',
		active: '.active',
		autoHeight : false
	});					 
});

function createPageIndex(){
	pageIndex = [];
	
	//Add anchors to H3s and insert name and text into array
	$('.content-main h1, .content-main h2, .content-main h3, .content-main h4, .content-main h5').each(function(count){
		if( !$(this).hasClass('page-header') ){ //Don't add page header																								
			$(this).wrapInner('<a name="content_section_' + count + '" href="#content_section_' + count + '"></a>');
			//$(this).attr('name', 'content_section_' + count);
			pageIndex.push([$(this).text(), 'content_section_' + count]);
		}
	});
	
	//Create links to h3 sections
	pageIndexListItems = '';
	jQuery.each(pageIndex, function(){
		pageIndexListItems += '<li><a href="#' + this[1] + '">' + this[0] + '</a></li>';
	});
	
	pageIndexListItems = $("<div class='page-index'><h3>Content Index</h3><div class='content'><p>Click any link below to jump to that section:</p><ul>" + pageIndexListItems + "</ul></div></div>");
	
	if(pageIndex.length < 5){ pageIndexListItems = ''; }
	
	return pageIndexListItems; 
}

$j(document).ready(function(){
	
	$j('#video_player').css({opacity: 0, display: 'none', top: -300}); //Set styles inline so that the fading effect works
	$j('#watchVideo').click(function(){
		if($j('#video_player').css('display') == 'none'){
			//Show video player
			$j('#video_player').css('display', 'block');
			$j('#video_player').animate({top: 0, opacity: 1}, 500, null, function(){
				$j('#video_player_container').html(getVideoCode()); //Add code to video player
				//playInfoVideo()
			});
			$j('#watchVideo').text('Hide video');
			
		} else {
			//Hide video player
			$j('#video_player_container').html(''); //Empty video player
			$j('#video_player').animate({top: -300, opacity: 0.3}, 500, null, function(){
				$j('#video_player').css('display', 'none');
			});
			$j('#watchVideo').text('Show video');
			
			$j.cookie('show_video', 'no', { expires: 30, path: '/' }); //If player closes video once, then don't play on future visits to site
			
		}
	});
	
	//Show video if on homepage
	if($j('body.home').length > 0 && $j.cookie('show_video') != 'no'){
		$j('#watchVideo').trigger('click');
	}
	
	$j('#close_video_player').click(function(){ $j('#watchVideo').trigger('click')} );
	
	
	$j.fn.fadeOnHover = function(el){
		return this.each(function(){
			$j(this).hover(
				function() { $j(this).animate({opacity: 1}, 300); },
				function() { $j(this).animate({opacity: 0}, 300); }
			);
		});
	};
	
	if($j.browser.mozilla){
		//Fade the logo and make it darker on hover
		$j('#logo a span').css({opacity : '0', visibility: 'visible'});
		$j('#logo a span').fadeOnHover();
	}
	
	//Add "rel" attribute to links that have images in them.  This makes links that link to images load in a lightbox (used in conjunction with a plugin
	$('.content-main a img').each(function(){
		parentAnchor = $(this).parent('a:first');
		if( parentAnchor.attr('href').search(/\.jpg|\.gif|\.png/) > 0){ // If link is linking to an image
			parentAnchor.attr('rel', 'lightbox');	
		}
	});	
	
	$j.fn.hoverClass = function(c) {
		return this.each(function(){
			$j(this).hover( 
				function() { $j(this).addClass(c);  },
				function() { $j(this).removeClass(c); }
			);
		});
	};
	
	
	//if(jQuery().elExists([ 'body.home', 'body.about' ]) ){
		//Inject page index into main content section
		$('h2.page-header').after(createPageIndex());
	//}
	
	//If main content section has more than 300 words, insert a "Top of page" link
	//console.log($('.content-main').text().length);
	if( $('.content-main').text().length > 2000 ){
		$('.post').append('<a href="#">Return to top of page</a>');
	}
	
	
	$j.localScroll({duration: 300, hash: true}); //Make anchors scroll

});

jQuery.fn.elExists = function(el){
	if(typeof(el) == 'object'){
		console.log('checking array');
		jQuery.each(el, function(){
			console.log('checking array item: ' + this);
			if($(this).length > 0){ console.log('returning true: ' + this + ' ' + this.length); return true; } //TODO: THis is always returning 1, it should be returnin 0
		});
		return false;
	} else {
		console.log('checking string');
		if($(el) > 0){ 
			return true; 
		} else {
			return false;	
		}
	}
}

$(document).ready(function() {

	if($.cookie("opt_in_popup") != 'true')
		{
		
		$("#opt_in_popup").fancybox({
			'width'					: 	600,
			'height'				: 	550,
			'enableEscapeButton'	:	false,
			'hideOnOverlayClick'	:	false,
			'padding'				: 	0,
			'showCloseButton'		:	true,
			'autoScale'				: 	false,
			'transitionIn'			: 	'none',
			'transitionOut'			: 	'none',
			'centerOnScroll'		:	'true',
			'overlayColor'			:	'#40392e',
			'overlayOpacity'		:	'0.5',
			'scrolling'				:	'no',
			'type'					: 	'iframe'
		}).trigger('click');
		
		$('#fancybox-close').live('click', function(e) {
			e.preventDefault();
			$.cookie("opt_in_popup", "true", { path: '/', expires: 7 });    
			$.fancybox.close()
		});
		
	}
	
});