/* Author: Damien Despas (despas.damien@gmail.com)

*/

$(document).ready(function()
   {
      highlight_init();
      discover_init( 1 );
      knowledge_init();
   }
);


/** 
	highlight component
*/

var highlightTimer = null;
var highlightTimerEnable = false;
var highlightItemSelected = 1;
var highlightNbrMaxItem = 4;

function highlight_init()
{   
   highlight_loadData();
}

function highlight_loadData()
{
	$.ajax
	(
		{
			type: "GET",
			url: "components/com_netbuilder_website/highlight/highlight_data.xml?" + Date(),
			dataType: "xml",
			success: function( data )
			{
			   $( "#highlightpictures" ).html( "" );
			   $( "#highlightitems" ).html( "" );
			   $( "#highlightpages" ).html( "" );

			   $( data ).find( "item" ).each( function ( pos )
			      {
				      if ( pos < highlightNbrMaxItem )
					  {
						 var curItem = pos + 1;

					         var pictureSrc = $( this ).find( "picture" ).attr( "src" );
					         var pictureAlt = $( this ).find( "picture" ).text();
					         var title = $( this ).find( "title" ).text();
					         var description = $( this ).find( "description" ).text();
					         var link = $( this ).find( "title" ).attr( "href" );
					         var position = $( this ).find( "title" ).attr( "position" );					  
							 
					         var output = "";
					         output += '<img id="highlightpicture' + curItem + '" src="' + pictureSrc + '" alt="' + pictureAlt + '" title="' + pictureAlt + '"/>';
						 
					         $( output ).appendTo( "#highlightpictures" );

					         output = "";
						 output += '<p id="highlightitem' + curItem + '" onClick="highlight_clickHandler( ' + curItem + ' )" class="highlightitem ' + position + '">';
						 output += '<a href="' + link + '">';
						 output += '<span class="title">' + title + "</span>";
						 output += "</a><br>";
						 output += description + "</p>";
					     
						 $( output ).appendTo( "#highlightitems" );
	
					     output = "";
						 output += '<p id="highlightpage' + curItem + '" onClick="highlight_clickHandler( ' + curItem + ' )" class="highlightpage ir">';
						 output += "" + title + "</p>";
					     
						 $( output ).appendTo( "#highlightpages" );
					 }
				  }
			   );
			   
			   highlight_clickHandler( 1 );
			   highlight_startTimer();

			   $( "#highlightpictures" ).find( "img" ).each( function ( pos )
				  {
					 $( this ).mouseover( highlight_stopTimer );
					 $( this ).mouseout( highlight_startTimer );
				  }
			   );
			   $( "#highlightitems" ).find( "p" ).each( function ( pos )
				  {
					 $( this ).mouseover( highlight_stopTimer );
					 $( this ).mouseout( highlight_startTimer );
				  }
			   );
			   $( "#highlightpages" ).find( "p" ).each( function ( pos )
				  {
					 $( this ).mouseover( highlight_stopTimer );
					 $( this ).mouseout( highlight_startTimer );
				  }
			   );
		    }
		}
	);
}

function highlight_startTimer()
{
   if ( !highlightTimerEnable )
   {
      highlightTimerEnable = true;
      highlightTimer = setTimeout( "highlight_timeoutHandler()", 8000 );
   }
}

function highlight_timeoutHandler()
{
   if ( highlightTimerEnable )
   {
      highlightItemSelected = highlightItemSelected % 4  + 1;
      highlight_clickHandler( highlightItemSelected );
   }
}

function highlight_stopTimer()
{
   if ( highlightTimerEnable )
   {
      highlightTimerEnable = false;
      clearTimeout(highlightTimer);
   }
}

function highlight_selectItem( obj )
{
   $( "#highlightitems" ).find( "p" ).each( function ( pos )
      {
		$( this ).hide();
		$( this ).removeClass( "highlightitem highlightitemselected" ).addClass( "highlightitem" );
	  }
   );
   
   $( obj ).removeClass( "highlightitem" ).addClass( "highlightitemselected" );
   $( obj ).slideToggle();
}

function highlight_selectPage( obj )
{
   $( "#highlightpages" ).find( "p" ).each( function ( pos )
      {
		 $( this ).removeClass( "highlightpage highlightpageselected" ).addClass( "highlightpage" );
	  }
   );
    
   $( obj ).removeClass( "highlightpage" ).addClass( "highlightpageselected" );
}

