(function( $ ){ //Start jQuery Wrapper

//Start Method Array
var methods = {
	init	: 	function(){
					if($.cookie("LC_FAVORITE") != null && $.cookie("LC_FAVORITE") != "" && $.cookie("LC_FAVORITE") != "\"\""){
						if($.cookie("LC_FAVORITE").indexOf("|") == -1) {
							$("#count_fav").html("(1)");
							if($(".btnAddTofavorites").length > 0 && $.cookie("LC_FAVORITE")==$(".btnAddTofavorites").attr("rel")) {
								$(".favoritesImage").attr("src","http://www.lenscrafters.ca/wcsstore/CustomB2CStorefrontAssetStoreLC/images/catalog/btn_favorites_Dsabl.gif");
								$(".btnAddTofavorites").html('This is a favorite. <a href="' + favoritesUrl + '">View favorites</a>');
								$(".btnAddTofavorites").addClass('thisIsAFavorite');
								$(".btnAddTofavorites").unbind();
								$(".btnAddTofavorites").removeClass("btnAddTofavorites");
							} else $(".favoritesImage").attr("src","http://french.lenscrafters.ca/img/lenscrafter/FR_57503.jpg");
						}
						else {
							var favoriteArray = $.cookie("LC_FAVORITE").split("|");
							$("#count_fav").html("("+favoriteArray.length+")");
							$.each(favoriteArray, function(index,value){
								if($(".btnAddTofavorites").length > 0 && value==$(".btnAddTofavorites").attr("rel")) {
									$(".favoritesImage").attr("src","http://www.lenscrafters.ca/wcsstore/CustomB2CStorefrontAssetStoreLC/images/catalog/btn_favorites_Dsabl.gif");
									$(".btnAddTofavorites").html('This is a favorite. <a href="' + favoritesUrl + '">View favorites</a>');
									$(".btnAddTofavorites").addClass('thisIsAFavorite');
									$(".btnAddTofavorites").unbind();
									$(".btnAddTofavorites").removeClass("btnAddTofavorites");
									return false;
								} else $(".favoritesImage").attr("src","http://french.lenscrafters.ca/img/lenscrafter/FR_57503.jpg");
							});
						}
					} else $(".favoritesImage").attr("src","http://french.lenscrafters.ca/img/lenscrafter/FR_57503.jpg");
						
					if($(".btnAddTofavorites").length > 0) {
						$(".btnAddTofavorites").click(function(){
							methods.doAjax();
						});
					}
				},
	doAjax	:	function(){ /* this link is hard-coded to US site - doesn't seem to cause any issues though. */
					$.get("http://french.lenscrafters.ca/webapp/wcs/stores/servlet/LCAddRemoveFavoriateCmd?langId=-1&storeId=11151&catalogId=10051&action=addItemId&ItemId="+$(".btnAddTofavorites").attr("rel"),function(data){
						if($.cookie("LC_FAVORITE").indexOf("|") == -1) $("#count_fav").html("(1)");
						else {
							var favoriteArray = $.cookie("LC_FAVORITE").split("|");
							$("#count_fav").html("("+favoriteArray.length+")");
						}
						$(".favoritesImage").attr("src","http://www.lenscrafters.ca/wcsstore/CustomB2CStorefrontAssetStoreLC/images/catalog/btn_favorites_Dsabl.gif");
						$(".btnAddTofavorites").html('This is a favorite. <a href="' + favoritesUrl + '">View favorites</a>');
						$(".btnAddTofavorites").addClass('thisIsAFavorite');
						$(".btnAddTofavorites").unbind();
						$(".btnAddTofavorites").removeClass("btnAddTofavorites");
					})
   					$("#modalAddToFavorite").center().after("<div id='modalBackground' style='opacity: 0.55;'>&nbsp;</div>").show();
	  				$("#closeFav").click(function(){$("#modalBackground").remove(); $("#modalAddToFavorite").hide();});
	  				//add to favorites - omniture
	  				s.events='event34';
	  				s.prop36=$(".btnAddTofavorites").attr("rel");
	  				void(s.t());
	  				s.events='';
	  				s.prop36='';
				}// End Methods
};// End Methed Array

// Start Method Fetch
$.myFavorites = function( method ) {
    if ( methods[method] ) {
      return methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 ));
    } else if ( typeof method === 'object' || ! method ) {
      return methods.init.apply( this, arguments );
    } else {
      $.error( 'Method ' +  method + ' does not exist in catalogNavigation' );
    }    
};//End Method Fetch


})(jQuery);//End jQuery Wrapper
