var Greod=Greod||{};Greod.Carousel=Greod.Carousel||{};Greod.Carousel.direction=($(document.body).css("direction")==="rtl")?"right":"left";Greod.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;var children=this._element.children();
children.css(Greod.Carousel.direction,function(i,v){return width*(i-currPos);});this._count=children.length;this._element.removeClass("no-slider");};$.extend(Greod.Carousel.Slider.prototype,{Count:function(){return this._count;},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(Greod.Carousel.direction,this._slideWidth);var animated=0,originalThis=this;
var obj={};obj[Greod.Carousel.direction]="-="+this._slideWidth;children.animate(obj,{complete:function(){animated++;if(animated===count){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(Greod.Carousel.direction,-this._slideWidth);var animated=0,originalThis=this;var obj={};obj[Greod.Carousel.direction]="+="+this._slideWidth;
children.animate(obj,{complete:function(){animated++;if(animated===count){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;if(typeof callback==="function"){callback();}return true;
}$(children).css(Greod.Carousel.direction,function(i,v){return slideWidth*(i-currPos);});var animateString=((targetPos>currPos)?"-=":"+=")+this._slideWidth*Math.abs(targetPos-currPos);var animated=0,originalThis=this;var obj={};obj[Greod.Carousel.direction]=animateString;
children.animate(obj,{complete:function(){animated++;if(animated===count){originalThis._lock=false;if(typeof callback==="function"){callback();}}}});this._currPos=targetPos;return true;}});Greod.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);}};};var fragment=$("<div></div>");for(var i=0;i<count;i++){$('<a href="#">&#9679;</a>').click(clickHandlerGen(clickEvent,i)).focus(function(){$(this).blur();
}).appendTo(fragment);}fragment.find("a").eq(currPos).addClass("active");this._element.append(fragment.children().unwrap());this._count=count;};$.extend(Greod.Carousel.DotIndicator.prototype,{Count:function(){return this._count;},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;}});Greod.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;this._count=children.length;
};$.extend(Greod.Carousel.Fader.prototype,{Count:function(){return this._count;},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[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;}});Greod.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(typeof(interval)!=="number"||interval<=0){throw new Error("Interval must be greater than 0.");}this._interval=interval;this._callback=callback;this._intervalId=null;
this.Run();};$.extend(Greod.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;}}});Greod.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 Greod.Carousel.Fader($(".carousel-text",selector));this._textFader=textFader;var adFader=new Greod.Carousel.Fader($(".carousel-ads",selector));this._adFader=adFader;var slider=new Greod.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 Greod.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 Greod.Carousel.IntervalTimer(function(){originalThis.Next();},interval);};$.extend(Greod.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);}});$(document).ready(function(){var HomepageCarousel=new Greod.Carousel.HomepageCarousel(".homepage-carousel",5000);
});
