﻿var Directors=Directors||{};Directors.Carousel=Directors.Carousel||{};Directors.Carousel.Slider=function(selector,width){if(!(this instanceof arguments.callee))throw new Error('Constructor cannot be called as a function.');if(selector==null)throw new Error('"selector" cannot be null.');if(typeof(width)!='number'||width<=0)throw new Error('"width" cannot be a valid.');this._element=$(selector);var currPos=0;this._currPos=currPos;this._slideWidth=width;this._element.children().each(function(i,el){$(el).css('left',width*(i-currPos));$(el).show();});};$.extend(Directors.Carousel.Slider.prototype,{Count:function(){return this._element.children().length;},Position:function(){return this._currPos;},Next:function(callback){if(this._lock){return false;}
this._lock=true;var children=this._element.children();var count=children.length;var nextPos=(this.Position()+1)%count;$(children[nextPos]).css('left',this._slideWidth).show();var animated=0,originalThis=this;children.animate({'left':'-='+this._slideWidth},{complete:function(){animated++;if(animated==count){children.not(children[nextPos]).hide();originalThis._lock=false;if(typeof callback=='function'){callback();}}}});this._currPos=nextPos;return true;},Prev:function(callback){if(this._lock){return false;}
this._lock=true;var children=this._element.children();var count=children.length;var prevPos=(this.Position()+count-1)%count;$(children[prevPos]).css('left',-this._slideWidth).show();var animated=0,originalThis=this;children.animate({'left':'+='+this._slideWidth},{complete:function(){animated++;if(animated==count){children.not(children[prevPos]).hide();originalThis._lock=false;if(typeof callback=='function'){callback();}}}});this._currPos=prevPos;return true;},GoTo:function(targetPos,callback){if(this._lock){return false;}
this._lock=true;targetPos=targetPos%this.Count();var children=this._element.children();var count=this.Count();var currPos=this.Position();var slideWidth=this._slideWidth;if(targetPos==currPos){this._lock=false;children.not(children[targetPos]).hide();if(typeof callback=='function'){callback();}
return true;}
children.each(function(i,el){$(el).css('left',slideWidth*(i-currPos)).show();});var animateString=((targetPos>currPos)?'-=':'+=')+this._slideWidth*Math.abs(targetPos-currPos);var animated=0,originalThis=this;children.animate({'left':animateString},{complete:function(){animated++;if(animated==count){originalThis._lock=false;if(typeof callback=='function'){callback();}}}});this._currPos=targetPos;return true;}});Directors.Carousel.DotIndicator=function(selector,count,currPos,clickEvent){if(!(this instanceof arguments.callee))throw new Error('Constructor cannot be called as a function.');if(selector==null)throw new Error('"selector" cannot be null.');if(typeof(count)!='number'||count<0){count=0;}
if(typeof(currPos)!='number'||currPos>=count||currPos<0){currPos=0;}
this._element=$(selector).empty();this._currPos=currPos;var originalThis=this;var clickHandlerGen=function(handler,i){return function(evt){evt.preventDefault();if(typeof(handler)==='function'){handler(i);}}}
for(var i=0;i<count;i++){var element=$('<a href="#">&#9679;</a>').appendTo(this._element);var index=i;element.focus(function(){$(this).blur();});element.click(clickHandlerGen(clickEvent,i));if(i==currPos){element.addClass('active');}}};$.extend(Directors.Carousel.DotIndicator.prototype,{Count:function(){return this._element.children().length;},Position:function(){return this._currPos;},Next:function(callback){return this.GoTo(this.Position()+1,callback);},Prev:function(callback){return this.GoTo(this.Position()+this.Count()-1,callback);},GoTo:function(targetPos){var oldPosition=this.Position();var newPosition=targetPos%this.Count();var children=this._element.children();$(children[oldPosition]).removeClass('active');$(children[newPosition]).addClass('active');this._currPos=newPosition;}});Directors.Carousel.Fader=function(selector,currPos){if(!(this instanceof arguments.callee))throw new Error('Constructor cannot be called as a function.');if(selector==null)throw new Error('"selector" cannot be null.');this._element=$(selector);var children=this._element.children();if(typeof(currPos)!='number'||currPos<0||currPos>=children.length){currPos=0;}
this._currPos=currPos;$(children).each(function(i,el){if(i!==currPos){$(el).hide();}
else{$(el).show();}});};$.extend(Directors.Carousel.Fader.prototype,{Count:function(){return this._element.children().length;},Position:function(){return this._currPos;},Next:function(callback){return this.GoTo(this.Position()+1,callback);},Prev:function(callback){return this.GoTo(this.Position()+this.Count()-1,callback);},GoTo:function(targetPos,callback){if(this._lock){return false;}
this._lock=true;var oldPosition=this.Position();var newPosition=targetPos%this.Count();if(oldPosition==newPosition){this._lock=false;if(typeof callback=='function'){callback();}
return true;}
var children=this._element.children();var originalThis=this;$(children).not(children[oldPosition]).hide();$(children[oldPosition]).fadeOut(200,function(){$(children[newPosition]).fadeIn(200,function(){originalThis._lock=false;if($.browser.msie){children[newPosition].style.removeAttribute('filter')}
if(typeof callback=='function'){callback();}});});this._currPos=newPosition;return true;}});Directors.Carousel.IntervalTimer=function(callback,interval){if(!(this instanceof arguments.callee))throw new Error('Constructor cannot be called as a function.');if(typeof(callback)!='function')throw new Error('Callback must be a function.');if(!(interval>0))throw new Error('Interval must be greater than 0.');this._interval=interval;this._callback=callback;this._intervalId=null;this.Run();}
$.extend(Directors.Carousel.IntervalTimer.prototype,{Reset:function(){this.Pause();this.Run();},Run:function(){if(this._intervalId!=null)return;this._intervalId=setInterval(this._callback,this._interval);},Pause:function(){if(this._intervalId!=null){clearInterval(this._intervalId);this._intervalId=null;}}});Directors.Carousel.HomepageCarousel=function(selector,interval){if(!(this instanceof arguments.callee))throw new Error('Constructor cannot be called as a function.');if(selector==null)throw new Error('"selector" cannot be null.');var originalThis=this;this._element=$(selector);var textFader=new Directors.Carousel.Fader($('.carousel-text',selector));this._textFader=textFader;var adFader=new Directors.Carousel.Fader($('.carousel-ads',selector));this._adFader=adFader;var slider=new Directors.Carousel.Slider($('.carousel-image',selector),670);this._slider=slider;if(this._textFader.Count()!==this._slider.Count()||this._textFader.Count()!==this._adFader.Count()){throw new Error('Number of elements in Fader and Slider do not match.');}
var clickEvent=function(index){originalThis._timer.Reset();return originalThis.GoTo(index);}
var indicator=new Directors.Carousel.DotIndicator($('.dot-indicator',selector),this._textFader.Count(),0,clickEvent);this._indicator=indicator;indicator._element.hover(function(){originalThis._timer.Pause();},function(){originalThis._timer.Run();});this._timer=new Directors.Carousel.IntervalTimer(function(){originalThis.Next();},interval);}
$.extend(Directors.Carousel.HomepageCarousel.prototype,{Next:function(){return this._slider.Next()&&this._textFader.Next()&&this._adFader.Next()&&this._indicator.Next();},GoTo:function(index){return this._slider.GoTo(index)&&this._textFader.GoTo(index)&&this._adFader.GoTo(index)&&this._indicator.GoTo(index);}})
