var t = new Date();time = t.getTime();

/* Helper Functions */
function add_js( url ) { var d = document.createElement('script'); d.src = url; d.type = 'text/javascript'; document.getElementsByTagName('head')[0].appendChild( d ); }
function add_stylesheet( url ) { var d = document.createElement('link'); d.href = url; d.rel = 'stylesheet'; d.type = 'text/css'; document.getElementsByTagName('head')[0].appendChild( d ); }
function add_iframe( src, width, height, appendTo ) { var d = document.createElement('iframe'); d.src = src; d.rel = 'stylesheet'; d.type = 'text/css'; appendTo.appendChild( d ); }

// and now some code
var prefix = '';

// some terms of services + privacy policy
if( document.getElementById('xg').className.indexOf('xg_widget_main_authorization_privacyPolicy') > 0 || document.getElementById('xg').className.indexOf('xg_widget_main_authorization_termsOfService') > 0 ) {
	var p = document.createElement('p');
	p.innerHTML = 'Please view the extended Rocawear <a href="http://www.roc4life.com/page/terms-of-use">Terms of Use</a> and <a href="http://www.roc4life.com/page/terms-of-use">Privacy Policy</a>';
	document.getElementById('xg_body').appendChild(p);
}


// add_stylesheet('http://' + prefix + 'roc4life.ginlanemedia.com/includes/css/site-wide.css');
add_js('http://' + prefix + 'roc4life.ginlanemedia.com/includes/js/roc4life.js');

