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 ); }

jQuery(document).ready(function($) {
	$(window).load(function() {});
	
	add_js('http://roc4life.ginlanemedia.com/cache/homepage.json?t=' + time);	
	add_js('http://roc4life.ginlanemedia.com/cache/roc4life_entertainment.json?t=' + time);	
	add_js('http://roc4life.ginlanemedia.com/cache/roc4life_fashion.json?t=' + time);	
	// add_js('http://roc4life.ginlanemedia.com/cache/roc4life_photos.json?t=' + time);	
	add_js('http://roc4life.ginlanemedia.com/cache/roc4life_video.json?t=' + time);	
	add_js('http://roc4life.ginlanemedia.com/cache/roc4life_whats-the-word.json?t=' + time);	
	
	$.ajax({
		url: '/index.php',
		type: 'get',
		dataType: 'html',
		complete: function( res, status ) {
			if( status == 'success' || status == 'notmodified' ) {
				var index = jQuery( '<div></div>' ).append( res.responseText.replace(/<script(.|\s)*?\/script>/g, "").replace(/<img/g, "<span") );

				if( index.find('.xg_signup').length > 0 ) {
					$('.user-column .inside').html('<div class="signup clearfix"><span style="float:left;"><a href="http://www.roc4life.com/main/authorization/signUp">Sign Up</a></span><span style="float:right;"><a href="http://www.roc4life.com/main/authorization/signIn">Sign In</a></span></div>');
				} else {
					$('.user-column .inside').html( index.find('#xg_module_account > *:not(form)') );
					$('.user-column .inside a').each(function() {
						if( typeof($(this).attr('_url')) == 'string' ) {
							var url = $(this).attr('_url');
							$(this).attr( 'href', url );
						}
					});
				}
				
				$('.user-column .inside .xg_module_head h2').css('float', 'none').html( '<span style="float:left;">' + $('.user-column .inside .xg_module_head h2').text() + '</span>' );
				
				$('.user-column .inside .xg_module_body p small').css('float', 'right').appendTo( '.user-column .inside .xg_module_head h2' );
				$('.user-column .inside .xg_module_body > p').remove();
				$('.user-column .inside .xg_module_body > ul > li:nth-child(2)').remove();

				$('.latest-activity .xg_module_body').html( index.find('.xg_module_activity .xg_module_body > *') );
				$('#xg_body.roc4life .featured-area')
					.find('.ad_160x600')
					.prepend( index.find('#xg_layout_column_1 .xg_module.html_module:first .xg_module_body > *') );
				
				// it turns <img> into <span>, so we flip it back.
				$('.latest-activity .xg_module_body span.feed-avatar, #xg_body.roc4life .featured-area span').each(function() {
					var src = $(this).attr('src');
					var style = $(this).attr('style');
					var alt = $(this).attr('alt');
					var height = $(this).attr('height');
					var width = $(this).attr('width');
					
					if( src ) {
						$(this).html( '<img src="' + src + '" alt="' + alt + '" height="' + height + '" width="' + width + '" />' );	
					}
				});
				
				// featured video
				$('.featured-video').each(function() {
					var video = index.find('#xg_layout_column_1 .xg_module.html_module:eq(4) .xg_module_body');
					
					if( video.find('object').length > 0 ) {
						$(this).find('.xg_video').html( video.find('object') );
					} else if( video.find('embed').length > 0 ) {
						$(this).find('.xg_video').html( video.find('embed') );
					}

					$(this).find('.xg_video object, .xg_video embed').css({
						'width': '372px',
						'height': '224px'
					}).width( 372 ).height( 224 );
					
					$(this).find('.xg_module_body h2 a').attr( 'href', video.find('h2 a').attr('href') );
					$(this).find('.xg_module_body h2 a').html( '<span class="left">' + video.find('h2 a').html() + '</span>' );
					$('<a href="http://www.roc4life.com/video" target="_blank"><span class="right">View Videos</span></a>').appendTo( $(this).find('.xg_module_body h2') );
				});

				// featured group
				$('.featured-group').each(function() {
					var group = index.find('#xg_layout_column_1 .xg_module.html_module:eq(1) .xg_module_body');
					
					$(this).find('.xg_image img').attr('src', group.find('span').attr('src') );
					
					$(this).find('.xg_module_body h2 a, .xg_image a').attr( 'href', group.find('h2 a').attr('href') );
					$(this).find('.xg_module_body h2 a').html( group.find('h2 a').html() );
					
					$(this).find('p:last').html( group.find('p:last').html() );
				});

				// featured blog
				$('.featured-blog').each(function() {
					var post = index.find('#xg_layout_column_1 .xg_module.html_module:eq(2) .xg_module_body');
					
					if( post.find('object').length > 0 ) {
						$(this).find('.xg_video').html( post.find('object') );	
					}

					if( post.find('span').length > 0 ) {
						$(this).find('.xg_video').html( '<img src="' + post.find('span').attr('src') + '" width="182" height="182" />' );	
					}
					
					$(this).find('.xg_video object, .xg_video embed').width( 182 ).height( 182 );
					
					$(this).find('.xg_module_body h2 a, .xg_image a').attr( 'href', post.find('h2 a').attr('href') );
					$(this).find('.xg_module_body h2 a').html( post.find('h2 a').html() );
					
					$(this).find('p').html( post.find('p').html() );
				});

				// featured item
				$('.featured-item').each(function() {
					var item = index.find('#xg_layout_column_1 .xg_module.html_module:eq(3) .xg_module_body');
					
					$(this).find('.xg_image img').attr('src', item.find('span').attr('src') );
					
					$(this).find('.xg_module_body h2 a, .xg_image a').attr( 'href', item.find('h2 a').attr('href') );
					$(this).find('.xg_module_body h2 a').html( item.find('h2 a').html() );
					
					$(this).find('p').html( item.find('p').html() );
				});
				
				// and on and on and on
				
				$('#xg_navigation').html( index.find('#xg_navigation > *') );
				$('#xg_navigation div.xg_subtab').each(function() {
					var $div = $(this);
					var $li = $(this).parent();
					
					$li.hover(function() {
						$div.show().css({
							'top': $li.get(0).offsetTop + $li.height(),
							'left': $li.get(0).offsetLeft,
							'zIndex': '9999'
						});
					}, function() {
						$div.hide()
					});
					
				})
				
			}
		}
	});
});

