var url_root = "http://www.manateecomics.com/";

    /*stuff goes here
Shadowbox.init({
	// skip the automatic setup, we'll do this later manually
	skipSetup: true,
	// include the quicktime and windows media player players for playing
	// movies (assuming we have some movies we want to show in either of
	// those formats)
	    players: ["iframe", "html"]
	    //	    initialWidth: 1000,
});
    */
/*
window.onload = function() {
    Shadowbox.setup("#login", {
	    player: "html",
	});
}
*/
function openHTML(url) {
    if (Shadowbox.isActive()) {
	Shadowbox.close();
	}
    $.ajax({url: url, success: displayLogin});
    return false;
};

function displayContactInfo() {
    displayHTML('<div style="text-align: center;"><a class="tabmenu" style="position: absolute; left: 37px; top: 42px;">please send questions and comments to manateecomics@gmail.com</a></div>',
		600,
		100);
};

function displayError(error) {
    displayHTML('<a style="color: red; margin: 20px; margin-right: auto; padding: 20px;">'+error+'</a>', 600, 100);
};

function displayLogin(html) {
    displayHTML(html, 600, 200);
};

function set_shadowboxes() {
    // set up all anchor elements with a "movie" class to work with Shadowbox
    /*   Shadowbox.clearCache();
    Shadowbox.setup("a.big_image", {
	    gallery:            "comics",
	    //autoplayMovies:     true
		player: "iframe",
		width: 1000,
		height: 340,
		handleOversize: "none",
	});
	return false;*/
};

function submitComic(in_filename, in_text, in_title, in_creator, in_path) {
    $.ajax({type: "POST",
		url: url_root + "publishcomic/",
		data: {filename: in_filename,
		    text: in_text,
		    title: in_title,
		    creator: in_creator,
		    path: in_path},
		success: function () {window.location = url_root;}});
    Shadowbox.close();
}

function submitVote(id, valence) {
    $.ajax({
	    type: "POST",
		url: url_root + "vote/",
		data: {id: id,
		    valence: valence},
		error: function () {voteCleanUp(id);}});
    return false;
}

function voteCleanUp(id) {
    //    alert('cleaning up');
    $('.comic_score').load(url_root + "comic_score/" + id + "/");
}

function displayHTML(html, width_int, height_int, finish_fun) {

    var options = {
	resizeDuration: 0.15,
	fadeDuration: 0.15,
	enableKeys: false,
	onFinish: finish_fun,
    };

    var page = {
	player: 'html',
	content: html,
	width: width_int,
	height: height_int,//360,
	options: options
    };
 
    Shadowbox.open([page]);
    //  finish_fun();
};

function setupHover() {
    $('.creator').hover(showCreator, hideCreator);
};

function displayComic(html) {
    displayHTML(html, 1005, 450);
};

function comicPage(file_name) {
    $.ajax({url: url_root + "comic/" + file_name + "/", success: displayComic});
};

function onloadComicPage(id) {
    $.ajax({url: url_root + "onloadcomic/" + id + "/", success: displayComic});
};



function submitPage(file_name, in_text, in_title, in_creator, in_path, source) {//, text, creator, subject_text) {
    $(".make_pane").html('<img class="large_image"  src="'+source+'" alt="word or phrase" title="word or phrase"/>');
    $("#publish_button").unbind('click');
    $("#publish_button").bind('click', function () {
	    submitComic(file_name, in_text, in_title, in_creator, in_path);
	});
};

function popular_next(place) {
    try {
	$(".nav_thumbs").load(url_root + "popular/" + place + "/", set_shadowboxes);
    } catch(err) {
	alert(err);
    }
};

function your_next(place) {
    try {
	$(".nav_thumbs").load(url_root + "your/" + place + "/", set_shadowboxes);
    } catch(err) {
	alert(err);
    }
};

function random_next() {
    try {
	$(".nav_thumbs").load(url_root + "random/", set_shadowboxes);
    } catch(err) {
	alert(err);
    }
};

function recent_next(place) {
    try {
	$(".nav_thumbs").load(url_root + "recent/" + place + "/", set_shadowboxes);
    } catch(err) {
	alert(err);
    }
};

