//--------------------------------------
// VARS
//--------------------------------------

// CONSTANTS
var globalLocalURL = "http://10.0.1.150/bullet/talkability/web/";
var globalWebURL = "http://www.talkability.com.br/";
var globalURL = "";

// OBJECTS
var menuObj;
var postsObj;
var navControlObj;
var contentTransformObj;
var contentShowObj;
var deepLinkObj;
var iphoneNavObj;
var bulletToolsObj;
var currentPageObj;
var galleryObj;

// CONTROLS
var delay = 1100;
var postsPerPage = 6;
var page = 1;
var postsTotal = 0;
var postsMaxTotal = 0;
var post = 1;
var postWidth = 300;
var postDistance = 330;
var postId;
var postName;
var _hash;
var navLeftStatus = false;
var navRightStatus = true;
var listCurrentPageStatus = false;
var keyStatus = true;
var _iphone = ((navigator.userAgent.match(/Android/i)) || (navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i)));
var _ipad = ( navigator.userAgent.match(/iPad/i) ); 

var msie7;
var msie8;



// BOUNDS
var postsOnScreen = 1;
var windowMarginLeft = 50;
var windowWidth = 0;

// LIST POSTS
var listPostsAction = "blog";
var listPostsValue = "";
var postArray = []; 

// CONTENT TRANSFORM
var divLastExpanded = new Array();




//--------------------------------------
// READY
//--------------------------------------

$(document).ready
(
	function()
	{
		// CHECK BROWSER
		checkBrowser();
		
		
		// DEEP LINK
		deepLinkInit();
	

		// MENU
		menuInit();
		
		
		// POSTS
		postsInit();
		
		
		// NAV
		navInit();
		
		// CURRENT PAGE
		currentPageInit();
		
		
		// CONTENT TRANSFORM
		contentTransformInit();
		
		
		// CONTENT SHOW
		contentShowInit();
		
		
		// GALLERY
		galleryInit();
		
		
		// KEYBOARD
		navKeyboardInit();
		
		
		// BOUNDS
		boundsUpdate();
		
		
		// BULLET TOOLS
		bulletToolsInit();
		
		
		// ONLOAD
		$(window).load
		(
			function()
			{
				// POSTS
				postsList();
				
				
				// IPHONE
				iphoneInit();
				
				
				// MOTION INIT
				motionInit();
			
			}
		);
	}
);




//--------------------------------------
// CLOSE
//--------------------------------------

$(window).unload
(
	function()
	{
		analyticsCloseWindow();
	}
);




//--------------------------------------
// RESIZE
//--------------------------------------

$(window).bind('resize', function()
{
	// BOUNDS
	boundsUpdate();
	
	
	// CONTENT SHOW
	contentShowObj.setPostsOnOff();
});





//--------------------------------------
// KEYBOARD
//--------------------------------------

function navKeyboardInit()
{
	//document.onkeypress = talkabilityKeys;   /* For Opera, Firefox and Chrome */
	 document.onkeydown = talkabilityKeys;
}

function talkabilityKeys( e )
{
	if( !e ) e = window.event;
	
	if( keyStatus )
	{
		switch( e.keyCode )
		{
			// Left
			case 37:
				navLeft();
				break;
			
			// Right
			case 39:
				navRight();
				break;
		}
	}
	
}



//--------------------------------------
// MENU
//--------------------------------------

function menuInit()
{
	menuObj = new menu();
	menuObj.init( listPostsAction );
}

function menuOver( id )
{
	menuObj.overObj( id )
}

function menuOut( id )
{
	menuObj.outObj( id )
}

function menuClick( id )
{
	menuObj.ClickObj( id )
}
function menuChange( page )
{
	menuObj.setMenu( page );
}

function menuBuscaClick( id )
{
	menuObj.buscaClick( id );
}

function menuBuscaFocus()
{
	menuObj.buscaFocus();
}

function menuBuscaBlur()
{
	menuObj.buscaBlur();
}




//--------------------------------------
// POSTS
//--------------------------------------

