/*
* jQuery glitzhigh - A HTML5 video player
*
* Version: 1.0.0
*
* Copyright (c) 2010 Ron Valstar
*
* Dual licensed under the MIT and GPL licenses:
*   http://www.opensource.org/licenses/mit-license.php
*   http://www.gnu.org/licenses/gpl.html
*
* in this update:
*
* in last update:
*
* Todos
*
*/
;(function($) {
	// private variables
	var oSettings = {};
	var ss = oSettings;
	var iW;
	var iH;
	//
	var iContentW;
	var iPagePadding; // (iW-iContentW)/2
	var aTdW = [null,20,15,10,20,null];
	//
	var sCurrentId;
	//
	var $Body;
	var mVideo;
	var mPage;
	var mItemInfo;
	var $Control;
	var aPages;
	var aPlaylist;
	//
	var sLastPage = "";
	var sCurrentPage = "iteminfo";
	//
	var oWiddio;
	//
	// default settings
	$.glitzhigh = {
		 id: "glitzhigh"
		,version: "1.0.0"
		,copyright: "Copyright (c) 2010 Ron Valstar"
		,defaults: {
			 debug:	true
		}
		,play: function(id,o,el) {
			trace("play: "+id+" "+o.mp4);
			oWiddio.play(o);
			sCurrentId = id;
			highlightPlaylistItem();
			//
			var o = trdata(el);
			$("#iteminfo>h2").html(o.title+"<small>"+o.place+" "+o.time+"</small>");
			//
			mItemInfo.find(".content").html(o.text).prepend('<p class="unhide">'+$(o.text).find("p:first").html()+'</p>').find(">div").addClass("hide");
			//
			showPage('iteminfo');
		}
	};
	// init
	$(function() {
		$.fn.glitzhigh();
	});
	//
	// call
	$.fn.extend({
		glitzhigh: function(_settings) {
			trace($.glitzhigh.id+" "+$.glitzhigh.version,true);
			$.each($.browser, function(i, val) {
				trace("\t"+i+": "+val,true);
			});
			$.extend(oSettings, $.glitzhigh.defaults, _settings);
			trace("debug: "+ss.debug,true);
			//
			init();
		}
		,setScrPos: function() {
			return $(this);
		}
	});
	//
	// init
	function init() {
		trace("init");
		//trace(jQuery.easing);
		// page
		$Body = $('body');
		mPage = $('#page');
		mItemInfo = mPage.find(">div#iteminfo");
		//
		iContentW = $(".content").css('width').replace('px','');
		trace("iContentW: "+iContentW);
		//
		// close buttons
		aPages = mPage.find(">div");
		mPage.find(">div").each(function(i,el){
			var mDivPage = $(this);
			var fnBack = function(e){showPage('iteminfo');};
			var fnLess = function(e){readMore(false);};
			var bDivNfo = mDivPage.attr('id')=='iteminfo';
			var mClose = $('<a class="close">x</a>').prependTo(mDivPage).click(bDivNfo?fnLess:fnBack);
			if (bDivNfo) {
				mClose.hide();
			} else {
				mDivPage.find(">h2").click(fnBack);
			}
		});
		//
		// video
		mVideo = $("video");
		oWiddio = mVideo.widdio({
			 debug:	ss.debug
			,fullscreen:	true
			,fadeOutBeforeLoad:		1000
			,controls:				[$.widdio.SCRUB,$.widdio.CENTER]//,$.widdio.MUTE
			,controlsPosition:		$.widdio.CONTROLS_OVER
			,controlsHeight:		4//mPage.css('bottom').replace('px','')
			,size:					$.widdio.SIZE_FULLSCREENNOBARS
			,controlsCenterPlay:	$('<div></div>').css({background:'url(css/centerPlay.png)'})
			,controlsCenterPause:	$('<div></div>').css({background:'url(css/centerPause.png)'})
			,controlsMuteOn:		$('<div>sound on</div>')
			,controlsMuteOff:		$('<div>no sound</div>')
			,overlay:				$("#overlay")
			,playEnd:				playEnd
		});
		//
		// playlist
		aPlaylist = $("tr");
		if ($.browser.msie) setTimeout(function(){$("tr:eq(1)").click();},1);
		else $("tr:eq(1)").click(); // IE need timeout... init takes longer?
		//
		$Control = $('.widdioControl').insertAfter('#iteminfo>h2');
		//
		// call resize manually one time
		$(window).resize(windowResize);
		windowResize();
		//
		trace("init_end");
	}

	// trdata
	function trdata(tr) {
		trace("trdata: "+tr);
		mTr = $(tr);
		oTr = {};
		mTd = mTr.find("td");
		mTr.parents("table:first").find("th").each(function(i,el){
			oTr[$(el).text()] = $(mTd[i]).html();
		});
		return oTr;
	}

	// showPage
	function showPage(id) {
		trace("showPage: "+id);
		sCurrentPage = id;
		mPage.animate({
			 maxHeight: "0%"
			,height: 0
		}, {
			 duration: 300
			,easing: 'easeOutQuad'
			,complete: function() {
				//
				mPage.find(">div").hide();
				var mCurPage = $("#"+id);
				var mCurCnt = mCurPage.find(">div.content");
				mCurPage.show();
				mCurCnt.find("div:first>p:first").css({fontWeight:"bold"});
				mCurCnt.find(">div:eq(0)").addClass("left");
				mCurCnt.find(">div:eq(1)").addClass("right");
				//
				var oTo = {maxHeight: "90%"};
				if (id=='iteminfo') {
					mPage.css({height:"auto"});
				} else {
					oTo.height = getPageHeight();
				}
				mPage.animate(oTo, {
					 duration: 600
					,easing: 'easeInQuad'
				});
			}
		});
	}

	// readMore
	function readMore(b) {
		if (b===null) b = true;
		if (b) { // show more
			$("#iteminfo .unhide").hide();
			$("#iteminfo .hide").slideDown();
			$("#iteminfo .close").fadeIn();
			mPage.animate({height:getPageHeight()}, {
				 duration: 600
				,easing: 'easeInQuad'
			});
		} else { // show less
			$("#iteminfo .unhide").show();
			$("#iteminfo .hide").slideUp();
			$("#iteminfo .close").fadeOut();
			mPage.css({minHeight:mPage.height(),height:"auto"});
			mPage.animate({minHeight:0}, {
				 duration: 600
				,easing: 'easeInQuad'
			});
		}
	}

	// getPageHeight
	function getPageHeight() {
		return iH-200;
	}

	// playEnd
	function playEnd() {
		oWiddio.play();
	}

	// windowResize
	function windowResize(e) {
		trace("windowResize: "+e);
		iW = $Body.width();
		iH = $Body.height();
		//
		iPagePadding = Math.floor((iW-iContentW)/2);
//		trace('iW:',iW,iH,iPagePadding); // TRACE ### iW
		//
		$Control.width(iW);
		//
		if (sCurrentPage!='iteminfo'||mItemInfo.find(".close:visible").length) mPage.height(getPageHeight());
		//
		if (aPages) {
			// resize content padding
//			aPages.find(">h2").css({paddingLeft:iPagePadding+"px",paddingRight:iPagePadding+"px"});
			//
			// resize playlist table
			var iTot = 0;
			$.each(aTdW,function(i,v){ iTot += v; });
			var fTot = iContentW/iTot;
			$("tr:eq(1)>td").each(function(i,td){
				$(td).width(aTdW[i]?Math.round(fTot*aTdW[i]):iPagePadding);
			});
		}
	}

	// highlightPlaylistItem
	function highlightPlaylistItem() {
		aPlaylist.removeClass("selected");
		$("#"+sCurrentId).addClass("selected");
	}

	// trace
	function trace(o,v) {
		if (!ss.debug) return;
		try {console.log.apply(console, arguments);} catch(e){};
	}
})(jQuery);

jQuery.extend( jQuery.easing, {
	easeInQuad: function (x, t, b, c, d) {
		return c*(t/=d)*t + b;
	},
	easeOutQuad: function (x, t, b, c, d) {
		return -c *(t/=d)*(t-2) + b;
	},
	foo: {}
});

// analytics
(function analytics() {
	var _gaq = _gaq || [];
	_gaq.push(['_setAccount', 'UA-359264-1']);
	_gaq.push(['_trackPageview']);
	(function() {
		var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
		ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
		var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
	})();
})();
