var currentTimestamp = new Date().getTime();

var preloadedImages = new Array();

function transition( counter, baseId, numberOfImages, timeOut) {
	if(counter > 0) {
		new Effect.Appear( baseId + (counter)%numberOfImages, { duration: 1.5, from: 0.0, to: 1.0} );
		new Effect.Appear( baseId + (counter-1)%numberOfImages, { duration: 1.5, from: 1.0, to: 0.0} );
	}
	counter++;
	var timeout = timeOut;
	if(counter == 1) {
		timeout -= 1500;
	}
	setTimeout ( 'transition(' + counter + ', "' + baseId + '", ' + numberOfImages + ', ' + timeOut + ' )', timeout); 
}

/**
* The userInputIsBlocked is incremented for every request to blockUserInput(). 
* Only for the first request, a blocking layer is created. The user input is unblocked
* once the userInputIsBlocked is zero again.
* Reason is that multiple scripts can require blocking user input at the same time,
* without being aware of other blocks. Thus, only until all blocks are released, the layer is removed.
*/
var userInputIsBlocked = 0;
var userInputBlockLayer = null;

/**
* Block the user input by placing a div with opacity and zIndez BIG.
*/
function blockUserInput() {

	if(userInputIsBlocked == 0) {
	
		var sXml = '<div class="block-user-input-layer" />';
		userInputBlockLayer = bb.html.createElementFromString(sXml);
	
		// set dimension of overlay to 100%
		// prevents unnecessary scrollbars when decreasing window size
		userInputBlockLayer.style.height = '100%';
		userInputBlockLayer.style.width = '100%';
	
		/*var position = bb.html.getStyle(document.body.parentNode, "position");
		if((document.body.parentNode.nodeName.toLowerCase()!='body') && (position != "relative") && (position != "absolute") && (position != "fixed")) {
			document.body.parentNode.style.position = "relative";
		}*/

		setBlockUserInputLayerDimensions();	
	
		userInputBlockLayer.style.zIndex = 10000;
	
		document.body.appendChild(userInputBlockLayer);
	
		if(bb.browser.ie) {
			window.attachEvent('onresize', resizeBlockUserInputLayer);
		} else {
			window.addEventListener('resize', resizeBlockUserInputLayer, false);
		}
	}
	
	userInputIsBlocked++;
}

/**
* Unblock the user input by remove the layer.
*/ 
function unblockUserInput() {

	if(userInputIsBlocked == 1) {
	
		document.body.removeChild(userInputBlockLayer);

		if(bb.browser.ie) {
			window.detachEvent('onresize', resizeBlockUserInputLayer);
		} else {
			window.removeEventListener('resize', resizeBlockUserInputLayer, false);
		}

	}
	
	if(userInputIsBlocked > 0) {
		userInputIsBlocked--;
	}
}

/**
* Sets the dimensions of the user input block layer.
*/
function setBlockUserInputLayerDimensions() {

	var iHeight, iWidth;

	iHeight = Math.max(document.body.parentNode.scrollHeight,document.body.parentNode.offsetHeight);
	iWidth = Math.max(document.body.parentNode.scrollWidth,document.body.parentNode.offsetWidth);

	var applyDimensions = function() {
		userInputBlockLayer.style.height = iHeight + 'px';
		userInputBlockLayer.style.width = iWidth + 'px';
	}

	if(bb.browser.ie) {
		setTimeout(applyDimensions, 1);
	} else {
		applyDimensions();
	}
}


/**
* Resizes the user input layer after window has been resized.
*/
function resizeBlockUserInputLayer() {

	// temporarily release the block layer to exclude it from the scrollHeight/offsetHeight 
	// calculation
	document.body.removeChild(userInputBlockLayer);
	setBlockUserInputLayerDimensions();
	document.body.appendChild(userInputBlockLayer);
}

function checkAll(groep) {
	for(i = 0; i < document.getElementsByTagName("input").length; i++){
		var cb = document.getElementsByTagName("input")[i];
		if(cb.name == 'naar[' + groep + '][]') {
			cb.checked = true;
		}
	}
}
	
function unCheckAll(groep) {
	for(i = 0; i < document.getElementsByTagName("input").length; i++){
		var cb = document.getElementsByTagName("input")[i];
		if(cb.name == 'naar[' + groep + '][]') {
			cb.checked = false;
		}
	}
}

/**
*
*  Base64 encode / decode
*  http://www.webtoolkit.info/
*
**/