function postsInit()
{
	postsObj = new Posts();
	postsObj.init();
	
	// ON LOAD
	$('body').bind( 'postsOnLoadEvent', postsOnLoadEvent );
	$('body').bind( 'postsTotalEvent', postsTotalEvent );
}

function postsOnLoadEvent()
{
	// ON_LOAD
	$('body').unbind( 'postsOnLoadEvent', postsOnLoadEvent );
	$('#debug').append("LOAD ");
	
	// BULLET: Video About
	bulletVideoaboutInit();
}

function postsTotalEvent()
{
	// NAV CONTROL
	navControlObj.setPositions();
	navControlObj.navControl();
	
	
	// CONTENT TRANSFORM
	contentTransformObj.update();
	
	//
	if( $( "#searchInputID" ).attr("value") == "" )
		blogPage();
	
	
	//
	if( listPostsAction == "search" && listCurrentPageStatus == true )
		window.setTimeout( "currentPageTexto( '"  + listPostsValue + "')" , delay );
	
}

function morePosts()
{
	postsObj.morePosts();
}

function postsList()
{
	if( _hash == "quemsomos" )
	{
		aboutPage();
	}
	else
	{
		postsObj.listPosts();
	}
}

function homePage()
{
	if( listPostsAction == "blog" && post > 1 )
	{
		post = 1;
		
		navControl();
		navMotion();
	}
	else if( globalURL == deepLinkObj.getRealLocation() )
	{
		blogPage();
	}
	else
	{
		window.location.href = globalURL;
	}
	
	// Contract div expanded
	contentTransformContract();
	
	// HASH
	deepLinkObj.changeHash( "blog/" );
}

function blogPage()
{
	listPostsAction = "blog";
	postsObj.newPage();
	
	
	// BUSCA
	$("#searchInputID").attr("value", "Busca");
	
	
	// MENU
	menuChange( listPostsAction );
	window.setTimeout( "currentPageTexto( '"  + "blog" + "')" , delay )
	
	
	// NAV
	navControl();
	
	
	// HASH
	deepLinkObj.changeHash( "blog/" );
	
	
	// ANALYTICS
	analyticsPageView( "blog/" );
}

function aboutPage()
{
	if( listPostsAction == "about" && post > 1 )
	{
		post = 1;
		
		navControl();
		navMotion();
	}
	else if( globalURL == deepLinkObj.getRealLocation() || _hash != "" )
	{	
		listPostsAction = "about";
		postsObj.newPage();
		
		// BUSCA
		$("#searchInputID").attr( "value", "Busca" );
		
		// MENU
		menuChange( listPostsAction );
		window.setTimeout( "currentPageTexto( '"  + "Quem Somos" + "')" , delay );
		
		// NAV
		navControl();
		
		// ANALYTICS
		analyticsPageView( "about/" );
	}
	else
	{
		window.location.href = globalURL;
	}
	
	
	// HASH
	deepLinkObj.changeHash( "quemsomos/" );
}

function postsSpecialSearch( type, value )
{
	listPostsAction = "search";
	type != "" ? listPostsValue = type + ': ' + value : listPostsValue = value;
	postsObj.newPage();
	
	
	// BUSCA
	$("#searchInputID").attr("value", listPostsValue );
	window.setTimeout( "currentPageTexto( '"  + listPostsValue + "')" , delay );
	listCurrentPageStatus = false;
	
	
	// MENU
	menuChange( listPostsAction );
	
	
	// NAV
	navControl();
	
	
	// HASH
	deepLinkObj.changeHash( "busca/" );
	
	
	// ANALYTICS
	if( type != "" ) analyticsPageView( "filter/" + type + "/" + value + "/" );
	else analyticsPageView( "filter/" + value + "/" );
}

function postsSearch( searchValue )
{
	if( listPostsValue != searchValue )
	{
		listPostsAction = "search";
		listPostsValue = searchValue;
		postsObj.searchPosts();
		
		
		// MENU
		menuChange( listPostsAction );
		listCurrentPageStatus = true;
		
		
		// NAV
		navControl();
		
		
		// HASH
		deepLinkObj.changeHash( "busca/" );
	}
}

