/* $Id: AutoTeaser.js 84386 2009-10-01 11:47:48Z k.reimer $
 * Copyright (C) 2006 IP Labs GmbH <http://www.iplabs.de/>
 * All rights reserved.
 */
 
 /**
 * @fileoverview
 *
 * Contains the AutoTeaser class. Depends on BtnStatus class.
 *
 * @author Andreas Kornetka <a.kornetka@iplabs.de>
 * @version $Revision: 84386 $
 */

/**
 * Constructor for an AutoTeaser Object.
 *
 * @class <p>The AutoTeaser class provides the abilty of "randomize" the teasers (and restrict each one by date) with the use a cookie.</p>
 *
 * @constructor
 * @param {String} strTeaser
 *            The string of the id to be reffered to. Inevitable parameter!
 */
function AutoTeaser(strTeaser) {
	this.__strTeaser     = '#' + strTeaser + '.';
	this.__objTeaser     = document.getElementById(strTeaser);
	this.__teaserList    = new Array();
	this.__visualEffects = null;
}

/**
 * Internal constructor for an teaser object.
 * 
 * @param {String} srcImg
 *			  The src towards an teaser image. Inevitable parameter!
 * @param {String} lnk
 *            The reference to an optional link that will be created when value is not empty.
 * @param {String} srcBtn
 *            The src towards an optional button.
 */
AutoTeaser.prototype.__Teaser = function(srcImg, lnk, srcBtn) {
	if(typeof(srcImg) != 'undefined') {
        this.__img     = new Image();
        this.__img.src = srcImg;
	}
	this.__lnk = lnk;	
	if(typeof(srcBtn) != 'undefined') {
		this.__btn     = new Image();
		this.__btn.src = srcBtn;
	}
}

/**
 * Adds a new teaser.
 * 
 * @param {String} srcImg
 *			  The src towards an teaser image. Inevitable parameter!
 * @param {String} lnk
 *            The reference to an optional link that will be created when value is not empty. The
 *            "setDefaultLnk()" method should be used instead when all or most links are equal.
 * @param {String} srcBtn
 *            The src towards an optional button. The "setDefaultBtn()" method should be used instead, 
 *            but if one btn or all of them are different then this is more sufficent.
 * @param {String} dateStart
 *            Optional start date of a teaser with the pattern 'dd.mm.yyyy'.
 * @param {String} dateEnd
 *            Optional end date of a teaser with the pattern 'dd.mm.yyyy'.
 */
AutoTeaser.prototype.add = function(srcImg, lnk, srcBtn, dateStart, dateEnd) {
	var dateArr = new Array(); dateValid = true, dateCurrent = new Date(), re = /[0-9]{0,2},[0-9]{0,2},[0-9]{4}/;
	if(typeof(dateStart) == 'string') {
		dateStart = dateStart.replace(/\./g, ',');
		if(dateStart.match(re) != null) {
			dateArr   = dateStart.split(','); 
			dateStart = new Date();
			dateStart.setYear(dateArr[2]);
			dateStart.setMonth(dateArr[1] - 1);
			dateStart.setDate(dateArr[0]);
		}
		else {dateStart = null;}
	} else {dateStart = null;}
	if(typeof(dateEnd) == 'string') {
		dateEnd = dateEnd.replace(/\./g, ',');
		if(dateEnd.match(re) != null) {
			dateArr = dateEnd.split(',');
			dateEnd = new Date();
			dateEnd.setYear(dateArr[2]);
			dateEnd.setMonth(dateArr[1] - 1);
			dateEnd.setDate(dateArr[0]);
		}
		else {dateEnd = null;}
	} else {dateEnd = null;}
	if(dateStart != null && dateEnd == null) {
		if(dateCurrent < dateStart) {dateValid = false;}
	}
	if(dateStart == null && dateEnd != null) {
		if(dateCurrent > dateEnd) {dateValid = false;}
	}
	if(dateStart != null && dateEnd != null) {
		if((dateCurrent < dateStart) || (dateCurrent > dateEnd)) {dateValid = false;} 
	}
    srcBtn = typeof(srcBtn) != 'undefined' ? srcBtn : this.__srcDefaultBtn;		
    if(typeof(srcBtn) != 'undefined' && this.__visualEffects == null) {this.__visualEffects = true;}	
	if(typeof(srcImg) == 'string' && dateValid) {
		if(srcImg.length) {this.__teaserList.push(new this.__Teaser(srcImg, typeof(lnk) != 'undefined' ? lnk : this.__lnkDefault, srcBtn));}
	}
}

/**
 * Sets the default btn.
 * 
 * @param {String} srcDefaultBtn
 *			  The src towards the default btn. Inevitable parameter!
 */
AutoTeaser.prototype.setDefaultBtn = function(srcDefaultBtn) {
	if(typeof(srcDefaultBtn) != 'undefined') {this.__srcDefaultBtn = srcDefaultBtn;}
}

/**
 * Sets the default link.
 * 
 * @param {String} lnkDefault
 *			  The default link reference. Inevitable parameter!
 */
AutoTeaser.prototype.setDefaultLnk = function(lnkDefault) {
	if(typeof(lnkDefault) != 'undefined') {this.__lnkDefault = lnkDefault;}
}