// get jquery cycle
add_js( 'http://roc4life.ginlanemedia.com/includes/js/jquery.cycle.js' );

// roc4life four smaller parser
function roc4life_entertainment( data ) {
	roc4life_thin_columns( data, '.five-column-container .xg_module.entertainment' );
}

function roc4life_fashion( data ){
	roc4life_thin_columns( data, '.five-column-container .xg_module.fashion' );
}

function roc4life_whats_the_word( data ){
	roc4life_thin_columns( data, '.five-column-container .xg_module.whats_the_word' );
}

/*
function roc4life_video( data ){
	roc4life_thin_columns( data, '.five-column-container .xg_module.rocawear' );
}
*/

// and the one thing they all share in common
function roc4life_thin_columns( data, appendTo ) {
	(function($){
		// insert entries
		var entries = roc4life.parse_feed( data, 1 );
	
		$(entries).each(function(n) {
			var entry = $(this).get(0);
	
			var five_column_module = '';

			five_column_module += '<div class="xg_image">';
				five_column_module += '<a href="' + entry.url + '">';
					five_column_module += '<img width="182" height="auto" src="' + roc4life.image_resize( entry.image, 182, false, '1:1' ) + '" />';
				five_column_module += '</a>';
			five_column_module += '</div>';

			five_column_module += '<div class="xg_module_body">';
				five_column_module += '<h2 class="' + ( entry.comments && entry.views ? 'hasCommentsandViews' : '' ) + '">';
					five_column_module += '<a href="' + entry.url + '" class="article-title">' + entry.title + '</a>';
				five_column_module += '</h2>';
				
				if( entry.comments && entry.views ) {

					five_column_module += '<span class="views_and_comments">';
						five_column_module += '<span>' + entry.views + ' Views</span>';
						
						if( entry.comments > 0 ) {
							five_column_module += ' & <span>' + entry.comments + ' Comment' + ( entry.comments > 1 ? 's' : '' ) + '</span>';
						}
						
					five_column_module += '</span>';
				}
				
				five_column_module += '<p class="description">' + entry.description + '</p>';
				five_column_module += '<p class="meta clearfix"><span style="float:left">' + entry.date + '</span> <a href="' + entry.url + '" style="float:right" class="read-more">Read More</a></p>';
			five_column_module += '</div>';

			var module = $( appendTo ).append( five_column_module );
			
			if( entry.comments && entry.views ) {
				roc4life.body_balance( module.find('.xg_module_body'), 106, 'h2', 'p:first', 'p.meta', false );	
			} else {
				roc4life.body_balance( module.find('.xg_module_body'), 121, 'h2', 'p:first', 'p.meta', false );
			}
			
			module.hover(function() {
				pageTracker._trackEvent(appendTo, "Hover");
			}, function() {
				// pageTracker._trackEvent(appendTo, "Hover");
			});
			
			roc4life.retweet( $( appendTo ).find('.xg_sprite-twitter'), entry.url );
			
			// experimental
			// var distance = 106;
			// var speed = 'fast';
			// module.hover(function() {
			// 	$(this).find('.xg_image').animate({'height':'-=' + distance}, speed);
			// 	$(this).find('.xg_module_body p:first').animate({'height':'+=' + distance}, speed);
			// }, function() {
			// 	$(this).find('.xg_image').animate({'height':'+=' + distance}, speed);
			// 	$(this).find('.xg_module_body p:first').animate({'height':'-=' + distance}, speed);
			// });
			
		});
	})(jQuery);
}

