// handle seek mode
var seek_mode = 0;
// hold the state
var state = null;
// set the max depth
var max_depth = null;

$.fn.equalHeights = function(px) {
	// hold max
	var max = 0;
	// get handle to scrollable api
	var api = $("div.scrollable").scrollable();
	// get the visible items
	var items = api.getVisibleItems();
	// need this
	var ids = new Array();
	// set the ids
	for(i=0;i<items.length;i++) {
		// set the ids
		ids[i] = $(items[i]).attr('id');
	}
	// standard padding
	var padding = 40;
	// get all the divs
	$(this).each(function() {
		// check the children here
		$(this).children().each(function(i) {
			// set the id
			var id = $(this).attr('id');
			// use only visible
			if(in_array(id, ids)) {
				// set the current tallest here and hold for now
				if ($(this).height() > max) { max = $(this).height(); }
			}
		});
		// set the scrollable size here and get back to business
		$("div.scrollable").css("height", max+padding + "px");
	});
	// return
	return this;
};

function handleChoose(context, checkbox, tag_id) {
	// check the context
	if(context == 'browse') {
		return;
	} else {
		// get the action
		action = 'unclick'; if(checkbox.checked) { action = 'click'; }
		// set the url
		url = "/tags/"+action+"/"+context+"/"+tag_id;
		// make json call here
		$.getJSON(url, function(data) {
			// sanity check
			if(data.slug) {
				// set the slug
				$("#slug").html(data.slug);
			}
			// sanity check
			if(data.error) {
				alert(data.error.name);
				// uncheck the box
				$("#cb_"+tag_id).attr('checked', false);
				// blur the box here
				$("#cb_"+tag_id).blur();
			}
		});
	}
}

function handleSeek(context, tag_id) {
	// set seek mode
	seek_mode = 1;
	// need this
	$.getJSON('/tags/seek/'+context+'/'+tag_id, function(data) {
		// sanity check
		if(data.list) {
			// clear box
			clearBox(0);
			// clear state
			clearState(0);
			// loop the results
			for(i=0;i<max_depth;i++) {
				// sanity check
				if(data.list[i]) {
					// echo the divs
					$("#div_"+i).html(data.list[i]);
					// sanity check
					if(data.state[i]) {
						// recreate the state
						setState(i, data.state[i]);
					}
				} else {
					// get handle to scrollable api
					var api = $("div.scrollable").scrollable();
					// scroll to the left
					api.seekTo(eval(i - 3));
					// break out
					break;
				}
			}
		}
		// set the height
		$('div.items').equalHeights();
	});
}

function handleDrill(context, level, tag_id) {
	// set the child
	child = eval(level+1);
	// check active
	if(state[level]!=tag_id) {
		// make the ajax call here - get the data we need
		$.getJSON("/tags/"+context+"_drill/"+child+"/"+tag_id+"/"+seek_mode, function(data) {
			// sanity check
			if(data.list[0]) {
				// set the div
				$("#div_"+child).html(data.list[0]);
			} else {
				// set the div
				$("#div_"+child).html("");
			}
			// set the height
			$('div.items').equalHeights();
		});
		// select the tag
		setSelected(state[level], tag_id);
		// set the stat
		setState(level, tag_id);
		// scroll
		handleScroll();	
	} else {
		// deselect the tag here
		setSelected(state[child], null);
	}
	// clear the box
	clearBox(child+1);
	// clear the state
	clearState(child);
}

function clearBox(index) {
	for(i=index;i<max_depth;i++) {
		// echo the divs
		$("#div_"+i).html('');
	}
	// set the height
	$('div.items').equalHeights();
}

function clearState(index) {
	// clear the child divs
	for(i=index;i<max_depth;i++) {
		// empty active
		setState(i, 0);
	}
}

function setState(level, tag_id) {
	// set the state here
	state[level] = tag_id;
}

function handleScroll() {
	// get handle to scrollable api
	var api = $("div.scrollable").scrollable();
	// get the visible items here 
	items = api.getVisibleItems();
	// get the id of the last visible item
	last_visible_id = items[api.getConf().size-1].id;
	// get the index
	last_visible_index = last_visible_id.split('_')[1];
	// move next if the div is not visible
	if(level == last_visible_index && level < max_depth-1) {
		// move
		api.next();
		// write the button
		$('div.back').css('visibility', 'visible');
	}
	// reload
	api.reload();
}

function handleSlug(context) {
	// set the slug here
	$("#slug").load("/tags/slug/"+context);
}

function setSelected(tag_id_off, tag_id_on) {
	// sanity check
	if(tag_id_off) {
		// set deselect here
		$("#li_"+tag_id_off).removeClass('current');
	}
	if(tag_id_on) {
		// set selected here
		$("#li_"+tag_id_on).addClass('current');
	}
}

// php clone here - no biggie
function in_array(needle, haystack, argStrict) {
    var key = '', strict = !!argStrict;
    if (strict) {
        for (key in haystack) {
            if (haystack[key] === needle) {
                return true;
            }
        }
    } else {
        for (key in haystack) {
            if (haystack[key] == needle) {
                return true;
            }
        }
    }
    return false;
}