var $i = 0;

jQuery.fn.innerWrap = function() {
	var a, args = arguments;
	return this.each(function() {
		if (!a)
			a = jQuery.clean(args, this.ownerDocument);
		// Clone the structure that weâ€™re using to wrap
		var b = a[0].cloneNode(true),
		    c = b;
		// Find the deepest point in the wrap structure
		while ( b.firstChild )
			b = b.firstChild;
		// append the child nodes to the wrapper
		jQuery.each(this.childNodes, function(i, node) { 
			b.appendChild(node); 
		});
		jQuery(this)
			// clear the element
			.empty()
			// add the new wrapper with the previous child nodes appended
			.append(c);
	});
};


$.fn.toolboxEntry = function (options) {
	var defaults = {toolboxurl : "http://www.bouwmaat.nl/toolbox/"};
	var options = $.extend(defaults, options);

	var locatie = window.location.href;
	var split = locatie.split(options.toolboxurl);	
	newloc = options.toolboxurl + '#/' + split['1'];
	
	window.location = newloc;		
		
}

function pageHeight(){


	var comparewith;
	
	if($('#sidebar-left').height() >= $('#sidebar-right').height()){
		comparewith = $('#sidebar-left');
	}else{
		  	comparewith = $('#sidebar-right');
	}
	
	if (comparewith.height() >= $('.content.bread').height()) {
	    $('.content.bread').css({
	        minHeight: comparewith.outerHeight()
	    });
	}	

}

function minimizeCats(){
	$('#toolbox .toolbox-item-container:not(.minimized)').fadeOut(500, function(){
		$(this).addClass('minimized').fadeIn();	
	})

}

function maximizeCats(){
	$('#detail-wrapper').fadeOut(500, function(){
		$(this).remove();
	});
	$('#toolbox .toolbox-item-container.minimized').fadeOut(500, function(){
		$(this).removeClass('minimized').fadeIn();
		
	})}

function activateItem(url){
	$('#toolbox-details').addClass('loading');
	$('#toolbox-details').load(url + ' #detail-wrapper', function(){
		$('.loading').removeClass('loading');		
		$.doTimeout(500, function(){
			document.title = $('#toolbox .detail-content h2').html();
		})
	});
	$('div.crumbs').load(url + ' .crumbs .reset');	
	
	minimizeCats()
}
function activateCat(item, tburl){
	maximizeCats()
	$('#toolbox .toolbox-category').hide().removeClass('active');
	$('#toolbox-navigation .cat-trigger').removeClass('active');

	$(item).addClass('active')
	container = $(item).data("specs").name + "_container";
	$('#' + container).toggle().addClass('active');
	
	$('div.crumbs').load(tburl + $(item).data("specs").name + ' .crumbs .reset');	
	
}
function setupItem(item, url){
	var href = item.attr('href');
	$(item).data("specs", {
		href: href
	})	
	var ajaxhref = href.split(url);
	$(item).attr('href', '#/' + ajaxhref[1] )

}
$.fn.toolbox = function (options) {
	var defaults = {toolboxurl : "http://bouwmaat.brandinteractive.nl/toolbox/"};
	var options = $.extend(defaults, options);

	$('#toolbox').prepend('<div id="toolbox-navigation" class="home-sprite"></div>')
	$('#toolbox').append('<div id="toolbox-details"></div>')
	
	var $trig = $('#toolbox .cat-trigger');
	var $item = $('#toolbox a.toolbox-item');
	
	var $cont = $('#toolbox .toolbox-category');
		
	$trig.appendTo('#toolbox-navigation');
	$cont.not('.active').hide();
	
	$trig.each(function(){
	
		$(this).data("specs", {
			name: $(this).attr('id').split("_trigger")["0"]
		});
		
		$(this).innerWrap('<a href="#/'+ $(this).data("specs").name + '/"></a>')
	})
	
	var loc = window.location.href;
	
	if(loc.indexOf("#") != -1){
	
		loc = loc.split("/#/")[1].split("/");
		
		if(loc[0] != ''){
			var catpage = loc[0];
			$actItem = '#' + catpage + '_trigger';
			activateCat($actItem, options.toolboxurl);			
		}	
		if(loc[1] != ''){
			var $itempage = options.toolboxurl + loc[0] + '/' + loc[1];
			activateItem($itempage);
			
			$('a#' + loc[1]).addClass('active')	
		}
				
	};	
	
	$item.each(function(){	
		setupItem($(this), options.toolboxurl);
	});
		
	$trig.click(function(){
		activateCat(this, options.toolboxurl);
		pageHeight();
		_gaq.push(['_trackPageview', "/toolbox/"+$(this).data("specs").name+"/"]);
	});
		
	$item.click(function(){		
		$item.removeClass('active');
		$(this).addClass('active');
		activateItem($(this).data("specs").href);
		$(this).addClass('loading');
		pageHeight();
		var statsurl = $(this).data("specs").href;
		statsurl = statsurl.replace(options.toolboxurl, "");
		_gaq.push(['_trackPageview', "/toolbox/"+statsurl+"/"]); 
	});

	
	
	
}
