$(document).ready(function(){
	$('.silverheader a').css('background', 'none');

	var slideOffset = 250;
	var slideWidth = 761;
	$('.my-slider').each(function(i, slider){
		var activeSlide = 3;
                var sliderInterval = null;
                var autoPlay = $(this).hasClass('my-slider-autoplay');
		// support html
		var html = 
			'<div class="my-slider-alpha-left"></div>' +
			'<div class="my-slider-alpha-right"></div>' +
			'<div class="my-slider-arr-left"><img src="http://www.webika.com/clients/AmebaMarketing/wp-includes/images/arr-left.png"/></div>' +
			'<div class="my-slider-arr-right"><img src="http://www.webika.com/clients/AmebaMarketing/wp-includes/images/arr-right.png"/></div>';
		$(html).prependTo(slider);

		$(slider).find('ul:first').addClass('my-slider-wrap');

		// find all slides
		var slides = $(slider).find('ul:first > li');

		// create tabs
		var tabs = '<div class="my-slider-tabs">';
		$(slides).each(function(i, slide){
							if($(slide).find('h3:first').text() == 0)
							{
							}else{
			$(slide).addClass('slide-' + i);
			tabs = tabs + '<a href="#" class="my-slider-tab-' + i + (i==0 ? ' active':'') + '">' + $(slide).find('h3:first').text() + '</a> &nbsp; ';
			$(slide).find('h3:first').hide();}
		});
		tabs = tabs + '</div>';
		$(tabs).prependTo(slider);

		$(slider).find('.my-slider-tabs a').each(function(i, a){
			$(a).click(function(){
				clearInterval(sliderInterval);
				sliderInterval = null;
				activeSlide = i + 2;
				mySlide();
				return false;
			});
			
		});

		// prepend and append new tabs
		var slideFirst = $(slides[0]);
		var slideSecond = $(slides[1]);
		var slideLast = $(slides[slides.length - 1]); 
		var slideBeforeLast = $(slides[slides.length - 2]); 

		var slideFirstClone = $(slideFirst).clone();
		var slideSecondClone = $(slideSecond).clone();
		var slideLastClone = $(slideLast).clone();
		var slideBeforeLastClone = $(slideBeforeLast).clone();


		$(slideSecondClone).insertAfter(slideLast).addClass('cloned');		
		$(slideFirstClone).insertAfter(slideLast).addClass('cloned');
		
		$(slideBeforeLastClone).insertBefore(slideFirst).addClass('cloned');
		$(slideLastClone).insertBefore(slideFirst).addClass('cloned');


		// find all slides
		var slides = $(slider).find('ul:first > li');
		$(slides).each(function(j, slide){
			if (!$(slide).hasClass('cloned')) $(slide).addClass('original');
			$(slide).addClass('my-slider-slide');
			var slideLeft = (j*slideWidth);// + slideOffset);
			$(slide).css('left', slideLeft + 'px');

			$(slide).find('h2:first').addClass('my-slider-title');
			//$(slide).find('img:first').attr('alt', 'Click to start/stop').attr('title', 'Click to start/stop');
			$(slide).find('div:first').addClass('my-slider-content');

			if (autoPlay)
			{
				$(slide).click(function()
				{
					if (sliderInterval != null)
					{
						clearInterval(sliderInterval);
						sliderInterval = null;
					}
					else
					{
						mySlide();
						//sliderInterval = setInterval(mySlide, 4000);
					}
				});
			}	
		});			

		// tune up wrapper
		var sliderWrap = $(slider).find('.my-slider-wrap:first');
		var sliderWrapMargin = 0 - 2 * slideWidth + slideOffset;
		$(sliderWrap).width(slides.length * slideWidth);
		$(sliderWrap).css('margin-left', sliderWrapMargin + 'px');

		var mySlide = function()
		{
			var sliderWrapNewMarginLeft = 0 - slideWidth * activeSlide + slideOffset;
			$(sliderWrap).animate({marginLeft: sliderWrapNewMarginLeft + 'px'}, 500, function(){
				activeSlide++;
				if (activeSlide < 3)
				{
				       	activeSlide = slides.length - 2; 
					var sliderWrapNewMarginLeft = 0 - slideWidth * (slides.length - 3) + slideOffset;
					$(sliderWrap).css('margin-left', sliderWrapNewMarginLeft + 'px');
				}
				else if (activeSlide > slides.length - 2)
				{
					activeSlide = 3;
					var sliderWrapNewMarginLeft = 0 - slideWidth * 2 + slideOffset;
					$(sliderWrap).css('margin-left', sliderWrapNewMarginLeft + 'px');
				}

				$('.my-slider-tabs a').removeClass('active');
				$('.my-slider-tabs a.my-slider-tab-' + (activeSlide - 3)).addClass('active');
				$('.my-slider-tabs1 a' + (activeSlide - 3)).addClass('active');

			});
		}


		var correctPosition = function()
		{
			$(".entry-content link").each(function(){
				$(this).appendTo('head');
			});
			$(".my-slider-alpha-left").appendTo("#masthead");
			$(".my-slider-alpha-right").appendTo("#masthead");
			$(".my-slider-arr-left").appendTo("#masthead");
			$(".my-slider-arr-right").appendTo("#masthead");
			$(".my-slider-tabs").appendTo("#masthead");

		}

		if (autoPlay)
		{
			sliderInterval = setInterval(mySlide, 4000);
		}

		$(slider).find('.my-slider-arr-left:first').click(function(){
	         	clearInterval(sliderInterval);
			sliderInterval = null;
			activeSlide = activeSlide - 2;
			if (activeSlide < 0)
			{
				activeSlide = slides.length - 2; 
			}
			mySlide();
		});

		$(slider).find('.my-slider-arr-right:first').click(function(){
	         	clearInterval(sliderInterval);
			sliderInterval = null;
			mySlide();
		});

		correctPosition();

	});
});
