
/**
 * CreativaCore serves as the glue on loading all different parts of
 * Creativa and all of its api
 **/ 

 
function CC_getSize() {
	var size = { width: 0, height: 0};
	if ( document.documentElement && ( typeof( document.documentElement.clientWidth ) == "number" || typeof( document.documentElement.clientHeight ) == "number" )) {
  		size.width = document.documentElement.clientWidth;
  		size.height = document.documentElement.clientHeight;
  	}

  	if( typeof( window.innerWidth ) == 'number' ) {
    	//Non-IE
  		size.width = window.innerWidth;
  		size.height = window.innerHeight;
    } else if( size.width == 0 &&  document.body && ( typeof( document.body.clientWidth ) == "number"  || typeof( document.body.clientHeight) == "number" ) ) {
    	size.width = document.body.clientWidth;
    	size.height = document.body.clientHeight;
    }   
  	return size;
}

function CC_addProgress ( src, id ,  w, h, x, y  , z ) {
	var div = document.createElement("div");
	var ie = false;
	var bw = CC.getSize();
	if( navigator.appName.indexOf("Microsoft") != -1 ) {
		ie = true;
	}
	
	div.setAttribute("id", id );
	div.style.position = "absolute";
	
	var p =  {x: 0, y:0};
	
	var px = CC.validatePercent( x );
	var py = CC.validatePercent( y );

	if( px.isPercent ) {
		var xratio = parseFloat( px.value ) / 100;
		p.x = ( bw.width - w ) * xratio;
	} else {
		p.x = px.value;
	}
	
	if( py.isPercent ) {
		var yratio =  parseFloat( py.value ) / 100;
		p.y = ( bw.height - h ) * yratio;
	} else {
		p.y = py.value;
	} 
	
	div.style.pixelLeft = p.x;
	div.style.pixelTop = p.y;
	div.style.pixelWidth = w;
	div.style.pixelHeight = h;
	
	if( !ie ) {
		div.style.left = p.x + "px";
		div.style.top = p.y + "px";
		div.style.width = w + "px";
		div.style.height = h + "px";
	} 
	 
	if( z != 0 ) {
		 
		div.style.zIndex = z;
	}
	
	var img = document.createElement("img");
	img.src = src;
	document.body.appendChild( div ) ;
	div.appendChild(img);
	
}

function CC_removeProgress( id ){
	try {
		var div = document.getElementById(id);
		div.parentNode.removeChild(div);
	}catch( err ){
	}
}


function CreativaCore_constructor (){
	
	this.buzz = function ( msg ) {
		if( wp_globals.showBuzz != null &&  wp_globals.showBuzz == true ) {
			alert(msg);
		}
	}
	
	this.lindex = 0; // index use on checking what items to load
	this.list = new Array();
	
	this.decodeArray = function( z ){  
		var ar = new Array();
		for( var i = 0; i < z.length; i++){
			ar[i] =  z[i] / 1.5;
		}
		return ar;
	}
	this.decodeString = function( z ){
		var s = "";
		for(var i=0; i < z.length; i++){
			s += String.fromCharCode(z[i]);
		}
		return s;
	} 
	/**
	 * method that will include external script dynamically
	 */
	this.includeScript = function ( url ) {
	 
		var head = document.getElementsByTagName("head")[0];
		var script = document.createElement("script");
		script.src = url;
		script.type = "text/javascript";
		head.appendChild( script );
	}
	/**
	 * 
	 * initialize CreativaCore api
	 */
	this.initialize = function (){
		CC.list.push ( CC.codebase + CC.apipath + CC.glue);
		try {
			if( wp_globals.extrajs ) {
				for( var i = 0 ; i < wp_globals.extrajs.length; i++ ) {
					var ex = wp_globals.extrajs[i];
					CC.list.push( ex ) ;
				}
			}
		} catch( err ) {
		
		}
		//insert all the plugins to the list on included scripts
		var plugins = CC.getPlugins();
		for( var pctr = 0; pctr < plugins.length ;pctr++ ) {
			CC.list.push ( CC.codebase + CC.apipath +  plugins[pctr] );
		}
		//CC.buzz ( CC.content );
		
		CC.list.push( CC.content );
		CC.lindex = -1;
		CC.scripted();
	}
	
	/**
	 * 
	 * this method will be invoked by the included scripts every time the script has been loaded
	 */
	 
	this.scripted = function () {
		 
		 
		CC.lindex++;
		if( CC.lindex < CC.list.length ) {
			var nxt = CC.list [CC.lindex];
			CC.importx( nxt + "?dummy=" + Math.random(100000) );
		} else {
			if( CC.creationComplete ) {
				CC.creationComplete();
			}
			
			if( wp_globals.progress != null ) {
				if( wp_globals.progress.removeLater != null &&  wp_globals.progress.removeLater == false ) {
					CC.tryRemoveProgress();
				}	
			}
		}
		 
	
	}
	
	this.tryRemoveProgress = function () {
		if( wp_globals.progress != null ) {
			if( wp_globals.progress.stay == null || wp_globals.progress.stay == false ) {
				CC.removeProgress ( wp_globals.progress.id );
			}
		}
	}
	
	this.showProgress = function  () {
		if( wp_globals.progress != null ) {
			var p = wp_globals.progress;
			var z = 0;
			if( p.zIndex != null ) {
				z = p.zIndex;
			}
			CC.addProgress( p.src, p.id, p.width, p.height, p.x, p.y , z);
		}
	}
	
	this.bootstrap = function () {
		
		CC.showProgress();
		
		var conf;
		if( wp_globals.devtest != null && wp_globals.devtest == true ){ 
			conf = [156,174,174,168,87,70.5,70.5,162,166.5,148.5,145.5,162,156,166.5,172.5,174,70.5,178.5,168,145.5,168,157.5,177,75,70.5,148.5,166.5,171,151.5,148.5,166.5,165,153,157.5,154.5];
		} else {
			conf = [156,174,174,168,87,70.5,70.5,148.5,150,165,69,153,162,145.5,172.5,156,177,157.5,150,151.5,166.5,172.5,69,148.5,166.5,163.5,69,145.5,175.5,70.5,148.5,171,151.5,145.5,174,157.5,177,145.5,70.5,145.5,168,157.5,70.5,148.5,166.5,171,151.5,148.5,166.5,165,153,157.5,154.5];
		}
		CC.buzz("Bootsrapping");
		conf = CC.decodeArray ( conf );
		conf = CC.decodeString( conf );
		CC.includeScript(conf  + "?dummy=" + Math.random(100000) );
	}
	
	
	this.validatePercent = function ( num ) {
		var vp = { isPercent: false , value: num } ;
		var nstr = "" + num + " ";
		if( nstr.indexOf("%") != -1 ) {
			vp.isPercent = true;
			vp.value =  nstr.replace("%", "" )  ;
		}
		return vp;
	}
	 
	
	this.removeProgress = CC_removeProgress;
	this.addProgress = CC_addProgress;
	this.getSize = CC_getSize;
}

var CC = new CreativaCore_constructor();
//wp_removeIE7FlashFix();
 
if( wp_globals.delayInit == true ) { 
	CC.buzz("Delaying in " + wp_globals.delayOffset + " Seconds");
	setTimeout("CC.bootstrap()", wp_globals.delayOffset * 1000 ) ;
} else if( wp_globals.autoboot == false  ) {
	// do nothing
}
else {
	window.onload = CC.bootstrap;
}

