// kill jQuery alias
var $j = jQuery.noConflict();


var config = {};
config.url = "http://www.silenceit.ca/";

var search = {};

var past = { page: "", section: ""};

var navigation = {};
navigation.display = 0;
navigation.timeout = 250; // milliseconds


/*var link_refresh = function (element)
{

		$j(element).each( function () {
		
			if ( $j(this).attr('target') != '_blank' )
			{
				$j(this).click( function() {
						
					//if ( $j(this).text() != 'Blog' && $j(this).text() != 'Careers' && !$j(this).hasClass("no-ajax"))
					if ( $j(this).text() != 'Careers' )
					{
						if (location.href != config.url)
						{
							location.href = config.url + "#/" + $j(this).attr('href').replace(config.url, "");
						}
						else
						{
							$j.address.value( $j(this).attr('href').replace(config.url, "") );
						}
					}
					
					
					return false;
				});
			}
		
		});
		
}*/

( function ($) {

	$.fn.extend({
	
		refresh_links: function () {
			
			$.each( $(this), function () {
			
				if ($(this).attr("target") != "_blank" && !$(this).hasClass("no-ajax"))
				{
					$(this).click( function () {
					
					
						//if ( $j(this).text() != 'Blog' && $j(this).text() != 'Careers' && !$j(this).hasClass("no-ajax"))
						if ( $(this).text() != '' )
						{
							if (location.href != config.url)
							{
								location.href = config.url + "#/" + $(this).attr('href').replace(config.url, "");
							}
							else
							{
								$.address.value( $(this).attr('href').replace(config.url, "") );
							}
							
							return false;
						}
					
					
					});
				}
			});
			
		}
	
	});

})(jQuery);



/*
$j(function ()
{
	$j('ul#overview_navigation > li').each( function () {
		$j(this).mouseover( function () {
			navigation.animate($j('div.overview_subnavigation_container', this));
		});
	});
});
*/