function highlight_selectPicture( obj )
{
   $( "#highlightpictures" ).find( "img" ).each( function ( pos )
	  {
		 $( this ).hide();
      }
   );

   $( obj ).fadeIn( 500 );
}

function highlight_clickHandler( pos )
{
   if ( highlightTimer )
   {
      highlight_stopTimer();
   }

   if ( $( "#highlightitem" + pos ).hasClass( "highlightitemselected" ) == false )
   {
	   highlight_selectPicture( document.getElementById( "highlightpicture" + pos ) );
	   highlight_selectPage( document.getElementById( "highlightpage" + pos ) );
	   highlight_selectItem( document.getElementById( "highlightitem" + pos ) );
   }
   
   if ( highlightTimer )
   {
      highlightItemSelected = pos;
      highlight_startTimer();
   }
   
   discover_init( pos );
}

/** 
	discover component
*/

var discoverNbrPage = 1;
var discoverCurPage = 1;
var discoverNbrMaxByPage = 4;

function discover_init( type )
{
   discoverNbrPage = 1;
   discoverCurPage = 1;
   discoverNbrMaxByPage = 4;

   discover_loadData( type );
   
   $( "#discovermoveleft" ).click( discover_moveLeftHandler );
   $( "#discovermoveright" ).click( discover_moveRightHandler );
   
   $( "#discoverhome" ).mouseover( highlight_stopTimer );
   $( "#discoverhome" ).mouseout( highlight_startTimer );
}

function discover_loadData( type )
{
	$.ajax
	(
		{
			type: "GET",
			url: "components/com_netbuilder_website/discover/discover_data.xml?" + Date(),
			dataType: "xml",
			success: function( data )
			{
			   $( "#discoveritems" ).html( "" );
			   var discoverNbrItem = $( data ).find( 'item[type="' + type + '"]' ).length;
			   discoverNbrPage = Math.ceil( discoverNbrItem / discoverNbrMaxByPage );
			   
			   var output = "";
			   if ( discoverNbrItem > 0 )
			   {
					output += '<div id="discoveritemblock1">';
			   }
			   
			   var block = 1;
			   $( data ).find( 'item[type="' + type + '"]' ).each( function ( pos )
			      {
				     var curBlock = Math.floor( pos / discoverNbrMaxByPage ) + 1;
					 
					 if ( block != curBlock )
					 {
						block = curBlock;
						output += '</div>';
						output += '<div id="discoveritemblock' + block + '">';
					 }

					 var href = $( this ).attr( "href" );
					 var pictureSrc = $( this ).find( "picture" ).attr( "src" );
					 var pictureAlt = $( this ).find( "picture" ).text();
					 var title = $( this ).find( "title" ).text();
					 var info = $( this ).find( "info" ).text();
					 var description = $( this ).find( "description" ).text();
				  
					 var curItem = pos + 1;
					 output += '<a id="discoveritem' + curItem + '" class="discoveritem" href="' +  href + '">';
					 output += '<img id="discoveritempicture' + curItem + '" class="discoveritempicture pictureshadow" alt="' + pictureAlt + '" src="' + pictureSrc + '"/>';
					 output += '<p id="discoveritemtitle' + curItem + '" class="discoveritemtitle">' + title + '</p>';
					 output += '</a>';
				  }
			   );

			   if ( discoverNbrItem > 0 )
			   {
					output += '</div>';
					$( output ).appendTo( "#discoveritems" );
					/*
				    $( "#discoveritems" ).delay( 300 ).animate( { opacity: .5 }, 500, "linear", function() {   
					   $( "#discoveritems" ).animate( { top:0, opacity: 1 }, 500, "linear", function() {} );
				    } );*/
			   }
			   
			   discover_updateArrow();
			   discover_displayEffect( discoverCurPage );
			}
		}
	);
}

function discover_updateArrow()
{
   $( "#discovermoveleft" ).removeClass( "discovermoveleft discovermoveleftdisable" );
   $( "#discovermoveright" ).removeClass( "discovermoveright discovermoverightdisable" );
   
   if ( discoverCurPage == 1 || discoverNbrPage == 1 )
   {
      $( "#discovermoveleft" ).addClass( "discovermoveleftdisable" );
   }
   else
   {
      $( "#discovermoveleft" ).addClass( "discovermoveleft" );   
   }

   if ( discoverCurPage == discoverNbrPage )
   {
      $( "#discovermoveright" ).addClass( "discovermoverightdisable" );
   }
   else
   {
      $( "#discovermoveright" ).addClass( "discovermoveright" );
   }
}

