/**
 * @author frank
 */
function RotatedPicture(id){
    this.id = id;
    this._ele = document.getElementById(id);
    this.element = $(this._ele);
    this.pictures = this.element.find(".picture-container").children();
	this.pagerItems = this.element.find(".rotated-pager").children();
	this.count = this.pagerItems.length;
    this.firstTime = true;
    this.delaySeconds = 5000;
    this.currentIndex = 0;
    this.stopFlag = false;
	
	this._bindControlEvent();
	this._bindPagerEvent();
}

RotatedPicture.prototype.getPicture = function(){
    return this.pictures[this.currentIndex];
}

RotatedPicture.prototype.getPagerItem = function(){
    return this.pagerItems[this.currentIndex];
}

RotatedPicture.prototype.change = function(){
	var pic = $(this.getPicture());
	var pageItem = $(this.getPagerItem());
	
	pic.css("display","block");
	pic.siblings().css("display","none");
	
	pageItem.addClass("current");
	pageItem.siblings().removeClass("current");
	
	this.currentIndex++;
}

RotatedPicture.prototype.rotate = function(){
	if (this.firstTime) {
        setTimeout(function() { }, this.delaySeconds);
        this.firstTime = false;
    }

    if (!this.stopFlag) {
        if (this.currentIndex == this.pagerItems.length) {
            this.currentIndex = 0;
        }

        try {
            this.change();
        }
        catch (e) { }
    }

    var currentRotate = RotatedPicture.prototype.rotate.bindFunc(this, null);
    setTimeout(currentRotate, this.delaySeconds);
}

RotatedPicture.prototype._bindPagerEvent = function() {
    var context = this;
    context.pagerItems.click(function() {
        for (var i = 0; i < context.pagerItems.length; i++) {
            if (context.pagerItems[i] == this) {
				context.currentIndex = i;
                context.change();
                break;
            }
        }
    });
}

RotatedPicture.prototype._bindControlEvent = function() {
    var context = this;
    this.element.mouseover(function() { context.stopFlag = true; }).mouseout(function() { context.stopFlag = false; })
}

Function.prototype.bindFunc = function(obj) {
    var method = this;
    var argument = arguments && arguments.length > 1 ? arguments[1] : [];
    var temp = function() {
        return method.call(obj, argument);
    };

    return temp;
}

$(document).ready(
	function(){
		new RotatedPicture("index_content_left_photo").rotate();
	}
);