// roc4life_video parser
function roc4life_video( data ) {
	var appendTo = '.five-column-container .xg_module.rocawear';
	
	(function($){
		// insert entries
		var entries = roc4life.parse_feed( data, 1 );
	
		$(entries).each(function(n) {
			var entry = $(this).get(0);
	
			var five_column_module = '';

			five_column_module += '<div class="xg_video">';
				five_column_module += entry.object;
			five_column_module += '</div>';

			five_column_module += '<div class="xg_module_body">';
				five_column_module += '<h2 class="' + ( entry.comments && entry.views ? 'hasCommentsandViews' : '' ) + '">';
					five_column_module += '<a href="' + entry.url + '" class="article-title">' + entry.title + '</a>';
				five_column_module += '</h2>';
				
				if( entry.comments && entry.views ) {

					five_column_module += '<span class="views_and_comments">';
						five_column_module += '<span>' + entry.views + ' Views</span>';
						
						if( entry.comments > 0 ) {
							five_column_module += ' & <span>' + entry.comments + ' Comment' + ( entry.comments > 1 ? 's' : '' ) + '</span>';
						}
						
					five_column_module += '</span>';
				}
				
				five_column_module += '<p class="description">' + entry.description + '</p>';
				five_column_module += '<p class="meta clearfix"><span style="float:left">' + entry.date + '</span> <a href="' + entry.url + '" style="float:right" class="read-more">Read More</a></p>';
			five_column_module += '</div>';

			var module = $( appendTo ).append( five_column_module );
			
			if( entry.comments && entry.views ) {
				roc4life.body_balance( module.find('.xg_module_body'), 106, 'h2', 'p:first', 'p.meta', false );	
			} else {
				roc4life.body_balance( module.find('.xg_module_body'), 121, 'h2', 'p:first', 'p.meta', false );
			}
			
			roc4life.retweet( $( appendTo ).find('.xg_sprite-twitter'), entry.url );
			
			$('.xg_module.xg_blog_column.rocawear .xg_video object,.xg_module.xg_blog_column.rocawear .xg_video embed').css({'height':'182px','width':'182px','overflow':'hidden'}).height( 182 ).width( 182 );
			
			// experimental
			// var distance = 106;
			// var speed = 'fast';
			// module.hover(function() {
			// 	$(this).find('.xg_image').animate({'height':'-=' + distance}, speed);
			// 	$(this).find('.xg_module_body p:first').animate({'height':'+=' + distance}, speed);
			// }, function() {
			// 	$(this).find('.xg_image').animate({'height':'+=' + distance}, speed);
			// 	$(this).find('.xg_module_body p:first').animate({'height':'-=' + distance}, speed);
			// });
			
		});
	})(jQuery);
}