$j(function () {

	//$j.address.history(false);
	$j("#home #global").hide();

	// clean search
	$j("#search_input").focus( function () {
	
		if ( $j(this).val() == "Search" )
		{
			$j(this).val("");
		}
	
	}).blur( function () {
		if (!$j(this).val())
		{
			$j(this).val("Search");
		}
	});

	// initialize the history event.
	$j.address.change( function (address) {
	
		page = address.value.substr(1);
		section = address.value.split("/")[1];
		
		updated = "";
				
		if (page != past.page) {
		
			// redirect the if they aren't in the root.
			if (location.href != config.url)
			{
				location.href = config.url + "#/" + page;
			}
		
			// check whether we need all or just partial content.
			if (section != past.section)
			{
				// check which section we're in.
				if ( $j("#section-" + section).length > 0 || section == "")
				{
					$j("#overview_navigation > li > a, #overview-singles > li > a").removeClass('current');
					$j("#section-" + section).addClass('current');
				}
				
			
				$j.get(config.url + page, { ajax: 'full' }, function (data)
				{

					$j("#container").fadeOut( function () { 
					
						if (page == "")
							$j("body").attr("id", "home");
						else
							$j("body").removeAttr("id");
						
						$j(this).empty().html(data).fadeIn( function () {
						
							$j("a", this).refresh_links();
						
						if (section == 'contact-us')
							{
								initialize();
							}
						
						});
						
					});
					
					$j('#sub-navigation').accordian(true);
					//link_refresh("#container a");
					
					//console.log("got this far");
					//$j("#container a").click( function () { console.log("test"); return false; });
					
					$j("#global").show();
					
				});	
			}
			else
			{
				$j.get(config.url + page, { ajax: 'content' }, function (data)
				{
			
					
					$j("#content-pane").fadeOut( function () { $j("body").removeAttr("id"); $j(this).empty().html(data).fadeIn( function () {
						
							$j("a", this).refresh_links();
						
						}); });
					//link_refresh("#content-pane a");
					
					$j("#global").show();
				
				});
			}			
		}
		else
		{
			$j("#global").show();
		}
				
		// set the globals to the current selection.	
		past.page = page;
		past.section = section;
	});
	
	$j("a").refresh_links();
	
	//link_refresh("a");
	
	/*
	//if ($j(".do-not-use-ajax").length == 0)
	//{
		$j("a").click( function() {
			
			//if ( $j(this).text() != 'Blog' && $j(this).text() != 'Careers' && !$j(this).hasClass("no-ajax"))
			//{
				if (location.href != config.url)
				{
					location.href = config.url + "#/" + $j(this).attr('href').replace(config.url, "");
				}
				else
				{
					$j.address.value( $j(this).attr('href').replace(config.url, "") );
				}
				
				return false;
			//}
		});
	//}*/

	$j('#home #client-rotation').cycle();

	$j('#sub-navigation').accordian();
	
	navigation.current = $j('ul#overview_navigation a.current');
	
	$j('#header').appendTo('#overview');
	
	$j('ul#overview_navigation > li').each( function ()
	{
		$j('div', this).removeClass('overview_subnavigation_css').addClass('overview_subnavigation_js');
	});
	
	// main navigation
	/*$j('ul#overview_navigation > li').hover(
		function ()
		{
			$j("#section-" + past.section).removeClass('current');
		
			clearTimeout(navigation.menu_timer);
			navigation.current.removeClass('current');
			navigation.display = true;
			
			$j('#header, #division_navigation').css('overflow', 'hidden');
			$j('#header').stop().animate({height: '0px'},{queue:false, duration: 'fast', complete: function ()
			{
				$j('#header').css('border-bottom', 'none');
			}});
			$j('h2 a', this).addClass('current');
			$j('div.overview_subnavigation_container', this).css('border-bottom', '1px solid #B6C0C6');
			
			
			var h = 0;
			
			$j('ul.overview_subnavigation', this).each(
				function () { if ($j(this).height() > h) h = $j(this).height() + 20; }
			);
			
			$j('div.overview_subnavigation_container', this).stop().animate({height: h+'px'},{queue:false, duration: 'slow'});
		},
		function ()
		{
			$j("#section-" + past.section).addClass('current');
		
			navigation.display = false;
			
			$j('h2 a', this).removeClass('current');
			$j('div.overview_subnavigation_container', this).stop().animate({height:'0px'},{queue:false, duration: 'fast'});
			$j('div.overview_subnavigation_container', this).css('border-bottom', 'none');
			
			if ($j(window).scrollTop() == 0)
			{
				navigation.menu_timer = setTimeout( function ()
				{
					navigation.current.addClass('current');
					$j('#header').css('border-bottom', '1px solid #B6C0C6');
					$j('#header, #division_navigation').css('overflow', 'hidden');
					$j('#header').stop().animate({height: '40px'},{queue:false, duration: 'slow', complete: function ()
					{
						$j('#header, #division_navigation').css('overflow', 'visible');
					}});
				}, navigation.timeout);
			}
		}
	);*/
	
		
	var hover_i = {
		sensitivity: 3,
		interval: 250,
		timeout: 250,
		over: function()
		{
			
			$j('#header, #division_navigation').css('overflow', 'hidden');
			$j('#header').stop().animate({height: '0px'},{queue:false, duration: 'fast', complete: function ()
			{
				$j('#header').css('border-bottom', 'none');
			}});
			$j('h2 a', this).addClass('current');
			$j('div.overview_subnavigation_container', this).css('border-bottom', '1px solid #B6C0C6');
			
			
			var h = 0;
			
			$j('ul.overview_subnavigation', this).each(
				function () { if ($j(this).height() > h) h = $j(this).height() + 20; }
			);
			
			$j('div.overview_subnavigation_container', this).stop().animate({height: h+'px'},{queue:false, duration: 'slow'});
		},
		out: function()
		{			
			$j('h2 a', this).removeClass('current');
			$j('div.overview_subnavigation_container', this).stop().animate({height:'0px'},{queue:false, duration: 'fast', complete: function()
				{
					if (!navigation.display)
					{
						$j("#section-" + past.section).addClass('current');
					}
				}
			});
			$j('div.overview_subnavigation_container', this).css('border-bottom', 'none');
			
			navigation.display--;
		}
	};
	
	$j('ul#overview_navigation > li').hoverIntent(hover_i).mouseover(
	
	function () { 
			navigation.display++;$j("#section-" + past.section).removeClass('current'); }

	);
	
	
	// options navigation
	/*$j('ul#options_navigation > li').hover(
		function ()
		{
			clearTimeout(navigation.menu_timer);
			navigation.current.removeClass('current');
			navigation.display = true;
			
			$j('#header, #division_navigation').css('overflow', 'hidden');
			$j('#header').stop().animate({height: '0px'},{queue:false, duration: 'fast', complete: function ()
			{
				$j('#header').css('border-bottom', 'none');
			}});
			
			$j('h2 a', this).addClass('current');
			$j('div.options_navigation_dropdown', this).css('border-bottom', '1px solid #B6C0C6');
			$j('div.options_navigation_dropdown', this).stop().animate({height: '100px'},{queue:false, duration: 'slow'});
		},
		function ()
		{
			navigation.display = false;
			
			$j('h2 a', this).removeClass('current');
			$j('div.options_navigation_dropdown', this).stop().animate({height:'0px'},{queue:false, duration: 'fast'});
			$j('div.options_navigation_dropdown', this).css('border-bottom', 'none');
			
			if ($j(window).scrollTop() == 0)
			{
				navigation.menu_timer = setTimeout( function ()
				{
					navigation.current.addClass('current');
					$j('#header').css('border-bottom', '1px solid #B6C0C6');
					$j('#header').css('overflow', 'hidden');
					$j('#header').stop().animate({height: '40px'},{queue:false, duration: 'slow', complete: function ()
					{
						$j('#header, #division_navigation').css('overflow', 'visible');
					}});
				}, navigation.timeout);
			}
		}
	);
	
	$j('#division_navigation > li').hover(
		function ()
		{
			var h = $j('ul.division_dropdown', this).height() + 1;
			$j('div.division_dropdown_wrapper', this).stop().animate({height: h+'px'},{queue:false, duration: 'slow'});
		},
		function ()
		{
			$j('div.division_dropdown_wrapper', this).stop().animate({height:'0px'},{queue:false, duration: 'fast'});
		}
	);*/

	
	$j(window).scroll( function (e)
	{
		if ($j(window).scrollTop() > 0)
		{
			navigation.current.removeClass('current');
			$j('#header').css('overflow', 'hidden');
			$j('#header').stop().animate({height: '0px'},{queue:false, duration: 'slow', complete: function ()
			{
				$j('#header').css('border-bottom', 'none');
			}});
		}
		else
		{
			if (!navigation.display)
			{
				navigation.current.addClass('current');
				$j('#header').css('border-bottom', '1px solid #B6C0C6');
				$j('#header').css('overflow', 'hidden');
				$j('#header').stop().animate({height: '40px'},{queue:false, duration: 'slow', complete: function ()
				{
					$j('#header').css('overflow', 'visible');
				}});
			}
		}
	});
});

