Date.prototype.setISO8601 = function (string) {
    var regexp = "([0-9]{4})(-([0-9]{2})(-([0-9]{2})" +
        "(T([0-9]{2}):([0-9]{2})(:([0-9]{2})(\.([0-9]+))?)?" +
        "(Z|(([-+])([0-9]{2}):([0-9]{2})))?)?)?)?";
    var d = string.match(new RegExp(regexp));

    var offset = 0;
    var date = new Date(d[1], 0, 1);

    if (d[3]) { date.setMonth(d[3] - 1); }
    if (d[5]) { date.setDate(d[5]); }
    if (d[7]) { date.setHours(d[7]); }
    if (d[8]) { date.setMinutes(d[8]); }
    if (d[10]) { date.setSeconds(d[10]); }
    if (d[12]) { date.setMilliseconds(Number("0." + d[12]) * 1000); }
    if (d[14]) {
        offset = (Number(d[16]) * 60) + Number(d[17]);
        offset *= ((d[15] == '-') ? 1 : -1);
    }

    offset -= date.getTimezoneOffset();
    time = (Number(date) + (offset * 60 * 1000));
    this.setTime(Number(time));
}

var zeropad = function (num) { return ((num < 10) ? '0' : '') + num; }

function ning_img( url, params ) {
	if( typeof( url ) == 'undefined' ) {
		return url;
	} else {
		return url.split('?')[0] + '?' + params;
	}
}

function createTime( time ) {
	var date = new Date;
	date.setISO8601( time );
	
	var hours = date.getHours();
	hours = ((hours > 12) ? hours - 12 : hours);
	
	var dayandnite = ((hours > 12 ) ? 'am' : 'pm');
	
	return zeropad(hours) + ':' + zeropad(date.getMinutes()) + dayandnite;
}

var gt = new Date().getTime();

/* -- those were additions -- */

var user_topnews = '1qwl94fyxq52g';
var user_editorials_rocawear = '0jbhq5zutdydq';
var user_editorials_fashion = '392qljgpkah1k';
var user_editorials_culture = '11mhev8r6sr6z';
var user_editorials_essentials = '127r003btjhz3';
var user_video = '1bnyqrvkg5qgt';
var direct_video = '2612226:Video:72938';

var direct_video = false;
var user_video = false;