var Base64 = {

    // private property
    _keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",

    // public method for encoding
    encode : function (input) {
        var output = "";
        var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
        var i = 0;

        input = Base64._utf8_encode(input);

        while (i < input.length) {

            chr1 = input.charCodeAt(i++);
            chr2 = input.charCodeAt(i++);
            chr3 = input.charCodeAt(i++);

            enc1 = chr1 >> 2;
            enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
            enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
            enc4 = chr3 & 63;

            if (isNaN(chr2)) {
                enc3 = enc4 = 64;
            } else if (isNaN(chr3)) {
                enc4 = 64;
            }

            output = output +
            this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) +
            this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);

        }

        return output;
    },

    // public method for decoding
    decode : function (input) {
        var output = "";
        var chr1, chr2, chr3;
        var enc1, enc2, enc3, enc4;
        var i = 0;

        input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");

        while (i < input.length) {

            enc1 = this._keyStr.indexOf(input.charAt(i++));
            enc2 = this._keyStr.indexOf(input.charAt(i++));
            enc3 = this._keyStr.indexOf(input.charAt(i++));
            enc4 = this._keyStr.indexOf(input.charAt(i++));

            chr1 = (enc1 << 2) | (enc2 >> 4);
            chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
            chr3 = ((enc3 & 3) << 6) | enc4;

            output = output + String.fromCharCode(chr1);

            if (enc3 != 64) {
                output = output + String.fromCharCode(chr2);
            }
            if (enc4 != 64) {
                output = output + String.fromCharCode(chr3);
            }

        }

        output = Base64._utf8_decode(output);

        return output;

    },

    // private method for UTF-8 encoding
    _utf8_encode : function (string) {
        string = string.replace(/\r\n/g,"\n");
        var utftext = "";

        for (var n = 0; n < string.length; n++) {

            var c = string.charCodeAt(n);

            if (c < 128) {
                utftext += String.fromCharCode(c);
            }
            else if((c > 127) && (c < 2048)) {
                utftext += String.fromCharCode((c >> 6) | 192);
                utftext += String.fromCharCode((c & 63) | 128);
            }
            else {
                utftext += String.fromCharCode((c >> 12) | 224);
                utftext += String.fromCharCode(((c >> 6) & 63) | 128);
                utftext += String.fromCharCode((c & 63) | 128);
            }

        }

        return utftext;
    },

    // private method for UTF-8 decoding
    _utf8_decode : function (utftext) {
        var string = "";
        var i = 0;
        var c = c1 = c2 = 0;

        while ( i < utftext.length ) {

            c = utftext.charCodeAt(i);

            if (c < 128) {
                string += String.fromCharCode(c);
                i++;
            }
            else if((c > 191) && (c < 224)) {
                c2 = utftext.charCodeAt(i+1);
                string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
                i += 2;
            }
            else {
                c2 = utftext.charCodeAt(i+1);
                c3 = utftext.charCodeAt(i+2);
                string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
                i += 3;
            }

        }

        return string;
    }

}

function updateSubCatId( campaignId, subCatId ) {
	var subCat = bb.document.getElementById( 'subcategory_id' )
	var actId = document.getElementById( 'camp_act_id' ).value;
	
	if( campaignId == 0 ) {
		subCat.setAttribute( 'disabled', '' );
	} else {
		subCat.setProperty( 'value', subCatId );
		subCat.setAttribute( 'disabled', 'disabled' );
	}
	
	bb.command.load('/campaignplanner/index/editactivityvalue/id/'+actId+'/sub_cat_id/'+subCatId+'/value/', 'GET', '', null, bb.document.getElementById('div_camp_act_value_print'), 'replaceChildren');
} 

function getFlashMovieObject(movieName)
{
  if (window.document[movieName]) 
  {
      return window.document[movieName];
  }
  if (navigator.appName.indexOf("Microsoft Internet")==-1)
  {
    if (document.embeds && document.embeds[movieName])
		return document.embeds[movieName]
	else {
		var oElement = Ext.select('object#' + movieName).first();
		if (oElement) {
			return oElement.dom;
		}
	}
  }
  else // if (navigator.appName.indexOf("Microsoft Internet")!=-1)
  {
    return document.getElementById(movieName);
  }
}
 
function donothing() {
	
}

Ext.QuickTips.init();

//Plugin is configured with a listeners config object.
//The Component is appended to the argument list of all handler functions.
Ext.ElementEvents = Ext.extend(Object, {
 constructor: function(config) {
     this.listeners = config.listeners ? config.listeners : config;
 },

 // Component passes itself into plugin's init method
 init: function(c) {
     var p, l = this.listeners;
     /*for (p in l) {
         if (Ext.isFunction(l[p])) {
             l[p] = this.createHandler(l[p], c);
         } else {
             l[p].fn = this.createHandler(l[p].fn, c);
         }
     }*/

     // Add the listeners to the Element immediately following the render call
     c.render = c.render.createSequence(function() {
         var e = c.getEl();
         if (e) {
        	 for(p in l) {
        		 e.on(p, l[p]);
        	 }
         }
     });
 }
});