function postImageResize( post_name )
{
	postsObj.imageResize( post_name );
}



//--------------------------------------
// NAV
//--------------------------------------

// INIT
function navInit()
{
	navControlObj = new navControl();
	navControlObj.init();
}

// LEFT
function navLeft()
{
	if( navLeftStatus )
	{
		// NavControl
		navControlObj.navControlLeft();
		
		
		// Contract div expanded
		contentTransformContract();
		
		
		// CONTENT SHOW
		contentShowObj.setPostsOnOff();
		contentShowObj.commentHide();
		
		
		//BLUR SEARCH
		$('#searchInputID').blur();
		listCurrentPageStatus = false;
	}
}

// RIGHT
function navRight()
{
	if( navRightStatus )
	{
		// NavControl
		navControlObj.navControlRight();
		
		
		// Contract div expanded
		contentTransformContract();
		
		// Posts
		morePosts();
		
		
		// CONTENT SHOW
		contentShowObj.setPostsOnOff();
		contentShowObj.commentHide();
		
		
		//BLUR SEARCH
		$('#searchInputID').blur();
		listCurrentPageStatus = false;
	}
}

function navControl()
{ 
	navControlObj.navControl();
}

function navMotion()
{ 
	navControlObj.navMotion();
}

function setaOver( id )
{ 
	navControlObj.motionSetaOver( id );
}

function setaOut( id )
{ 
	navControlObj.motionSetaOut( id );
}

function setaClick( id )
{ 
	navControlObj.motionSetaClick( id );
}



//--------------------------------------
// CURRENT PAGE
//--------------------------------------

// INIT
function currentPageInit()
{
	currentPageObj = new currentPage();
	currentPageObj.init();
}

function currentPageTexto( _text )
{
	currentPageObj.setText( _text );
	if( !_iphone && !_ipad )navControlObj.sideControlsInitialMotion();
}



//--------------------------------------
// CONTENT TRANSFORM
//--------------------------------------

// INIT
function contentTransformInit()
{
	contentTransformObj = new contentTransform();
	contentTransformObj.init();
}

function contentTransformExpand( _videoType, _postID, _videoID )
{
	contentTransformObj.expand( _videoType, _postID, _videoID );
}

function contentTransformContract()
{
	contentTransformObj.contract();
}


//--------------------------------------
// CONTENT SHOW
//--------------------------------------

// INIT
function contentShowInit()
{
	contentShowObj = new contentShow();
	contentShowObj.init();
}

function buttonCommentOver( id )
{
	contentShowObj.commentOver( id );
}

function buttonCommentOut( id )
{
	contentShowObj.commentOut( id );
}

function buttonCommentClick( id )
{
	contentShowObj.commentClick( id );
}



//--------------------------------------
// GALERY
//--------------------------------------

// INIT
function galleryInit()
{
	galleryObj = new gallery();
	galleryObj.init();
	
	//
	$('body').bind( 'galleryOneEvent', galleryOneEvent );
	$('body').bind( 'gallerySeveralEvent', gallerySeveralEvent );
}

function gallerySetNew( post_name )
{
	if( !msie7 )/// NO IE7
	{
		//Gambis pra erro de Carregamento das imgs do post_1
		if( post_name == "post_1" )
		{
			var quant_Img = 0;

			$( '#post_1 #post_content img' ).load(function() 
			{
				quant_Img++;
				contentTransformObj.setImagesNO_IE7( this );
				
				
				if($( '#post_1 #post_content img' ).length == quant_Img )
				{
					galleryObj.setGallery( post_name );
					quant_Img = 0;
				}
			});
		}
		else
		{
			galleryObj.setGallery( post_name );
		}
	}
	else/// IE7
	{
		contentTransformObj.setImages_IE7( post_name );
	}
}


function galleryOneEvent()
{
	contentTransformObj.setGallerOne();
}


function gallerySeveralEvent()
{
	contentTransformObj.setGallerSeveral();
}


//--------------------------------------
// IPHONE NAV
//--------------------------------------

