/*
$Date: 2010-04-13 10:37:52 +0200 (di, 13 apr 2010) $
$Revision: 5335 $
*/

var ePCServiceType  = { ClickToMail:1, ClickToChat:2, WebToPhone:4, CallMeNow:8, CallMeLater:16 };

var pcti_protocol = (document.location.protocol=="https:")? "https:" : "http:";
var pcti_movieLoc = pcti_protocol + "//smartcontactbutton.pushcall.com/TargetInfoJScon.swf";

if(typeof document.compatMode!='undefined'&&document.compatMode!='BackCompat'){
	pc_t1_DOCtp="_top:expression(document.documentElement.scrollTop+document.documentElement.clientHeight-this.clientHeight);_left:expression(document.documentElement.scrollLeft + document.documentElement.clientWidth - offsetWidth);}";
}else{
	pc_t1_DOCtp="_top:expression(document.body.scrollTop+document.body.clientHeight-this.clientHeight);_left:expression(document.body.scrollLeft + document.body.clientWidth - offsetWidth);}";
}

var pcti_fixedCSS='#pcti_fixed{position:fixed;';
pcti_fixedCSS+= '_position:absolute;';
//pcti_fixedCSS+= 'visibility:hidden;';
pcti_fixedCSS+= 'top:0px;';
pcti_fixedCSS+= 'right:0px;';
pcti_fixedCSS+= 'z-index: 10000;';
pcti_fixedCSS+= pc_t1_DOCtp;

document.write('<style type="text/css">'+pcti_fixedCSS+'</style>');
/**
 * Helper function to get an element in the html document. This is more-or-less browser independant
 * @param elementId A string with the id of the element
 */
function pcti_getElement( elementId ){
	if(document.getElementById) return document.getElementById(elementId);
		else if(document.layers) return document.layers[elementId];
			else if(document.all) return eval("document.all."+elementId);
}


/**
 * Helper function to generate the html element which embeds a Flash movie. This code does some browser detection
 * @param element A reference to the parent element that should hold the flash movie object
 * @param movieUrl A string containing URL of the Flash movie (.swf)
 * @param elementId A string containing the id of the element. This can be used for future references to the flash movie
 * @param width An integer value specifying the width of the movie
 * @param height An integer value specifying the height of the movie
 * @param flashVars A string containing the variables which should be passed on to the flash movie (form: "param1name=param1value&param2name=param2value")
 */
function pcti_addFlashElement( element, movieUrl, elementId, width, height, flashVars, transparent ){
	var isIE = navigator.appName.indexOf("Microsoft") != -1;
	
	if (isIE){
		var flashElement = '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" onprogress="aler(\'!\');"';
		flashElement+= 'codebase="'+pcti_protocol+'//download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0"';
		flashElement+= 'width="' + width + '" ';
		flashElement+= 'height="' + height + '" ';
		flashElement+= 'id="' + elementId + '" ';
		flashElement+= 'align="" ';
		flashElement+= '>';
		flashElement+= '<param name="movie" value="' + movieUrl + '"></param>';
		flashElement+= '<param name="quality" value="high" />';
		flashElement+= (transparent)? '<param name="wmode" value="transparent" />' : '';
		flashElement+= '<param name="flashvars" value="' + flashVars + '" />';
		flashElement+= '<param name="allowscriptaccess" value="always" />';
		flashElement+= '</object>';
	}else{
		var flashElement = '<embed " ';
		flashElement+= 'type="application/x-shockwave-flash" ';
		flashElement+= 'src="' + movieUrl + '" ';
		flashElement+= 'width="' + width + '" ';
		flashElement+= 'height="' + height + '" ';
		flashElement+= 'id="' + elementId + '" ';
		flashElement+= 'name="' + elementId + '" ';
		flashElement+= 'align="" ';
		flashElement+= 'flashvars="' + flashVars + '" ';
		flashElement+= (transparent)? 'wmode="transparent" ' : '';
		flashElement+= 'quality="high" ';
		flashElement+= 'allowscriptaccess="always" '; 
		flashElement+= '>';
		flashElement+= '</embed>';
	}
	element.innerHTML = flashElement;
}