( function($) {
	function bottomRow( user_id, appendClass ) {
		$.ajax({
			url: '/profiles/blog/feed?user=' + user_id,
			type: 'get',
			dataType: 'text',
			success: function( data ) {
				var feed = $.xml2json( data );
				for( var x = 0; x<2; x++ ) {
					var container = jQuery('<div class="article"><img src="" /><div class="content"><h2></h2><p class="main"></p><h3></h3><h3></h3></div></div>');

					if( x == 0 ) { container.addClass('first-child'); }

					date = createTime( feed.entry[x].updated );
					link = feed.entry[x].link.href;
					title = feed.entry[x].title;
					summary = feed.entry[x].summary.text;

					var summary = summary.replace(/.jpg/g, '.jpg?width=138&crop=1:1').replace(/.jpeg/g, '.jpeg?width=138&crop=1:1');
					
					container.find('h2').html( '<a href="' + link + '">' + title + '</a>' );
					container.find('h3:first').html( 'Posted at <span>' + date + '</span>' );
					container.find('h3:last').html( '<a href="' + link + '">Read More</a>' );
					container.find('p.main').html( summary );
					
					if( container.find('p img').length == 0 && container.find('p object').length > 0 ) {
						container.find('p.main object:first').prependTo( container );
						container.find('object:first, object:first embed').attr('height', '138').attr('width', '138').css('marginBottom', 0).css('display', 'block');
					} else if( container.find('p img').length > 0 ) {
						image = ning_img( container.find('p img:first').attr('src'), '&width=138&crop=1:1' );
						container.find('img:first').attr( 'src', image );
					}
					
					container.find('p.main').html( container.find('p.main').text() );
					container.find('p.main').html( container.find('p.main').html().substr(0,100) + ( container.find('p.main').html().length > 100 ? '...' : '' ) );

					container.click(function() {
						window.location = $(this).find('h2 a').attr('href');
					});

					jQuery('.' + appendClass).append( container );
				}
			}
		});
	}
	
	function loadEditorialsTop( user_id, appendClass ) {
		$.ajax( {
			url: '/profiles/blog/feed?user=' + user_id,
			type: 'get',
			dataType: 'text',
			success: function( data ) {
				jQuery('.loading').addClass('loaded_' + user_id);
				var feed = $.xml2json( data );
				
				date = createTime( feed.entry[0].updated );
				link = feed.entry[0].link.href;
				title = feed.entry[0].title;
				summary = feed.entry[0].summary.text;
				
				var summary = summary.replace(/.jpg/g, '.jpg?width=280&crop=1:1').replace(/.jpeg/g, '.jpeg?width=280&crop=1:1');
				
				var container = jQuery( '.' + appendClass );
				container.find('.content p').html( summary );
				container.find('h2').html( '<a href="' + link + '">' + title + '</a>' );
				container.find('h3:first').html( 'Posted at <span>' + date + '</span>' );
				container.find('h3:last').html( '<a href="' + link + '">Read More</a>' );
				
				image = ning_img( container.find('p img:first').attr('src'), '&width=280&crop=1:1' );
				container.find('img:first').attr( 'src', image );
				
				container.find('.content p:first').html( container.find('.content p:first').text().substr(0,210) + '...' );
				
				container.click(function() {
					window.location = $(this).find('h2 a').attr('href');
				});
			}
		});
	}
	
	$(document).ready( function() {

		bottomRow( user_editorials_fashion, 'rocawearfashion' );
		bottomRow( user_editorials_culture, 'rocawearmusic' );

		loadEditorialsTop( user_editorials_essentials, 'rocawearessentials' );
		loadEditorialsTop( user_editorials_rocawear, 'rocawearsection' );
	
		// load in left+right sidebar
		$.ajax({
			url: '/index.php?_=' + gt,
			type: 'get',
			dataType: 'html',
			complete: function( res, status ) {
				if( status == 'success' || status == 'notmodified' ) {
					jQuery('.loading').addClass('loaded_leftandrightsidebar');

					var index = jQuery( '<div></div>' ).append( res.responseText.replace(/<script(.|\s)*?\/script>/g, "") );
					index.find('#xg_layout_column_1 .xg_module:first').css('marginTop', '0px');
					
					index.find('.xg_1col.last-child a[href="#"]').each( function() {
						$(this).attr( 'href', $(this).attr('_url') );
					});
					if( index.find('.xg_signup').length == 1 ) {
						index.find('.xg_1col.last-child a').slice(0,2).each( function() {
							$(this).attr('href', $(this).attr('href').split('?')[0] );
						});
					}
					index.find('#xg_module_account form').remove();
					index.find('.xg_module.module_about').remove();
	
					index.find('.xg_signup').html('<p align="center">Welcome to</p><img src="/images/header_150x48.png" width="150" /><div class="left"><a href="/main/authorization/signIn">Login</a></div><div class="right"><a href="/main/authorization/signUp">Sign Up</a></div><div class="clear" />');
	
					jQuery('.leftsidebar').html( index.find('#xg_layout_column_1').html() );
					jQuery('.rightsidebar').prepend( index.find('.xg_colgroup .xg_1col.last-child') );
					jQuery('.rightsidebar > .xg_1col.last-child').removeClass('xg_1col').removeClass('last-child');
					
					if( document.getElementById('#ad_160x600') ) {
						jQuery('.blogroll').insertAfter('#ad_160x600');
					} else {
						jQuery('.blogroll').insertAfter('#xg_module_account');
					}
					
					showAfterSignIn();
				}
			}
		});
		
		$.ajax({
			url: '/profiles/blog/feed?promoted=1',
			type: 'get',
			dataType: 'text',
			success: function( data ) {
				jQuery('.loading').addClass('loaded_featured');
				var feed = $.xml2json( data );

				for( var x = 0; x<6; x++ ) {
					var container = jQuery('.featuredarticle').eq(x);

					var date = createTime( feed.entry[x].updated );
					var link = feed.entry[x].link.href;
					var title = feed.entry[x].title;
					var summary = feed.entry[x].summary.text;
					
					var summary = summary.replace(/.jpg/g, '.jpg?width=70&crop=1:1').replace(/.jpeg/g, '.jpeg?width=70&crop=1:1');
					
					container.find('h2').html( '<a href="' + link + '">' + title + '</a>' );
					container.find('.content').html( summary );
					container.find('.footer').html( '<h3 class="first">Posted at <span>' + date + '</span></h3><h3 class="last"><a href="' + link + '">Read More</a></h3>' );
					
					var image = container.find('.content img:first').attr('src');
					container.find('.content').html( container.find('.content').text() );
					
					if( x == 0 ) {
						container.find('.content').html( '<img src="' + ning_img( image, 'width=267&crop=1:1' ) + '" width="267" />' + '<h2 align="left" style="padding-left:0px !important;"><a href="' + link + '">' + title + '</a></h2>' + container.find('.content').html().substr(0,700) + ( container.find('.content').html().length > 700 ? '...' : '' ) );
					} else {
						container.find('.xg_1col.first').html( '<img src="' + ning_img( image, 'width=70&crop=1:1' ) + '" width="70" />' );
						container.find('.content').html( container.find('.content').html().substr(0,125) + ( container.find('.content').html().length > 125 ? '...' : '' ) );
					}
					
					container.bind('click', function() {
						window.location = $(this).find('h2 a').attr('href');
					});
				}
			}
		});
		
		if( direct_video ) {
			jQuery('.xg_2col').prepend('<div class="xg_module welcome"><div class="xg_module_body"><div class="showvideo_container"><center><embed wmode="opaque" src="http://static.ning.com/rocaning3/widgets/video/flvplayer/flvplayer.swf?v=3.14.2%3A16906" FlashVars="config=http://rocaning3.ning.com/' + escape('/video/video/showPlayerConfig?id=' + direct_video) + '&amp;video_smoothing=on&amp;autoplay=off&amp;hideVisit=1&amp;hideRelated=1&amp;hideWatermark=1" width="555" height="364" bgColor="#000000" scale="noscale" allowScriptAccess="always" allowFullScreen="true" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" style="margin:0px;"></embed></center></div></div></div>');
		} else if( user_video ) {
			$.ajax({
				url: '/video/video/listForContributor?screenName=' + user_video + '&rss=yes',
				type: 'get',
				dataType: 'text',
				success: function( data ) {
					jQuery('.loading').addClass('loaded_video');
					var feed = $.xml2json( data );
					
					var url = feed.entry[0].link.href;
					var segment = url.split('/');
					segment = segment[segment.length-1];
					
	//				config=http%3A%2F%2Fwww.roc4life.com%2Fvideo%2Fvideo%2FshowPlayerConfig%3Fid%3D2612226%253AVideo%253A29310%26ck%3D1216407768&video_smoothing=on&autoplay=on&hideVisit=1&hideRelated=1&hideWatermark=1
					
	/* 				segment = '2612226:Video:41516'; */
					
					var config = '/video/video/showPlayerConfig?id=' + segment;
					var flashVars = 'config=http://rocaning3.ning.com/' + escape(config) + '&amp;video_smoothing=on&amp;autoplay=off&amp;hideVisit=1&amp;hideRelated=1&amp;hideWatermark=1';
					
					jQuery('.xg_2col').prepend('<div class="xg_module welcome"><div class="xg_module_body"><div class="showvideo_container"><center><embed wmode="opaque" src="http://static.ning.com/rocaning3/widgets/video/flvplayer/flvplayer.swf?v=3.14.2%3A16906" FlashVars="' + flashVars + '" width="555" height="364" bgColor="#000000" scale="noscale" allowScriptAccess="always" allowFullScreen="true" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" style="margin:0px;"></embed></center></div></div></div>');
				}
			});
		}
		
		// add linking to the headers
		jQuery('.culturerollover').click(function() {
			window.location = '/profiles/blog/list?user=11mhev8r6sr6z';
		});
		
		jQuery('.fashionrollover').click(function() {
			window.location = '/profiles/blog/list?user=392qljgpkah1k';
		});
		
		jQuery('.essentialrollover').click(function() {
			window.location = '/profiles/blog/list?user=127r003btjhz3';
		});
		
		jQuery('.rocawearrollover').click(function() {
			window.location = '/profiles/blog/list?user=0jbhq5zutdydq';
		});
	} );
} )(jQuery);