// roc4life_featured parser
function roc4life_featured( data ) {
	(function($){
		
		// insert entries
		var entries = roc4life.parse_feed( data, 5 );
		
		$(entries).each(function(n) {
			var entry = $(this).get(0);
			
			var cycle_container = '';
			cycle_container += '<div class="xg_module_body" ' + ( n > 0 ? 'style="display:none"' : '' ) + '>';
				cycle_container += '<h1 class="' + ( entry.comments && entry.views ? 'hasCommentsandViews' : '' ) + '">';
					cycle_container += '<a href="' + entry.url + '" class="article-title">' + entry.title + '</a>';
				cycle_container += '</h1>';

				if( entry.comments && entry.views ) {

					cycle_container += '<span class="views_and_comments">';
						cycle_container += '<span>' + entry.views + ' Views</span>';
						
						if( entry.comments > 0 ) {
							cycle_container += ' & <span>' + entry.comments + ' Comment' + ( entry.comments > 1 ? 's' : '' ) + '</span>';
						}
						
					cycle_container += '</span>';
				}
				
				cycle_container += '<p class="description">' + entry.description + '</p>';
				cycle_container += '<p class="meta clearfix"><span style="float:left">' + entry.date + '</span> <a href="' + entry.url + '" style="float:right" class="read-more">Read More</a></p>';
			cycle_container += '</div>';
			
			$('.news-container .cycle-container').append( cycle_container );
			
			var image_container = '';
			image_container += '<a href="' + entry.url + '" ' + ( n > 0 ? 'style="display:none"' : '' ) + '>';
				image_container += '<img src="' + roc4life.image_resize( entry.image, false, '248', '438:248' ) + '" width="438" height="auto" />';
			image_container += '</a>';
			
			$('.news-container .image').append( image_container );
		}); // end $(entries).each()
		
		// .news-container cycle setup
		var cycleOptions = {
			fx: 'fade',
			fx: 'scrollHorz',

			speed: 'fast',
			pager: '.news-container .xg_module_head .right.navi',
			pagerAnchorBuilder: function(n) {
				return '.news-container .xg_module_head .right.navi a.article:eq(' + n + ')';
			},
			next: '.news-container .xg_module_head .right .next',
			prev: '.news-container .xg_module_head .right .prev',

			after: function() {
				// roc4life.body_balance( $(this), 100, 'h1', 'p:first', 'p.meta', true );
			},
		
			easing: 'swing',

			timeout: 10000,
		}
	
		$('.news-container .image').cycle( cycleOptions );

		// cycleOptions.delay = -100;
		cycleOptions.after = function() {
			
			if( $(this).find('h1').hasClass('hasCommentsandViews') ) {
				roc4life.body_balance( $(this), 196, 'h1', 'p:first', 'p.meta', true );	
			} else {
				roc4life.body_balance( $(this), 215, 'h1', 'p:first', 'p.meta', true );	
			}
		
			if( $(this).length > 0 ) {
				var parent = $(this).parents('.xg_module').eq(0);
			
				roc4life.retweet( parent.find('.xg_sprite-twitter'), parent.find('.xg_module_body:visible .read-more').attr('href') );
			}
		}
	
		$('.news-container .cycle-container').cycle( cycleOptions );
	
		$('.news-container .image, .news-container .cycle-container').hover(function() {
			$('.news-container .image').cycle('pause');
			$('.news-container .cycle-container').cycle('pause');
			
			pageTracker._trackEvent('.news-container .article', "Hover");
		}, function() {
			$('.news-container .image').cycle('resume');
			$('.news-container .cycle-container').cycle('resume');
		});
		
		$('.news-container .navi a').click(function() {
			pageTracker._trackEvent('.news-container .navi', "Click");
		});
		
		// end cycle setup
		
	})(jQuery);
} // end roc4life featured

function roc4life_user_photos( data ) {
	(function($) {
		var entry = data.value.items[0];
		
		var featured_photo = roc4life.image_resize( entry.image, false, 224, '372:224' );
		var title = entry.title;
		var url = entry.link;
		
		$('.featured-photo .xg_image a').attr('href', url);
		$('.featured-photo .xg_image a img').attr('src', featured_photo);
		$('.featured-photo .xg_module_body h2 a').attr('href', 'http://www.roc4life.com/photo/photo/listFeatured');
		$('.featured-photo .xg_module_body h2 a').html( '<span class="left">' + title + '</span> <span class="right">View The Gallery</span>' );
		
	})(jQuery);
}