/**
 * Helper function to generate a random key of the specified length
 * @param length The number of characters for the random key
 */
function pcti_getRandomKey(length){
	length = ( length != 'undefined' && typeof length == 'number' )? length : 8;
	var randomKey = '';
	var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
	this._objectId = "pcti_";
	for (var i=0; i<length; i++) {
		var rnum = Math.floor(Math.random() * chars.length);
		randomKey += chars.substring(rnum,rnum+1);
	}
	return randomKey;
}

/** 
 * Returns the class name of the argument or undefined if it's not a valid JavaScript object. 
 */  
function pcti_getObjectClass(obj) {  
    if (obj && obj.constructor && obj.constructor.toString) {  
        var arr = obj.constructor.toString().match(  
            /function\s*(\w+)/);  
  
        if (arr && arr.length == 2) {  
            return arr[1];  
        }  
    }  
  
    return undefined;  
}


/**
* Class definition of the Target Info library
*/
function PCTargetInfo( target, options ){
	this._target = target;

	this._switchboardAddress = ( (options !== undefined) && (typeof options.switchboardAddress == 'string') )? options.switchboardAddress : "switchboard.pushcall.com";
	
	//Initialize member variables
	this._requestQueue = new Array();
	this._activeRequest = null;
	
	//generate random name which will be used as objectId
	//globally register the name of the objectId as a variable for the callback requests from the flash movie
	this._objectId = "pcti_" + pcti_getRandomKey(8);
	eval(this._objectId+' = this;');
	
	//create the DIV element for the target info movie
	document.write("<div id=\"pcti_fixed\"></div>");
	this._movieContainter = pcti_getElement( 'pcti_fixed' );
	
	//Put the flash movie in the DIV
	var flashVars = 'callBackObjectId='+this._objectId+'&switchboardAddress='+this._switchboardAddress;
	pcti_addFlashElement( this._movieContainter, pcti_movieLoc, "targetInfoMovie", 1, 1, flashVars, true );
	this._targetInfoMovie = pcti_getElement('targetInfoMovie');
}

PCTargetInfo.prototype.checkAvailability = function( serviceTypes, availabilityCallback ){
	if (availabilityCallback === undefined || typeof availabilityCallback != 'function' ) throw ('no (valid) callback method specified');
	newAvailabilityRequest = new PCAvailabilityRequest( this._target, serviceTypes, availabilityCallback )
	
	this._requestQueue.push( newAvailabilityRequest );
	this.processQueue();
}

PCTargetInfo.prototype.processQueue = function(){
	if( this._activeRequest == null ){
		if( this._requestQueue.length > 0 ){
			this._activeRequest = this._requestQueue.shift();
			this._retryCounter=0;
			switch( pcti_getObjectClass(this._activeRequest) ){
				case 'PCAvailabilityRequest':
					this.postAvailabilityRequest();
					break;
				default:
					alert("unknown request type: " + pcti_getObjectClass(this._activeRequest));
			}
		}
	}
}


PCTargetInfo.prototype.postAvailabilityRequest = function(){
	try{
		this._targetInfoMovie.checkAvailability( this._activeRequest._target, this._activeRequest._serviceTypes );
	}catch(err){
		this._retryCounter++;
		if (this._retryCounter > 200 ) throw(err);
		setTimeout( this._objectId + '.postAvailabilityRequest()', 10);
	}
}

PCTargetInfo.prototype.cbCheckAvailabilitySuccess = function(target, availability){
	//convert JSON string to object
	var availabilityObject = eval('(' + availability + ')');
	
	//call callback method
	this._activeRequest._availabilityCallback( availabilityObject );
	
	this._activeRequest = null;
	this.processQueue();
}

PCTargetInfo.prototype.cbCheckAvailabilityError = function(target, errDetails){
	throw('CheckAvailabilityError: ' + errDetails);
}


function PCAvailabilityRequest( target, serviceTypes, availabilityCallback ){
	this._target = target;
	this._serviceTypes = serviceTypes;
	this._availabilityCallback = availabilityCallback;
}


