
var Slider = (function($)
{
	var img;
	var imgTop;
	var tOver;
	var tOverTop;
	var index = 0;
	var skipNext = false; // skip next transition - invoked if user manually changes a slide
	var transitionSpeed = 650;
	
	var slides =
	[
		{
			frame : skinPath + "img/thumb/design.png",
			image : skinPath + "img/design.png",
			text : "<h3>Web Design</h3>We develop websites for organisations of all sizes, from SME's to Multi-national corporations, turning your ideas into engaging, successful websites...",
			link : "http://www.webwisebusiness.co.uk/Websitedesign/tabid/56/Default.aspx"
		},
		{
			frame : skinPath + "img/thumb/intranet.png",
			image : skinPath + "img/intranet.jpg",
			text : "<h3>Intranet Development</h3>Web based intranet systems that help your business communicate, collaborate and work more efficiently and effectively? We provide all of that and more...",
			link : "http://www.webwisebusiness.co.uk/Websitedesign/IntranetandExtranet/Intranetdevelopment/tabid/114/Default.aspx"
		},
		{
			frame : skinPath + "img/thumb/ecommerce.png",
			image : skinPath + "img/ecommerce.jpg",
			text : "<h3>E-commerce Web Design</h3>Affordable, attractive ecommerce website design. Looking to sell successfully online? Then we've got the solutions to suit your needs...",
			link : "http://www.webwisebusiness.co.uk/Websitedesign/Ecommercewebsites/tabid/58/Default.aspx"
		},
		{
			frame : skinPath + "img/thumb/dnn.png",
			image : skinPath + "img/dnn.jpg",
			text : "<h3>DNN Developers</h3>From DotNetNuke consultancy to module development, website skinning, DNN intranets and DNN support, if you've got DNN issues, we can help...",
			link : "http://www.webwisebusiness.co.uk/DotNetNuke/DNNwebsitesandsystems/tabid/126/Default.aspx"
		},		
		{
			frame : skinPath + "img/thumb/marketing.png",
			image : skinPath + "img/marketing.png",
			text : "<h3>Online Marketing Scotland</h3>Want to get noticed online? Online Marketing could be the answer your looking for. From Pay Per Click to Search Engine Optimisation, we can help...",
			link : "http://www.webwisebusiness.co.uk/Buildingblocks/OnlineMarketing/tabid/134/Default.aspx"
		},
		{
			frame : skinPath + "img/thumb/air.png",
			image : skinPath + "img/air.jpg",
			text : "<h3>Adobe AIR Developers</h3>Looking for developers with Adobe AIR skills?  Then you've come to the right place...",
			link : "http://www.webwisebusiness.co.uk/AdobeAIR/tabid/149/Default.aspx"
		}
	];
	
	// loads next slide
	var next = function()
	{
		if (!skipNext)
		{
			img.css("background-image", "url(" + slides[index].image + ")");
			tOver.html(slides[index].text);
			
			imgTop.stop(true, false).animate(
			{
				"margin-left": "-" + img.outerWidth()
			}, transitionSpeed, function() 
			{
				imgTop.css(
				{
					"background-image": img.css("background-image"),
					"margin-left" : "0"
				});
				tOverTop.html(tOver.html());
				syncFrames();
			});	
			
			
			incIndex();	
		}
		else
		{
			skipNext = false;
		}
	};
	
	// loads previous slide
	var back = function()
	{
		
	};
	
	//jump to specified slide
	var jumpTo = function(i)
	{
		if (i < slides.length && i > -1)
		{
			skipNext = false;
			index = i;
			next();
			skipNext = true;
		};
	};
	
	// re-orders frames to match current index
	var syncFrames = function()
	{
		$("#services-controls .frame").unbind('click').remove();
		
		var framesT = slides.slice(0, index);
		var framesB = slides.slice(index);
		
		// add 'frames'
		$.each(framesB, function(i, slide)
		{
			$("#services-controls .slider").append("<div class=\"frame\" idxVal=\"" + (index + i) + "\" style=\"background: url('" + framesB[i].frame + "') 50% 50% no-repeat\"></div>");
		});
		$.each(framesT, function(i, slide)
		{
			$("#services-controls .slider").append("<div class=\"frame\" idxVal=\"" + i + "\" style=\"background: url('" + framesT[i].frame + "') 50% 50% no-repeat\"></div>");
		});
		
		// bind click events to frames
		$("#services-controls .frame").click(function()
		{
			jumpTo(parseInt($(this).attr("idxVal")));	
		});	
		
	};
	
	var incIndex = function()
	{
		index++;		
		if (index == slides.length) { index = 0 }		
	};
	
	var decIndex = function()
	{
		index--;
		if (index == 0) { index = slides.length }			
	};
	
	var prevIndex = function()
	{
		if (index == 0) { return slides.length-1 }
		return index-1;
	};
	
	var init = function()
	{
		img = $("#services-viewer .image-area");
		tOver = $("#services-viewer .image-area .text-overlay:first");
		imgTop = $("#services-viewer .image-area-top");
		tOverTop = $("#services-viewer .image-area-top .text-overlay");
		
		// set initial slide
		imgTop.css("background-image", "url(" + slides[index].image + ")");
		tOverTop.html(slides[index].text);		
		syncFrames();
		incIndex();
		
		// add event handlers to slide area	
		$("#services-viewer").click(function()
		{
			window.location.href = slides[prevIndex()].link;
		});		
		
		setInterval(function() { next() }, 9000);
	};
	
	return {
		init : init,
		next : next,
		back : back,
		jumpTo : jumpTo
	};
})(jQuery);



jQuery(document).ready(function()
{
	Slider.init();	
});