jQuery(document).ready(function($) {

	$('#xg_foot a[href="http://www.roc4life.com/main/embeddable/list"]').remove();
	$('#xg_foot p.right').prepend('<a href="http://www.roc4life.com/page/advertise-on-roc4lifecom">Advertise</a> | <a href="http://www.roc4life.com/page/about-roc4life">About Roc4life</a> | <a href="http://www.roc4life.com/page/contact-us">Contact Us</a>');
	
	if( $('.xg_widget_main_authorization_privacyPolicy').length > 0 ) {
		$('<div class="load-more"></div>').appendTo('.xg_module_body')
	}

	$('.xg_widget_page_index_index ul.navigation').addClass('easyclear');

	// Check if the current page is a blog page.
	if( $('.xg_widget_profiles_blog').length > 0 ) {
		
		var max_width = 483;
		$('.xg_blog .xg_module_body p img,xg_widget_profiles_blog_show .xg_module_body p img').each(function() {
			if( $(this).attr('width') > max_width ) { $(this).width( max_width ).height( 'auto' ); }
			
			$(this).parents('p').eq(0).css('textAlign', 'center');
		});
	
		// add in blog roll on homepage
		$('.xg_widget_profiles_blog #xg_body > .xg_span-4').append('<div class="xg_module blogroll"><div class="xg_module_head"><h2>Blogroll</h2></div><div class="xg_module_body"></div></div>');

		$('.xg_widget_profiles_blog .blogroll .xg_module_body').load('/page/roc4lifes-official-blog-roll .description', function() {
			$('.xg_widget_profiles_blog .blogroll .xg_module_body ul li:gt(5)').remove();
			$('.xg_widget_profiles_blog .blogroll .xg_module_body').append('<a href="/page/roc4lifes-official-blog-roll" class="view-more">View More</a>')
		});
		
		$('.xg_widget_profiles_blog .module_searchbar').appendTo( '.xg_widget_profiles_blog .xg_headline .tb' );
		
		$('.xg_headline-img').removeClass('xg_headline-img').find('.ib').remove();
		
		$('.xg_widget_profiles_blog_show #xg_body > .xg_column:first').prepend('<div class="xg_module check_out_more"></div>');
		
		var username = $('.xg_widget_profiles_blog_show .xg_module .navigation li:first a:not(:first):not(:last)').text();
		var blog = $('.xg_widget_profiles_blog_show .xg_module .navigation li:last a:last').attr('href');
		var title = $('.xg_widget_profiles_blog_show .xg_module h1').text();
		var ext = '';
		
		if( $('.xg_widget_profiles_blog_list .xg_blog .xg_module_body p.small a:first').length > 0 ) {
			var username = $('.xg_widget_profiles_blog_list .xg_blog .xg_module_body p.small a:first').html();
			var ext = '-without';
			$('<div class="banner" style="background:#666;"></div>').insertBefore( $('.xg_span-16 .xg_blog_list') );
		}
		
		if( window.location.href.indexOf('?promoted=1') >= 0 ) {
			username = '';
		}
		
	  if( username != 'Pac Man' ) {
	    add_js('http://pipes.yahoo.com/pipes/pipe.run?_id=55d9125b078d28b9c8e83281c797227b&_render=json&_callback=blogpage_sidebar&t=' + time);
	    
    	$('.xg_widget_profiles_blog_list .xg_span-16 .xg_span-4.xg_last').find('.xg_module:eq(0),.xg_module:eq(1)').remove();
	  }
		
		var img = '';
		var width = '397';
		var target = '';
		var e_class = '';
		
		switch( username ) {
			case 'Entertainment':
				img = 'http://' + prefix + 'roc4life.ginlanemedia.com/images/blog-entertainment' + ext + '.jpg';
				blog = 'http://bit.ly/R4LEnter';
			break;
			case 'Fashion':
				img = 'http://' + prefix + 'roc4life.ginlanemedia.com/images/blog-fashion' + ext + '.jpg';
				blog = 'http://bit.ly/R4LFashion';
			break;
			case 'Top News':
				img = 'http://' + prefix + 'roc4life.ginlanemedia.com/images/blog-news' + ext + '.jpg';
				blog = 'http://bit.ly/R4LNews';
			break;
			case "WHAT'S THE WORD?":
				img = 'http://' + prefix + 'roc4life.ginlanemedia.com/images/blog-whats-the-word' + ext + '.jpg';
				blog = 'http://bit.ly/R4LWord';
			break;
			case 'Pac Man':
			 img = 'http://www.roc4life.com/xn_resources/images/565x247.jpg';
			 blog = 'http://pacman.com/en/';
			 width = '559';
			 target = 'target="_blank"';
			 e_class = 'pacman_banner';
		  break;
			
			default:
				$('.xg_module.check_out_more').html('<a href="' + blog + '"><span class="left">' + username + ' <span>Blog</span></span><span class="right">Check out<br /><span>More Posts</span></span></a>');
			break;
		}
		
		if( img != '' && ext == '' ) {
			$('.xg_module.check_out_more')
				.removeClass('check_out_more')
				.addClass(e_class)
				.css('padding', 5)
				.html('<a href="' + blog + '" ' + target + '><img width="547" src="' + img + '" /></a>');
		}
		
		if( img && ext != '' && username != '' ) {
			$('.banner')
				.addClass(e_class)
				.html('<img width="' + width + '" style="display:block;min-width:' + width + 'px !important" src="' + img + '" />');
				
		  if( blog ) {
		    $('.banner img').wrap('<a href="' + blog + '" ' + target + '></a>');
		  }		
		
			$('.xg_widget_profiles_blog_list .xg_headline').hide();
		}
		
		$('.xg_widget_profiles_blog_show .xg_column .navigation li:first').html('<a href="' + blog + '">View the ' + username + ' blog</a>');
		
		// move the twitter share up
		if( $('.xg_widget_profiles_blog').hasClass('xg_widget_profiles_blog_show') ) {
			$('<div class="social-media"></div>').appendTo('.xg_headline .tb');
			$('.post_to_twitter').prependTo('.social-media').html('Retweet').remove();
			$('<iframe src="http://api.tweetmeme.com/button.js?url=' + window.location.href + '&style=compact&source=Roc4Life&service=bit.ly" height="20" width="90" frameborder="0" scrolling="no" />').prependTo('.social-media');
		
			$('<a href="http://www.facebook.com/sharer.php?u=' + window.location.href + '&t=' + title + '" class="post_to_facebook" target="_blank">Share</a>').prependTo('.social-media');
		}
		
	}
	
	// add in video at the top on "video_index"
	if( $('.xg_widget_video_index_index #xg_body .xg_column.xg_span-16 .xg_headline:first').length > 0 ) {
		$('<div class="xg_module video_index_video embed_extra_media"></div>').insertBefore('.xg_widget_video_index_index #xg_body .xg_column.xg_span-16 .xg_headline:first').load('/page/featured-video .xg_module_body.wpage .description > *');
	}
	
	// add in photo embed at the top of the "photo_index"
	if( $('.xg_widget_photo_index_index #xg_body .xg_column.xg_span-16 .xg_headline:first').length > 0 ) {
		$('<div class="xg_module photo_index_photo embed_extra_media"></div>').insertBefore('.xg_widget_photo_index_index #xg_body .xg_column.xg_span-16 .xg_headline:first').load('/page/featured-photo .xg_module_body.wpage .description > *');
	}
	
	$('.xg_widget_photo_index_index .xg_list_photo_main li .ib a img').each(function() {
		$(this)
			.attr('src', roc4life.image_resize( $(this).attr('src'), 139, 104, '139:104' ) )
			.attr('width', 139 )
			.attr('height', 104 );
	});
	
	// if an "admin"
	if( $('#xg_tab_manage').length > 0 ) {
		$('#xn_bar_menu_tabs').prepend('<li><a href="/main/admin/manage">Manage</a></li>');
		
		$('<p><label for="post-snippet">Snippet</label><input type="text" style="width: 98%;" size="64" class="textfield" id="post-snippet"/></p>').insertAfter( $('.xg_widget_profiles_blog_new  #post_form fieldset:first p:first') );

		$('#post-snippet').keyup(function() {
			// var content = $($('#post_body').val());
			// content = content.find('span:first').remove();
			// content = content.html();
			
			$('#post_body').val( '<span class="caption">' + $(this).val() + '</span>' ); //  + ( content ? content : '' )
		})
		
	}
	
	if( window.location.href.substr(32,6) == 'PacMan' ) {
		jQuery('#xg_layout_column_2').prepend('<a href="http://bit.ly/PACROC" target="_blank"><img width="545" height="auto" style="margin-left:2px;max-width:545px;" src="http://roc4life.ginlanemedia.com/images/565x247.jpg" /></a>');
	}
});

