/*
 * requires jQuery (written with jQuery-1.4.2.min.js)
 *
 * Author: Philipp Wrann
 *         philippwrann@gmx.at
 *
 */


/*
 * show loading animation during ajax request
 *
 */

jQuery.fn.prependAjaxLoader = function()
{
	$(this).prepend('<div class="ajax-loader-div"><div></div></div>');

	$('.ajax-loader-div', this).css(
	{
		width	:	$(this).attr('offsetWidth'),
		height	:	$(this).attr('offsetHeight')
	});
}


/*
 * Loads the content of the url and puts it into the specified element "tar"
 * TODO: set a timeout
 */
var loading = false; // helper to prevent multiple calling of the loading function

jQuery.fn.ajaxLoad = function(tar)
{
	$(this).click(function(e)
	{
		el = $(this);
		ec = this.className;
		
		if (loading) return false;

		if (el.attr('href') == undefined) return false;

		loading = true;

		// add loader
		$(tar).prependAjaxLoader();

		var url = el.attr('href').split('#')[0];

		// load content
		$(tar).load(url,{isajaxrequest:'yes'},function(responseText, textStatus, XMLHttpRequest)
		{
			$('.'+ec).unbind('click');
			$('.'+ec).ajaxLoad(tar);
			$('a.external').externalLink();
			//$('.ajax-ready-portfolio').ajaxLoad('#portfolio-sub');
			loading = false;
		});

		// switch 'active' class
		$(this).parent().siblings('li').removeClass('active');
		$(this).parent().addClass('active');

		return false;
	});
};

jQuery.fn.externalLink = function()
{
	$(this).click(function()
	{
		window.open($(this).attr('href'));
		return false;
	});
}

$(document).ready(function(){

	$('a').focus(function()
	{
		this.blur();
	});
	
	$('a.external').externalLink();

	$('.ajax-ready').ajaxLoad('#sec_con_container');
	//$('.ajax-ready-portfolio').ajaxLoad('#portfolio-sub');
	
	$('.dynmenu ul').hide();
	$('.dynmenu li div').bind('click',function()
	{
		$(this).siblings('ul').toggle();
		return false;
	});

});
