(function($) {

	var opt 		= new Array;
	var played		= new Array;
	var usevid		= new Array;
	var currentpos	= new Array;
	var autoInterval = new Array;
	
	$.fn.wegaSlider= $.fn.wegaSlider = function(options){
		
		init = function(tg){
			opt[tg.id] = $.extend({}, $.fn.wegaSlider.defaults, options);
			played[tg.id]		= new Array();
			usevid[tg.id]		= new Array();
			currentpos[tg.id]	= 0;
			
			$('#'+tg.id+'').css('background-image', 'none');
			$('.vidinfeat').css('display', 'none');
					
			$.each($('#'+tg.id+' .played_list'), function(i,item){
				played[tg.id][i] = $(item);
				usevid[tg.id][i] = $(item).find('.vidinfeat');
			});
					
			$.setBulletNavi(tg);//create the bullet navigation
			
			//display the first div
			if($(usevid[tg.id][0]).length != 0){
				$.buildvideo(tg, 0);
			}
			
			if ($.browser.msie) {
			//litle different in IE, fail to animate the transparent shadow..then we play with the class then :)
				var firstShadsmall = $('#'+tg.id+' .played_list:eq(0)').find('.with_bg_shadow_half');
				var firstShadbig = $('#'+tg.id+' .played_list:eq(0)').find('.with_bg_shadow');
				
				firstShadsmall.removeClass('with_bg_shadow_half');
				firstShadbig.removeClass('with_bg_shadow');
				$('#'+tg.id+' .played_list:eq(0)')
					.fadeIn(opt[tg.id].animateSpeed, function() {
						firstShadsmall.addClass('with_bg_shadow_half');
						firstShadbig.addClass('with_bg_shadow');
					})
					.addClass('played');
				$("#wegabutton-0").addClass('press');
				
			} else {
			
				$('#'+tg.id+' .played_list:eq(0)')
					.fadeIn(opt[tg.id].animateSpeed)
					.addClass('played');
				$("#wegabutton-0").addClass('press');
				
			};
			
			if( opt[tg.id].autoPlay ) //if autoplay == true, then do the animation.
				$.transition(tg);
			
		};
		
		// transitions
		$.transition = function(tg){

			autoInterval[tg.id] = setInterval(function() { $.tp(tg) }, opt[tg.id].delay);		
		};
		
		$.tp = function(tg, direction){
		
				$('.objectinside').remove();
				
				if ($.browser.msie) { 
				//litle different in IE, fail to animate the transparent shadow..then we play with the class then :)
					var currShadSmall = $('.played').find('.with_bg_shadow_half');
					var currShadbig = $('.played').find('.with_bg_shadow');
					
					currShadSmall.removeClass('with_bg_shadow_half');
					currShadbig.removeClass('with_bg_shadow');
					
					$('.played').fadeOut(opt[tg.id].animateSpeed, function(){
						var thisp = $(this);
						
						currShadSmall.addClass('with_bg_shadow_half');
						currShadbig.addClass('with_bg_shadow');
						$(this).removeClass('played');
						$('.press').removeClass('press');
						$("#wegabutton-"+currentpos[tg.id]+"").addClass('press');
						
						var nextShadSmall = $(played[tg.id][currentpos[tg.id]]).find('.with_bg_shadow_half');
						var nextShadbig = $(played[tg.id][currentpos[tg.id]]).find('.with_bg_shadow');
						
						nextShadSmall.removeClass('with_bg_shadow_half');
						nextShadbig.removeClass('with_bg_shadow');
						
						$(played[tg.id][currentpos[tg.id]]).fadeIn(opt[tg.id].animateSpeed, function(){ 
							nextShadSmall.addClass('with_bg_shadow_half');
							nextShadbig.addClass('with_bg_shadow');
							
							if($(usevid[tg.id][currentpos[tg.id]]).length != 0){
								$.buildvideo(tg, currentpos[tg.id]);
							}
							
						});
						
						$(played[tg.id][currentpos[tg.id]]).addClass('played');
						
					});
				} else {
					$('.played').fadeOut(opt[tg.id].animateSpeed, function(){
						$(this).removeClass('played');
						$('.press').removeClass('press');
						$("#wegabutton-"+currentpos[tg.id]+"").addClass('press');
						$(played[tg.id][currentpos[tg.id]]).fadeIn(opt[tg.id].animateSpeed, function(){
							if($(usevid[tg.id][currentpos[tg.id]]).length != 0){
								$.buildvideo(tg, currentpos[tg.id]);
							}
						});
						$(played[tg.id][currentpos[tg.id]]).addClass('played');
					});
				}

				
				if(typeof(direction) == "undefined")
					currentpos[tg.id]++;
				else
					currentpos[tg.id] = direction;

				if  (currentpos[tg.id] == played[tg.id].length ) {
					currentpos[tg.id] = 0;
				}	

				if (currentpos[tg.id] == -1){
					currentpos[tg.id] = played[tg.id].length-1;
				}
				
		};
		
		$.buildvideo = function(tg, pos){
			$(usevid[tg.id][pos]).parent('.inim').append('<div class="objectinside"></div>');
			switch($(usevid[tg.id][pos]).attr('rel')){
					case 'youtube':
						if($(usevid[tg.id][pos]).text() == "small")
							w = 440;
						else
							w = 930;
							
						h = 320;

						movie = 'http://www.youtube.com/v/'+wega_grab_param('v', $(usevid[tg.id][pos]).attr('href'));
					
						Insert = opt[tg.id].flashMark.replace(/{width}/g,w).replace(/{height}/g,h).replace(/{path}/g,movie);
					break;
					case 'vimeo':
						if($(usevid[tg.id][pos]).text() == "small")
							w = 440;
						else
							w = 930;
							
						h = 320;
						
						movie_id = $(usevid[tg.id][pos]).attr('href');
						movie = "http://vimeo.com/moogaloop.swf?clip_id="+ movie_id.replace('http://vimeo.com/','');
					
						Insert = opt[tg.id].flashMark.replace(/{width}/g,w).replace(/{height}/g,h).replace(/{path}/g,movie);
					break;
					case 'flash':
						if($(usevid[tg.id][pos]).text() == "small")
							w = 440;
						else
							w = 930;
							
						h = 320;
						movie = $(usevid[tg.id][pos]).attr('href');
						Insert = opt[tg.id].flashMark.replace(/{width}/g,w).replace(/{height}/g,h).replace(/{path}/g,movie);
					break;
			}
			$('.objectinside').html(Insert);
		};
		
		$.setBulletNavi = function(tg){
			//append ul after the main div
			$('<ul id="wegaslide_bullet"></ul>').insertAfter($('#'+tg.id+''));
			
			for(k=0;k<played[tg.id].length;k++){
				$('#wegaslide_bullet').append("<li><a href='#' class='wegabutton' id='wegabutton-"+k+"'></a></li>");
			}
			
			$.each($('.wegabutton'), function(i,item){
				$(item).click( function(e){
					$('.objectinside').remove();
					$('.press').removeClass('press');
					$(this).addClass('press');
					e.preventDefault();
					
					$.tp(tg,i);

					if( opt[tg.id].autoPlay ){
						clearInterval(autoInterval[tg.id]);
						$.transition(tg);
					}
				})
			});
			
			if( opt[tg.id].autoPlay )
				if(opt[tg.id].hoverPause)$.pause(tg);
		};
		
		$.pause = function(tg){
			$('#'+tg.id+'').hover(function(){
				clearInterval(autoInterval[tg.id]);
			}, function(){
				clearInterval(autoInterval[tg.id]);
				$.transition(tg);
			});
		}
		
		function wega_grab_param(name,url){
		  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
		  var regexS = "[\\?&]"+name+"=([^&#]*)";
		  var regex = new RegExp( regexS );
		  var results = regex.exec( url );
		  if( results == null )
			return "";
		  else
			return results[1];
		};

		this.each (
			function(){ init(this); }
		);


	};
	
	// default values
	$.fn.wegaSlider.defaults = {	
		delay : 6000, // delay between div/object
		animateSpeed : 700, //speed on amimation per div/object
		hoverPause : true,
		autoPlay : true,
		flashMark : '<object style="z-index:0;" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="{width}" height="{height}"><param name="wmode" value="transparent" /><param name="allowfullscreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="movie" value="{path}" /><embed src="{path}" type="application/x-shockwave-flash" allowfullscreen="true" allowscriptaccess="always" width="{width}" height="{height}" wmode="transparent"></embed></object>'
	};	
	
})(jQuery);