function discover_displayEffect( curPage )
{
   for ( var i = 0; i < $( "#discoveritems" ).find( "div" ).length; i++ )
   {
	  $( "#discoveritemblock" + ( i + 1 ) ).animate( { marginLeft: -524 * ( curPage - ( i + 1 ) ) } );
   }
}

function discover_moveLeftHandler()
{
   if ( discoverCurPage > 1 && $( "#discovermoveleft" ).hasClass( "discovermoveleft" ) )
   {
	   discover_displayEffect( --discoverCurPage );
	   discover_updateArrow();
   }
}

function discover_moveRightHandler()
{
   if ( discoverCurPage <= discoverNbrPage && $( "#discovermoveright" ).hasClass( "discovermoveright" ) )
   {
	   discover_displayEffect( ++discoverCurPage );
	   discover_updateArrow();
   }
}

/** 
	knowledge component
*/

var knowledgeTimer = null;
var knowledgeTimerEnable = false;
var knowledgeItemSelected = 1;
var knowledgeNbrMaxItem = 1;

function knowledge_init()
{   
   knowledge_loadData();
}

function knowledge_loadData()
{
	$.ajax
	(
		{
			type: "GET",
			url: "components/com_netbuilder_website/knowledge/knowledge_data.xml?" + Date(),
			dataType: "xml",
			success: function( data )
			{
			   $( "#knowledgeitems" ).html( "" );

			   knowledgeNbrMaxItem = $( data ).find( "item" ).length;

			   $( data ).find( "item" ).each( function ( pos )
			      {
					 var curItem = pos + 1;

					 var statement = $( this ).find( "statement" ).text();
					 
					 var output = "";
					 output += '<p id="knowledgeitem' + curItem + '" class="knowledgeitem">'
					 output += statement + '</p>';
					 
					 $( output ).appendTo( "#knowledgeitems" );
				  }
			   );
			   
			   knowledge_clickHandler( 1 );
			   knowledge_startTimer();

			   $( "#knowledgeitems" ).find( "p" ).each( function ( pos )
				  {
					 $( this ).mouseover( knowledge_stopTimer );
					 $( this ).mouseout( knowledge_startTimer );
				  }
			   );
		    }
		}
	);
}

function knowledge_startTimer()
{
   if ( !knowledgeTimerEnable )
   {
      knowledgeTimerEnable = true;
      knowledgeTimer = setTimeout( "knowledge_timeoutHandler()", 7000 );
   }
}

function knowledge_timeoutHandler()
{
   if ( knowledgeTimerEnable )
   {
      knowledgeItemSelected =  knowledgeItemSelected % knowledgeNbrMaxItem + 1;
      knowledge_clickHandler( knowledgeItemSelected );
   }
}

function knowledge_stopTimer()
{
   if ( knowledgeTimerEnable )
   {
      knowledgeTimerEnable = false;
      clearTimeout(knowledgeTimer);
   }
}

function knowledge_clickHandler( pos )
{
   if ( knowledgeTimer )
   {
      knowledge_stopTimer();
   }

   $( "#knowledgeitems" ).find( "p" ).each( function ( pos )
	  {
		 $( this ).hide();
	  }
   );

   $( document.getElementById( "knowledgeitem" + pos ) ).fadeIn( 500 );
   
   if ( knowledgeTimer )
   {
      knowledgeItemSelected = pos;
      knowledge_startTimer();
   }
}

/** 
	navigation component
*/

function nav_clickHandler( id )
{
   $( ".my-nav" ).find( "li" ).each( function ( pos )
	  {
	  	if ( $( this ).hasClass( "selected" ) )
	  	{
		    $( this ).removeClass( "selected" );
	  	}
	  }
   );
   
   $( ".my-nav-content" ).find( "div" ).each( function ( pos )
	  {
		$( this ).hide();
	  }
   );
   
   $( "#item-" + id ).addClass( "selected" );
   $( "#" + id ).show();
}

/** 
	form component
*/

function contactForm_typeChangeHandler( value )
{
	if ( value == "Business" )
	{
		$( ".form-job" ).hide();
		$( ".form-defined" ).show();
		$( ".form-business" ).show();
	}
	else if ( value == "Job" )
	{
		$( ".form-business" ).hide();
		$( ".form-defined" ).show();
		$( ".form-job" ).show();
	}
	else
	{
		$( ".form-defined" ).hide();
		$( ".form-job" ).hide();
		$( ".form-business" ).hide();
	}
}
