//
// (c) 2011 DCN GmbH - Patrik Remmele(PHP) & Benedict Burckhart(JS)
// eMail: pr@dcn.de, bb@dcn.de
// web:   www.dcn.de
//

jQuery(document).ready(function() {
    slider.init();
    slider.reset();

    jQuery(".homeTeaser").mouseenter(function() {
	slider.timer.stop();
    });

    jQuery(".homeTeaser").mouseleave(function() {
	slider.timer.start();
    });
});

//Slider Object

function Slider(config, count) {
    //self pointer
    var self = this;

    //internal objects
    this.images = Array();
    this.timer;
    this.pager;
    this.config = config;

    //control vars
    this.count = count;
    this.aktTeaser = 0;

    //Init Slider
    this.init = function() {
	//generate teaser objects
	for (var i = 0; i < this.count; i++) {
	    this.images[i] = new Teaser(jQuery(("#htPic-" + i)), jQuery(("#htHeadline-" + i)), jQuery(("#htContent-" + i)), this.config.ImagesWidth);
	}

	//create Timer
	this.timer = new Timer(this.config.SwitchTime, self.slideNext);
	this.timer.start();

	//create Pager
	this.pager = new Pager(this, this.count, this.config.ActivePagerColor, this.config.InactivePagerColor, this.config.Speed);
	this.pager.init();
    }

    this.reset = function() {
	//hide all Elements
	for (var i = 0; i < this.images.length; i++) {
	    this.images[i].hide();
	}

	//show all Elements
	this.images[0].show();
    }

    this.slideNext = function() {
	self.slideTo(self.aktTeaser + 1);
    }

    this.slideTo = function(destination) {
	var nextTeaser = destination;

	self.images[self.getId(self.aktTeaser)].slideOut(function() {
	    self.pager.slideTo(self.getId(nextTeaser));

	    self.images[self.getId(nextTeaser)].slideIn();
	    self.aktTeaser = destination;
	});
    }

    this.setPager = function(pager) {
	this.pager = pager;
    }

    this.getAktTeaserId = function() {
	return this.getId(this.aktTeaser);
    }

    this.getId = function(id) {
	if (this.images.length != 0) {
	    if (id > (this.images.length - 1)) return this.getId(id - this.images.length)
	    else if (id < 0) return this.getId(this.images.length + id)
	    else
	    return id;
	}
	return false;
    }
}

//Pager Object

function Pager(slider, count, inactivePagerColor, activePagerColor, speed) {
    //self pointer
    var self = this;

    //pointer to slider
    this.slider = slider;

    //internal objects
    this.paper;
    this.activeButton;
    this.buttons = Array();

    //config params
    this.count = count;
    this.inactivePagerColor = inactivePagerColor;
    this.activePagerColor = activePagerColor;
    this.speed = speed;

    this.init = function() {
	//generate pager
	this.paper = Raphael("htPager");
	for (var i = 0; i < this.count; i++) {
	    this.buttons[i] = this.paper.circle((i + 1) * 15, 10, 5).attr({
		'fill': this.inactivePagerColor,
		'stroke-width': '0px',
		'stroke': this.inactivePagerColor
	    });

	    //set the number to an attribute
	    jQuery(this.buttons[i].node).attr('number', i);

	    //set click events
	    jQuery(this.buttons[i].node).click(function(event) {
		var number = parseInt(jQuery(this).attr('number'));

		slider.slideTo(number);

		self.buttons[number].animate({
		    'r': 6
		}, 50, function() {
		    self.buttons[number].animate({
			'r': 5
		    }, 50)
		});
	    });
	}

	//draw aktive point	
	this.activeButton = this.paper.circle(15, 10, 5).attr({
	    'fill': this.activePagerColor,
	    'stroke-width': '0px',
	    'stroke': this.ctivePagerColor
	});
    }

    this.slideTo = function(destination) {
	var newX = (destination + 1) * 15;

	if (destination < this.slider.getAktTeaserId()) {
	    self.activeButton.animate({
		'fill': self.inactivePagerColor,
		'stroke': self.inactivePagerColor
	    }, self.speed / 2, function() {
		self.activeButton.animate({
		    'cx': newX
		}, 0);
		self.activeButton.animate({
		    'fill': self.activePagerColor,
		    'stroke': self.activePagerColor
		}, self.speed / 2);
	    });
	} else {
	    self.activeButton.animate({
		'cx': newX - 7.5,
		'fill': self.inactivePagerColor,
		'r': 10,
		'stroke': self.inactivePagerColor
	    }, self.speed / 2, function() {
		self.activeButton.animate({
		    'cx': newX,
		    'fill': self.activePagerColor,
		    'r': 5,
		    'stroke': self.activePagerColor
		}, self.speed / 2);
	    });
	}
    }
}


//Timer Object

function Timer(time, func) {
    var self = this;
    this.time = time;
    this.enabled = false;
    this.func = func;

    //the Loop for the timer
    this.loop = function() {
	if (this.enabled) {
	    self.func();
	    setTimeout(function() {
		self.loop()
	    }, self.time);
	}
    }

    //start Timer
    this.start = function() {
	this.enabled = true;
	setTimeout(function() {
	    self.loop()
	}, self.time);
    }

    //stop Timer
    this.stop = function() {
	this.enabled = false;
    }
}

//Teaser Object

function Teaser(picture, headline, content, width) {
    //self pointer
    var self = this;

    //internal objects
    this.picture = picture;
    this.headline = headline;
    this.content = content;

    //config params
    this.width = width;
    this.speed = 1000;

    //control vars
    this.progress = false;

    //slide in function
    this.slideIn = function() {
	if (this.progress == false) {
	    this.progress = true;

	    this.picture.animate({
		left: 0
	    }, this.speed, function() {
		self.content.fadeIn(self.speed);
		self.headline.fadeIn(self.speed, function() {
		    self.progress = false;
		});
	    });
	}
    }

    //slide out function
    this.slideOut = function(callback) {
	if (this.progress == false) {
	    this.progress = true;

	    this.headline.fadeOut(this.speed, function() {
		callback();
		self.picture.animate({
		    left: self.width
		}, self.speed, function() {
		    self.hide();
		});
	    })
	    this.content.fadeOut(this.speed);
	}
    }

    //show function
    this.show = function() {
	this.picture.css({
	    left: 0
	});
	this.content.show();
	this.headline.show();
	this.progress = false;
    }

    //hide function
    this.hide = function() {
	this.picture.css({
	    left: -this.width
	});
	this.content.hide();
	this.headline.hide();
	this.progress = false;
    }
}

//Config Object

function Config(ActivePagerColor, ImagesHeight, ImagesWidth, InactivePagerColor, SwitchTime) {
    this.ActivePagerColor = ActivePagerColor;
    this.ImagesHeight = ImagesHeight;
    this.ImagesWidth = ImagesWidth;
    this.InactivePagerColor = InactivePagerColor;
    this.SwitchTime = SwitchTime;
    this.Speed = 1000;
}
