/**
 * @author Nivaria Innova Team
 */
function CarouselAlbum() {
	this.id = null;
	this.simplefields = new Array();	//Simple fields
	this.items = new Array();			//Items of Medialibrary
	this.carousel = null;				//Reference to carousel object instance
	this.itemsMap = new Array();		//Relate index and id of item
	this.subcategoryId = null;			//Subcategory selected by user
}

CarouselAlbum.prototype = {
	initialize: function(objectId) {
		this.id = objectId;
		//Getting component simple fields values
		ncm.initSimpleFields(CarouselAlbum,this);
		//Init Carousel
		this.initCarousel();
		//Init subcategory filter
		setTimeout("CarouselAlbum.get("+this.id+").initSubcategory()",500);
	},
	
	initEvents: function() {
		//Initialize events on the jCarousel
		var selector = this.getCarouselSelector();
		selector += " li";
		var Me = this;
		if(this.simplefields["PREVIEW_AS_POPUP"]) {
			$jq(selector).mouseover(function(eventObj){
				var mediaId = $jq("img",this).attr("id");
				Me.createPreview(mediaId);
				var x = eventObj.clientX;
				var y = eventObj.clientY;
				var w = $jq(Me.getPreviewSelector()).parent().width();
				var w_w = $jq(window).width();
				if(x+w>w_w-100) x -= x+w-w_w+100; 
				$jq(Me.getPreviewSelector()).parent().css("left",x+"px").css("top",y+"px").show();
			});
		} else {
			$jq(selector).click(function(){
				var mediaId = $jq("img",this).attr("id");
				Me.createPreview(mediaId);
				Me.initPagerByMedia(mediaId); 
			}).keypress(function(){
				var mediaId = $jq("img",this).attr("id");
				Me.createPreview(mediaId);
				Me.initPagerByMedia(mediaId);
			});	
		}
	},
	
	initCarousel: function() {
		var isVertical = this.simplefields["VERTICAL"]?this.simplefields["VERTICAL"]:false;
		var scrollStep = this.simplefields["SCROLL_STEP"]?parseInt(this.simplefields["SCROLL_STEP"]):3;
		var animationSpeed = this.simplefields["ANIMATION"]?this.simplefields["ANIMATION"]:"fast";
		var m_reg = /[0-9]+/g;
		if(animationSpeed.match(m_reg)) animationSpeed = parseInt(animationSpeed);
		var scrollAuto = this.simplefields["SCROLL_AUTO"]?parseInt(this.simplefields["SCROLL_AUTO"]):0;
		var wrapping = this.simplefields["WRAP"]?this.simplefields["WRAP"]:"null";
		if(wrapping=="null") wrapping=null;
		var Me = this;
		$jq(this.getCarouselSelector()).jcarousel({
			vertical: isVertical,
			scroll: scrollStep,
			animation: animationSpeed,
			auto: scrollAuto,
			wrap: wrapping,
			itemLoadCallback: function(carousel,state) {
				Me.carousel = carousel;
				// Check if the requested items already exist
    			if (carousel.last>1 && carousel.has(carousel.first-1, carousel.last-1)) {
        			return;
    			}
				var jspfile = CarouselAlbum.getProperty("jsp_default");
				if(jspfile==null) jspfile="components/albums/carousel/CarouselAlbum.jsp";
				var sel1 = Me.getPreviewSelector();
				var w = $jq(sel1).width();
				if(!w) w = parseInt(CarouselAlbum.getProperty("size_width_default"));
				var h = $jq(sel1).height();
				if(!h) h = parseInt(CarouselAlbum.getProperty("size_height_default"));
				if(!Me.simplefields["VERTICAL"]) {
					h = (w*2-w*2%3)/3;
				}
				var data = {
					action: 0,
					objId: Me.id,
					issueId: CarouselAlbum.issueId,
					language: CarouselAlbum.language,
					channel: CarouselAlbum.channel,
					isPreview: CarouselAlbum.inPreview,
					width: w,
					height: h 
				};
				if(Me.subcategoryId!=null) {
					$jq.extend(data,{subcategoryId:Me.subcategoryId});
				}
				$jq.post(jspfile,data,function(json){
					carousel.size(json.length);
					Me.items = new Array();
					Me.itemsMap = new Array();
					for(var i=0;json!=null && i<json.length;i++) {
						var url = "";
						var alt = "";
						var title = "";
						switch(json[i].type) {
							case 0:		//Image
							case 1:		//WebCam
								url = json[i].url;
								alt = json[i].alt;
								title = json[i].title;
								break;
							case 2:		//Video
							case 3:		//Flash
								url = json[i].imageUrl;
								alt = json[i].imageAlt;
								title = json[i].imageTitle;
								break;
						}
						var html = "<img src=\""+url+"\" width=\"75\" height=\"75\" alt=\""+alt+"\" title=\""+title+"\" id=\""+json[i].ID+"\"/>";
						carousel.add(i,html);
						Me.items[""+json[i].ID] = json[i];
						Me.itemsMap[i] = json[i].ID;
					}
					if(json!=null && json.length>0) {
						Me.createPreviewForPage(1);
						//$jq(sel1).html(Me.getPreviewHTML(json[0]));
						Me.setSelectedItem(0);
						Me.initPager(0,json.length);
					}
					Me.initEvents();
				},"json");
			}
		});
	},
	
	getPreviewHTML: function(jsonItem) {
		var res = "";
		if(jsonItem!=null) {
			res += jsonItem.preview;
			if(this.simplefields["SHOW_COMMENT"] && jsonItem.DESCRIPTION && $jq.trim(jsonItem.DESCRIPTION)!="") {
				res += "<div class=\"jcarousel-preview-comment\">";
				res += jsonItem.DESCRIPTION;
				res += "</div>";
			}
			if(this.simplefields["SHOW_AUTHOR"] && jsonItem.AUTHOR && $jq.trim(jsonItem.AUTHOR)!="") {
				res += "<div class=\"jcarousel-preview-author\">";
				res += CarouselAlbum.getProperty("lang_author")
				res += ": ";
				res += jsonItem.AUTHOR;
				res += "</div>";
			}
			if(this.simplefields["SHOW_DOWNLOAD"] && jsonItem.downloadUrl && $jq.trim(jsonItem.downloadUrl)!="") {
				res += "<div class=\"jcarousel-preview-download\">";
				res += "<a href=\"noscript.html\" onclick=\"CarouselAlbum.get(";
				res += this.id;
				res += ").download(";
				res += jsonItem.ID;
				res += "); return false;\" onkeypress=\"CarouselAlbum.get(";
				res += this.id;
				res += ").download(";
				res += jsonItem.ID;
				res += "); return false;\" title=\"";
				res += CarouselAlbum.getProperty("lang_download");
				res += "\">";
				res += CarouselAlbum.getProperty("lang_download");
				res += " <img src=\"";
				res += CarouselAlbum.getProperty("img_download");
				res += "\" class=\"download-icon\" alt=\"";
				res += CarouselAlbum.getProperty("lang_download");
				res += "\" title=\"";
				res += CarouselAlbum.getProperty("lang_download");
				res +="\" /></a>";
				res += "</div>";
			}
		}
		return res;
	},
	
	getPreviewSelector: function() {
		var res = "#carouselalbum";
		res += this.id;
		res += " .jcarousel-preview-";
		res += this.simplefields["VERTICAL"]?"vertical":"horizontal";
		res += " div.jcarousel-preview-container";
		return res;
	},
	
	getCarouselSelector: function() {
		var selector = "#carousel";
		selector += this.id;
		return selector;
	}, 
	
	download: function(mediaId) {
		if(this.items[""+mediaId]) {
			ncm.openWindow(this.items[""+mediaId].downloadUrl);
		}	
	},
	
	createPreview: function(mediaId) {
		var selector = this.getCarouselSelector();
		selector += " li";
		if(this.items[mediaId]) {
			$jq(this.getPreviewSelector()).html(this.getPreviewHTML(this.items[mediaId]));
			$jq(selector).removeClass("jcarousel-item-selected");
			$jq(selector+" img#"+mediaId).parent().addClass("jcarousel-item-selected");
			this.initPreviewButtons(mediaId);
		}
		$jq(".jcarousel-preview-close",$jq(this.getPreviewSelector()).parent()).mouseover(function(){
			$jq(this).addClass("jcarousel-preview-close-hover");
		}).mouseout(function(){
			$jq(this).removeClass("jcarousel-preview-close-hover");
		}).click(function(){
			$jq(this).parent().hide();
		});
	},
	
	createPreviewForPage: function(page) {
		this.createPreview(this.itemsMap[page-1]);
	},
	
	setSelectedItem: function(index) {
		var selector = this.getCarouselSelector();
		selector += " li";
		$jq(selector).removeClass("jcarousel-item-selected").eq(index).addClass("jcarousel-item-selected");
	},
	
	initPager: function(current,total) {
		if(!this.simplefields["SHOW_PAGER"]) return;
		var selector = "#pager";
		selector += this.id;
		var style = this.simplefields["PAGER_STYLE"];
		$jq(selector).jpager({
			current: current,
			total: total,
			style: style,
			prevText: CarouselAlbum.getProperty("lang_previous"),
			nextText: CarouselAlbum.getProperty("lang_next")
		});
		//Initialize events for pager
		var Me = this;
		var sel2 = selector;
		sel2 += " ul li a";
		$jq(sel2).each(function(i){
			var page = ncm.intval($jq(this).text());
			if(page>0) {
				$jq(this).bind("click",function(eventObj){
					Me.carousel.scroll(page);
					Me.setSelectedItem(page-1);
					Me.createPreviewForPage(page);
					Me.initPager(page-1,total);		
				});	
			} else {
				page = ncm.intval($jq("li.active",$jq(this).parent().parent()).text());
				if(i==0) {
					if(page>1) {
						$jq(this).bind("click",function(eventObj){
							Me.carousel.prev();
							Me.setSelectedItem(page-2);
							Me.createPreviewForPage(page-1);
							Me.initPager(page-2,total);		
						});
					}
				} else {
					if(page<total) {
						$jq(this).bind("click",function(eventObj){
							Me.carousel.next();
							Me.setSelectedItem(page);
							Me.createPreviewForPage(page+1);
							Me.initPager(page,total);		
						});
					}
				}				
			}
		});
	},
	
	initPagerByMedia: function(mediaId) {
		var total = this.carousel.size();
		var current = $jq.inArray(mediaId,this.itemsMap);
		if(current<0) current=0;
		this.initPager(current,total);	
	},
	
	initPreviewButtons: function(mediaId) {
		var total = this.carousel.size();
		var current = $jq.inArray(mediaId,this.itemsMap);
		if(current<0) current=0;
		var Me = this;
		var jObjP = $jq(".jcarousel-prev",$jq(Me.getPreviewSelector()).parent());
		var jObjN = $jq(".jcarousel-next",$jq(Me.getPreviewSelector()).parent());
		jObjP.unbind("click");
		jObjN.unbind("click");
		if(current==0) {
			jObjP.addClass("jcarousel-prev-disabled").addClass("jcarousel-prev-disabled-"+(Me.simplefields["VERTICAL"]?"vertical":"horizontal")).css("display","block");
		} else {
			jObjP.removeClass("jcarousel-prev-disabled").removeClass("jcarousel-prev-disabled-"+(Me.simplefields["VERTICAL"]?"vertical":"horizontal")).css("display","block").one("click",function(){
				Me.carousel.prev();
				Me.setSelectedItem(current-1);
				Me.createPreviewForPage(current);
				Me.initPager(current-1,total);
			});
		}
		if(current==total-1) {
			jObjN.addClass("jcarousel-next-disabled").addClass("jcarousel-next-disabled-"+(Me.simplefields["VERTICAL"]?"vertical":"horizontal")).css("display","block");
		} else {
			jObjN.removeClass("jcarousel-next-disabled").removeClass("jcarousel-next-disabled-"+(Me.simplefields["VERTICAL"]?"vertical":"horizontal")).css("display","block").one("click",function(){
				Me.carousel.next();
				Me.setSelectedItem(current+1);
				Me.createPreviewForPage(current+2);
				Me.initPager(current+1,total);
			});			
		}
	},
	
	initSubcategory: function(){
		var jspfile = CarouselAlbum.getProperty("jsp_default");
		if(jspfile==null) jspfile="components/albums/carousel/CarouselAlbum.jsp";
		var data = {
			action: 1,
			objId: this.id,
			issueId: CarouselAlbum.issueId,
			language: CarouselAlbum.language,
			channel: CarouselAlbum.channel,
			isPreview: CarouselAlbum.inPreview
		};
		var Me = this;
		$jq.post(jspfile,data,function(json){
			if(json!=null && json.hasFilter) {
				var html = "<div class=\"jcarousel-subcategory\" id=\"subcategory"+Me.id+"\">\n";
				html += "<div class=\"mod001\">\n";
				html += "<div class=\"title-bar\">\n";
				if($jq.trim(json.title)!="") {
					html += "<h3>";
					html += json.title;
					html += "</h3>";	
				}
				html += "</div>\n";
				html += "<div class=\"jcarousel-subcategory-content\">\n";
				html += json.html.replace(/QueryResultsAjax./ig,"CarouselAlbum.");
				html += "</div>\n";
				html += "</div>\n";
				html += "</div>";
				
				if(json.position==1) {
					var sel = "#carouselalbum";
					sel += Me.id;
					$jq(sel).prepend(html);
				} else if(json.position==2) {
					var sel = "#carouselalbum";
					sel += Me.id;
					$jq(sel).append(html);
				} else if(json.position==3) {
					var sel = "#left-content";
					$jq(sel).append(html);
				} else if(json.position==4) {
					var sel = "#right-content";
					$jq(sel).prepend(html);
				}
			} 
		},"json");
	},
	
	filterCategory: function(catId,domElem) {
		this.subcategoryId = catId;
		if(this.carousel!=null) {
			this.carousel.reset();
			for(var i=this.carousel.size()-1;i>=0;i--){
				this.carousel.remove(i);
			} 
		}
		this.initCarousel();
		var selector = "#subcategory";
		selector += this.id;
		selector += " .jcarousel-subcategory-content ul li a.category-selected";
		$jq(selector).removeClass("category-selected");
		$jq(domElem).addClass("category-selected");
	}
};

CarouselAlbum.instances = new Array();
CarouselAlbum.properties = new Array();
CarouselAlbum.datatype = null;
CarouselAlbum.jsp_utils = null;
CarouselAlbum.language = null;
CarouselAlbum.pageUrl = null;
CarouselAlbum.channel = null;
CarouselAlbum.inPreview = false;
CarouselAlbum.issueId = null;
CarouselAlbum.skin = null;

CarouselAlbum.register = function(objectId) {
	var aObject = new CarouselAlbum();
	aObject.initialize(objectId);
	CarouselAlbum.instances[""+objectId] = aObject;
}

CarouselAlbum.getRegistered = function(objectId) {
	return CarouselAlbum.instances[""+objectId];
}

CarouselAlbum.get = function(objectId) {
	return CarouselAlbum.getRegistered(objectId);
}

CarouselAlbum.initProperties = function(options) {
	ncm.initProperties(CarouselAlbum,options);
}

CarouselAlbum.getProperty = function(propName) {
	var res = CarouselAlbum.properties[propName];
	if(res==null) res="";
	if(typeof(res)=="undefined") res="";
	return res;
}

