;(function($){

	var SzjrtPromoRotator = function(element, options)
	{
		var $this = $(element),
		$nav = $('.nav', $this),
		$promoitems = $('.promoitems', $this),
		containerHeight = $this.height(),
		currentIndex,
		timer,
		numItems;
		
		
		var settings =	{
			overlap : 10,
			speed : 500,
			delay : 3000,
			pad: 20,
			xmlStr: null,
			xml: 'http://www.szerencsejatek.hu/xml/mainpromo_hu.xml',
			startIndex: 0,
			easing : 'easeOutExpo',
			$blobMiddle : $('<span class="middle"></span>'),
			$blobTop : $('<span class="top"></span>'),
			$blobBottom : $('<span class="bottom"></span>'),
			$blob : $('<li class="blob"></li>')

		};


		var settings = $.extend(settings, options || {});

		var loadXML = function(){
			if(settings.xmlStr && !$.browser.msie){
				OnXMLloaded(settings.xmlStr);
			}else{
				$.get(settings.xml,OnXMLloaded,'xml');
			}
		};

		var OnXMLloaded = function(data, textStatus, jqXHR){

			var $promos = $(data).find('promo'),
				$frame = $('a.frame', $this);

			numItems = $promos.length;

			$promos.each(function(i){
				var $listItem = $('<li />');
				var $link = $('<a href="'+$(this).attr('url')+'" target="_blank"><span>'+$(this).attr('id')+'</span></a>');
				var $promo = $('<div class="item"><p><a href="'+$(this).attr('url')+'" target="_blank">'+$(this).attr('description')+'<span>Tovább</span></a></p></div>')
				.data('targetUrl', $(this).attr('url'))
				.data('contentPath', $(this).attr('contentPath'))
				.data('interval', $(this).attr('interval'));


				$link.hover(function(e){
					showPromo(i);
					stop();
				},function(){
					start()
				})

				$link.appendTo($listItem);
				$listItem.appendTo($nav)

				
			//	$listItem.height(containerHeight/$promos.length)


				$promo.appendTo($promoitems);

			})
			var pad = ($this.height() - $nav.height() +5) / numItems;

			$('li', $nav).each(function(i){
				
					$listItem = $(this)
					$link = $('a',$listItem);
				// $(this).height($(this).height())
			
				$link.css({
					'padding-top':pad/2
					,'padding-bottom':pad/2
				});
				
			})
			
			$frame.hover(stop, start)
			
			
			createBlob();


		}
		
		var getMedia = function(contentPath){
			
			var fileType = contentPath.substr( (contentPath.lastIndexOf('.') +1) ),
				tag = '',
				bannerId = 'promobanner-'+parseInt(Math.random()*100000),
				isHTTP =  contentPath.substr(0,7) == 'http://';
				
				
			
		//	if(!isHTTP) contentPath = 'http://szerencsejatek.hu/' + contentPath;

			
			switch (fileType){
			
				case 'swf':

					tag = 	'<object width="438" height="285" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" id="'+bannerId+'">'
							+'<param name="movie" value="'+contentPath+'" />'
							+'<param name="quality" value="high" />'
							+'<param name="wmode" value="transparent" />'
							+'<param name="allowscriptaccess" value="always" />'
							+'<embed name="'+bannerId+'" src="'+contentPath+'" quality="high" wmode="transparent" allowscriptaccess="always" width="438" height="285" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer"></embed>'
							+'</object>';
					break;
				
				default:
					tag = '<img src="'+contentPath+'" alt="" />';
					break;

			}
			return '<div class="media">' + tag + '</div>';
		}
		
		var showNextItem = function(){

			nextIndex = (currentIndex >= numItems-1) ? 0 : currentIndex+1;

			showPromo(nextIndex);

		}

		var start = function(){
			
			var interval = $($('div', $promoitems).get(currentIndex)).data('interval') || settings.delay;
			timer = setTimeout(showNextItem, interval);
		}

		var stop = function(){
			clearTimeout(timer)

		}


		var createBlob = function(){

			settings.$blobTop.height(settings.pad),
			settings.$blobBottom.height(settings.pad),
			
			settings.$blob
			.css({
				top : (settings.overlap / 2) - settings.pad
			})
			.append(settings.$blobTop)
			.append(settings.$blobMiddle)
			.append(settings.$blobBottom)
			.appendTo($nav);

			showNextItem();

		}



		var showPromo = function(index){
			
			clearTimeout(timer)


			if(!index) index = 0;

			var $promoItem = $($('div.item', $promoitems).get(index)),
		//	$image = $('div.media', $promoItem),
			$text = $('p', $promoItem),
			$frame = $('a.frame', $this)
			$listItem = $($('li', $nav).get(index)),
			url = $promoItem.data('targetUrl'),
			interval = $promoItem.data('interval') || settings.delay,
			$image = $(getMedia($promoItem.data('contentPath')));
		

			$frame.attr("href", url);

			if(currentIndex === index) return true;

			currentIndex = index;
			
			$('li', $nav).removeClass('selected');
			$('div.item', $promoitems).hide();
			$('div.media object', $promoitems).remove();
			$('div.media', $promoitems).remove();

			$listItem.addClass('selected');

			settings.$blob.stop().animate(
				{
					top : $listItem.position().top - settings.pad -(settings.overlap/2)
				},
				{
					duration : settings.speed,
					easing : settings.easing,
					queue : false
				}
			);
			settings.$blobMiddle.stop().animate(
				{
					height : $listItem.outerHeight() + settings.overlap			
				},
				{
					duration : settings.speed,
					easing : settings.easing,
					queue : false
				}

			);

			$promoItem.show()
			
			$promoItem.append($image);
			
			$image.css('top', -$image.height())

			$image.animate(
				{
					'top': 0
				},
				{
					duration : settings.speed*2,
					easing : settings.easing,
					queue : false
				}
			);

			$text.css('left', -$text.width()).stop().delay(settings.speed).animate(
				{
					'left': 0
				},
				{
					duration : settings.speed*2,
					easing : settings.easing
				}
			);


			

			timer = setTimeout(showNextItem, interval);


		}



		loadXML();

	};

	$.fn.extend({
		szjrtPromoRotator: function(options){
			return this.each(function(){

				if ($(this).data('szjrtPromoRotator')){
					return;
				} 

				var szjrtPromoRotator = new SzjrtPromoRotator(this, options);

				$(this).data('szjrtPromoRotator', SzjrtPromoRotator);
			})
		}
	})

})(jQuery);