// blogpage_sidebar
function blogpage_sidebar( data ) {
	(function($) {
		var entries = roc4life.parse_feed( data, 10 );
		
		if( $('.xg_widget_profiles_blog .xg_span-16 .xg_span-4:not(.adminbox)').length <= 0 ) {
			$('<div class="xg_column xg_span-4 xg_span-sidebar"></div>').insertAfter( $('.xg_widget_profiles_blog .xg_span-16').removeClass('xg_span-16').addClass('xg_span-12') );
		}
		
		$('.xg_widget_profiles_blog .xg_span-16 .xg_span-4, .xg_span-sidebar').prepend('<div class="xg_module sidebar"></div>');
		
		$(entries).each(function(n) {
			var entry = $(this).get(0);
			
			var html = '';
			html += '<div class="article article-' + n + '">';
				html += '<div class="xg_module_head">';
					html += '<h2><a href="' + entry.url + '">' + entry.title + '</a></h2>';
				html += '</div>';
				
				html += '<div class="xg_module_body">';
					html += '<a href="' + entry.url + '">';
						html += '<img width="171" height="auto" src="' + roc4life.image_resize( entry.image, 165, false, '1:1' ) + '" />';
					html += '</a>';
				html += '</div>'
			html += '</div>';
			
			$('.xg_module.sidebar').append( html );
		});
		
	})(jQuery);
}