/* Browse Functions */

var datapage = "view-list.php";
var currentsort = "id";

function sortby_newest() {
    new Ajax.Updater("flyer_browser","http://www.fly-post.com/modules/view-flyers/ajax/" + datapage + "?sort=id&my_lat=41.6889900&my_lon=-85.9676480", { evalScripts: true });
    currentsort = "id";
    scroll_to_top();
}

function sortby_best() {
    new Ajax.Updater("flyer_browser","http://www.fly-post.com/modules/view-flyers/ajax/" + datapage + "?sort=rating&my_lat=41.6889900&my_lon=-85.9676480", { evalScripts: true });
    currentsort = "rating";
    scroll_to_top();
}

function sortby_popular() {
    new Ajax.Updater("flyer_browser","http://www.fly-post.com/modules/view-flyers/ajax/" + datapage + "?sort=rsvps&my_lat=41.6889900&my_lon=-85.9676480", { evalScripts: true });
    currentsort = "rsvps";
    scroll_to_top();
}

function sortby_now() {
    new Ajax.Updater("flyer_browser","http://www.fly-post.com/modules/view-flyers/ajax/" + datapage + "?sort=start_date&my_lat=41.6889900&my_lon=-85.9676480", { evalScripts: true });
    currentsort = "start_date";
    scroll_to_top();
}

function changePage(page) {
    new Ajax.Updater("flyer_browser","http://www.fly-post.com/modules/view-flyers/ajax/" + datapage + "?sort=" + currentsort + "&page=" + page + "&my_lat=41.6889900&my_lon=-85.9676480", { evalScripts: true });
    scroll_to_top();
}

function scroll_to_top() {
	new Effect.Tween('scrollspot', window.scrollY, 0, { duration: 1 }, function(p) { window.scrollTo(0,p); } );
}

function switch_normal() {
    $('small_thumb').src = "http://www.fly-post.com/images/small_thumb_off.png";
    $('med_thumb').src = "http://www.fly-post.com/images/med_thumb_on.png";
    datapage = "view-thumbs.php";
    if (currentsort == "id")
    	sortby_newest();
    if (currentsort == "rating")
    	sortby_best();
    if (currentsort == "rsvps")
    	sortby_popular();
    if (currentsort == "start_date")
    	sortby_now();
}

function switch_list() {
    $('small_thumb').src = "http://www.fly-post.com/images/small_thumb_on.png";
    $('med_thumb').src = "http://www.fly-post.com/images/med_thumb_off.png";
    datapage = "view-list.php";
    if (currentsort == "id")
    	sortby_newest();
    if (currentsort == "rating")
    	sortby_best();
    if (currentsort == "rsvps")
    	sortby_popular();
    if (currentsort == "start_date")
    	sortby_now();
}

/* Details Functions */

var busy = false;

function revealPost() {
    if (busy == true)
    	return;
    busy = true;
    if ($('postcomment').style.display == "none")
    	new Effect.BlindDown("postcomment",{ duration: 0.5, afterFinish: function() { busy = false; }});
    else
    	new Effect.BlindUp("postcomment",{ duration: 0.5, afterFinish: function() { busy = false; } });
}

function postComment(flyer) {
    if (busy == true)
    	return;
    new Ajax.Request("http://www.fly-post.com/modules/view-flyers/ajax/post-comment.php?comment=" + escape($('postcomment_comment').value) + "&flyer=" + flyer);
}

function appendComment(user,photo,date,comment) {
    insert = "<li><h4>";
    if (photo != "")
    	insert += '<img src="http://www.fly-post.com/vault/users/' + photo + '" alt="Avatar" align="top" />';
    else
    	insert += '<img src="http://www.fly-post.com/images/no_user_icon.png" alt="" align="top" />';
    insert += '</h4><b>' + user + '</b> <span>' + date + '</span><br />' + comment + '</li><br clear="all" />';
    
    $('comment_area').innerHTML = insert + $('comment_area').innerHTML;
    $('postcomment_comment').value = "";
}

function flyerAttend() {
    new Ajax.Request("http://www.fly-post.com/modules/view-flyers/ajax/attend.php?flyer=" + myflyer);
}

function flyerSave() {
    new Ajax.Request("http://www.fly-post.com/modules/view-flyers/ajax/save.php?flyer=" + myflyer);
}

function detailsShowHide() {
    if (busy == true)
    	return;
    busy = true;
    if ($('more_details').style.display == "none") {
    	new Effect.BlindDown('more_details', { duration: 0.5, afterFinish: function() { busy = false; }});
    	$('detailsimage').src = "http://www.fly-post.com/images/minusicon.png";
    	$('detailsimage').alt = "(-)";
    } else {
    	new Effect.BlindUp('more_details', { duration: 0.5, afterFinish: function() { busy = false; }});
    	$('detailsimage').src = "http://www.fly-post.com/images/plusicon.png";
    	$('detailsimage').alt = "(+)";
    }
}