var popupNewslettersFormValidation;

function initLayout()
{
	setCurrentPlatform( currentPlatform );
	setupTabs();
	openSocialBar();

	setupTooltips( '.help' );

	if( typeof( resizeIFrame ) != 'undefined' )
		resizeIFrame();

	setupTwitter();
	setupRSSBlog();
}

function setCurrentPlatform( platform )
{
	var curTab = $( 'navTab_' + platform );
	
	currentPlatform = platform;
	
	$$( '#platformTabs .selectedTab' ).each( function( el )
	{
		el.removeClassName( 'selectedTab' );
	} );
	
	if( curTab )
		curTab.addClassName( 'selectedTab' );
}

function setupTooltips( sel )
{
	$$( 'div.tooltip' ).each( Element.hide );

	$$( sel ).each( function( el )
	{
		new Tooltip( el, { 
			mouseFollow: 		false, 
			opacity:  			1,
			backgroundColor: 	'black'
		} );
	});
}

function setupTabs()
{
	$$( 'div.tab' ).each( function( el )
	{
		new Fabtabs( el );
	} );
}

function setupTwitter()
{
	if( $( 'DCTwitter' )) {
		
		if( twitterID == '' )
			twitterID = defaultTwitterID;
	
	    getTwitters( 'DCTwitter', {
	        id: 			twitterID, 
	        template: 		'<h3><a href="http://twitter.com/%user_screen_name%/statuses/%id%">%time%</a></h3>' +
							'<p style="margin-top: 0px; padding-top: 0px">%text%</p>', 
	        clearContents: 	true,
	        count: 			3, 
	        withFriends: 	false,
	        ignoreReplies: 	false,
	        newwindow: 		true,
	        onTimeout: 		function() 
			{
	            this.innerHTML = DC_ERR_TWITTER_DOWN1 +
								 ' <a href="http://twitter.com/' + twitterID + '">' + 
								 DC_ERR_TWITTER_DOWN2 + '</a> ' + DC_ERR_TWITTER_DOWN3;
	        }
	    });
	}
}

function setupRSSBlog()
{
 	if( $( 'SCRSS' ))
		new Ajax.Updater( 'SCRSS', getRootPath(), 
		{
			parameters: { 
				act: 		'rssbox',
				tag: 		scTag
			}
		} );
}

function switchPlatform( platform )
{
	if( typeof( setPlatformFilter ) == 'function' )
		setPlatformFilter( platform );
	else
		location.href = '/?platform=' + platform;
}

function setCookie( szName, szValue, szExpires, szPath, szDomain, bSecure )
{
 	var szCookieText = escape( szName ) + '=' + escape( szValue );
	
	szCookieText +=	szExpires ? '; EXPIRES=' + szExpires.toGMTString() : '' ;
	szCookieText += szPath ? '; PATH=' + szPath : '';
	szCookieText += szDomain ? '; DOMAIN=' + szDomain : '';
	szCookieText += bSecure ? '; SECURE' : '';
	
	document.cookie = szCookieText;
}

function getCookie( szName )
{
 	var szValue = null;

	if( document.cookie ) {
       	var arr = document.cookie.split( escape( szName ) + '=' );
       	
		if( 2 <= arr.length ) {
           	var arr2 = arr[ 1 ].split( ';' );

       		szValue = unescape( arr2[ 0 ] );
       	}
	}

	return szValue;
}

function openSocialBar()
{
	var sb = $( 'socialBar' );

	if( sb && !getCookie( 'noSocialBar' ))
		sb.show();
}

function closeSocialBar()
{
	$( 'socialBar' ).hide()
	setCookie( 'noSocialBar', 1 );
}

function showNotificationPopup()
{
	var id = parseInt( $F( 'notificationID' ), 10 );
	var lastID = parseInt( getCookie( 'notificationID' ), 10 );
	var seen = getCookie( 'notificationSeen' );

	if( !seen || ( lastID < id )) {
		
		popupNewslettersFormValidation = new Validation( 'popupNewslettersForm' );
		
		$( 'popupOverlay' ).show();
	}
}

function closeNotificationPopup()
{
	var subscribe = $F( 'popupNewsletter' );
	
	if( !subscribe || popupNewslettersFormValidation.validate() ) {
		
		if( subscribe ) {
			new Ajax.Request( getRootPath(), 
			{
				parameters: { 
					act: 		'user.subscribeNewsletter', 
					name: 		$F( 'popupNLName' ),
					email: 		$F( 'popupNLEMail' )
				}
			} );
		}
	
		if( $F( 'notificationDoNotShowAgain' )) {
			setCookie( 'notificationSeen', 1, new Date( 2099, 1, 1 ));
			setCookie( 'notificationID', $F( 'notificationID' ), new Date( 2099, 1, 1 ));
		}

		$( 'popupOverlay' ).hide();
	}
}