//$('.creator').live('mouseover', showCreator);
//$('.creator').live('mouseout', hideCreator);
/*
$('.clickable').live('mouseover', function (e) {
	$(this).css('color', 'red');
    });

$('.clickable').live('mouseout', function (e) {
	$(this).css('color', 'white');
    });

$('.whole_image').live('mouseover', function (e) {
	$('.hidden').css('visibility', 'visible');
    });

$('.whole_image').live('mouseout', function (e) {
	try {
	    var relTarg = e.relatedTarget || e.toElement;
	    //alert($(relTarg).hasClass('hidden'));
	    if ((! $(relTarg).hasClass('hidden')) && (! $(relTarg).hasClass('hidden2'))) {
		$('.hidden').css('visibility', 'hidden');
	    }
	}catch(e) {alert(e);}
    });

	/*    $('.whole_image').hover(
			function() {
			    alert('hovering');
			    $('.hidden').show();
			},
			function() {
			    $('.hidden').hide();
			});
			});
*/
$("a.inactive").live("click", function(e) {
	$("a.active").addClass('inactive');
	$("a.active").removeClass('active');
	$(this).removeClass('inactive');
	$(this).addClass('active');
    }); 

function highlightThis (obj) {
	$("div.menu_active").addClass('menu_inactive');
	$("div.menu_active").removeClass('menu_active');
	$(obj).removeClass('menu_inactive');
	$(obj).addClass('menu_active');
    } 

$('.mini_image').live('mouseover', function(e) {
        if (! $(this).is('.selected')) {
	    $(this).css('opacity', '1.0');
	}
    });

$('.mini_image').live('mouseout', function(e) {
        if (! $(this).is('.selected')) {
	    $(this).css('opacity', '0.7');
	}
    });

$('.mini_image2').live('mouseover', function(e) {
        if (! $(this).is('.selected')) {
	    $(this).css('opacity', '1.0');
	}
    });

$('.mini_image2').live('mouseout', function(e) {
        if (! $(this).is('.selected')) {
	    $(this).css('opacity', '0.7');
	}
    });

var global_comic_count = 0;
var max_comics = 8;

function setupAdvancedPage() {
    var i = 0;
    var temp = '';
    for (i=0; i < max_comics; i++) {
	try {
	    $.get(url_root + "drawComics/" + i.toString() + "/" + $("#input_make").val().replace(/ /g, "_") + '/', drawAdvancedCleanUp);
	} catch(e) {
	    alert(e);
	}
    }
};

function setupFreeForm() {
    try {
	$("#comics_pane_small").load(url_root + "freeform/" + 
				     $("#image_query").val().replace(/ /g, "_") + '/' + 
				     $("#p0p0").val().replace(/ /g, "_") + '/' + 
				     $("#p0p1").val().replace(/ /g, "_") + '/' + 
				     $("#p1p0").val().replace(/ /g, "_") + '/' + 
				     $("#p1p1").val().replace(/ /g, "_") + '/' + 
				     $("#p2p0").val().replace(/ /g, "_") + '/' + 
				     $("#p2p1").val().replace(/ /g, "_") + '/',
				     drawFreeFormCleanUp);
    } catch(e) {
	alert(e);
    }
};

function setupMeme(id) {
    try {
	$("#comics_pane_small").load(url_root + "customjoke/" + 
				     $("#meme_subject").val().replace(/ /g, "_") + '/' + 
				     $("#hidden_joke_id").val() + '/',
				     drawFreeFormCleanUp);    
    } catch(e) {
	alert(e);
	}
}

function setupMakePage() {
    $(".make_pane").html("<div style=\"width: 900px; height: 306px; border: solid 1px black;\"></div>");
    //$(".make_pane").html("<br/><br/><br/><div class=\"progress_bar\"><div class=\"progress_bar_chunk\"></div></div><a>generating comics, please wait</a>");
    global_comic_count = 0;
    try {
	$("#comic0").load(url_root + "drawComics/0/"+ $("#input_make").val().replace(/ /g, "_") + '/', drawComicsCleanUp);
	$("#comic1").load(url_root + "drawComics/1/"+ $("#input_make").val().replace(/ /g, "_") + '/', drawComicsCleanUp);
	$("#comic2").load(url_root + "drawComics/2/"+ $("#input_make").val().replace(/ /g, "_") + '/', drawComicsCleanUp);
	$("#comic3").load(url_root + "drawComics/3/"+ $("#input_make").val().replace(/ /g, "_") + '/', drawComicsCleanUp);
	$("#comic4").load(url_root + "drawComics/4/"+ $("#input_make").val().replace(/ /g, "_") + '/', drawComicsCleanUp);
	$("#comic5").load(url_root + "drawComics/5/"+ $("#input_make").val().replace(/ /g, "_") + '/', drawComicsCleanUp);
	$("#comic6").load(url_root + "drawComics/6/"+ $("#input_make").val().replace(/ /g, "_") + '/', drawComicsCleanUp);
	$("#comic7").load(url_root + "drawComics/7/"+ $("#input_make").val().replace(/ /g, "_") + '/', drawComicsCleanUp);
    } catch(e) {
	alert(e);
    }
};

