(function($) { 
	
	$.fn.wtdbanner = function(options) {
		
		// Added for iOS Support
		var Counter = 0;
		var opts = $.extend({}, $.fn.wtdbanner.defaults, options);
		
		if(this.length){
			return this.each(function(){
				setBanner($(this), opts);				  
			});
		}
		
		function setBanner($obj, opts){
			if(opts.xmldriven == true){
				$obj.html('<div class=\'banner-content\'><ul class=\'banner-carousel\'></ul></div><div class=\'banner-nav\'><ul></ul></div>');

				$.ajax({
					type: 'GET',
					url: opts.xmlpath,
					dataType: 'xml',
					success: function(data) {
						xmlParser(data, $obj, true);
					}
				});
			}
			else{
				var data = '';
				xmlParser(data, $obj, false)
			}

			function xmlParser(xml, $obj, noXML){

				var carousel_1 = '';
				var carousel_2 = '';
				var carousel_3 = '';
				var carousel_4 = '';
						
				var nav_1 = '';
				var nav_2 = '';
				var nav_3 = '';
				var nav_4 = '';	
			
				if(noXML == true){
					$(xml).find('banner-item').each(function(index, element){
						var title = $(element).find('title').text();
						var subTitle = $(element).find('subtitle').text();
						var image = $(element).find('image-path').text();
						var linkText = $(element).find('link-text').text();
						var linkURL = $(element).find('link-url').text();
						var navTitle = $(element).find('nav-title').text();

						var priority = $(element).find('spotlite-priority').text();

						title= title.replace(/'/g,"&#39;");
						title= title.replace(/£/g,"&#163;");
						navTitle= navTitle.replace(/£/g,"&#163;");

						if(priority == '1'){
							carousel_1 += '<li><a href=\'' + linkURL + '\'><img src=\'' + image + '\' alt=\'' + title + '\' title=\'' + title + '\' /></a></li>';
							nav_1 += '<li><a href=\'' + linkURL + '\' title=\'' + navTitle + '\'>' + navTitle + '</a></li>';
						}else if(priority == '2'){
							carousel_2 += '<li><a href=\'' + linkURL + '\'><img src=\'' + image + '\' alt=\'' + title + '\' title=\'' + title + '\' /></a></li>';
							nav_2 += '<li><a href=\'' + linkURL + '\' title=\'' + navTitle + '\'>' + navTitle + '</a></li>';
						}else if(priority == '3'){
							carousel_3 += '<li><a href=\'' + linkURL + '\'><img src=\'' + image + '\' alt=\'' + title + '\' title=\'' + title + '\' /></a></li>';
							nav_3 += '<li><a href=\'' + linkURL + '\' title=\'' + navTitle + '\'>' + navTitle + '</a></li>';
						}else if(priority == '4'){
							carousel_4 += '<li><a href=\'' + linkURL + '\'><img src=\'' + image + '\' alt=\'' + title + '\' title=\'' + title + '\' /></a></li>';
							nav_4 += '<li><a href=\'' + linkURL + '\' title=\'' + navTitle + '\'>' + navTitle + '</a></li>';
						}

					});
				}

				$obj.find('div.banner-content ul.banner-carousel').append(
					carousel_1 + carousel_2 + carousel_3 + carousel_4
				);
				$obj.find('div.banner-nav ul').append(
					nav_1 + nav_2 + nav_3 + nav_4
				);


				$obj.find('div.banner-nav ul li:last-child').addClass('last');
				if($('div.spotlight').length){
					console.log('hi');
					$obj.find('div.banner-nav ul li').addClass('spotlight-background');
					$obj.find('div.banner-nav ul li:first-child').addClass('spotlight-active');
				}
				else{
					$obj.find('div.banner-nav ul li:first-child').addClass('active');
				}
				if(jQuery.browser.msie && jQuery.browser.version == '6.0'){
				}
				else{
					sIFR.replace(albertBold, {
					  selector: 'div.banner-content h2',
					  css: '.sIFR-root {  color:#193d73; font-size:25px; font-weight:bold; line-height:40px;}',
					  wmode: 'transparent'//,
					 // ratios:[8, 1.31, 12, 1.27, 15, 1.21, 16, 1.22, 22, 1.2, 23, 1.17, 25, 1.19, 33, 1.17, 42, 1.16, 47, 1.15, 48, 1.16, 66, 1.15, 70, 1.14, 72, 1.15, 78, 1.14, 79, 1.15, 1.14]
					});
				}

				sIFR.replace(albertLight, {
				  selector: 'div.banner-content h3',
				  css: '.sIFR-root { color:#193d73; font-size:20px; leading:-4; }',
				  wmode: 'transparent', 
				  ratios: [7, 1.32, 8, 1.31, 12, 1.27, 15, 1.21, 16, 1.22, 22, 1.2, 23, 1.17, 28, 1.19, 30, 1.17, 31, 1.18, 33, 1.17, 38, 1.16, 39, 1.17, 48, 1.16, 76, 1.15, 77, 1.14, 80, 1.15, 84, 1.14, 85, 1.15, 1.14]
				});
				
				if($('div.spotlight').length){
					sIFR.replace(albert, {
					  selector: 'div.content.home-page div.section div.banner-nav ul li a',
					  css: '.sIFR-root { color:#FFFFFF; font-size:20px; cursor:pointer; line-height:48px; leading:-4; }',
					  wmode: 'transparent',
					  ratios: [7, 1.32, 8, 1.31, 12, 1.27, 19, 1.23, 29, 1.19, 36, 1.18, 49, 1.17, 50, 1.16, 51, 1.17, 74, 1.16, 75, 1.15, 80, 1.16, 1.15]
					});
				}
				else{
					sIFR.replace(albert, {
					  selector: 'div.content.home-page div.section div.banner-nav ul li a',
					  css: '.sIFR-root { color:#003399; font-size:20px; cursor:pointer; line-height:48px; leading:-4; }',
					  wmode: 'transparent',
					  ratios: [7, 1.32, 8, 1.31, 12, 1.27, 19, 1.23, 29, 1.19, 36, 1.18, 49, 1.17, 50, 1.16, 51, 1.17, 74, 1.16, 75, 1.15, 80, 1.16, 1.15]
					});
				}

				var bannerInterval;

				if(opts.auto == true){
					bannerInterval = setInterval(function(){ autoCarousel($obj, opts);}, opts.timer);
				}

				setNavigation($obj, opts, bannerInterval);
			}			
		}

		function setNavigation($obj, opts, bannerInterval){
			$overflowWidth = $obj.find('div.banner-content').width() * 4;
			$obj.find('div.banner-content ul.banner-carousel').css('width', $overflowWidth + 'px');
			
			$obj.find('div.banner-nav ul li a').bind('mouseenter focus', function() {
				
				// Added for iOS Support
				if(!$(this).parent().hasClass('active')){
					Counter = 0;
				}
				if(opts.auto == true){
					clearInterval(bannerInterval);	
				}
				if($('div.spotlight').length){
					$obj.find('div.banner-nav ul li').removeClass('spotlight-active');
					$(this).parent().addClass('spotlight-active');
				}
				else{
					$obj.find('div.banner-nav ul li').removeClass('active');
					$(this).parent().addClass('active');
				}
				var index = $obj.find('div.banner-nav ul li').index($(this).parent());	
				var bannerPos = ($overflowWidth / 4) * index;

				moveCarousel($obj, opts, bannerPos);

			})

			$obj.find('div.banner-nav ul li a').bind('mouseleave blur', function() {
				if(opts.auto == true){
					bannerInterval = setInterval(function(){ autoCarousel($obj, opts);}, opts.timer);
				}
			});

			$obj.find('div.banner-content').mouseenter(function() {
				if(opts.auto == true){
					clearInterval(bannerInterval);
				}
			}).mouseleave(function(){
				if(opts.auto == true){
					bannerInterval = setInterval(function(){ autoCarousel($obj, opts);}, opts.timer);
				}
			});
			
			// Added for iOS Support
			if (isiOSDevice()){
				$obj.find('div.banner-nav ul li a').bind('click', function() {
					if (Counter == 0){		
						Counter ++;		
						return false;
					}

				});
			}
		}

		function moveCarousel($obj, opts, bannerPos){
			if(opts.slide == true){
				$obj.find('div.banner-content ul.banner-carousel').stop().animate({
					left: '-' + bannerPos + 'px'
				}, opts.speed, opts.easing);
			}
			else{
				$obj.find('div.banner-content ul.banner-carousel').css('left', '-' + bannerPos + 'px');
			}
		}

		function autoCarousel($obj, opts){
			var index = $obj.find('div.banner-nav ul li').index($('div.banner-nav ul li.active'));
			if(index > 2){
				index = -1;
			}
			index = index + 1;
			var bannerPos = ($overflowWidth / 4) * index;

			$obj.find('div.banner-nav ul li').removeClass('active');

			var activeItem = index;
			$obj.find('div.banner-nav ul li').eq(activeItem).addClass('active');

			if(opts.slide == true){
				$obj.find('div.banner-content ul.banner-carousel').stop().animate({
					left: '-' + bannerPos + 'px'
				}, opts.speed, opts.easing);
			}
			else{
				$obj.find('div.banner-content ul.banner-carousel').css('left', '-' + bannerPos + 'px');
			}
		}
	};
	
	// Banner Defaults
	$.fn.wtdbanner.defaults = {
		xmldriven: true,
		xmlpath: '/tw/common/xml/wtdbanner.xml',
		speed: 700,
		slide: true,
		timer: 5000,
		auto: false,
		easing: 'easeOutQuint'
	};
	
	
})(jQuery);