/**
 * Sets the default btn.
 * 
 * @param {Boolean || String} val
 *			  If true or 'all' then btn will have visual effects (by default on). 'All' extends the
 *            btn visual changes to the whole teaser. Set to false if visual effects are not wanted 
 *            or an incorrect input was given. Inevitable parameter!
 */
AutoTeaser.prototype.setVisualEffects = function(val) {
	this.__visualEffects = typeof(val) == 'boolean' ? val : (val.toLowerCase() == 'all' ? 'all' : false);
}

/**
 * Activate link (and possible visual effects of the btn) for the whole teaser.
 */
AutoTeaser.prototype.activateOnlyBtnLnk = function() {
	this.__onlyBtnLnk = true;
}

/**
 * Private method: render the actual teaser.
 */
AutoTeaser.prototype.__renderTeaser = function() {
	var img, btn;
	if(typeof(this.__teaserList[this.__active].__img) != 'undefined') {
	   img               = document.createElement('img');
	   img.src           = this.__teaserList[this.__active].__img.src;
	   img.style.display = 'block';
	   this.__objTeaser.appendChild(img);
	}
	if(typeof(this.__teaserList[this.__active].__btn) != 'undefined') {
		btn           = document.createElement('img');
		btn.className = 'btn';
		btn.src       = this.__teaserList[this.__active].__btn.src;
		this.__objBtn = btn;
		if(typeof(this.__onlyBtnLnk) != 'undefined' && typeof(this.__teaserList[this.__active].__lnk) != 'undefined') {
			btn.style.cursor = 'pointer';
			btn.__href       = this.__teaserList[this.__active].__lnk;
			btn.onclick      = new Function('location.href = this.__href');
		}
		if(this.__visualEffects) {
            // BtnStatus is deprecated
            //btn.onmouseover = new Function('if(typeof(BtnStatus) != "undefined") {BtnStatus.changeStatus(this, "over");}');
			//btn.onmousedown = new Function('if(typeof(BtnStatus) != "undefined") {BtnStatus.changeStatus(this, "down");}');
			//btn.onmouseout  = new Function('if(typeof(BtnStatus) != "undefined") {BtnStatus.changeStatus(this, "");}');
		}
		this.__objTeaser.appendChild(btn);
	}
	if(typeof(this.__onlyBtnLnk) == 'undefined' && typeof(this.__teaserList[this.__active].__lnk) != 'undefined') {
		this.__objTeaser.style.cursor = 'pointer';
	    this.__objTeaser.__href       = this.__teaserList[this.__active].__lnk;
        this.__objTeaser.onclick      = new Function('location.href = this.__href');
	}
	if(this.__visualEffects == 'all') {
		// BtnStatus is deprecated
        //this.__objTeaser.onmouseover = new Function('if(typeof(BtnStatus) != "undefined") {BtnStatus.changeStatus(this.lastChild, "over");}');
		//this.__objTeaser.onmousedown = new Function('if(typeof(BtnStatus) != "undefined") {BtnStatus.changeStatus(this.lastChild, "down");}');
		//this.__objTeaser.onmouseout  = new Function('if(typeof(BtnStatus) != "undefined") {BtnStatus.changeStatus(this.lastChild, "");}');
	}
}

/**
 * Private method: get Cookie content.
 * 
 * @param {Boolean} all
 *			  Optional paramter that should be true if all cookie data is requested.
 * @return
 *            Returns the cookie content (or part of it) as a string.
 */
AutoTeaser.prototype.__getCookie = function(all) {
	var start, end = -1, str = new String();
    start = document.cookie.indexOf('AutoTeaser=');
    if((start != -1 && typeof(all) != 'undefined') || (start != -1 && typeof(all) == 'undefined' && document.cookie.indexOf(this.__strTeaser, start) != -1)) {
        if(typeof(all) == 'undefined') {
        	start = document.cookie.indexOf(this.__strTeaser, start) + this.__strTeaser.length;
        	end = document.cookie.indexOf('#', start);
        } else {start += 'AutoTeaser='.length;}
        if(end == -1) {end = document.cookie.indexOf(';', start);}
        if(end == -1) {end = document.cookie.length;}
        str = document.cookie.substring(start, end);
        return (typeof(all) == 'undefined') ? parseInt(str) : str;
    } else {return str;}
}

/**
 * Private method: set Cookie content.
 * 
 * @param {String} data
 *			  Write additional data in the cookie. Inevitable parameter!
 */
AutoTeaser.prototype.__setCookie = function(data) {
    var nextYear = new Date(), re = eval('/' + this.__strTeaser.replace('.', '\\.') + '\\d+/g'), previous = new String();
    previous = (this.__getCookie(true)).replace(re, '');
    nextYear.setFullYear(nextYear.getFullYear() + 1);
    document.cookie = 'AutoTeaser=' + previous + this.__strTeaser + data + ';' + ' expires=' + nextYear.toGMTString();
}

/**
 * Run & display a teaser (must be called last).
 */
AutoTeaser.prototype.run = function() {
	this.__active = typeof(this.__getCookie()) != 'number' ? 0 : this.__getCookie();
	if(this.__active > this.__teaserList.length - 1) {this.__active = 0;}
	if(this.__teaserList.length) {this.__renderTeaser();}
	this.__setCookie(this.__active + 1);
}