function setupMemeMaker(id) {
    $("#mememaker").html('<input type="hidden" id="hidden_joke_id" value="'+id.toString()+'"/><input type="text" class="text_input_field" id="meme_subject"/><input type="submit" class="arrow_button" value="" onclick="makeMeme(); return false;"/>');
}

function displaySubmission(html) {
    displayHTML(html, 1005, 620, setupMakePage);
};

function displayAdvanced(html) {
    displayHTML(html, 1005, 620, setupAdvancedPage);
};

function displayFreeForm(html) {
    displayHTML(html, 1005, 620, setupFreeForm);
};

function displayMeme(html) {
    displayHTML(html, 1005, 620, setupMeme);
};

function makeMeme() {
    $.ajax({url: url_root + "make/", success: displayMeme});    
}

function draw_comics() {
    $.ajax({url: url_root + "make/", success: displaySubmission});    
    //return false;
};

function advanced_draw() {
    $.ajax({url: url_root + "make/", success: displayAdvanced});    
};

function free_form() {
    $.ajax({url: url_root + "make/", success: displayFreeForm});    
};

function getJokes() {
    $('#newjokes').load(url_root + 'loadjokes/')
}

function saveJoke() {
    try {
	$.ajax({url: url_root + "savejoke/" + 
		    $("#joke_title").val().replace(/ /g, "_") + '/' + 
		    $('#p00').val().replace(/ /g, "_") + '/' + 
		    $('#p01').val().replace(/ /g, "_") + '/' + 
		    $('#p10').val().replace(/ /g, "_") + '/' +
		    $('#p11').val().replace(/ /g, "_") + '/' +
		    $('#p20').val().replace(/ /g, "_") + '/' + 
		    $('#p21').val().replace(/ /g, "_") + '/' + 
		    $("#extraction").val().replace(/ /g, "_") + '/' + 
		    $("#pos").val().replace(/ /g, "_") + '/' +
		    $("#thresh").val().replace('.', "_") + '/', 
		    success: function(html) {alert(html);}});    
    } catch(e) {
	alert(e);
	}
}

function loadJokes() {

}

function extraction() {
    //alert('testing');
    //return false;

    phrases = ($("#p00").val().replace(/ /g, "_") + '+' +
	       $("#p01").val().replace(/ /g, "_") + '+' +
	       $("#p10").val().replace(/ /g, "_") + '+' +
	       $("#p11").val().replace(/ /g, "_") + '+' +
	       $("#p20").val().replace(/ /g, "_") + '+' +
	       $("#p21").val().replace(/ /g, "_"));

    try {
	$.ajax({url: url_root + "extraction/" + 
		    $("#subject").val().replace(/ /g, "_") + '/' + 
		    phrases + '/' + 
		    $("#extraction").val().replace(/ /g, "_") + '/' + 
		    $("#pos").val().replace(/ /g, "_") + '/' +
		    $("#thresh").val().replace('.', "_") + '/', 
		    success: function(html) {alert(html);}});    
    } catch(e) {
	alert(e);
	}
};

function drawAdvancedCleanUp(newhtml) {
    if (global_comic_count < max_comics) {
	global_comic_count += 1;
	$(".progress_bar_chunk").css('width', 80/global_comic_count);
    } else {
	$("#progress_div").html("");
    }
    $("#comics_pane_small").html($("#comics_pane_small").html() + newhtml);
};

function drawFreeFormCleanUp() {
    $("#progress_div").html("");
    //$("#comics_pane_small").html($("#comics_pane_small").html() + newhtml);
};

function drawComicsCleanUp() {
    if (global_comic_count < 7) {
	global_comic_count += 1;
	$(".progress_bar_chunk").css('width', global_comic_count * 10);
    } else {
	global_comic_count += 1;
	$(".progress_bar_chunk").css('width', global_comic_count * 10);
	$("#progress_div").html("");
	//$(".make_pane").html("");
	
    }
};


