/*************************************
* Favorites AJAX and UI stuffs
* Jacob Swartz @ FontShop, 2009
*************************************/

function doFade(id,opacStart,opacEnd,ms) {
    // each fade speed
    var speed=Math.round(ms/100);
    var timer=0;

    // direction of fade
    if (opacStart>opacEnd) {
        for(i=opacStart; i>=opacEnd; i--) {
            setTimeout("fade("+i+",'"+id+"')",(timer*speed));
            timer++;
        }
    } else if (opacStart<opacEnd) {
        for(i=opacStart;i<=opacEnd;i++) {
            setTimeout("fade("+i+",'"+id+"')",(timer*speed));
            timer++;
        }
    }
}

function fade(opacity,id) {
    var object=document.getElementById(id).style;
    object.opacity=(opacity/100);
    object.MozOpacity=(opacity/100);
    object.KhtmlOpacity=(opacity/100);
    object.filter="alpha(opacity="+opacity+")";
}

function addFavorite(fontID) {
	fontID=fontID.split('_');
	fontID=fontID.pop();

	/* Perform the fancy UI changes before taxing the DOM */
	$('favmessage_'+fontID).style.display='none';
	$('favmessage_update_'+fontID).show();
	$('favmessage_update_'+fontID).update('Added to Favorites');
	doFade("favmessage_update_"+fontID, 100, 0, 1500);
	$('favorite_'+fontID).removeClassName('favstar');
	$('favorite_'+fontID).addClassName('favstar_added');
	$('favorite_'+fontID).id='added_favorite_'+fontID;
	/* \o/ */

	// Listen for the new click to remove it, again
	Event.observe('added_favorite_'+fontID, 'click',
		function(ev) {
			ev.element().stopObserving();
			removeFavorite('added_favorite_'+fontID,'fromAdd');
		}
	);

	Event.observe('added_favorite_'+fontID, 'mouseover', function() { showMessage('added_favorite_'+fontID,'Remove from Faves'); });

	if ($('favorite_'+fontID)) {
		Event.observe('favorite_'+fontID, 'mouseout', function() { $('favmessage_'+fontID).style.display='none'; });
	} else if ($('added_favorite_'+fontID)) {
		Event.observe('added_favorite_'+fontID, 'mouseout', function() { $('favmessage_'+fontID).style.display='none'; });
	}

	// Perform AJAX here
	var url="/account/favorites.php?font_id="+fontID + "&act=add&a=1";
	new Ajax.Request(url, {
		method:"get",
		onSuccess: function(transport) {
			if (strpos(window.location,'/account/favorites/',0)) {
				window.location = '/account/favorites/';
				return false;
			}

			$('favorites_nav').update(transport.responseText);
		},
		onFailure: function() { }
	});
}

function removeFavorite(fontID) {
	fontID=fontID.split('_');
	fontID=fontID.pop();

	/* Perform the fancy UI changes before taxing the DOM */
	$('favmessage_'+fontID).style.display='none';
	$('favmessage_update_'+fontID).show();
	$('favmessage_update_'+fontID).update('Removed');
	doFade("favmessage_update_"+fontID, 100, 0, 1500);
	if (!strpos(window.location, '/account/favorites/')) {
		$('added_favorite_'+fontID).removeClassName('favstar_added');
		$('added_favorite_'+fontID).addClassName('favstar');
	}

	$('added_favorite_'+fontID).id='favorite_'+fontID;
	/* \o/ */

	// Listen for the new click to add it, again
	Event.observe('favorite_'+fontID, 'click',
		function(ev) {
			ev.element().stopObserving();
			addFavorite(this.id);
		}
	);

	Event.observe($('favorite_'+fontID), 'mouseover', function() { showMessage('favorite_'+fontID,'Add to Favorites'); });

	if ($('favorite_'+fontID)) {
		Event.observe('favorite_'+fontID, 'mouseout', function() { $('favmessage_'+fontID).style.display='none'; });

	} else if ($('added_favorite_'+fontID)) {
		Event.observe('added_favorite_'+fontID, 'mouseout', function() { $('favmessage_'+fontID).style.display='none'; });
	}

	// Perform AJAX here
	var url="/account/favorites.php?font_id="+fontID + "&act=remove&a=1";
	new Ajax.Request(url, {
		method:"get",
		onSuccess: function(transport) {
			if (strpos(window.location,'/account/favorites/',0)) {
				$('favmessage_wrapper_'+fontID).up(10).hide();

				if (transport.responseText=='<a href="/account/favorites/">Favorites</a>') {
					window.location = '/account/favorites/';
					return false;
				}
			}

			$('favorites_nav').update(transport.responseText);
		},
		onFailure: function() { }
	});
}

function showMessage(fontID,message) {
	fontID=fontID.split('_');
	fontID=fontID.pop();

	if ($('favmessage_update_'+fontID)) {
		$('favmessage_update_'+fontID).hide();
	}

	// Check if the element is visible
	if ($('favmessage_'+fontID)) {
		$('favmessage_'+fontID).style.display='block';
		$('favmessage_'+fontID).update(message);
	}

	if ($('favorite_'+fontID)) {
		Event.observe('favorite_'+fontID, 'mouseout', function() { $('favmessage_'+fontID).style.display='none'; });

	} else if ($('added_favorite_'+fontID)) {
		Event.observe('added_favorite_'+fontID, 'mouseout', function() { $('favmessage_'+fontID).style.display='none'; });
	}

}

function doInit() {
	if ($('notify')) {
		Event.observe('notify_close', 'click', function() {
			doFade('notify',100,0,500);
			setTimeout("$('notify').hide()", 500);
			var url="/account/favorites.php?notified=1";
			new Ajax.Request(url, { method:"get" });
		});
	}

	elems=$$('span[id^="favorite_"]');
	addedElems=$$('span[id^="added_favorite_"]');

	for(i=0; i<addedElems.length; i++) {
		Event.observe(addedElems[i], 'click', function(ev) {
			ev.element().stopObserving();
			removeFavorite(this.id);
		});

		Event.observe(addedElems[i], 'mouseover', function() { showMessage(this.id,'Remove from Faves'); });
	}

	for(i=0; i<elems.length; i++) {
		Event.observe(elems[i], 'click', function(ev) {
			ev.element().stopObserving();
			addFavorite(this.id);
		});
	}

	for(i=0; i<elems.length; i++) {
		Event.observe(elems[i], 'mouseover', function() { showMessage(this.id,'Add to Favorites'); });
	}
}


Event.observe(window, 'load', doInit);