/**************************************************************************
 * jquery.themepunch.paradigm.js - jQuery Plugin for paradigm Slider
 * @version: 1.0 (09.11.2011)
 * @requires jQuery v1.2.2 or later 
 * @author Krisztian Horvath
**************************************************************************/




(function($,undefined){	
	
	
	
	////////////////////////////
	// THE PLUGIN STARTS HERE //
	////////////////////////////
	
	$.fn.extend({
	
		
		// OUR PLUGIN HERE :)
		paradigm: function(options) {
	
		
			
		////////////////////////////////
		// SET DEFAULT VALUES OF ITEM //
		////////////////////////////////
		var defaults = {	
			width: 876, // width of banner
			height: 300, // height of banner						
			thumbWidth:90,
			thumbHeight:50,
			thumbAmount:6,
			thumbSpaces:4,
			thumbPadding:4,
			shadow:'true',
			parallaxX:200,	//MAX DISTANCE
			parallaxY:50,	//MAX DISTANCE
			captionParallaxX:50,
			captionParallaxY:25,
			timer:2000,
			touchenabled:"on"
		};
		
			options = $.extend({}, $.fn.paradigm.defaults, options);
		

			return this.each(function() {
			
				var opt=options;
				var top_container=$(this);
				
				top_container.append('<div class="paradigm-preloader"></div>');
				prepareSlidesContainer(top_container,opt);
				prepareSlides(top_container,opt);
				prepareShadows(top_container,opt);							
				createThumbnails(top_container,opt);				
				
				//initImagePreLoading(top_container);
				//preloadba_images(top_container,opt)
				
				top_container.waitForImages(function() {   
					startRotation(top_container,opt)	
				});
				
				if (opt.timer>0)
					startTimer(top_container,opt);
					
				// TOUCH ENABLED SCROLL
				if (opt.touchenabled=="on")
						
						top_container.swipe( {data:top_container, 
										swipeLeft:function() 
												{ 
													var newitem = top_container.data('currentSlide');	
													if (newitem.index()<opt.maxitem-1) {
														var next=top_container.find('ul li:eq('+(newitem.index()+1)+')');
													} else {
														var next=top_container.find('ul li:first');
													}
													swapBanner(newitem,next,top_container,opt);
												}, 
										swipeRight:function() 
												{
													var newitem = top_container.data('currentSlide');	
													if (newitem.index()>0) {
														var next=top_container.find('ul li:eq('+(newitem.index()-1)+')');
													} else {
														var next=top_container.find('ul li:eq('+(opt.maxitem-1)+')');
													}
													swapBanner(newitem,next,top_container,opt);
												}, 
									allowPageScroll:"auto"} );
			
			})
	}
})
		

		///////////////////////////////
		//  --  LOCALE FUNCTIONS -- //
		///////////////////////////////
		
					
					///////////////////////////////////////////
					//	--	Set the Containers of Slides --	 //
					///////////////////////////////////////////
					function prepareSlides(top,opt) {
						
						top.find('ul').wrap('<div class="slide_mainmask" style="z-index:10;position:absolute;top:'+(opt.padtop+1)+'px;left:'+(opt.padleft+1)+'px;width:'+opt.width+'px;height:'+opt.height+'px;overflow:hidden"></div>');
						top.find('ul .slide_mainmask').css({'opacity':'0.0'});
						
						opt.maxitem=0;
						top.find('ul >li').each(function(i) {
							opt.maxitem=opt.maxitem+1;
							var $this = $(this);
							$this.find('.creative_layer').wrap('<div class="layer_container" style="position:absolute;left:0px;top:0px;width:'+opt.width+'px;height:'+opt.height+'px"></div>');
							$this.wrapInner('<div class="slide_container" style="z-index:10;position:absolute;top:0px;left:0px;width:'+opt.width+'px;height:'+opt.height+'px;overflow:hidden"><div class="parallax_container" style="position:absolute;top:0pxleft:0px"><div class="kb_container"></div></div></div>');

		
							/******************************* 
							################################
								THE STRUCTUE:
								
								->slide_container
									->parallax_container	
										->kb_container
							################################
							********************************/
							$this.find('.slide_container').css({'opacity':'0.0'});
							
							$this.find('.slide_container .parallax_container .kb_container .video_pradigm').each(function() {
								var $this=$(this);
								$this.closest('.slide_container').append('<div class="paradigm-video-overlay"></div>');								
								$this.closest('.slide_container').append('<div class="paradigm-video-button"></div>');	
								
								$this.closest('.slide_container').data('video',1);
								
								var pbutton = $this.closest('.slide_container').parent().find('.paradigm-video-button');
								var over = $this.closest('.slide_container').parent().find('.paradigm-video-overlay');
								var _width  = parseInt(pbutton.css('width'),0);
								var _height = parseInt(pbutton.css('height'),0);
								var mwidth  = parseInt($this.closest('.slide_container').css('width'),0);
								var mheight = parseInt($this.closest('.slide_container').css('height'),0);
								
								pbutton.css({'left':(mwidth/2-_width/2)+'px','top':(mheight/2-_height/2)+'px'});
								pbutton.data('top',top);
								pbutton.data('url',$this.html());
								$this.remove();
								over.data('origopa',over.css('opacity'));
								
								
								// VIDEO IS DEFINED, SO HOVER ON VIDEO BUTTON SHOULD MAKE SOME EFFECT
								pbutton.hover(
										function() {
											
											var $this = $(this);																					
											var $over = $this.parent().find('.paradigm-video-overlay');											
											if ( $.browser.msie )
												$over.animate({'opacity':'0.5'},{duration:100});
											else
												$over.cssAnimate({'opacity':'0.5'},{duration:100});
											
											if ($.browser.version > 7 && $.browser.version < 9) {											
												$over.css({'display':'block'});
											}
										}, 
										function() {
											var $this = $(this);											
											var $over = $this.parent().find('.paradigm-video-overlay');
											
											if ( $.browser.msie )
												$over.animate({'opacity':$over.data('origopa')},{duration:100});
											else
												$over.cssAnimate({'opacity':$over.data('origopa')},{duration:100});
											
											if ($.browser.msie && $.browser.version > 7 && $.browser.version < 9) {												
												$over.css({'display':'none'});
											}
									});

									
								// VIDEO IS DEFINED, SO CLICK ON VIDEO BUTTON SHOULD START TO PLAY THE VIDEO HERE
								pbutton.click(
									function() {
										var $this=$(this);			
										var top=$this.data('top');
										var slidemask = top.find('.slide_mainmask');
										slidemask.addClass("videoon");
										top.data('currentSlide').animate({'top':opt.height+"px"},{duration:500,queue:false});										
										
										top.find('.slide_mainmask').append('<div class="video_paradigm" style="z-index:9999;width:'+opt.width+'px;height:'+opt.height+'px">'+$this.data('url')+'</div>');
										var video = top.find('.slide_mainmask .video_paradigm');
										video.css({'top':(0-opt.height)+"px"});
										video.animate({'top':'0px'},{duration:500,queue:false});		
								
										video.find('* #close').click(
											function() {
												
												var slidemask = top.find('.slide_mainmask');
												slidemask.removeClass("videoon");
												top.data('currentSlide').animate({'top':"0px"},{duration:600,queue:false});																																										
												video.animate({'top':(0-opt.height)+'px'},{duration:600,queue:false});		
												setTimeout(function() {video.remove()},600);
											});
									});
								
							});
						
						});
					}
					
					
					////////////////////////////////////////////////
					//	--	BACKGROUND AND DEFAULT VALUES --	 //
					//////////////////////////////////////////////
					function prepareSlidesContainer(top,opt) {
						top.append('<div class="paradigm-bg" style="z-index:7;position:absolute;top:0px;left:0px;width:'+opt.width+'px;height:'+opt.height+'px;overflow:hidden"></div>');
						var bg=top.find('.paradigm-bg');
						opt.padtop=parseInt(bg.css('paddingTop'),0);
						opt.padleft=parseInt(bg.css('paddingLeft'),0);
						opt.padright=parseInt(bg.css('paddingRight'),0);
						opt.padbottom=parseInt(bg.css('paddingBottom'),0);
					}
					
		
					////////////////////////////////////////////////
					//	--	ADD THE SHADOWS IN CASE WE NEED --	 //
					//////////////////////////////////////////////
					function prepareShadows(top,opt) {
					
						if (opt.shadow=="true") {
								// CALCULATE THE SIZES OF THE SHADOWS
								var full = opt.width + opt.padleft + opt.padright;
								var fifty = full/2;
								
								if (fifty>187) fifty=187;
								full = full - 2*fifty;
								
								// CREATE LEFT, MIDDLE AND RIGHT SHADOWS
								var leftshadow=$('<div class="paradigm-leftshadow" style="top:'+(opt.height+opt.padtop+opt.padbottom)+'px;left:0px;width:'+fifty+'px"></div>');
								var rightshadow=$('<div class="paradigm-rightshadow" style="top:'+(opt.height+opt.padtop+opt.padbottom)+'px;right:0px;"></div>');
								var repeatshadow=$('<div class="paradigm-repeatshadow" style="top:'+(opt.height+opt.padtop+opt.padbottom)+'px;left:'+fifty+'px;width:'+full+'px"></div>');
								
								// APPEND THE SHADOWS
								top.append(leftshadow);
								top.append(repeatshadow);
								top.append(rightshadow);
						} else {
							var thc=top.find('.paradigm_thumb_container');
								alert(thc);
						}
					}
					
					
					
					///////////////////////////
					// CREATE THE THUMBNAILS //
					//////////////////////////
					function createThumbnails(top,opt) {
						
						var maxitem = top.find('ul >li').length;
						
											
						// CALCULATE THE MAX WIDTH OF THE THUMB HOLDER
						var maxwidth = (opt.thumbAmount * opt.thumbWidth)	+ ((opt.thumbAmount-1) * opt.thumbSpaces);
						var maxheight = opt.thumbHeight;
						
						var bgwidth = maxwidth-opt.thumbSpaces + 2*opt.thumbPadding;
						var bgheight = maxheight + 2*opt.thumbPadding;
						var full = opt.width + opt.padleft + opt.padright;
						var centerl = Math.round(full /2 - bgwidth/2);

						var max= (maxitem * opt.thumbWidth)	+ ((maxitem-1) * opt.thumbSpaces);
						
						
						// CREATE THE BACKGROUND 1 PIXEL ROUND BG 
						top.append('<div class="paradigm_thumb_container" style="position:absolute;left:'+centerl+'px;top:'+(opt.height+opt.padtop+opt.padbottom-opt.thumbPadding)+'px;width:'+(bgwidth+2)+'px;height:'+(bgheight+2)+'px;overflow:hidden"></div>');
						
						// CREATE THE WHITE HOLDER
						var thc=top.find('.paradigm_thumb_container');
						thc.append('<div class="paradigm_thumb_container_bg" style="position:absolute;left:1px;top:1px;width:'+(bgwidth)+'px;height:'+(bgheight)+'px;overflow:hidden"></div>');
	
						// CREATE THE MASK INSIDE
						thc.append('<div id="thumbmask" style="overflow:hidden;position:absolute;top:'+(opt.thumbPadding+1)+'px;left:'+(opt.thumbPadding+1)+'px; width:'+(maxwidth-opt.thumbSpaces)+'px;	height:'+opt.thumbHeight+'px"></div>');					
						var thma=thc.find('#thumbmask');							
	
						// CREATE THE SLIDER CONTAINER
						thma.append('<div class="thumbsslide" style="width:'+max+'px"></div>');										
						var thbg=thma.find('.thumbsslide');
						
						
						/**********************************************
						##############################################
								STRUCTURE OF THUMBNAILS
							
							->.paradigm_thumb_container
									->#thumbmask
										->.thumbsslide
											->thumb (thumb"i")
							->.paradigm_thumb_container_bg
							
						##############################################
						*********************************************/
						
						// GO THROUGHT THE ITEMS, AND CREATE AN THUMBNAIL AS WE NEED
						top.find('ul >li').each(function(i) {
									var $this=$(this);
									
									// READ OUT THE DATA INFOS
									var img=$this.find('img:first');
									var bgsrc=img.data('thumb_bw');
									var src=img.data('thumb');
									var isvideo = $this.find('.slide_container').data('video')==1;
									
									// CREATE THE THUMBS
									var thumb=$('<div class="paradigm-thumbs" id="thumb'+i+'" style="cursor:pointer;position:absolute;top:0px;left:'+((i*opt.thumbWidth)+((i-1)*opt.thumbSpaces))+'px;width:'+opt.thumbWidth+'px;height:'+opt.thumbHeight+'px;background:url('+bgsrc+');"></div>');
									thumb.data('id',i);
									if (i==maxitem)	thumb.css({'margin-right':'0px'});							
							
									thbg.append(thumb);

									// CREATE THE IMG ON IT
									var new_img=$('<div id="over" style="cursor:pointer"><img id="over_img" src="'+src+'"></div>');
									thumb.append(new_img);									
									var ovv=thumb.find('#over');
									
									ovv.css({'opacity':'0.0'});
									
									if ($.browser.msie && $.browser.version > 7 && $.browser.version < 9) {
										//ovv.css("filter","alpha(opacity=0)");
										ovv.css({'display':'none'});
									}
									
									
									ovv.css({'overflow':'hidden','position':'absolute','left':'0px','opacity':'0.0','height':opt.thumbHeight+"px",'width':opt.thumbWidth+"px"});
									ovv.find('img').css({'position':'absolute','left':'0px'});
									
									if ( $.browser.msie ) {
										ovv.animate({'left':'0px','opacity':'0.0','height':opt.thumbHeight+"px",'width':opt.thumbWidth+"px"},{duration:50,queue:false});
										ovv.find('img').animate({'left':'0px'},{duration:50,queue:false});
										
									} else {
										ovv.cssAnimate({'left':'0px','opacity':'0.0','height':opt.thumbHeight+"px",'width':opt.thumbWidth+"px"},{duration:50,queue:false});
										ovv.find('img').cssAnimate({'left':'0px'},{duration:50,queue:false});
									}
									
									// CREATE VIDEO BUTTON ON THE THUMBNAIL
									if (isvideo) {
										var new_video=$('<div class="video"></div>');
										thumb.append(new_video);									
										thumb.find('.video').css({'position':'absolute',
																  'top':opt.thumbHeight/2+'px',
																  'left':opt.thumbWidth/2+'px',
																  'z-index':'1000'});
										}
										
										 
									// ADD SHADOWS
									if (opt.shadow=="true") {
										var repeatshadow=$('<div class="paradigm-repeatshadow" style="position:relative;margin-left:0px;margin-top:-59px;width:'+opt.thumbWidth+'px"></div>');
										thumb.append(repeatshadow);
									}
									
									///////////////////////////////////////
									// SHOW THE COLORED THUMBNAIL HERE  //
									//////////////////////////////////////
									var thumbnail = thbg.find('#thumb'+i);
									thumbnail.hover(
										function() {
											var $this=$(this).find('#over');
											
											if (!$this.parent().hasClass("selected")) {												
													//$this.stop();									
													if ( $.browser.msie ) {
														if ($.browser.msie && $.browser.version > 7 && $.browser.version < 9)
																$this.css({'display':'block'});
															else
																$this.animate({'opacity':'1.0'},{duration:300,queue:false});												
														
													 } else {														
														$this.css({'left':'0px','display':'block'});
														$this.find("img").css({'display':'block','opacity':'1.0','left':'0px'});												
														$this.cssAnimate({'opacity':'1.0'},{duration:300,queue:false});
														
													}
											}
										},
										function() {
											var $this=$(this).find('#over');
											
											if (!$this.parent().hasClass("selected")) {
												if ( $.browser.msie ) {
																									
													if ($.browser.msie && $.browser.version > 7 && $.browser.version < 9)
														$this.css({'display':'none'});
													else
														$this.animate({'opacity':'0'},{duration:300,queue:false});												
													
												 } else {
													$this.cssAnimate({'opacity':'0.0'},{duration:300,queue:false});
												}
											}
										});
									
									thumbnail.click(function() {
										var $this=$(this);
										if (!$this.hasClass("selected")) {
											var newslide = top.find('ul li:eq('+$this.data('id')+')');										
											swapBanner(top.data('currentSlide'),newslide,top,opt);
										}
									});
								
							});
							
							////////////////////////							
							// SLIDE TO POSITION  //
							////////////////////////							
							if (maxwidth<max) {
								$(document).mousemove(function(e) {
									$('body').data('mousex',e.pageX);							
								});
								
								var diff=(max- maxwidth);								
								top.data('thumbnailmaxdif',diff);
								
								var steps = diff / (maxwidth-opt.thumbWidth);
								
								
								thma.data('steps',steps);				
								thma.data('thw',opt.thumbWidth);		
								thma.data('maxw',diff);
								
								
								// ON MOUSE MOVE ON THE THUMBNAILS EVERYTHING SHOULD MOVE :)
								
								thma.mouseenter(function() {
									var $this=$(this);
										$this.addClass('overme');
										
										var offset = $this.offset();
										var x = $('body').data('mousex')-offset.left;
										x=x-$this.data('thw')/2;
										
										// STEPS AND ETC
										var steps=$this.data('steps');												
										
										//ANIMATE TO POSITION
										var pos=(0-steps*x);

										if (pos>0) pos =0;						
										if (pos<0-diff) pos=0-diff;
										
										$this.addClass("overon");
										$this.find('.thumbsslide').css({'position':'absolute'});
										
										if ( $.browser.msie ) 
											$this.find('.thumbsslide').animate({'left':pos+'px'},{duration:200,queue:false});										
										else
											$this.find('.thumbsslide').cssAnimate({'left':pos+'px'},{duration:200,queue:false});										
										
								});
								
								thma.mousemove(function() {										
										var $this=$(this);
										$this.addClass('overme');
										
										var offset = $this.offset();
										var x = $('body').data('mousex')-offset.left;
										x=x-$this.data('thw')/2;
										
										// STEPS AND ETC
										var steps=$this.data('steps');												
										
										//ANIMATE TO POSITION
										var pos=(0-steps*x);

										if (pos>0) pos =0;						
										if (pos<0-diff) pos=0-diff;
										
										if (!$this.hasClass("overon")) {
											$this.find('.thumbsslide').css({'position':'absolute'});
											//$this.find('.thumbsslide').stop();
											if ( $.browser.msie ) 
												$this.find('.thumbsslide').animate({'left':pos+'px'},{duration:0,queue:false});										
											else
												$this.find('.thumbsslide').cssAnimate({'left':pos+'px'},{duration:0,queue:false});										
										} else {
											setTimeout(function() {$this.removeClass('overon');},100);
										}
								});
								
								thma.mouseout(function() {
									var $this=$(this);
									$this.removeClass('overme');
										
								});
							}

					}
					
		
						
					/////////////////////////////////////////////
					// - START THE ROTATION OF THE BANNER HERE //
					/////////////////////////////////////////////						
					function startRotation(item,opt) {
						if ( $.browser.msie ) {
							item.find('.paradigm-preloader').animate({'opacity':'0.0'},{duration:300,queue:false});
							
						} else {
							item.find('.paradigm-preloader').cssAnimate({'opacity':'0.0'},{duration:300,queue:false});
						}
						setTimeout(function() {item.find('.paradigm-preloader').remove();},300);
						var first_slide = item.find('ul li:first') ;
						swapBanner(first_slide,first_slide,item,opt);	
						startParallax(item,opt);
						opt.loaded=1;
						
					}
					
					
					
					
					/////////////////////////////////
					// - START THE PARALLAX EFFECT //
					////////////////////////////////						
					function startParallax(slidertop,opt) {
												
						// FIND THE RIGHT OBJECT 
						var top = slidertop.find('.slide_mainmask');
												
						// SET WIDTH AND HEIGHT
						top.data('maxwidth',opt.width+opt.padleft+opt.padright);
						top.data('maxheight',opt.height+opt.padtop+opt.padbottom);
						top.data('pdistance',opt.parallaxX);
						top.data('pdistancey',opt.parallaxY);
						top.data('cdistance',opt.captionParallaxX);
						top.data('cdistancey',opt.captionParallaxY);
						top.data('opt',opt);
						
						// SOME HELP DIV IF WE NEED TO DEBUG //
								//top.append('<div style="z-index:1000000;opacity:0.3;background-color:#ff0000;width:'+opt.width+'px;height:'+opt.height+'px;position:absolute"></div>');						
							$('body').append('<div id="mpinfo" style="z-index:1000000;background-color:#fff;position:absolute;top:10px;left:10px;font-size:15px"></div>');
						
						// THE FIRST MOUSE OVER ON THE TOP
						top.mouseenter(function(e) {							
							var $this = $(this);			
							// FIND THE RIGHT THUMBNAIL HOLDER OBJECT
							var thma = $this.parent().find('.paradigm_thumb_container #thumbmask');
							
							// IF MOUSE IS NOT OVER THE THUMBS AND START ANIMATION NOT RUNNING
							
									
								var slide = $this.parent().data('currentSlide');							
								var par = slide.find('.parallax_container');
								var layers = slide.find('.layer_container');
								
								var obj=parallaxMe(e.pageX,e.pageY,$this)								
								par.stop();
								layers.stop();
								$this.addClass('overon');								
								
								par.css({'left':parseInt(par.css('left'),0)+"px",'top':parseInt(par.css('top'),0)+"px"});//,{duration:0,queue:false});
								if ( $.browser.msie ) {
									par.animate({'left':(0 - obj.slideFactorX)+'px','top':(0 - obj.slideFactorY)+'px'},{duration:200,queue:false});
									layers.animate({'left':(0 - obj.slideFactorCX)+'px','top':(0 - obj.slideFactorCY)+'px'},{duration:200,queue:false});
								} else {
									par.cssAnimate({'left':(0 - obj.slideFactorX)+'px','top':(0 - obj.slideFactorY)+'px'},{duration:200,queue:false});
									layers.cssAnimate({'left':(0 - obj.slideFactorCX)+'px','top':(0 - obj.slideFactorCY)+'px'},{duration:200,queue:false});
								}
								
							
							
						});
						
						// BACK TO CENTER AFTER LEAVE
						top.mouseleave(function(e) {							
							var $this = $(this);																			
								var slide = $this.parent().data('currentSlide');							
								var par = slide.find('.parallax_container');
								var layers = slide.find('.layer_container');
								$this.removeClass("overme");
								
								var obj=parallaxMe($(window).width()/2,$(window).height/2,$this)								
								//alert($this.data('maxwidth')+"  "+e.pageX);
								$this.addClass('overon');
								par.stop();
								layers.stop();
								if ( $.browser.msie ) {
										par.animate({'left':(0 - obj.slideFactorX)+'px','top':(0 - obj.slideFactorY)+'px'},{duration:200,queue:false});
										layers.animate({'left':(0 - obj.slideFactorCX)+'px','top':(0 - obj.slideFactorCY)+'px'},{duration:200,queue:false});												
								} else {
										par.cssAnimate({'left':(0 - obj.slideFactorX)+'px','top':(0 - obj.slideFactorY)+'px'},{duration:200,queue:false});
										layers.cssAnimate({'left':(0 - obj.slideFactorCX)+'px','top':(0 - obj.slideFactorCY)+'px'},{duration:200,queue:false});												
								}
						});
						
						// HERE COME THE DIRECT PARALLAX HANDLING FOR QUICK ANIMATIONS
						top.mousemove(function(e) {
						
							var $this = $(this);	
							$this.addClass("overme");
							// FIND THE RIGHT THUMBNAIL HOLDER OBJECT							
							var thma = $this.parent().find('.paradigm_thumb_container #thumbmask');
							
							// IF MOUSE IS NOT OVER THE THUMBS AND START ANIMATION NOT RUNNING
							if (!thma.hasClass('overme') && !$this.hasClass('overon')) {
									
									
									var slide = $this.parent().data('currentSlide');							
									var par = slide.find('.parallax_container');
									var layers = slide.find('.layer_container');																		
									
									var obj=parallaxMe(e.pageX,e.pageY,$this)																	
																											
									if (obj.y<$this.data('maxheight')) {
										par.stop();
										layers.stop();
										if ( $.browser.msie ) {
											par.css({'left':(0 - obj.slideFactorX)+'px','top':(0 - obj.slideFactorY)+'px'});//,{duration:0,queue:false});
											layers.css({'left':(0-obj.slideFactorCX)+'px','top':(0 - obj.slideFactorCY)+'px'});//,{duration:0,queue:false});							
										} else {
											
											par.cssAnimate({'left':(0 - obj.slideFactorX)+'px','top':(0 - obj.slideFactorY)+'px'},{duration:0,queue:false});
											layers.cssAnimate({'left':(0-obj.slideFactorCX)+'px','top':(0 - obj.slideFactorCY)+'px'},{duration:0,queue:false});							
										}
									}
							} else {
								
								setTimeout(function() {$this.removeClass('overon')},100);
							}
						});
						
						
					
					}
						
						
					////////////////////////////////////					
					// CALCULATE PARALLAX ENPOSITIONS //
					////////////////////////////////////
					function parallaxMe(mouseX,mouseY,slide_mainmask) {
												
												var $this=slide_mainmask;												
												var opt = $this.data('opt');
												var newitem=$this;
												var slide = $this.parent().data('currentSlide');
												
												
												// CALCULATE X POSITION AND Y FACTORS
												var x=mouseX - $this.offset().left;									
												var maxwidth=$this.data('maxwidth');
												var factorX = x / maxwidth;
												if (factorX>1) factorX=1;
												if (factorX<-1) factorX=-1;
												
												// CALCULATE Y POSITION AND Y FACTORS
												var y=mouseY - $this.offset().top;									
												var maxheight= $this.data('maxheight');
												var factorY = y / maxheight
												if (factorY>1) factorY=1;
												if (factorY<-1) factorY=-1;
												
												// SETMAX DISTANCE X
												var maxwayX = $this.data('pdistance');																											
												
												// SETMAX DISTANCE Y
												var maxwayY = $this.data('pdistancey');
												
												// SET OFFSET DISTANCE FOR CAPTIONS
												var maxCwayX = $this.data('cdistance');
												var maxCwayY = $this.data('cdistancey');
												
												
												// CHECH IF IMAGE SMALLER IS THAN THE PARALLAX EFFECT WISH TO USE IT
												var img=slide.find('.parallax_container .kb_container img:first');
												var possiblewidth = img.width() - maxwidth;
												var possibleheight = img.height() - maxheight;									
												if (maxwayX>possiblewidth) maxwayX = possiblewidth;
												if (maxwayY>possibleheight) maxwayY = possibleheight;
												
												if (maxwayX<0) {maxwayX = 0;maxCwayX=0;}
												if (maxwayY<0) {maxwayY = 0;maxCwayY=0;}
												
												
												// CALCULATE THE TARGET POSITION X
												var obj={};
												factorX = factorX - 0.5;
												factorY = factorY - 0.5;
												obj.slideFactorX = (maxwayX * factorX);
												obj.slideFactorCX = (maxCwayX * factorX);
												
												// CALCULATE THE TARGET POSITION X
												obj.slideFactorY = (maxwayY * factorY);
												obj.slideFactorCY = (maxCwayY * factorY);
												obj.x = x;
												obj.y = y;
												
												
												// SOME HELP DIV IF WE NEED TO DEBUG //
												//$('body').find('#mpinfo').html("maxwayX:"+maxwayX+" imgwidth:"+img.width()+'<br> imgsrc:'+img.attr('src')+'<br>LayerDistnace:'+obj.slideFactorX+'<br>Endpoint: ');												
												
												return obj;												
										}
										
					////////////////////////////////////////////////////
					// - THE BANNER SWAPPER, ONE AGAINST THE OTHER :) //
					////////////////////////////////////////////////////
					function swapBanner(item,newitem,slider_container,opt) {
											
							var trans=false;
							
							slider_container.find('ul >li').each(function(i) {
								var $this=$(this);
								
								if ($this.index() !=item.index() && $this.index() !=newitem.index()) {
									$this.css({'display':'none','position':'absolute','left':'0px','z-index':'994'});									
								}
							});
							
									
							var video = slider_container.find('.slide_mainmask .video_paradigm');														
							setTimeout(function() {video.remove()},600);
							
							var slidemask = slider_container.find('.slide_mainmask');
							slidemask.removeClass("videoon");
							
							item.css({'position':'absolute','top':'0px','left':'0px','z-index':'900'});
							newitem.css({'position':'absolute','top':'0px','left':'0px','z-index':'1000'});
							newitem.css({'display':'block'});
							
							var oldW=newitem.find('img').width();
							var oldH=newitem.find('img').height();							
												
							// SET THE MIN. IMAGE SIZE
								if (newitem.find('img').width()>0 && newitem.find('img').width()<opt.width) {														
									var factor=opt.width / oldW;														
									oldW=oldW*factor;
									oldH=oldH*factor;														
								}
															
								if (newitem.find('img').height()>0 && newitem.find('img').height()<opt.height) {														
									var factor=opt.height / oldH;														
									oldW=oldW*factor;
									oldH=oldH*factor;														
								}
							
							// SET THE IMAGE IN THE CENTER POSITION
							if (oldW>opt.width) 												
								newitem.find('img:first').css({'position':'absolute','left':(opt.width/2 - oldW/2)+"px", 'width':oldW+"px",'height':oldH+"px"});													
							
							if (oldH>opt.height) 												
								newitem.find('img:first').css({'position':'absolute','top':(opt.height/2 - oldH/2)+"px", 'width':oldW+"px",'height':oldH+"px"});
						
									
						
							// TRANSITION OF THE SLIDES
							if (newitem.data('transition')=="slide") {
									if (trans==false) {
												var left=true;																				
												if (item.index()>newitem.index()) left = false;
												
												if (left) {		
													
													video.animate({'left':(0-opt.width)+'px'},{duration:600,queue:false});		
													newitem.find('.slide_container').css({'opacity':'1.0','left':opt.width+"px"});
													
													if ( $.browser.msie )  {
														item.find('.slide_container').animate({'left':0-opt.width+'px'},{duration:600,queue:false});
														newitem.find('.slide_container').animate({'left':'0px','top':'0px','opacity':'1.0'},{duration:600,queue:false});
													} else {
														item.find('.slide_container').cssAnimate({'left':0-opt.width+'px'},{duration:600,queue:false});
														newitem.find('.slide_container').cssAnimate({'left':'0px','top':'0px','opacity':'1.0'},{duration:600,queue:false});
													}
													
													
														
													
												} else {
												
													video.animate({'left':(opt.width)+'px'},{duration:600,queue:false});		
													newitem.find('.slide_container').css({'opacity':'1.0','position':'absolute','top':'0px','left':0-opt.width+'px'});	
													
													if ( $.browser.msie )  {
														item.find('.slide_container').animate({'left':opt.width+'px'},{duration:600,queue:false});											
														newitem.find('.slide_container').animate({'left':'0px','top':'0px','opacity':'1.0'},{duration:600,queue:false});
													} else {
														item.find('.slide_container').cssAnimate({'left':opt.width+'px'},{duration:600,queue:false});											
														newitem.find('.slide_container').cssAnimate({'left':'0px','top':'0px','opacity':'1.0'},{duration:600,queue:false});
													}
													
													
													
													
												}
										}
							} else {																								
								if ( $.browser.msie ) 
									item.find('.slide_container').animate({'opacity':'0'},{duration:600,queue:false});	
								else
									item.find('.slide_container').cssAnimate({'opacity':'0'},{duration:600,queue:false});	
								
								video.animate({'opacity':'0.0'},{duration:600,queue:false});		
								newitem.find('.slide_container').css({'opacity':'0.0','left':'0px','top':'0px'});								
								
								if ( $.browser.msie ) 
									newitem.find('.slide_container').animate({'opacity':'1.0'},{duration:600,queue:false});
								else
									newitem.find('.slide_container').cssAnimate({'opacity':'1.0'},{duration:600,queue:false});
							}
							
							
							
						// SET THE THUMBNAIL
												
						var thumb = slider_container.find('.paradigm_thumb_container #thumbmask .thumbsslide #thumb'+newitem.index()+' #over');
					
						slider_container.find('.paradigm_thumb_container #thumbmask .thumbsslide #thumb'+item.index()).each(function(i) {						
								var $this=$(this);								
								if ($this.attr('id')!="thumb"+newitem.index()) {
									
									$this.removeClass('selected');																
									var over=$this.find('#over');
									over.stop();		
									over.css({'position':'absolute'});
									over.find('img').css({'position':'absolute','left':'0px'});
									
									if ( $.browser.msie ) { 												
										over.animate({'left':'0px','opacity':'0.0','height':opt.thumbHeight+"px",'width':opt.thumbWidth+"px"},{duration:0,queue:false});										
									} else {
										over.cssAnimate({'left':'0px','opacity':'0.0','height':opt.thumbHeight+"px",'width':opt.thumbWidth+"px"},{duration:0,queue:false});
									}
									
									if ($.browser.msie && $.browser.version > 7 && $.browser.version < 9)
										over.css({'display':'none'});
									
								} 
						});
						
						thumb.parent().addClass('selected');
						thumb.animate({'opacity':'1.0'},{duration:300,queue:false});
						if ($.browser.msie && $.browser.version > 7 && $.browser.version < 9)
							thumb.css({'display':'block'});
						
						
						
						// SET THE CURRENT ITEM IN DATA
						slider_container.data('currentSlide',newitem);								
						slider_container.data('currentThumb',thumb);
						
						// START 
						textanim(newitem,100,slider_container);
						opt.cd=0;
																
					}
					
					
				////////////////////////////////////
				// AUTOMATIC COUNTDOWN FOR SLIDER //
				////////////////////////////////////
				function startTimer(top,opt) {
					opt.cd=0;
					opt.timer=opt.timer*10;
					//opt.timer=opt.timer*10;
					if ( $.browser.msie ) 									
						top.find('.paradigm_thumb_container #thumbmask .thumbsslide').cssAnimate({'left':'0px'},{duration:300,queue:false});
					else
						top.find('.paradigm_thumb_container #thumbmask .thumbsslide').animate({'left':'0px'},{duration:300,queue:false});
					var tmask = top.find('.paradigm_thumb_container #thumbmask');
					var tslide = tmask.find('.thumbsslide');
					
					var slidemask = top.find('.slide_mainmask');
					
					// HIER COMES THE INTERVAL ES IT SHOULD
					setInterval(function() {
						if (opt.loaded==1) {
							var newitem = top.data('currentSlide');							
							var thumb = top.data('currentThumb');
							
							if (!tmask.hasClass('overme') && !slidemask.hasClass('overme') && !slidemask.hasClass('videoon')) {
												opt.cd=opt.cd+1;
											
											var offsetme = Math.floor(opt.thumbWidth * ((opt.cd/opt.timer)))
											
											if ( $.browser.msie ) 											
												thumb.animate({'left':offsetme+'px'},{duration:20,queue:false});
											else
												thumb.cssAnimate({'left':offsetme+'px'},{duration:20,queue:false});
											
											if ( $.browser.msie ) 											
												thumb.find('img').animate({'left':(0-offsetme)+'px'},{duration:20,queue:false});
											else
												thumb.find('img').cssAnimate({'left':(0-offsetme)+'px'},{duration:20,queue:false});
												
											if (opt.cd==opt.timer) {
												opt.cd=0;
												
												if (newitem.index()<opt.maxitem-1) {
													var next=top.find('ul li:eq('+(newitem.index()+1)+')');
													swapBanner(newitem,next,top,opt);
													var minus = 0-parseInt(thumb.parent().css('left'),0);
													tslide.css({'position':'absolute'});
													if (Math.abs(minus)<=top.data('thumbnailmaxdif')) {							
														if ( $.browser.msie ) 											
															tslide.animate({'left':minus+'px'},{duration:300,queue:false});
														else
															tslide.cssAnimate({'left':minus+'px'},{duration:300,queue:false});
													} else {
														minus = 0-top.data('thumbnailmaxdif');							
														if ( $.browser.msie ) 																					
															tslide.animate({'left':minus+'px'},{duration:300,queue:false});
														else
															tslide.cssAnimate({'left':minus+'px'},{duration:300,queue:false});
													}
													
												} else {
													swapBanner(newitem,top.find('ul li:first'),top,opt);	
													if ( $.browser.msie ) 																					
														tslide.animate({'left':'0px'},{duration:300,queue:false});
													else
														tslide.cssAnimate({'left':'0px'},{duration:300,queue:false});
												}
												
												

											}
								}
						}
					},100);
				}
				
							
				
				
				///////////////////
				// TEXTANIMATION //
				//////////////////			
				function textanim (item,edelay,slider_container) {
												
								var counter=0;
				
									item.find('.creative_layer div').each(function(i) {
															
															var $this=$(this);
															
															// REMEMBER OLD VALUES
															if ($this.data('_top') == undefined) $this.data('_top',parseInt($this.css('top'),0));
															if ($this.data('_left') == undefined) $this.data('_left',parseInt($this.css('left'),0));
															if ($this.data('_op') == undefined) { $this.data('_op',$this.css('opacity'));}
															
													
															// CHANGE THE z-INDEX HERE
															$this.css({'z-index':1200});
															
															
																	
																	
																	
																	
																	//// -  FADE UP   -   ////
																	if ($this.hasClass('fadeup')) {
																			$this.animate({'top':$this.data('_top')+20+"px",
																							 'opacity':0},
																							{duration:0,queue:false})
																				   .delay(edelay + (counter+1)*200)
																				   .animate({'top':$this.data('_top')+"px",
																							 'opacity':$this.data('_op')},
																							{duration:300,queue:true})
																		counter++;
																	}
																	
																	
																	//// -  FADE RIGHT   -   ////
																	if ($this.hasClass('faderight')) {
																		$this.animate({'left':$this.data('_left')-20+"px",
																					 'opacity':0},
																					{duration:0,queue:false})
																		   .delay(edelay + (counter+1)*200)
																		   .animate({'left':$this.data('_left')+"px",
																					'opacity':$this.data('_op')},
																					{duration:300,queue:true})
																		counter++;
																	}
																	
																	
																	//// -  FADE DOWN  -   ////
																	if ($this.hasClass('fadedown')) {
																			$this.animate({'top':$this.data('_top')-20+"px",
																							 'opacity':0},
																							{duration:0,queue:false})
																				   .delay(edelay + (counter+1)*200)
																				   .animate({'top':$this.data('_top')+"px",
																							 'opacity':$this.data('_op')},
																							{duration:300,queue:true})
																		counter++;
																	}
																	
																	
																	//// -  FADE LEFT   -   ////
																	if ($this.hasClass('fadeleft')) {
																		$this.animate({'left':$this.data('_left')+20+"px",
																					 'opacity':0},
																					{duration:0,queue:false})
																		   .delay(edelay + (counter+1)*200)
																		   .animate({'left':$this.data('_left')+"px",
																					'opacity':$this.data('_op')},
																					{duration:300,queue:true})
																		counter++;
																	}
																	
																	//// -  FADE   -   ////
																	if ($this.hasClass('fade')) {
																		$this.animate({'opacity':0},
																					{duration:0,queue:false})
																		   .delay(edelay + (counter+1)*200)
																		   .animate({'opacity':$this.data('_op')},
																					{duration:300,queue:true})
																		counter++;
																	}
															
														});	// END OF TEXT ANIMS ON DIVS
					
				}
})(jQuery);			

				
			

			   
