$(document).ready(InitPage);
	
	function InitPage()
	{
		var news, tips, newControl, tipsControl;
		InitHeader();
		ClientsSlideshow();
		news = $("#footer-widget-area .widget_categoryposts h3:contains('News')").next("ul");
		tips = $("#footer-widget-area .widget_categoryposts h3:contains('Tips')").next("ul");
		newControl = new NewsSlider(news);
		tipsControl = new NewsSlider(tips);
		TogglePostSidebar();
	}
	function InitHeader()
	{
		var lang = $("html:first").attr("lang");
		if(lang != "he")
		{	
			$("body:first").addClass("LTR");
			$("form").attr("action", "/lang/en/");	
			$("a[rel='home']").each(function(){
				$(this).attr("href", $(this).attr("href") + "/lang/en/");
			});
		}
	}
	function ClientsSlideshow()
	{
		var _container, items, currentIndex, prevIndex, parentWidth;
		_container = $("#footer-widget-area .widget_categoryposts h3:contains('Customers')").next("ul");
		if(_container.length > 0)
		{
			parentWidth = _container.parent().width() + "px";
			_container.css( { position : 'relative', width : parentWidth});
			currentIndex = 1;
			items =  _container.find("li");
			items.css({ position : "absolute", width : parentWidth, 'text-align' : 'center' });
			$(items[0]).fadeIn(200).find("a:last").fadeIn(200);
			setInterval(function(){
				prevIndex = currentIndex - 1;
				if(currentIndex == 0)
				{
					prevIndex = items.length - 1;					
				}
				
				$(items[prevIndex]).fadeOut(200).find("a:last").fadeOut(200);				
				$(items[currentIndex]).fadeIn(500).find("a:last").fadeIn(500);
				
				currentIndex = currentIndex+1;
				if(currentIndex >= items.length)
				{
					currentIndex = 0;
				}
			}, 3000);
		}
	}
	
	function NewsSlider(container)
	{		
		var _this, items, parentWidth, currentIndex, containerWidth, itemWidth;
		_this = this;
		currentIndex = 0;	
		
		
		this.createPager = function(){
			var parent, nextButton, prevButton, nextButtonText, prevButtonText;
			if($("html:first").attr("lang") === "he")
			{
				prevButtonText = "&lt; הקודם";
				nextButtonText = "הבא &gt;";
			}
			else
			{
				prevButtonText = "&lt; Prev";
				nextButtonText = "Next &gt;";
			}
			parent = container.parent();
			parent.append("<div> <span class='nextPost'>"+nextButtonText+"</span>  <span class='prevPost'>"+prevButtonText+"</span><div>");
			nextButton = parent.find(".nextPost");
			prevButton = parent.find(".prevPost");
			
			nextButton.click(function(){
				_this.showNext(nextButton, prevButton);
			});
			
			prevButton.click(function(){
				_this.showPrev(nextButton, prevButton);
			});
			
			$(items[currentIndex]).show(500);
			
			_this.toggleNav(nextButton, prevButton);
		};
		
		this.showNext = function(nextButton, prevButton){
			currentIndex = currentIndex+1;
			
			if(currentIndex >= items.length)
			{
				currentIndex = items.length - 1;				
			}
			
			var prevIndex = currentIndex - 1;
			if(prevIndex < 0)
			{
				prevIndex = items.length - 1;					
			}
			
			$(items[prevIndex]).hide(200);				
			$(items[currentIndex]).show(500);
						
			_this.toggleNav(nextButton, prevButton)
		};
		
		this.showPrev = function(nextButton, prevButton){
			currentIndex = currentIndex - 1;
			if(currentIndex < 0)
			{
				currentIndex = 0;				
			}
			
			var nextIndex = currentIndex + 1;
			if(nextIndex >= items.length)
			{
				nextIndex = 0;				
			}
			
			$(items[nextIndex]).hide(200);				
			$(items[currentIndex]).show(500);			
			
			_this.toggleNav(nextButton, prevButton);
		};
		
		this.toggleNav = function(nextButton, prevButton)
		{
			if(items.length < 2)
			{
				nextButton.hide();
				prevButton.hide();
			}
			else if(currentIndex <= 0)
			{
				nextButton.show();
				prevButton.hide();
			}
			else if(currentIndex >= items.length -1)
			{
				nextButton.hide();
				prevButton.show();
			}
			else
			{
				nextButton.show();
				prevButton.show();
			}
		};
		
		//constructor
		(function(){
			parentWidth = container.parent().width() - 10;
			containerWidth = parentWidth.toString() + "px";
			itemWidth = parentWidth.toString() + "px";
			container.css( { position : 'relative', width : containerWidth, height: '200px', border : '1px solid silver', padding: '5px'});
			items =  container.find("li");
			items.css({ position : 'absolute', width : itemWidth });
			items.find("a").css("display", "inline-block");
			_this.createPager();			
		}());
	}
	
	function TogglePostSidebar()
	{
		
		 var postTitle = $("h1[data-category]");
		 if(postTitle.length > 0)
		 {
			if(postTitle.attr("data-category") === "Customers")
			{
				$("#menu-news-events").hide();				
			}
			else
			{
				$("#menu-customers").hide();
			}
		 }
		
		
	}