/*
$j(function () {
	$j('ul#overview_navigation > li').mouseover(
		function () {
			$j('h2 a', this).addClass('current');
			$j('div.overview_subnavigation_container', this).stop().slideDown('normal');
		}
	);
	
	$j('ul#overview_navigation > li').mouseout(
		function () {
			$j('h2 a', this).removeClass('current');
			$j('div.overview_subnavigation_container', this).stop().slideUp('normal');
		}
	);
});

/*
$j(function () {
	$j('ul#overview_navigation > li').hover( function () {
		$j('h2 a', this).addClass('current');
		$j('div.overview_subnavigation_container', this).slideDown('normal');
		/*clearTimeout();
		
		if (navigation.display)
		{
			$j('ul.overview_subnavigation', this).show();
		}
		else
		{
			navigation.display = true;
			$j('ul.overview_subnavigation', this).slideDown('normal');
		}
	}, function () {
		$j('h2 a', this).removeClass('current');
		$j('div.overview_subnavigation_container', this).fadeOut('slow');
		/*navigation.display = false;
		var $jthis = $j('ul.overview_subnavigation', this);
		
		setTimeout(function () {
			if (!navigation.display)
			{
				$jthis.slideUp('normal');
			}
			else
			{
				$jthis.hide();
			}
		}, navigation.timeout);
	});
});
*/
