var start_offset = -1;
var checkOffset = function (startOffset, callCount) {
	if (callCount == 0) {
		return;
	}
	var timeTest = Framewelder_flash_player_v2_remoting.getTimeCode("flash_player");
	if (timeTest < startOffset) {
		Framewelder_flash_player_v2_remoting.setTimeCode("flash_player", startOffset);
		setTimeout(function () {
			checkOffset(startOffset, (callCount - 1));
		}, 2000);
	}
}

$(document).ready(function() {
	//check to see if there is a search offset
	start_offset = $.jget['start_offset'];
	var terms = $.jget['search'];
	var presentation_id = $("#presentation_id").val();
	var minisite_id = $("#minisite_id").val();
	if (start_offset != "" && start_offset != null && start_offset != 'undefined') {
		if (terms != null && terms != '') {
			var from_presentation = $.jget['from_presentation'];
			var from_presentation_offset = $.jget['from_presentation_offset'];
			$("#search_input").val(terms);
			$.ajax({
				type: "GET",
				url: "/ajax/search_json/index.php",
				data: {mode: 'single', search: terms, presentation_id: presentation_id, minisite_id: minisite_id, from_presentation: from_presentation, from_presentation_offset: from_presentation_offset},
				dataType: "json",
				success: function(json) {
					render_search(json);
				},
				error: function() {
					alert('error');
				}
			});
		}
		try {
			//this needs to be called in a loop where we check the current timecode to make sure it worked
			Framewelder_flash_player_v2_remoting.setTimeCode("flash_player", start_offset);
			setTimeout(function () {
				checkOffset (start_offset, 8);
			}, 2000);
		} catch (err) {}
	}
	
	//add behaviors to search button
	$("#search_button").click(function (e) {
		$('#search_form').submit();
	}).css({
		cursor: 'pointer'
	});
	 
	single_search = function() {
		var options = {
			target: null, 
			beforeSubmit: null,  
			success: render_search, 
			url: '/ajax/search_json/index.php?mode=single', 
			type: 'get', 
			dataType: 'json', 
			clearForm: false, 
			resetForm: false
		}; 
        $(this).ajaxSubmit(options);
        return false; 
    };
    global_search = function() {
		var options = {
			target: null, 
			beforeSubmit: null,  
			success: render_search, 
			url: '/ajax/search_json/index.php?mode=global', 
			type: 'get', 
			dataType: 'json', 
			clearForm: false, 
			resetForm: false
		}; 
        $(this).ajaxSubmit(options);
        return false; 
    };
	$('#search_form').submit(single_search);
    
    $("#search_input")
    	.focus(function (e) {
    		$(this)
    			.val('')
    			.css({color: '#000000'});
    	});
});
