var lushopedia = {
	currentIngredient : '',
	keyword : '',
	currentCategory: '',
	categoryView : false,
	keywordInput : "#keyword",
	categories : ".category",
	bookmark : "#bookmark",
	keywordOptions :  "#keywords",
	
	initialise : function ()
	{
		lushopedia.bindLinks();
		//lushopedia.bindKeywordSearch();
		lushopedia.setInterface();
	},
	
	getIntro : function () {},
	
	bindLinks : function () {
		
		$j.each($j(".category"), function (x, y)
		{
			$j(y).bind('click', function ()
			{
				lushopedia.getCategoryView(parseInt($j(y).attr('id')));
				return false;
			});
		});
	},
	bindCategoryLinks : function ()
	{
		$j.each($j('.ingredientLink'), function (x, y )
		{
			$j(y).bind('click', function ()
			{
				lushopedia.getIngredientView(parseInt($j(y).attr('id')));
				return false;
			});
		});
	},

	bindKeywordSearch: function () 
	{
		$j(lushopedia.keyword).bind("keyup", function (e) {
			if( e.keyCode == 13)
			{
				lushopedia.keyword= lushopedia.currentIngredient;
			}
			else if (e.keyCode == 40)
			{
				x = $j(lushopedia.keywordOptions).find("li.active");
				if(! $j(lushopedia.keywordOptions +'li:first-child').hasClass("active"))
				{
					$j(x).removeClass("active");
					$j(x).next().addClass("active");
					lushopedia.currentIngredient = $j(x).next().find("a").text();
				}
			}
			else if (e.keyCode == 38)
			{
				x = $j(lushopedia.keywordOptions).find("li.active");
				if(! $j(lushopedia.keywordOptions +'li:first-child').hasClass("active"))
				{
					$j(x).removeClass("active");
					$j(x).prev().addClass("active");
					lushopedia.currentIngredient = $j(x).prev().find("a").text();
				}
			}
			else if( e.keyCode == 27)
                        {
				$j( lushopedia.keywordOptions).html("");
				$j( lushopedia.keywordOptions).hide();
                        }
			else
			{
				lushopedia.currentIngredient = '';
				$j.ajax( {
					type: "GET",
					url: "/lushopedia/application/ajax.php?option=com_plugins&module=lushopedia&action=getKeywords&keyword=" + $j(lushopedia.keywordInput).val(),
					dataType : "xml"
				});
			}
		});
		$j(lushopedia.keyword).bind("blur", function (e) {
			$j(lushopedia.keywordOptions).hide();
		});
		return;
	},
	
	setInterface : function ()
	{
		lushopedia.closeBookmark();
		// create book stuff
		//start with first category and load page view
		lushopedia.bindKeywordSearch();
		lushopedia.bindBookmark();
		return '';
	},
	
	toggleBookmark : function ()
	{
		if( $j('#bookmark').css('z-index') == '100')
		{
			lushopedia.closeBookmark();
		}
		else
		{
			lushopedia.openBookmark();
		}
	},
	
	bindBookmark : function ()
	{
		$j('#bookmarkToggle').bind('mouseover', function () {
			if(  $j('#bookmark').css('z-index') == '100')
			{
				return;
			}
			else
			{
				$j('#bookmark').css('top','10px;');
			}
		});
	},
	closeBookmark : function ()
	{
		$j('#bookmark').css('z-index', '1');
	},
	openBookmark : function ()
	{
		$j('#bookmark').css('z-index', '100');
	},
	
	getCategoryView: function (cat)
	{
		$j.ajax({
			type : "GET",
			url: "/lushopedia/application/ajax.php?option=com_plugins&module=lushopedia&action=getCategory&lcat="+cat,
			dataType: "xml",
			success: function () {
				 $j('#prevButtonTop').hide();
                                $j('#nextButtonTop').hide();
				//fontReplace();
				setTimeout("lushopedia.bindCategoryLinks()", 200);
				$j(document).pngFix();;
//                              $j('#view').show();
			}
		});
	},
	getIngredientView: function (ingredient)
	{
		$j.ajax({
			type : "GET",
			url: "/lushopedia/application/ajax.php?option=com_plugins&module=lushopedia&action=getIngredientGroup&ingredientId="+ingredient,
			dataType: "xml",
			success: function ()
			{
				$j('#prevButtonTop').hide();
				$j('#nextButtonTop').hide();
				$j(document).pngFix();
// 				$j('#view').show();

			}
		});
		
	},

	tweenNextStep : function (current) {
		$j('#prevButtonTop').show();
		$j('.current-left').css({ "z-index":9});
		$j('.current-right').css({"z-index":8});
		
		$j('.next1-left').css( {"zIndex": "10"});
		$j('.next1-right').css( {"z-index" : "7", "left": "426px", "width": "426px"});
		
		$j(".next1-left").animate({left : "0px", width: '426px'}, "1000", function () { lushopedia.getIngredientView( current); });
		$j('.current-right').animate({ left: '0px'}, "1000");
	},
	
	 tweenNextCat : function (current) {
	 
		$j('#prevButtonTop').show();
		$j('.current-left').css({ "z-index":9});
		$j('.current-right').css({"z-index":8});
		
		$j('.category-left-next').css( {"zIndex": "10"});
		$j('.category-right-next').css( {"z-index" : "7", "left": "426px", "width": "426px"});
		
		$j(".category-left-next").animate({left : "0px", width: '426px'}, "1000", function () { lushopedia.getCategoryView( current); });
		$j('.current-right').animate({ left: '0px'}, "1000");
		

        },
	
	next: function ()
	{
		if(typeof($j(".category-right-next").attr('id'))!='undefined')
		{
			current = $j(".category-right-next").attr('id');
			lushopedia.tweenNextCat(current);
		}
		else
		{
			current = parseInt($j('.next1-left').attr('id'));
			lushopedia.tweenNextStep(current);
		}
		
		//do some tweening motion to next
	},
	
	tweenPrevStep : function (current) {
		$j('#nextButtonTop').show();
//		$j('#view').hide();
		$j('.current-right').css({ "z-index":9});
		$j('.current-left').css({"z-index":8});
		
		$j('.prev1-right').css( {"zIndex": "10"});
		$j('.prev1-left').css( {"z-index" : "7", "left": "0px", "width": "426px"});
		
		$j(".prev1-right").animate({left : "426px", width: '426px'}, "1000", function () { lushopedia.getIngredientView( current);});
		$j('.current-left').animate({ left: '426px'}, "1000");
	},
	
	 tweenPrevCat : function (current) {
                $j('#nextButtonTop').show();
//              $j('#view').hide();
                $j('.current-right').css({ "z-index":9});
                $j('.current-left').css({"z-index":8});

                $j('.category-right-prev').css( {"zIndex": "10"});
                $j('.category-left-prev').css( {"z-index" : "7", "left": "0px", "width": "426px"});

                $j(".category-right-prev").animate({left : "426px", width: '426px'}, "1000", function () { lushopedia.getCategoryView( current);});
                $j('.current-left').animate({ left: '426px'}, "1000");
        },
	
	prev: function ()
	{
		if(typeof($j(".category-right-prev").attr('id'))!='undefined')
		{
			current = $j(".category-right-prev").attr('id');
			lushopedia.tweenPrevCat(current);
		}
		else
		{
			current = parseInt($j('.prev1-left').attr('id'));
			lushopedia.tweenPrevStep(current);
		}
		//do some tweening motion to next
		
	}
	
}
