//based : tiny carusel
(function($){
	$.tiny = $.tiny || { };
	
	$.tiny.carousel = {
		options: {	
			start: 1, // where should the carousel start?
			display: 1, // how many blocks do you want to move at 1 time?
			axis: 'x', // vertical or horizontal scroller? ( x || y ).
			controls: true, // show left and right navigation buttons.
			pager: false, // is there a page number navigation present?
			interval: false, // move to another block on intervals.
			intervaltime: 3000, // interval time in milliseconds.
			rewind: false, // If interval is true and rewind is true it will play in reverse if the last slide is reached.
			animation: true, // false is instant, true is animate.
			duration: 500, // how fast must the animation move in ms?
			slider2: false,
			flash: false,
			gotoheight: false,
			fullsizeimg: false,
			centerimgwrapper: false,
			share: false,
			remove: false,
			callback: null // function that executes after every move.
		}
	};
	
	$.fn.slidecarousel = function(options) {
		var options = $.extend({}, $.tiny.carousel.options, options);
		this.each(function(){ $(this).data('tcl', new Carousel($(this), options)); });
		return this;
	};
	$.fn.slidecarousel_start = function(){ $(this).data('tcl').start(); };
	$.fn.slidecarousel_stop = function(){ $(this).data('tcl').stop(); };
	$.fn.slidecarousel_move = function(iNum){ $(this).data('tcl').move(iNum-1,true); };
	
	function Carousel(root, options){
		var oSelf = this;
		var oViewport = $('.viewport:first', root);
		var oContent = $('.overview:first', root);
		var oPages = oContent.children();
		var oBtnNext = $('.next:first', root);
		var oBtnPrev = $('.prev:first', root);
		var oPager = $('.pager:first', root);
		var iPageSize, iSteps, iCurrent, oTimer, bPause, bForward = true, bAxis = options.axis == 'x';
		
		function initialize(){
			iPageSize = bAxis ? $(oPages[0]).outerWidth(true) : $(oPages[0]).outerHeight(true);
			var iLeftover = Math.ceil(((bAxis ? oViewport.outerWidth() : oViewport.outerHeight()) / (iPageSize * options.display)) -1);
			iSteps = Math.max(1, Math.ceil(oPages.length / options.display) - iLeftover);
			iCurrent = Math.min(iSteps, Math.max(1, options.start)) -2;
			oContent.css(bAxis ? 'width' : 'height', (iPageSize * oPages.length));
			oSelf.move(1);
			setEvents();
			gotoheight(iCurrent);
			return oSelf;
		};
		function setEvents(){
			if(options.controls && oBtnPrev.length > 0 && oBtnNext.length > 0){
				oBtnPrev.click(function(){
					bBack = iCurrent == 0 ? +(iSteps-1) : -1;
					oSelf.move(bBack);
					oBtnPrev.attr('rel',(iCurrent+1));
					return false;
					});
				oBtnNext.click(function(){
					aForward = iCurrent +1 == iSteps ? -(iSteps-1) : +1;
					oSelf.move(aForward);
					oBtnNext.attr('rel',(iCurrent+1));
					return false;
					});
			}
			if(options.interval){ root.hover(oSelf.stop,oSelf.start); }
			if(options.pager && oPager.length > 0){ $('a',oPager).click(setPager); }
		};
		function setButtons(){
			if(options.controls){
				oBtnPrev.toggleClass('enable', !(iCurrent > 0));
				oBtnNext.toggleClass('enable', !(iCurrent +1 < iSteps));
			}
			if(options.pager){
				var oNumbers = $('.pagenum', oPager);
				oNumbers.removeClass('active');
				$(oNumbers[iCurrent]).addClass('active');
			}			
		};
		function setPager(oEvent){
			if($(this).hasClass('pagenum')){ oSelf.move(parseInt(this.rel), true); }
			return false;
		};
		function gotoheight(i) {
			if(options.gotoheight) {
				if(options.axis == 'x') {
					var itm = $(oPages[i]);
				} else {
					var itm = $(oPages[i]).find(".slide-inner");
				}
				var goheight = itm.height();

				oContent.height(goheight);
				oViewport.animate({
					height: goheight
				}, 200);
				
				if(options.slider2 == true){
					$('.client-bar').animate({
						height: goheight
					}, 200);
					$('html, body').animate({scrollTop: ($('#page').height() + goheight)}, 400);
				}
			}
		}
		function getthistitle(i) {
			if(options.slider2==true){
				var i = parseInt(i);
				var itm = $(oPages[i]);
				var oSlider = itm.parent().parent().parent();
				var gettitlelayer = oSlider.parent().find('h3.getthistitle');
				var gettitle = itm.find("span.thisname").html();
				if(gettitle!='') {
				  gettitlelayer.html(gettitle);
				}

				if(options.share==true){
					var getfavlink = oSlider.parent().find('.addtofavorites').removeClass('current');
					var gettwitterlink = oSlider.parent().find('.twitter');
					var getfacebooklink = oSlider.parent().find('.facebook');
					var getfacebooklink = oSlider.parent().find('.facebook');
					var getaddthislink = oSlider.parent().find('.addthis');
					
					var getthisid = itm.find("span.thisid").html();
					var getthisisfav = itm.find("span.thisisfav").html();
					var getfavname = itm.find("span.thisshare").html();
					var getshareurl = itm.find("span.thisshareurl").html();
					
					getfavlink.attr('href','?add='+getfavname+'&addid='+getthisid).attr('rel',i);
					if(getthisisfav=='true') {
						getfavlink.addClass('current');
					}
					getfacebooklink.attr('href','http://www.addthis.com/bookmark.php?s=facebook&url=' + getshareurl);
					gettwitterlink.attr('href','http://www.addthis.com/bookmark.php?s=twitter&url=' + getshareurl);
					getaddthislink.attr('href','http://www.addthis.com/bookmark.php?url=' + getshareurl);
				}
			}
		}
		function resizeevent(i) {
			var oPosition = {};
			$(window).resize(function() {
				iPageSize = bAxis ? $(oPages[0]).outerWidth(true) : $(oPages[0]).outerHeight(true);
				oPosition[bAxis ? 'left' : 'top'] = -(i * (iPageSize * options.display));
				// $("#lbox_headline .getthistitle").html(i + ' ' + iPageSize);
				oContent.css(bAxis ? 'width' : 'height', (iPageSize * oPages.length)).css(oPosition);
				if(options.fullsizeimg==true){
					oBtnPrev.css('height',$(oPages[i]).outerHeight(true));
					oBtnNext.css('height',$(oPages[i]).outerHeight(true));
				}
				setimgsize(i);
			});
		}
		function setimgsize(i) {
			if(options.fullsizeimg==true){
				var w_w  = $(oPages[i]).outerWidth(true),
					w_h  = $(oPages[i]).outerHeight(true),
					r_w  = w_h / w_w,
					i_w  = $(oPages[i]).find('img').attr('width'),
					i_h  = $(oPages[i]).find('img').attr('height'),
					r_i  = i_h / i_w,
					new_w,new_h,
					new_l,new_t;
		
				if (w_h==0) w_h = i_h
				if (r_w < r_i) {
					new_h  = w_h;
					new_w  = w_h / r_i;
					} else {
					new_h  = w_w * r_i;
					new_w  = w_w;
				}
				if(new_w > i_w || new_h > i_h) {
				  new_w = i_w;
				  new_h = i_h;
				}
				new_t = '0';
				if(new_h < w_h) {
					new_t = (w_h - new_h)/2;
				}
				if(!$(oPages[i]).hasClass('noresize')) {
					$(oPages[i]).find('img').css({ 'height': new_h, 'width': new_w, 'margin-top': new_t});
					$(oPages[i]).find('.external-link').css({'width': new_w});
				}
				// oSlider.parent().find(".getthistitle").html(new_w);
				if($(oPages[i]).find('.object_center').hasClass('object_show')) {
					var o_h = $(oPages[i]).find('.object_center').attr('height');
						new_t = (w_h - o_h)/2;
					
					$(oPages[i]).find('.object_center').parent().css({'margin-top':new_t});
				}
			}
		}
		function centerimgwrapper(i) {
			$(oPages[i]).find('.slider-image').each(function(){
				var opheight = oViewport.outerHeight(true);
				if(opheight==0) {
				  opheight = oViewport.css('min-height').replace('px','');
				  opheight = parseInt(opheight);
				}
				var theight = $(oPages[i]).find('.slider-image').find('img').height()+46;
				var mtop = (opheight - theight)/2;
				if(mtop > 0) {
					$(this).css({'margin-top':mtop});
				}
			});
		}
		function setRemove(i) {
			if(oViewport.parent().find('.remove').length > 0) {
				oViewport.parent().find('.remove').attr('href','?remove='+$(oPages[i]).find("span.thisremoveid").html()).attr('rel',i);
			}
			if(oViewport.parent().parent().find('.remove').length > 0) {
				oViewport.parent().parent().find('.remove').attr('href','?remove='+$(oPages[i]).find("span.thisremoveid").html()).attr('rel',i);
			}
		}
		function setTimer(){
			if(options.interval && !bPause){
				clearTimeout(oTimer);
				oTimer = setTimeout(function(){
					iCurrent = iCurrent +1 == iSteps ? -1 : iCurrent;
					bForward = iCurrent +1 == iSteps ? false : iCurrent == 0 ? true : bForward;
					oSelf.move(bForward ? 1 : -1);
				}, options.intervaltime);
			}
		};
		this.stop = function(){ clearTimeout(oTimer); bPause = true; };
		this.start = function(){ bPause = false; setTimer(); };
		this.move = function(iDirection, bPublic){

			iPageSize = bAxis ? $(oPages[0]).outerWidth(true) : $(oPages[0]).outerHeight(true);
			oContent.css(bAxis ? 'width' : 'height', (iPageSize * oPages.length));
			
			if(options.flash==true) {
				clearflash();
				setnewyoutubeobj($(oPages[iCurrent]),'0');
			}
			
			iCurrent = bPublic ? iDirection : iCurrent += iDirection;
			if(iCurrent > -1 && iCurrent < iSteps){

				resizeevent(iCurrent);
				if(options.slider2==true) getthistitle(iCurrent);
				if(options.fullsizeimg==true) setimgsize(iCurrent);
				if(options.centerimgwrapper==true) centerimgwrapper(iCurrent);
				if(options.gotoheight==true) gotoheight(iCurrent);
				if(options.remove==true) setRemove(iCurrent);
				
				var oPosition = {};
				oPosition[bAxis ? 'left' : 'top'] = -(iCurrent * (iPageSize * options.display));	
				oContent.animate(oPosition,{
					queue: false,
					duration: options.animation ? options.duration : 0,
					complete: function(){
						if(typeof options.callback == 'function')
						options.callback.call(this, oPages[iCurrent], iCurrent);
					}
				});
				setButtons();
				setTimer();
			}
		};
		return initialize();
	};
})(jQuery);
