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

function addFavorite(el) {
	var favID = el.id.split('_').pop();

	jQuery(el).removeClass('favstar');
	jQuery(el).addClass('favstar_added');
	jQuery(el).attr({ title: 'Remove from Favorites', id: 'added_favorite_' + favID});
	jQuery(el).unbind('click');
	jQuery(el).click(function() { removeFavorite(el); });
	
	var url = '/account/favorites.php?font_id=' + favID + '&act=add&a=1';
	jQuery.get(url, function(data) {
		if (strpos(window.location, '/account/favorites/', 0)) {
			window.location = '/account/favorites';
			return false;
		}
		
		if (typeof data != "undefined") {
			jQuery('#favorites_nav').html(data);
		}
	});

	return false;
}

function removeFavorite(el) {
	var favID = el.id.split('_').pop();
	
	if (!strpos(window.location, '/account/favorites/')) {
		jQuery(el).removeClass('favstar_added');
		jQuery(el).addClass('favstar');
	}

	jQuery(el).attr({ title: 'Add to Favorites', id: 'favorite_' + favID });
	jQuery(el).unbind('click');

	jQuery(el).click(function() { addFavorite(el); });
	
	var url = '/account/favorites.php?font_id=' + favID + '&act=remove&a=1';

	jQuery.get(url, function(data) {
		if (strpos(window.location, '/account/favorites/', 0)) {
			jQuery("li[prodid='" + favID + "']").remove();
		}

		jQuery('#favorites_nav').html(data);
	});

	return false;
}

function initFavs() {
	setupTagLinks();
	setupFilterLinks();
	initProductHovers();
	
	if (jQuery('#notify')) {
		jQuery('#notify_close').click(function() {
			doFade('notify', 100, 0, 500);
			setTimeout("jQuery('notify').hide()", 500);
			var url = '/account/favorites.php?notified=1';
			jQuery.get(url);
		});
	}

	var elems = jQuery("span[id^='favorite_']");
	var addedElems = jQuery("span[id^='added_favorite_']");
	
	jQuery(elems).each(function() {
		jQuery(this).attr('title', '');

		jQuery(this).click(function() {
			addFavorite(this);
		})
	})

	jQuery(addedElems).each(function() {
		jQuery(this).attr('title', 'Remove from Favorites');
		jQuery(this).click(function() {
			removeFavorite(this);
		})
	})
}

function initProductHovers() {
	jQuery('.product').hover(function() {
		jQuery(this).find("a[id^='addtag_']").addClass('addtag_hovered');
	}, function() { jQuery(this).find("a[id^='addtag_']").removeClass('addtag_hovered') });
	
}

function setupFilterLinks() {
	var c = jQuery("#tagfilters input:checkbox");
	
	for(i=0; i<c.length; i++) {
		jQuery(c[i]).unbind('click');
	}
	
	jQuery(c).click(function() {
		console.log(this.id);
		if (jQuery(this).is(':checked')) {
			console.log('is checked');
			jQuery(this).parent().addClass('selected');
		} else {
			console.log('isnt checked');
			jQuery(this).parent().removeClass('selected');
		}
	})
	
	// for crappy IE
    jQuery("#tagfilters label").click(function(evt) {
		if (jQuery(this).find("input:checkbox").is(':checked')) {
			jQuery(this).find("input:checkbox").attr('checked', false);
			jQuery(this).removeClass('selected');
		} else {
			jQuery(this).find("input:checkbox").attr('checked', true);
			jQuery(this).addClass('selected');
		}
    });
}

function setupTagLinks() {
	// setup the "add a tag" link handler
	var elems = jQuery("a[id^='addtag_']");
	
	jQuery(elems).click(function() {
		var id = this.id.split('_').pop();
		
		jQuery('#tagform_wrapper_' + id).show();
		
		//if (jQuery('#tagform_wrapper_' + id).is(":visible")) {
			
			
		//} else {
			//jQuery('#taglist_' + id).hide();
			//jQuery('#tagform_wrapper_' + id).slideDown();
	
			// reset the text input
			jQuery('#tagform_' + id + ' [name="tag"]').val('');
	
			jQuery('#tagform_' + id + ' [name="tag"]').focus();
			jQuery("#cancel_tag_" + id).click(function() {
				//jQuery('#taglist_' + id).show();
				jQuery('#tagform_wrapper_' + id).hide();
	
				return false;
			});
		
		//}
		return false;
	});
	
	// setup form submit handler
	var forms = jQuery("form[id^='tagform_']");
	
	for(i=0; i<elems.length; i++) {
		jQuery(forms[i]).unbind('submit');
	}
	for(i=0; i<elems.length; i++) {
		jQuery(forms[i]).submit(function() {
			submitTagForm(this);
			return false;
		})
	}
	
	setupProductTags();
}

function setupProductTags() {	
	
	var els = jQuery("li[id^='tag']");
	
	for(i=0; i<els.length; i++) {
		jQuery(els[i]).find('span.delete').unbind('click');
	}
	
	// now check if there are tags, setup their click actions
	var els = jQuery("li[id^='tag']");
	for(i=0; i<els.length; i++) {
		jQuery(els[i]).find('span.delete').click(function() {
			processTagDelete(jQuery(this).parent());
		});
	}
}

function submitTagForm(form) {
	var url = '/account/favorites/';
	var parameters = jQuery(form).serialize();
	var id = form.favid.value;
	
	jQuery.post(url, parameters, function(data) {
		if (data.success == 1 && data.new_tag == 1) {
			
			jQuery("#taglist_" + id).html(stripslashes(data.taglisthtml));
			
			setupTagLinks();
			
			jQuery("#tagfilter_update_container").html(stripslashes(data.alltagshtml))
			setupFilterLinks();
		}
	}, 'json');
	
	jQuery('#taglist_' + id).show();
	jQuery('#tagform_wrapper_' + id).hide();

	return true;
}

function processTagDelete(el) {
	var url = '/account/favorites/';
	var	tagid = jQuery(el).attr('tag_id');
	var favid = jQuery(el).attr('favid');
	jQuery.post(url, {	act: "delete_tag",
						tagid: tagid,
						favid: favid	},
					function(data) {
						jQuery("li[tag_id='" + tagid + "']").remove();
						console.log("just removed li[tag_id='" + tagid + "']");
						
						jQuery("#tagfilter_update_container").html(stripslashes(data.alltagshtml))
						setupFilterLinks();
						
					}, 'json');
}

function observeTagDelete(obj) {
	jQuery(obj).bind('click', function() { processTagDelete(this); });
}

jQuery(document).ready(initFavs);
