$(window).load(function() {
	
	var list_array = new Array();
	
	function get_position( array_name , array_value ){
		
		for(var i=0; i<array_name.length; i++){ 
		
			if( array_name[i] == array_value ){
				
				return i;
				
			}
	    }
	}
	
	function li_html( label ){
		
		var id = label.replace(/\//g, "").replace(/ /g, "-").toLowerCase();
		
		var li = $('<li></li>');
		
		var div = $('<div class="icon" style="background:url(images/icons/'+id+'.png); height:50px; width:50px;"></div>')
		
		$(li).attr('id', id);
		
		
		
		$(li).html( label );
		
		$(li).prepend( $(div) );

		return $(li);
	}
	
	function ul_html(num, label, children){
		
		var id = label.replace(" ", "-").toLowerCase();
		
		list_array[num] = id;
		
		var ul = $('<ul></ul>');
		
		$(ul).addClass(id);
		
		var i = 0;
		
		var html = "";
		
		while( children[i] != undefined ){
			
			var li = li_html( children[i].label );
			
			$(ul).append( li );
				
			i++;
		}

		var element = $("<li></li>");
		
		$(element).addClass('top')
		
		$(element).html( '<div>'+label+'</div>' );
		
		$('#subslide-list').append(ul);
		
		$(element).attr('id', id);


		return $(element);
	}
	
	var stop = false;
	
	function cta_html( content ){
		

		var div = $('<div>&nbsp;</div>');
		
		$(div).addClass('li-cta');
		
		var a = $('<a></a>');
		
		$(a).attr('href', '#case-callout');
		
		$(a).html('contact us about your case');
		
		$(a).fancybox({
			'titlePosition'		: 'inside',
			'transitionIn'		: 'none',
			'transitionOut'		: 'none',
			'onStart'			: function(){
				/*<p>We've handled numerous cases handling "+content+". </p>*/
				$('#case-callout').html( "<p>Call (713) 224-7644 for a free phone consultation.</p>");
				
				stop = true;
			},
			'onClosed'			: function(){
				
				stop = false;
			}
		});
		
		$(div).append(a);

		
		
		
		return $(div);
	}
	
	var active = -1;
	
	
	
	function next_active(){
		
		if(stop == false){
		
			var a = active + 1;
			
			set_active( a );
		
		}
		
	}
	
	function set_active( a ){
		
		$('#'+list_array[active]).removeClass('active');
	
		$('.'+list_array[active]).removeClass('active');
		
		active = a == list_array.length ? 0 : a;
		
		$('#'+list_array[active]).addClass('active');
	
		$('.'+list_array[active]).addClass('active');
		
	}
	
	$("#slidecontainer").hover(
		
		function(){
		
			stop = true;
		
		},
		
		function () {
			
			stop = false;
			
		}
	);
	

	$.getJSON('js/cases.js', function(data) {
		
		var i = 0;
		var html = "";
		
		while( data.cases[i] != undefined){

			var ul = ul_html( i, data.cases[i].label, data.cases[i].children );
			
			$('#mainslide-list').append( ul );
			
			i++;
			
		}
		
		$('li.top').click(
			function(){
				
				set_active( get_position( list_array , $(this).attr('id') ) );
				
			}
		);
		
		$("div#subslide-list ul li").hover(

			function(){
				
				$(this).append( cta_html( $(this).text() ) );

			},

			function () {

				$('.li-cta').remove();

			}
		);
		
		next_active();
		
		setInterval(function(){ 
			
			next_active(); 
			
		}, 8000);
		
	});
	
});
