/* Global Js Document */

/*
//Add the auto scroll
//get the top offset of the target anchor
trgt = $(this).parent().children('ul').slideToggle()
var target_offset = $("#"+trgt).offset();
var target_top = target_offset.top;

//goto that anchor by setting the body scroll top to anchor top
$('html, body').animate({scrollTop:target_top}, 500);
*/

document.base_path = '/'

function hide_all_categories()
{
	$('#client-list ul.open-category').slideToggle('slow').removeClass('open-category')
}

function hide_all_case_studies()
{
	$('#case-studies .content.open-case-study').slideToggle('slow').removeClass('open-case-study')
}

function scroll_to_client()
{
	
	if( document.current_client == 'none' )
	{
		return false;
	}
	else
	{
		//Add the auto scroll
		//get the top offset of the target anchor
		trgt = 'view-' + document.current_client;
		var target_offset = $("#"+trgt).offset();
		var target_top = target_offset.top;

		//goto that anchor by setting the body scroll top to anchor top
		$('html, body').animate({scrollTop:target_top}, 500);
	}
}

/* On Document Ready To Interact With */

jQuery(document).ready(

	function($)
	{
		/* Home Page Slideshow */
		
		$('#slideshow').cycle( { timeout: 7000 } )
		
		/* What Makes Aerios Unique Scrollable Content */

		if ( $('#unique-scroller').length )
		{
			$('#unique-scroller').jScrollPane(
				{
					showArrows : true,
					scrollbarWidth : 15
				}
			)
		}

		/* Key People Widget */

		if ( $('#widget-key-people a').length )
		{
			$('#widget-key-people a').keyPeople()
		}

		/* Aerios Unique Widget Flow Box */

		var flowbox_template = '\
		<div id="facebox" class="unique-facebox" style="display:none;"> \
			<div class="popup"> \
				<table> \
					<tbody id="facebox-tbody"> \
						<tr> \
							<td class="tl"/><td class="b"/><td class="tr"/> \
						</tr> \
						<tr> \
							<td class="b"/> \
							<td class="body"> \
								<div class="content"> \
								</div> \
							</td> \
							<td class="b"/> \
						</tr> \
						<tr> \
							<td class="bl"/><td class="b"/><td class="br"/> \
						</tr> \
					</tbody> \
				</table> \
			</div> \
		</div>';
		
		$('a.flow-box').facebox(

			{
				loadingImage : document.base_path + '_assets/plugins/facebox/loading.gif',
				closeImage : document.base_path + '_assets/plugins/facebox/closelabel.gif',
				faceboxHtml : flowbox_template
			}

		)

		$('.unique-facebox a.arrow-next, .unique-facebox a.arrow-previous').live(

			'click',

			function()
			{	
				// get the value of the clicked link
				var load_id = $(this).attr('rel')

				var load_page = document.base_path + 'index.php?/popup/unique/' + load_id

				$.ajax(
					{
						url : load_page,
						success : function( html )
						{
							$('#facebox .content .popup-widget').fadeOut( 

								'fast',

								function()
								{
									$(this).remove()
									$('#facebox .content').append( html )
								}

							)
						}
					}	
				)

				return false;
			}

		)

		$('a.flow-box-close').live(

			'click',

			function()
			{
				$(document).trigger('close.facebox')

				return false
			}

		)

		/* Key Details Tool Tips */

		if ( $('#widget-key-details').length )
		{
			$('#widget-key-details .content a').tipTip(
				{
					defaultPosition : 'top',
					maxWidth : '300px',
					delay : 200
				}
			)
		}
		
		/* Unique Tool Tips */
		
		if ( $('#unique-tooltips').length )
		{
			$('#unique-tooltips a').tipTip(
				{
					defaultPosition : 'right',
					maxWidth : 'auto',
					delay : 200
				}
			)
		}

		/* Client List Widget */

		if ( $('#client-list').length )
		{
			$('#client-list ul').hide()

			$('#client-list a.category').bind(

				'click',

				function()
				{
					if ( $(this).parent().children('ul').attr('class') == 'open-category' )
					{
						hide_all_categories()
					}
					else
					{
						hide_all_categories()
						$(this).parent().children('ul').slideToggle().addClass('open-category')
					}

					return false;
				}

			)
		}
		
		

		/* Case Studies */

		if ( $('#case-studies').length )
		{
			
			$('#case-studies li .content').not('.open-case-study').hide()

			$('#case-studies li a.header').bind(

				'click',

				function()
				{
					if( $(this).parent().children('.content').attr('class') == 'content open-case-study')
					{
						hide_all_case_studies()
					}
					else
					{
						hide_all_case_studies()
						$(this).parent().children('.content').slideToggle().addClass('open-case-study')
					}

					return false
				}

			)
		}

		/* DB101 Widget */
		
		if ( $('#widget-md-101').length )
		{
			
			var db101_template = '\
			<div id="facebox" class="db101-facebox" style="display:none;"> \
				<div class="popup"> \
					<table> \
						<tbody id="facebox-tbody"> \
							<tr> \
								<td class="tl"/><td class="b"/><td class="tr"/> \
							</tr> \
							<tr> \
								<td class="b"/> \
								<td class="body"> \
									<div class="content"> \
									</div> \
								</td> \
								<td class="b"/> \
							</tr> \
							<tr> \
								<td class="bl"/><td class="b"/><td class="br"/> \
							</tr> \
						</tbody> \
					</table> \
				</div> \
			</div>';

			$('#widget-md-101 #db101').facebox(

				{
					loadingImage : document.base_path + '_assets/plugins/facebox/loading.gif',
					closeImage : document.base_path + '_assets/plugins/facebox/closelabel.gif',
					faceboxHtml : db101_template
				}

			)
			
		}
		
		/* Predictive Widget */
		
		if ( $('#widget-predictive').length )
		{
			
			var predictive_template = '\
			<div id="facebox" class="predictive-facebox" style="display:none;"> \
				<div class="popup"> \
					<table> \
						<tbody id="facebox-tbody"> \
							<tr> \
								<td class="tl"/><td class="b"/><td class="tr"/> \
							</tr> \
							<tr> \
								<td class="b"/> \
								<td class="body"> \
									<div class="content"> \
									</div> \
								</td> \
								<td class="b"/> \
							</tr> \
							<tr> \
								<td class="bl"/><td class="b"/><td class="br"/> \
							</tr> \
						</tbody> \
					</table> \
				</div> \
			</div>';
			
			$('#widget-predictive #predictive').click(
				function()
				{
					$("#facebox").removeClass('db101-facebox');
					$("#facebox").addClass('predictive-facebox');
				}
			);
			
			$('#widget-predictive #predictive').facebox(

				{
					loadingImage : document.base_path + '_assets/plugins/facebox/loading.gif',
					closeImage : document.base_path + '_assets/plugins/facebox/closelabel.gif',
					faceboxHtml : predictive_template
				}

			)
			
			$('.predictive-facebox a.arrow-next, .predictive-facebox a.arrow-previous').live(

				'click',

				function()
				{	
					// get the value of the clicked link
					var load_id = $(this).attr('rel')

					var load_page = document.base_path + 'index.php?/popup/predictive/' + load_id

					$.ajax(
						{
							url : load_page,
							success : function( html )
							{
								$('#facebox .content .popup-widget').fadeOut( 

									'fast',

									function()
									{
										$(this).remove()
										$('#facebox .content').append( html )
									}

								)
							}
						}
					)

					return false;
				}

			)
			
			/* $(document).bind( 'afterReveal.facebox', function(){
				
				if( $('#predictive-slideshow').length )
				{
					$('#predictive-slideshow').cycle( { 
						autostop: true,
						autostopCount: 5,
						delay: 0,
						timeout: 4000
					} )
				}
				
				return false
				
			} ); */
			
		}
		
		/* Behavioral Widget */
		
		if ( $('#widget-behavioral').length )
		{
			
			var behavioral_template = '\
			<div id="facebox" class="behavioral-facebox" style="display:none;"> \
				<div class="popup"> \
					<table> \
						<tbody id="facebox-tbody"> \
							<tr> \
								<td class="tl"/><td class="b"/><td class="tr"/> \
							</tr> \
							<tr> \
								<td class="b"/> \
								<td class="body"> \
									<div class="content"> \
									</div> \
								</td> \
								<td class="b"/> \
							</tr> \
							<tr> \
								<td class="bl"/><td class="b"/><td class="br"/> \
							</tr> \
						</tbody> \
					</table> \
				</div> \
			</div>';

			$('#widget-behavioral #behavioral').facebox(

				{
					loadingImage : document.base_path + '_assets/plugins/facebox/loading.gif',
					closeImage : document.base_path + '_assets/plugins/facebox/closelabel.gif',
					faceboxHtml : behavioral_template
				}

			)
			
			$('.behavioral-facebox a.arrow-next, .behavioral-facebox a.arrow-previous').live(

				'click',

				function()
				{	
					// get the value of the clicked link
					var load_id = $(this).attr('rel')

					var load_page = document.base_path + 'index.php?/popup/behavioral/' + load_id

					$.ajax(
						{
							url : load_page,
							success : function( html )
							{
								$('#facebox .content .popup-widget').fadeOut( 

									'fast',

									function()
									{
										$(this).remove()
										$('#facebox .content').append( html )
									}

								)
							}
						}
					)

					return false;
				}

			)
			
			/*$(document).bind( 'afterReveal.facebox', function(){
				
				if( $('#behavioral-slideshow').length )
				{
					$('#behavioral-slideshow').cycle( { 
						autostop: true,
						autostopCount: 5,
						delay: 0,
						timeout: 4000
					} )
				}
				
				return false
				
			} );*/
			
		}
		
		/* $(this).cycle('pause'); */
		
		/* Quick Tour Widget */
		
		if ( $('#widget-tour').length )
		{
			$('#widget-tour #tour').bind(
				
				'click',
				
				function()
				{
					$pm = $(this).children('div.plus-minus')
					
					if ( 'plus-minus open-tour' == $pm.attr('class') )
					{
						$pm.removeClass('open-tour')
					}
					else
					{
						$pm.addClass('open-tour')
					}
					
					return false
				}
				
			)
			
			var tourbox_template = '\
			<div id="facebox" class="tour-facebox" style="display:none;"> \
				<div class="popup"> \
					<table> \
						<tbody id="facebox-tbody"> \
							<tr> \
								<td class="tl"/><td class="b"/><td class="tr"/> \
							</tr> \
							<tr> \
								<td class="b"/> \
								<td class="body"> \
									<div class="content"> \
									</div> \
								</td> \
								<td class="b"/> \
							</tr> \
							<tr> \
								<td class="bl"/><td class="b"/><td class="br"/> \
							</tr> \
						</tbody> \
					</table> \
				</div> \
			</div>';

			$('#widget-tour #tour').facebox(

				{
					loadingImage : document.base_path + '_assets/plugins/facebox/loading.gif',
					closeImage : document.base_path + '_assets/plugins/facebox/closelabel.gif',
					faceboxHtml : tourbox_template
				}

			)

			$('.tour-facebox a.arrow-next, .tour-facebox a.arrow-previous').live(

				'click',

				function()
				{	
					// get the value of the clicked link
					var load_id = $(this).attr('rel')

					var load_page = document.base_path + 'index.php?/popup/tour/' + load_id

					$.ajax(
						{
							url : load_page,
							success : function( html )
							{
								$('#facebox .content .popup-widget').fadeOut( 

									'fast',

									function()
									{
										$(this).remove()
										$('#facebox .content').append( html )
									}

								)
							}
						}
					)

					return false;
				}

			)
			
			$(document).bind( 'afterClose.facebox', function(){
				
				$pm = $("#widget-tour #tour").children('div.plus-minus')
				
				if ( 'plus-minus open-tour' == $pm.attr('class') )
				{
					$pm.removeClass('open-tour')
				}
				
			} )
			
		}
		
		if ( $('#case-studies').length )
		{
			//Auto scroll the body
			scroll_to_client();
			
		}
		
	}

)