// INIT
function iphoneInit()
{
	// IPHONE
	iphoneNavObj = new iphoneNav();
	iphoneNavObj.init();
}




//--------------------------------------
// DEEP LINK
//--------------------------------------

// INIT
function deepLinkInit()
{
	deepLinkObj = new deepLink();
	deepLinkObj.init();
	
	
	// GLOBAL URL
	globalURL = deepLinkObj.getLocation( globalLocalURL, globalWebURL );
	
	
	// POST ID
	postId = deepLinkObj.getPost( globalURL );
	
	
	// GET HASH
	_hash = deepLinkObj.getHash();
	
	
	// ANALYTICS
	if( postId > 0 )
		analyticsPageView( "blog/" + postId + "/" );
	else
		analyticsPageView( "blog/" );
}



//--------------------------------------
// BULLET TOOLS
//--------------------------------------

// INIT
function bulletToolsInit()
{
	bulletToolsObj = new bulletTools();
	bulletToolsObj.init();
	
	
	// ON LOAD
	$('body').bind( 'bulletToolsOnVideoLoadEvent', bulletNowplayingInit );
	$('body').bind( 'bulletToolsOnNowplayingLoadEvent', bulletPodbilityInit );
}

function bulletVideoaboutInit()
{
	window.setTimeout( "bulletToolsObj.videoaboutInit()" , 1000 * 3  );
}

function bulletNowplayingInit()
{
	bulletToolsObj.nowPlayingInit();
}

function bulletPodbilityInit()
{
	bulletToolsObj.podbilityInit();
}




//--------------------------------------
// BOUNDS
//--------------------------------------

function boundsUpdate()
{
	// Window Width
	windowWidth = $(window).width() - windowMarginLeft;
	
	// Posts On Screen
	postsOnScreen = parseInt( windowWidth / postDistance );
	if( postsOnScreen < 2 ) postsOnScreen = 2;
}




//--------------------------------------
// HEADER
//--------------------------------------

function headerActualize()
{
	// Get 'WINDOW HEIGHT'
	var newHeaderHeight = document.getElementById('wrapper').offsetHeight;
	
	// Make the animation to set 'NEW HEIGHT' to header
	$( '#header' ).css( 'height', ( newHeaderHeight - 125 ) );
	$( '#footer' ).css( 'marginTop', ( document.getElementById('header').offsetHeight + 50 ) );
	
}




//--------------------------------------
// ANALYTICS
//--------------------------------------

function analyticsPageView( tag )
{
	_gaq.push(['_trackPageview', tag]);
	
	//alert( "_trackPageview: " + tag )
}

function analyticsEvent( eventName, tag )
{
	_gaq.push(['_trackEvent', eventName, tag]);
	
	//alert( "_trackEvent: " + eventName + " = " + tag )
}
function analyticsCloseWindow()
{
	_gaq.push(['_trackEvent', 'AVG Time', 'fechou']);
	
	//alert("_trackEvent: fechou")
}




//--------------------------------------
// MOTION INIT
//--------------------------------------

function motionInit()
{	
	//
	window.setTimeout( "menuObj.AdressMotionInit( '"  + 100 + "' )" , 1000 * 0.1 );
	
	window.setTimeout( "menuObj.footerMotionInit( '"  + 100 + "' )" , 1000 * 0.5 );
	
	window.setTimeout( "currentPageTexto( '"  + "blog" + "')" , 1000 * 1.1 );
	if( !_iphone && !_ipad ) window.setTimeout( "navControlObj.sideControlsInitialMotion()" , 1000 *  1.3  );
	
	
	//window.setTimeout( "menuObj.headerMotionInit( '"  + 1000 + "' )" , 1000 * 0.5 );
}



//--------------------------------------
// CHECK BROWSER
//--------------------------------------

function checkBrowser()
{	
	if( $.browser.msie )
	{
		if( $.browser.version == "8.0" )
		{
			msie8 = true;
			msie7 = false;
		}
		else if( $.browser.version == "7.0"  )
		{
			msie7 = true;
			msie8 = false;
		}		
	}
	else
	{
		msie8 = false;
		msie7 = false;
	}
	
}


