/*P-07/03/2011*/
var NT_carousel = function( args ) {
	this.args = typeof(args)=="object" ? this.args = args : {};
	this.intervalTimer = null; this.videosArray = []; this.videoPlaying = null; this.adF = false; this.adB = false; this.beginOffset = 0;
	this.vPlaying = false;
	this.currentItem = this.previousItem = this.previousItem2 = 0;
	this.transOffset = 1*($$(this.args.selectorString)[0].getStyle("width").replace("px",""))+1*($$(this.args.selectorString)[0].getStyle("marginRight").replace("px",""));
	this.elementsShown = this.args.type == 'transUL' ? ((1*($$(this.args.selectorString.replace(' UL.carousel_items LI', ' LI'))[1].getStyle("width").replace("px","")))/this.transOffset).round() : 1;
	this.updateIMGLabel = this.args.type ==  'main' ? "#CAR_IMGs A.img_" : this.args.type == 'IMGonly' ? "DIV.NOTE_selector UL.SEL_ul LI.SEL_imgCont #CAR_IMGs A.img_" : null;
	this.updateNFOLabel = this.args.type ==  'main' ? "#CAR_nxtPrv LI.CAR_NT_info DIV.NT_info_" : null;
	this.updateOTHLabel = this.args.type ==  'main' ? "#NOTE_carousel #CAR_nxtPrv LI.CAR_NT_rltd DIV.NT_rltd_" : null;
	this.itemsArray = $$(this.args.selectorString);
	this.prevButton = this.args.type ==  'main' ? $$('#CAR_nxtPrv LI.CAR_prvC') : this.args.type ==  'transUL' ? $$(this.args.selectorString.replace('UL.carousel_items LI','LI.prvVideo')) : null;
	if (this.prevButton) {this.prevButton.addEvent('click',function(){this.prevItem()}.bind(this))}
	this.nextButton = this.args.type ==  'main' ? $$('#CAR_nxtPrv LI.CAR_nxtC') : this.args.type ==  'transUL' ? $$(this.args.selectorString.replace('UL.carousel_items LI','LI.nxtVideo')) : null;
	if (this.nextButton) {this.nextButton.addEvent('click',function(){this.nextItem()}.bind(this))}
	if (typeof(this.args.select)!="undefined" && this.args.select == true) {
		$$(this.args.selectorString).each(function(el,n) {
			el.addEvent('click',function(){this.selectItem(n)}.bind(this));
		}.bind(this));
	}
	$$("#CAR_IMGs A IMG").tween('opacity', 0, 0);
	$$("#CAR_IMGs A.img_1 IMG").tween('opacity', 1, 1);
	this.start = function() {
		this.intervalTimer = setInterval( function(){this.nextItem()}.bind(this), this.args.duration);
	}
	this.stopPlaying = function() {
		removeBC_esmasPlayer('mainNoteHomeImage','esmas_bcplayer_info_hide',2);
	}
	this.nextItem = function() {
		if (this.videoPlaying == null) {
			if (this.vPlaying == true) {
				this.stopPlaying();
				this.vPlaying = false;
			}
			if (this.args.type ==  'transUL') {
				if (!(this.currentItem>-1*(this.transOffset*(this.itemsArray.length-this.elementsShown))) && !this.adF) {
					for(var n=0; n<this.elementsShown; n++){
						var copy = $$(this.args.selectorString)[n+(this.adB ? this.elementsShown : 0)].clone();
						$$(this.args.selectorString.replace("LI",""))[0].grab(copy);
						this.videosArray.push(this.videosArray[n+(this.adB ? this.elementsShown : 0)]);
					}
					this.setModalEvents(); this.elementsHTML = ($$(this.args.selectorString.replace(" LI","")).get("html")+'').replace(/(\r\n|\r|\n|\u0085|\u000C|\u2028|\u2029)/g,"").replace(/\t/g,"");
					this.adF = true;
				}
				if ((!(this.currentItem>-1*(this.transOffset*($$(this.args.selectorString).length-(this.elementsShown+1)))) && this.adF)) {
					var f = function(){$$(this.args.selectorString.replace(' LI', '')).setStyle('marginLeft',this.beginOffset); this.currentItem=this.beginOffset}
					f.delay(600,this);
				}
				$$(this.args.selectorString.replace(" LI", "")).tween('marginLeft', this.currentItem, (this.currentItem-this.transOffset));
				this.currentItem = (this.currentItem-this.transOffset);
			} else {
				this.itemsArray[this.currentItem].toggleClass('OPTN_actv');
				this.previousItem = this.currentItem; this.previousItem2 = this.previousItem
				this.currentItem = (this.currentItem + 1)>=this.itemsArray.length ? 0 : (this.currentItem + 1);
				this.itemsArray[this.currentItem].toggleClass('OPTN_actv');
				this.updateMain();
				if (typeof(this.args.autoplay)!="undefined" && this.args.autoplay == true) {clearInterval(this.intervalTimer); this.start()}
			}
		}
	}
	this.prevItem = function() {
		if (this.videoPlaying == null) {
			if (this.vPlaying == true) {
				this.stopPlaying();
				this.vPlaying = false;
			}
			if (this.args.type ==  'transUL') {
				if (this.currentItem>=0 && !this.adB) {
					var auxArr = []; var copyArr = []; var count = 0;
					for(var n=($$(this.args.selectorString).length-(this.elementsShown+(this.adF ? this.elementsShown : 0))); n<$$(this.args.selectorString).length-(this.adF ? this.elementsShown : 0); n++){
						copyArr[count] = $$(this.args.selectorString)[n].clone();auxArr.push(this.videosArray[n]); count++;
					}
					count = copyArr.length-1;
					for (var i in copyArr) {
						if(typeof(copyArr[count])!="undefined"){$$(this.args.selectorString.replace("LI",""))[0].grab(copyArr[count],"top");this.videosArray.unshift(auxArr[count])}
						count--;
					}
					this.setModalEvents(); this.elementsHTML = ($$(this.args.selectorString.replace(" LI","")).get("html")+'').replace(/(\r\n|\r|\n|\u0085|\u000C|\u2028|\u2029)/g,"").replace(/\t/g,"");
					this.beginOffset = -1*this.elementsShown*this.transOffset; this.currentItem=this.beginOffset
					$$(this.args.selectorString.replace(' LI', '')).setStyle('marginLeft',this.beginOffset);
					this.adB = true;
				}
				if(this.currentItem==(-1*this.transOffset)&& this.adB) {
					var f = function(){
						$$(this.args.selectorString.replace(' LI', '')).setStyle('marginLeft',-1*this.transOffset*($$(this.args.selectorString).length-(this.elementsShown+(this.adF ? this.elementsShown : 0)))); 
						this.currentItem=-1*this.transOffset*($$(this.args.selectorString).length-(this.elementsShown+(this.adF ? this.elementsShown : 0)));
					}
					f.delay(600,this);
				}
				$$(this.args.selectorString.replace(" LI", "")).tween('marginLeft', this.currentItem, (this.currentItem+this.transOffset));
				this.currentItem = (this.currentItem+this.transOffset);
			} else {
				this.itemsArray[this.currentItem].toggleClass('OPTN_actv');
				this.previousItem = this.currentItem; this.previousItem2 = this.previousItem
				this.currentItem = (this.currentItem - 1) < 0 ? (this.itemsArray.length-1) : (this.currentItem - 1);
				this.itemsArray[this.currentItem].toggleClass('OPTN_actv');
				this.updateMain();
				if (typeof(this.args.autoplay)!="undefined" && this.args.autoplay == true) {clearInterval(this.intervalTimer); this.start()}
			}
		}
	}
	this.selectItem = function(n) {
		this.itemsArray[this.currentItem].toggleClass('OPTN_actv');
		this.previousItem2 = this.currentItem; this.currentItem = n;
		this.previousItem = (this.currentItem - 1) < 0 ? (this.itemsArray.length-1) : (this.currentItem - 1);
		this.itemsArray[this.currentItem].toggleClass('OPTN_actv');
		this.updateMain();
		if (typeof(this.args.autoplay)!="undefined" && this.args.autoplay == true) {clearInterval(this.intervalTimer); this.start()}
	}
	this.updateMain = function() {
		if (this.updateIMGLabel) {
			if (this.previousItem2 != this.previousItem) {
				$$(this.updateIMGLabel.replace(".img_","")+" IMG").fade('out');
				$$(this.updateIMGLabel+(this.currentItem+1)+" IMG").fade('in');
			} else {
				$$(this.updateIMGLabel+(this.previousItem+1)+" IMG").fade('out');
				$$(this.updateIMGLabel+(this.currentItem+1)+" IMG").fade('in');
			}
		}
		if (this.updateNFOLabel) {
			$$(this.updateNFOLabel+(this.previousItem+1)).morph({marginTop: 82, opacity: 0});
			$$(this.updateNFOLabel+(this.currentItem+1)).morph({marginTop: 0, opacity: 1});
		}
		if (this.updateOTHLabel) {
			$$(this.updateOTHLabel+(this.previousItem+1)).morph({marginTop: 82, opacity: 0});
			$$(this.updateOTHLabel+(this.currentItem+1)).morph({marginTop: 0, opacity: 1});
		}
	}
	this.setModalEvents = function() {
		$$(this.args.selectorString+" A").each(function(el,n) {
			el.removeEvents(); el.addEvent('click', function(){this.openModal(Math.floor(n/3))}.bind(this));
		}.bind(this));
	}
	this.setVideoPlaying = function() {
		$$("#"+this.modContView+" LI.CARSL_close IMG").addEvent("click", function(){this.closeModal()}.bind(this));
	}
	this.openModal = function(n) {
		if (typeof(this.modContView)!="undefined") {
			fstElemnt = (-1*this.currentItem/this.transOffset);fstElemntre = "(<li>((.(?!(</li>)))*)[^<]</li>){"+fstElemnt+"}"; var re = new RegExp(fstElemntre, "i");
			output = this.elementsHTML.replace(re, ""); fstElemntre = "(<li>((.(?!(</li>)))*)[^<]</li>){"+(this.elementsShown)+"}"; re = new RegExp(fstElemntre, "i"); 
			var m = re.exec(output);$$(this.args.selectorString.replace(" LI",""))[0].set("html",m[0]);$$(this.args.selectorString.replace(" LI",""))[0].setStyle("margin-left","0");
			$(this.modCont).toggleClass("show"); $$(this.args.selectorString)[( n-fstElemnt )].toggleClass("itm_active"); this.videoPlaying = n;
			$$(this.args.selectorString.replace(" LI", "")).setStyle("width", "auto");
			if ((/\bMSIE 6/.test(navigator.userAgent) && !window.opera) || (/\bMSIE 7/.test(navigator.userAgent) && !window.opera)) {
				$$(this.args.selectorString.replace(" UL.carousel_items LI"," LI"))[1].setStyle("overflow","visible");
				$$(this.args.selectorString)[( n-fstElemnt )].setStyle("width","241px");
			}
			vidReqOptions = {
				method:'get', 
				url: this.videosArray[n],
				onRequest: function() {$(this.modContView).set('html', '<div id="overlay_bg"></div><ul><li style="width:790px; text-align:center; padding-top:120px">CARGANDO...</li><li class="CARSL_close"><a href="#"><img src="img/spacer.gif"></a></li></ul>');}.bind(this),
				onSuccess: function(texto, xmlrespuesta){$(this.modContView).set('html', texto); this.setVideoPlaying();}.bind(this), 
				onFailure: function(e){alert('Error al buscar el video. '+e.statusText);}
			};
			var videoRequest = new Request(vidReqOptions).send();
		}
	}
	this.closeModal = function() {
		if ((/\bMSIE 6/.test(navigator.userAgent) && !window.opera) || (/\bMSIE 7/.test(navigator.userAgent) && !window.opera)) {
			auxIE6width = $$(this.args.selectorString.replace(" UL.carousel_items LI"," LI"))[1].getStyle("width");
			auxIE6height = $$(this.args.selectorString.replace(" UL.carousel_items LI"," LI"))[1].getStyle("height");
		}
		$$(this.args.selectorString.replace(" LI",""))[0].set("html",this.elementsHTML);
		$$(this.args.selectorString.replace(" LI",""))[0].setStyle("margin-left",this.currentItem+"px");
		$(this.modCont).toggleClass("show"); $(this.modContView).set('html', "");
		$$(this.args.selectorString.replace(" LI", "")).setStyle("width", (this.itemsArray.length+8)*this.transOffset);
		if ((/\bMSIE 6/.test(navigator.userAgent) && !window.opera) || (/\bMSIE 7/.test(navigator.userAgent) && !window.opera)) {
			$$(this.args.selectorString.replace(" UL.carousel_items LI"," LI"))[1].setStyle("width",auxIE6width);
			$$(this.args.selectorString.replace(" UL.carousel_items LI"," LI"))[1].setStyle("height",auxIE6height);
			$$(this.args.selectorString.replace(" UL.carousel_items LI"," LI"))[1].setStyle("overflow","hidden");
		}
		this.setModalEvents(); this.videoPlaying = null;
	}
	if (typeof(this.args.autoplay)!="undefined" && this.args.autoplay == true) {this.start()}
	if (this.args.type ==  'transUL') {
		this.setModalEvents(); this.elementsHTML = ($$(this.args.selectorString.replace(" LI","")).get("html")+'').replace(/(\r\n|\r|\n|\u0085|\u000C|\u2028|\u2029)/g,"").replace(/\t/g,"");
		$$(this.args.selectorString.replace(" LI", "")).setStyle("width", (this.itemsArray.length+8)*this.transOffset);
	}
	this.Stop = function() {
        this.vPlaying = true;
		hideNavigation();
		clearInterval(this.intervalTimer);
		this.intervalTimer = null;
	}
}
