/**
* Timewheel plug-in for JQuery
* works with mouse/touch Events
*
* author 
*		Renato Hotz
*
* Version 0.6: 2011-03-31
**/
(function(a){a.fn.timewheel=function(b){var c={debug:false,ajaxId:false,ajaxLoaderId:false,delay:0,throwEnabled:true,throwSensitivity:0.9,bouncing:true,doActionOnInit:false,callbackOnSelect:"",callbackParam:"",timeParams:false};return this.each(function(){if(b){a.extend(c,b)}var p=this;a(p).replaceWith('<div class="timewheel_container"><div class="bg_container_left"></div><div class="bg_container_right"></div><div class="ul_wrapper"><ul class="timewheel">'+a(p).html()+'</ul></div><div class="arrow_left"></div><div class="arrow_right"></div></div>');var p=a("div.timewheel_container ul");var C;var y;var D;var m;var l;var n=a("div.timewheel_container li.active");var I;if(n.length==0){n=a("div.timewheel_container li:first");a(n).addClass("active")}I=a(n).attr("id");if(c.timeParams){var h=c.timeParams;var r=false;var g=false;if(h.doNotShowNames!=true){a("div.timewheel_container div.arrow_left").addClass("arrow_left_special").append("<p></p>");a("div.timewheel_container div.arrow_right").addClass("arrow_right_special").append("<p></p>")}else{a("div.timewheel_container div.arrow_left").addClass("arrow_left_special");a("div.timewheel_container div.arrow_right").addClass("arrow_right_special")}}else{var h=false}var o="";var t=false;var B=false;var i=0;var v=0;var s=c.throwSensitivity;var F=c.throwEnabled;var d=c.bouncing;var f=0;var E=0;var A=0;var k=false;var e=false;z();function z(){C=a("div.timewheel_container ul").width();y=a("div.timewheel_container").width();D=y/2;m=a("div.timewheel_container").position().left;l=m+y;j(I,true)}if(typeof document.body.ontouchstart!="undefined"){k=true;window.addEventListener("orientationchange",G,false);p.bind("touchstart",J)}else{p.bind("mousedown",J)}function G(){setTimeout(function(){z()},10)}a("div.timewheel_container ul li").click(function(K){if(v==0&&B){var L=a(this).attr("id");j(L,false)}K.preventDefault()});a("div.arrow_right").click(function(){if(window.delayTimer){window.clearTimeout(window.delayTimer)}if(!h){var K=a(n).next("li");if(K.length){j(K.attr("id"),false)}}else{if(h.reloadThresholdRight!=undefined){if("ts_"+h.reloadThresholdRight[0].timestamp==r){window.location.href=h.reloadThresholdRight[0].url}else{j(r,false)}}else{j(r,false)}}});a("div.arrow_left").click(function(){if(window.delayTimer){window.clearTimeout(window.delayTimer)}if(!h){var K=a(n).prev("li");if(K.length){j(K.attr("id"),false)}}else{if(h.reloadThresholdLeft!=undefined){if("ts_"+h.reloadThresholdLeft[0].timestamp==g){window.location.href=h.reloadThresholdLeft[0].url}else{j(g,false)}}else{j(g,false)}}});function J(N){var L,P,M,K;if(window.delayTimer){window.clearTimeout(window.delayTimer)}if(F){x()}L=a(N.target);B=true;v=0;if(k){var O=N.originalEvent;i=O.targetTouches[0].pageX-this.offsetLeft}else{var O=N;i=O.pageX-this.offsetLeft}P=function(Q){t=true;B=false;Q.preventDefault();if(k){var R=Q.originalEvent;A=R.targetTouches[0].pageX}else{var R=Q;A=R.pageX}p.css("left",A-i)};M=function(Q){Q.preventDefault();if(v==0&&!B){j(u(),false)}t=false;if(k){L.unbind("touchmove touchend")}else{a(document).unbind("mousemove mouseup")}};if(k){L.bind("touchmove",P).bind("touchend",M)}else{N.preventDefault();a(document).bind("mousemove",P).bind("mouseup",M)}}function x(){if(o!==""){w()}o=window.setInterval(function(){if(t){f=E;E=A;v=E-f}else{if(v!=0){var L=p.position().left+v;p.css({left:L});if(p.position().left<=-C+y/2){p.position().left=-C+y/2;if(d){v*=-1}else{v=0}}else{if(p.position().left>=D){p.position().left=D;if(d){v*=-1}else{v=0}}}v*=s;var K=(Math.round(Math.abs(v)*10)/10);if(K<=0.5||v==0){v=0;j(u(),false)}}}},30)}function w(){if(o!=""){window.clearInterval(o);o=""}}function u(){var K;var L=C;a("div.timewheel_container ul li").stop(true,true).each(function(M,O){var N=a(this);if(Math.abs(D-(N.position().left+p.position().left+(N.width()/2)))<L){L=Math.abs(D-(N.position().left+p.position().left+(N.width()/2)));K=N.attr("id")}});return K}function j(N,L){I=N;var K=a("li#"+N);var M=0-(K.position().left+(K.outerWidth()/2))+D;if(L){p.css({left:M+"px"});if(c.doActionOnInit==true){q(N)}}else{p.stop(true,true).animate({left:M+"px"},"normal",function(){n.removeClass("active");K.addClass("active");n=K;if(c.delay>0){if(window.delayTimer){window.clearTimeout(window.delayTimer)}window.delayTimer=window.setTimeout(function(){q(N)},(c.delay*1000))}else{q(N)}})}if(h){H()}}function q(M){var K=a("li#"+M+" a").attr("href");if(!jQuery.support.hrefNormalized){var L=window.location;K=K.replace(L.protocol+"//"+L.host+L.pathname,"")}K.substr(0,1)=="#";if(K!=""&&K!="#"&&typeof(K)!="undefined"){if(c.ajaxId){if(c.ajaxLoaderId){a("#"+c.ajaxLoaderId).show()}a.ajax({url:K,contentType:"application/x-www-form-urlencoded;charset=ISO-8859-1",success:function(N){if(c.ajaxLoaderId){a("#"+c.ajaxLoaderId).hide()}a("#"+c.ajaxId).html(N)}})}else{window.location.href=K}}if(typeof c.callbackOnSelect=="function"){param=c.callbackParam=="id"?M:c.callbackParam;c.callbackOnSelect(param)}w()}function H(){var N=false;for(var L=0;L<h.fieldNamesRight.length;L++){if((I>="ts_"+h.fieldNamesRight[L].timestamp[0])&&(I<="ts_"+h.fieldNamesRight[L].timestamp[1])){var K=h.fieldNamesRight[L].name;for(var M=0;M<h.moveToSections.length;M++){if(K==h.moveToSections[M].name){r="ts_"+h.moveToSections[M].timestamp;if(h.doNotShowNames!=true){a("div.arrow_right p").text(h.moveToSections[M].name)}N=true;break}}}if(N){break}}N=false;for(var L=0;L<h.fieldNamesLeft.length;L++){if((I>="ts_"+h.fieldNamesLeft[L].timestamp[0])&&(I<="ts_"+h.fieldNamesLeft[L].timestamp[1])){var K=h.fieldNamesLeft[L].name;for(var M=0;M<h.moveToSections.length;M++){if(K==h.moveToSections[M].name){g="ts_"+h.moveToSections[M].timestamp;if(h.doNotShowNames!=true){a("div.arrow_left p").text(h.moveToSections[M].name)}N=true}}}if(N){break}}}})}})(jQuery);
