<%# encoding: utf-8 %>
$.extend($.expr[':'], {
	'containsi': function (elem, i, match, array) {
		return (elem.textContent || elem.innerText || '').toLowerCase().indexOf((match[3] || "").toLowerCase()) >= 0;
	}
});
var galleryAPI = function(){
	g = this;
	this.urlVars = rcom.getUrlVars();
	this.albumArea = $("#orbit_gallery");
	this.loadArea = null;
	this.tagList = $(".taglist ul");
	// this.authenticated = false;
	this.initialize = function(callbackFn){
		$(document).ready(function(){
			//bindHandlers();
		})
		var bindHandlers = function(){
			
		}
	}
	
	this.loadAlbums = function(id){
		g.albumArea.find(".rghead .rgfn a").click(function(){g.makeNewAlbum($(this).attr("href"))});
		g.albumArea.addClass('o_gallery');
		g.albumArea.find("#imgholder").empty();
		if(id!="all"){
			var cids = [];
			var tids = [];
			var href = null;
		    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
		    for(var i = 0; i < hashes.length; i++){
		        hash = hashes[i].split('=');
		        if(hash[0]=="cat"){
		        	cids.push(hash[1]);
		        	href = "cat="+hash[1];
		    	}else if(hash[0]=="tag"){
		    		tids.push(hash[1]);
		    		href = "tag="+hash[1];
		    	}
		        $("#filter .filter_btns a[href='"+href+"']").addClass("active");
		    }
		    if(cids.length == 0)
		    		cids = "all";
		}else{
			var cids = "all";
		}
		$.getJSON("/panel/gallery/front_end/get_albums",{"cid":cids,"tid":tids,"inner":true},function(categories){
			$.each(categories,function(x,category){
				$.each(category,function(i,album){
					album.description = (album.description?album.description:"<%= I18n.t('gallery.no_description') %>")
					if(album.cover == "default")
						var $img = $("<div class='rgalbum'><a title='"+album.description+"' href='"+album.show_path+"'><img src='../../../assets/gallery/default.jpg'/></a><span class='albumname'><a title='"+album.description+"' href='?album="+album._id+"'>"+album.name+"</a></span><span class='categoryname'><i class='icon-folder-close'></i> "+album.category_name+"</span><span class='tagnames'></span></div>");
					else
						var $img = $("<div class='rgalbum'><a title='"+album.description+"' href='"+album.show_path+"'><img src='"+album.cover_path+"'/></a><span class='albumname'><a title='"+album.description+"' href='?album="+album._id+"'>"+album.name+"</a></span><span class='categoryname'><i class='icon-folder-close'></i> "+album.category_name+"</span><span class='tagnames'></span></div>");
					var tag_string = "";
					for(tag in album.tag_names){
						if(tag == album.tag_names.length-1)
							tag_string+= '<span class="tags">' + album.tag_names[tag] + '</span>';
						else
							tag_string+= '<span class="tags">' + album.tag_names[tag] + ', </span>';
					}
					$img.find(".tagnames").html(tag_string);
					g.albumArea.find("#imgholder").append($img);
				})
			})
		})
	}
	this.loadImages = function(id){
		g.albumArea.addClass('o_album');
		g.albumArea.find("#imgholder").empty();
		$.getJSON("panel/gallery/front_end/albums/"+id+"/imgs",{inner:true},function(album){
			$.each(album.images,function(i,image){
				var thumb = image.file.thumb.url;
				image.description = (image.description?image.description:"<%= I18n.t('gallery.no_description') %>")
				var $img = $("<div class='rgphoto'><a title='"+image.description+"' href='"+image.theater_link+"'><img src='"+thumb+"'/></a></div>");
				g.albumArea.find("#imgholder").append($img);
			})
		})
	}
	this.loadTheater = function(id){
		var imageArray;
		var imagecount = 0;
		// var picHeight = 0;
		var bindHandlers = function(){
			var $slidelist = $('.slidelist'),
				$rslide = $('.rslide'),
				$rslidenav = $('.rslidenav'),
				$img = $("#main_pic img"),
				wHeight = $(window).height();

				$("#main_pic").height("auto");
				$img.css("height","auto");
			
			var updatePhotoTag = function(){
				var tagnames = imageArray[imagecount].tag_names;
				g.tagList.empty();
				for(tag in tagnames){
					g.tagList.append("<li>"+tagnames[tag]+"</li>");
				}
				
			}
			
			
			$(".slidectrl a.togglelist").click(function(){
				var rslide_h = $rslide.outerHeight();
				if ( $slidelist.height() < 1 ){
					$slidelist.stop().animate({'height':rslide_h - 30}, 300);
					$slidelist.find("ul").show();
				} else {
					$slidelist.stop().animate({'height':'0px'}, 300);
					$slidelist.find("ul").hide();
				}
			})
			$(".slidectrl .togglescreen").click(function(){
				toggleFullscreen();
			})
			$(".slidectrl .browserfullscreen").click(function(){
				browserFullScreen();
			})
			$slidelist.find(".list_element").click(function(){
				imagecount = parseInt($(this).parent().attr("for"));
				changeImage($(this));
			})
			$rslidenav.find(".navN").click(function(){
				nextpic($(this));
			})
			$rslidenav.find("a.navP").click(function(){
				prevpic($(this));
			})
			$("#nextpic").click(function(){
				nextpic($(this));
			})

			$(document).keydown(function(e){
			    if (e.keyCode == 37){ 
			    	prevpic($rslidenav.find(".navP"));
			       return false;
			    }
			    if(e.keyCode == 39){
			    	nextpic($rslidenav.find(".navN"));
			    	return false;
			    }
			    if(e.keyCode == 27){
			    	toggleFullscreen();
			    	$rslide.removeClass('browserFullScreen');
			    	return false;
			    }
			    if(e.keyCode == 70 || e.keyCode == 102){
			    	if($rslide.hasClass("fullscreen"))
			    	 browserFullScreen();
			    }
			});
			var browserFullScreen = function(){
				var el = document.documentElement,
      				rfs = el.requestFullScreen || el.webkitRequestFullScreen || el.mozRequestFullScreen;
    			rfs.call(el);
			}
			$(window).resize(function(){
				if(window.innerWidth == screen.width && window.innerHeight == screen.height) {
					$rslide.addClass('browserFullScreen');
				} else {
					$rslide.removeClass('browserFullScreen');
				}
			})
			var nextpic = function(dom){
				if(imagecount == imageArray.length - 1)
					imagecount = 0;
				else
					imagecount++;
				changeImage(dom);
			}
			var prevpic = function(dom){
				if(imagecount == 0)
					imagecount = imageArray.length - 1
				else
					imagecount--;
				changeImage(dom);
			}
			var toggleFullscreen = function(){
				if($rslide.hasClass("fullscreen")){
					$rslide.css("position","relative");
					$rslide.removeClass("fullscreen");
					$(".slidectrl .browserfullscreen").hide();
					$img.css({"padding":""})
					$("#main_pic").height("auto");
				}else{
					$("#main_pic").height(wHeight);
					$img.css("height","auto");
					$rslide.css("position","");
					$rslide.addClass("fullscreen");
					$img.css({"padding":($rslide.height()-$img.height())/2})
					$(".slidectrl .browserfullscreen").show();
				}
			}
			var changeImage = function(dom){
				var pageurl = dom.attr('href');

				$img.fadeOut(200,function(){
					if($rslide.hasClass("fullscreen"))
						$img.attr({"src":dom.attr("data-content")}).css("height","100%");
					else
						$img.attr({"src":dom.attr("data-content")}).css("height","auto");
					$img.fadeIn(200);
	 				if($rslide.hasClass("fullscreen")){
	 					$img.css({"padding":($rslide.height()-$img.height())/2})
	 				}
	 				$slidelist.stop().animate({'height':'0px'}, 300);
					$slidelist.find("ul").hide();
					if(rcom.getInternetExplorerVersion() == -1){

					    if(pageurl!=window.location){
					    	pageurl = "gallery?id="+pageurl+"&controller_action=theater";
					      window.history.pushState({path:pageurl},'',pageurl);
					    }
					}else{
				   		window.location.hash = pageurl;
				   	}
				    // $(".slideinfo b.info").text(imageArray[imagecount].title);
				    	updatePhotoTag();
				    $(".slideinfo .info").text(imageArray[imagecount].description);
				    if(imageArray.length > 1)
				    	updateNavigation();
			    })

			}
			var updateNavigation = function(){
				var next, prev;
				if(imagecount == 0){
					next = 1;
					prev = imageArray.length - 1; 
				}else if(imagecount == imageArray.length - 1){
					next = 0
					prev = imageArray.length - 2;
				}else{
					next = imagecount + 1;
					prev = imagecount - 1;
				}
				$(".navN").attr({"href":imageArray[next]._id,"data-content":imageArray[next].file.url});
				$(".navP").attr({"href":imageArray[prev]._id,"data-content":imageArray[prev].file.url});
			};
			if(imageArray.length > 1)
				updateNavigation();
		}
		
		var preparestage = function(albumid){
			$.getJSON("panel/gallery/front_end/albums/"+albumid+"/imgs",{"inner":true},function(album){
				imageArray = eval(album.images);
				$.each(album.images,function(i,image){
					if(image._id == id)
						imagecount = i;
				})
				bindHandlers();
			})
		}
		if(rcom.getInternetExplorerVersion()!=-1){
		 	var url_constructor = window.location.href.split("/");
			var url = "";
			for(x=0;x<=url_constructor.length-1;x++){
				if(x == url_constructor.length-1){
					url+="#!/"+url_constructor[x];
				}else{
			   		url+=url_constructor[x]+"/";
			   	}
			 }
			 
			 window.location.href = url;
		}
		
		var albumid = $("#main_pic").attr("data-content");
		g.albumArea.css("margin-bottom","0");
		// picHeight = $(window).height() - ($("#orbit-bar").outerHeight() + $("#orbit_gallery .form-actions").outerHeight());
		preparestage(albumid);
		
	}

}
galleryAPI.prototype.locale = "en";
