﻿

		// if there is a hash redirect to that video ID
		function redirectHash(){
			var hash = location.hash.replace("#/", "");
			if(hash){
				var dl = document.location;
				var newLocation = dl.protocol + "//" + dl.hostname + dl.pathname + "?" + hash;
				window.location = newLocation;
			}
		}
		redirectHash();

		var paginator = {
			initialize: function(){
				this.pageList = $$("ul.paginator");
				
				// for most recent videos
				if(this.pageList.length <= 0){
					return;
				}
				
				this.contentArea = $("page_content");
				this.pageLinks = $$("ul.paginator a");
				this.prev = $$("a.prev");
				this.next = $$("a.next");
				this.attachEventListeners();
				this.newHTML = "";
				this.query = document.location.search.parseQuery();
				
				this.setActiveLink(this.pageLinks[0]);
			},
			attachEventListeners: function(){
				this.pageLinks.invoke("observe", "click", this.linkClick.bindAsEventListener(this));
				this.prev.invoke("observe", "click", this.prevClick.bindAsEventListener(this));
				this.next.invoke("observe", "click", this.nextClick.bindAsEventListener(this));
			},
			linkClick: function(e){
				e.stop();
				var clickedLink = e.element();
				this.requestNewData(clickedLink.rel);
				this.setActiveLink(clickedLink);
			},
			prevClick: function(e){
				if(this.currentPage > 1){
					var prevPage = this.pageLinks[this.currentPage-2];
					this.requestNewData(prevPage.rel);
					this.setActiveLink(prevPage);				
				}
			},
			nextClick: function(e){
				if(this.currentPage < (this.pageLinks.length/2)){
					var nextPage = this.pageLinks[this.currentPage];
					this.requestNewData(nextPage.rel);
					this.setActiveLink(nextPage);				
				}			
			},
			requestNewData: function(pageNum){
				var parametersObj = { page: pageNum}
				if (this.query["fid"]){
					parametersObj.fid = this.query["fid"]
				} else if (this.query["cid"]){
					parametersObj.cid = this.query["cid"]
				} else if (this.query["search"]){
					parametersObj.search = this.query["search"]
				}
				
				var ajax = new Ajax.Request('_VideoListerPager.aspx', {
					method: 'get',
					evalJSON: 'force',
					parameters: parametersObj,
					onCreate: function(){
						//this.showLoadingIndicator();
					},
  					onSuccess: function(response) {
      					var json = response.responseJSON;
      					for (video in json){
      						this.generateHTML(json[video]);
      					}
      					this.replaceData();
  					}.bind(this),
  					onFailure: function(){
  						console.log("failure");	
  					}
				});
			},
			/*
			showLoadingIndicator: function(){
				console.log("loading");
			},
			hideLoadingIndicator: function(){
				console.log("hiding");	
			},
			*/
			generateHTML: function(obj){
				var tempHTML = new Template('\
				<li class="video_result">\
					<div class="image">\
	    				<a rel="#{url},#{mainimage},true,#{id}" href="#">\
	    					<img border="0" src="#{thumbnail}" />\
	    				</a>\
					</div>\
    				<div class="sifftv_data">\
        				<h5>\
        					<a rel="#{url},#{mainimage},true,#{id}" href="#" class="videoTitle">#{title}</a>\
        				</h5>\
        				<p>#{duration}</p>\
        				<p class="videoDesc">\
        					#{description}\
        				</p>\
    				</div>\
    				<div class="clear"></div>\
				</li>');
				this.newHTML += tempHTML.evaluate(obj);
			},
			replaceData: function(){
				this.contentArea.update(this.newHTML);
				this.newHTML = "";
				siffTV.attachEventListeners();
			},
			setActiveLink: function(link){
				this.pageLinks.each(function(currentLink){
					if(currentLink.rel != link.rel) {
						currentLink.removeClassName("active").addClassName("inactive");
					} else {
						currentLink.removeClassName("inactive").addClassName("active");
					}
				});
				this.currentPage = parseInt(link.rel);
			}
		}   
        var siffTV = {
			initialize: function(options){
				this.HTML5 = !swfobject.hasFlashPlayerVersion("9")
				this.videoTitleNode = $("videoTitle");
				this.videoDescNode = $("videoDesc");
				this.attachEventListeners();
				if(!this.HTML5){
					this.embedFlash(options.videoURL, options.imageURL, options.autoPlay);
				} else {
					this.embedHTML5(options.videoURL, options.imageURL, options.autoPlay);
				}
			},
			loadNewVideo: function(options){
				this.embedFlash(options.videoURL, options.imageURL, options.autoPlay);
				this.updateHash(options.videoID);
				this.updateTitleDescription(options.videoTitle, options.videoDescription);
				//facebookLike.updateLikeButton();
				this.scrollToTop();
			},
			scrollToTop: function(){
				Effect.ScrollTo('content_layout');
			},
			updateTitleDescription: function(title, desc){
				this.videoTitleNode.update(title);
				this.videoDescNode.update(desc);
				document.title = "SiffTV - Seattle International Film Festival - " + title;
				/*
				update meta tags
				*/
				
			},
			attachEventListeners: function(){
				var links = $$("li.video_result a[rel]");
				links.invoke("observe","click", this.parseVideoData.bindAsEventListener(this));
			},
			parseVideoData: function(e){
				e.stop();
				var clickedLink = e.findElement("a");
				var clickedLinkData = clickedLink.rel.split(",");

				var options = {
					videoURL: clickedLinkData[0],
					imageURL: clickedLinkData[1],
					autoPlay: clickedLinkData[2],
					videoID: clickedLinkData[3],
					videoTitle: clickedLink.up(".video_result").down(".videoTitle").innerHTML,
					videoDescription:clickedLink.up(".video_result").down(".videoDesc").innerHTML
				}
				this.loadNewVideo(options);
			},
			updateHash: function(newHash){
				location.hash = "#/vid="+newHash;
			},
			embedHTML5: function(){
				//$("videoEmbed").insert("<video")
			},
			embedFlash: function(videoURL, imageURL, autoPlay){
				var settings = {
					path: '../_swf/siff_flvPlayer.swf?flvPath=' + videoURL + '&imgPath=' + imageURL,
					width: '480',
					height: '270',
					version: '9.0.0',
					id: 'videoEmbed', // flash replace id
					expressInstall: false
				};

				var flashvars = {
					flvWidth: '480',
					flvHeight: '270',
					loopBool: 'false',
					autoPlayBool: autoPlay,
					hideControlsBool: 'true',
					hideControlsBool: 'true',
					controlPadding: '0',
					controlSetup: 'PlayPause,ProgressBar,FlyoutVolumeControl,FullScreen',
					showActualSize: 'true'
				};

				var params = {
					loop: 'true',
					scale: 'noscale',
					align: 'middle',
					salign: 'tl',
					allowscriptaccess: 'always',
					wmode: 'opaque',
					allowFullScreen: true
				};

				swfobject.embedSWF(settings.path, settings.id, settings.width, settings.height, settings.version, settings.expressInstall, flashvars, params);			
			}
        }
        
/*
var facebookLike = {
	init: function(){
		this.iframeContainer = $("like_button_container");
		this.likeURL = document.URL;
		this.embedIframe();
	},
	updateLikeButton: function(){
		var dl = document.location;
		var hash = dl.hash.replace("#/vid=", "?vid=");
		this.likeURL = dl.protocol + "//" + dl.hostname + dl.pathname + hash;
		this.embedIframe();
	},
	embedIframe: function(){
		this.iframeContainer.update("<iframe src=\"http:\/\/www.facebook.com\/plugins\/like.php?href="+this.likeURL+"&layout=standard&show_faces=false&width=80&action=like&font=verdana&colorscheme=light\" scrolling=\"no\" frameborder=\"0\" style=\"border:none; overflow:hidden; width:450px; height:35px\" allowTransparency=\"true\"><\/iframe>");
		
	}
}
*/ 
