// Pagination Teaser Klasse
function PaginationTeaser(id) {
    this.id = id;
	this.count			= 0;
	this.current		= 0;
	this.teaserList		= {};
	this.clicks			= 0;
	this.container		= false;
	this.counter		= false;
	this.teaserHeader    = false;
    this.teaserDachzeile = false;
    this.teaserText      = false;
    this.teaserBild      = false;
    this.prevBild      = false;
    this.nextBild      = false;
    this.aktBild      = false;
	this.bewertung		= false;
	this.ivw             = false;
	this.leftivw         = false;
    this.jsp             = "";
    this.teaserType      = "";
    this.imageWidth      = "";
}

PaginationTeaser.prototype.nextTest = function() {
    alert(this.id);
    return false;
};

PaginationTeaser.prototype.get = function(sDocumentKey) {
    // Für Reiterteaser: Teaser mit übergebenem sDocumentKey laden
    if(this.test()) {
        this.loadTeaser(sDocumentKey);
        return false;
    }else{
        return false;
    }
};

PaginationTeaser.prototype.next = function() {
    if(this.test()) {
        if(this.teaserList.length) {
            var newCurrent;
       		if(this.current < this.count-1){
            	newCurrent = this.current+1;
            } else {
            	newCurrent = 0;
            }
            this.loadTeaser(newCurrent);
            this.current = newCurrent;
        }
        return false;
    } else {
        return true;
    }
};

PaginationTeaser.prototype.previous = function() {
	if(this.test()) {
		if(this.teaserList.length) {
		 	var newCurrent;
		 	if(this.current == 0){
		 		newCurrent = this.count-1;
				if(newCurrent < 0 ) newCurrent = 0;
		 	} else {
		 		newCurrent = this.current-1;
		 	}
		 	this.loadTeaser(newCurrent);
		 	this.current = newCurrent;
		}
        return false;
    } else {
        return true;
    }
};

PaginationTeaser.prototype.loadTeaser = function(nDocument) {
	var sDocumentKey = this.teaserList[nDocument];
	var sPrevDocKey = nDocument>0?this.teaserList[nDocument-1]:"";
	var sNextDocKey = nDocument<this.teaserList.length-1?this.teaserList[nDocument+1]:"";
	//console.log(nDocument+" / "+this.teaserList.length+" / "+this.count+" / ");
	//console.log(sDocumentKey+" / "+sPrevDocKey+" / "+sNextDocKey+" / ");
	
    var sRequest = "includes/triopaginationTeaser.jsp?documentKey="+sDocumentKey+'&jspLink='+this.jsp+'&imageWidth='+this.imageWidth;
    sRequest += '&prevdocKey='+sPrevDocKey+'&nextdocKey='+sNextDocKey+'&aktdocNr='+nDocument;
    var self = this;
	$.ajax({
	    url: sRequest,
	    type: 'GET',
	    dataType: 'xml',
	    success: function(xml){
	    	$(xml).find('titel').each(function(){ self.teaserHeader.innerHTML = $(this).text(); });
			$(xml).find('dachzeile').each(function(){ self.teaserDachzeile.innerHTML = $(this).text(); });
			$(xml).find('text').each(function(){ self.teaserText.innerHTML = $(this).text(); });
            $(xml).find('bild').each(function(){ self.teaserBild.innerHTML = $(this).text(); });
            $(xml).find('bildbreite').each(function(){ self.teaserBild.style.width = $(this).text(); self.container.style.marginLeft = $(this).text(); });
            $(xml).find('bildklasse').each(function(){ self.teaserBild.className = $(this).text() + ' klickteaserBild'; });
            $(xml).find('prevbild').each(function(){ self.prevBild.innerHTML = $(this).text(); });
            $(xml).find('nextbild').each(function(){ self.nextBild.innerHTML = $(this).text(); });
            $(xml).find('aktbild').each(function(){ self.aktBild.innerHTML = $(this).text(); });
            $(xml).find('bewertung').each(function(){ self.bewertung.innerHTML = $(this).text(); });
            $(xml).find('bewertungstext').each(function(){ self.bewertung.title = $(this).text(); });
          	self.counter.innerHTML = self.current+1;
            // für Reiterteaser: aktiven Reiter dunkel einfärben
            if(self.teaserType=="Reiter"){
            $('div.pagReiter_'+self.id).each(function(){ this.className = 'rtnavcard pagReiter_'+self.id+' Mittel'; });
            $('#pagReiter_'+self.id+'_'+sDocumentKey)[0].className='rtnavcard_active pagReiter_'+self.id+' Weiss reiterteaserbordercolor';
            }
			self.loadPixelImg();
	    }
	});
};

PaginationTeaser.prototype.test = function() {
		if(document.getElementById && document.getElementById != 'undefined') {
			if(!this.container || !this.counter) {
                this.container = $('#paginationTeaser_'+this.id+' div.teasercontentstart')[0];
                this.counter = $('#paginationTeaser_'+this.id+' span.currentTeaser')[0];
                this.teaserHeader = $('#paginationTeaser_'+this.id+' span.klickteaserHeader')[0];
                this.teaserText = $('#paginationTeaser_'+this.id+' span.klickteaserText')[0];
                this.teaserDachzeile = $('#paginationTeaser_'+this.id+' span.klickteaserDachzeile')[0];
                this.teaserBild = $('#paginationTeaser_'+this.id+' div.klickteaserBild')[0];
                this.prevBild = $('#paginationTeaser_'+this.id+' a.prev_img')[0];
                this.nextBild = $('#paginationTeaser_'+this.id+' a.next_img')[0];
                this.aktBild = $('#paginationTeaser_'+this.id+' div.akt_img')[0];
                this.bewertung = $('#paginationTeaser_'+this.id+' div.bewertungskasten')[0];
				//alert(this.bewertung);
                //alert(this.container);
                //alert(this.counter);
                //alert(this.teaserHeader);
                //alert(this.teaserText);
                //alert(this.teaserDachzeile.innerHTML);
                //alert(this.teaserBild);
                //alert(this.container.innerHTML);
			}
			if(this.container && this.counter && this.teaserHeader && this.teaserText
                && this.teaserDachzeile && this.teaserBild && this.container.innerHTML) {
    				return true;
			}
		}
		return false;
};

PaginationTeaser.prototype.loadPixelImg = function() {
    var image = new Image();
    image.src = "http://hr.ivwbox.de/cgi-bin/ivw/CP/"+this.leftivw+"; "+this.ivw+"?r=" + escape(document.referrer) + "&doc=" + this.clicks++;
};

