/*  Prototype JavaScript framework, version 1.6.0.2
 *  (c) 2005-2008 Creo Interactive
 *
 *--------------------------------------------------------------------------*/

// Outputs the banner to the page
function display_advert(advert_array,div_id) {
	//Get the banner to display
	var str = "";
	if(advert_array == undefined || advert_array.length == 0) return str;

	var current_array = advert_array.pop();
	switch(current_array[1]){
	case "image":

		// Add the opening href
		if(current_array[4] != "") {
			str = '<div class="advert_element">';
			str = str + '<a href="' +current_array[4]+ '" rel="external" ';
			// Insert our tracking code
			str = str + 'onclick="javascript:';

			if(current_array[5].indexOf('internal_click')) str = str + 'ajax_update_advert_stats('+current_array[0]+' , \'clicked\');';

			// Is there a google key?
			if(current_array[5].indexOf('google_click')) str = str + 'urchinTracker(\'advert\',' + current_array[0] + ');';

			str = str + 'return true;\">';
		}

		// Add image tag
		str = str + '<img src="/creo_files' +current_array[2] +'" ';
		if(current_array[6] != '') str = str + 'height="'+ current_array[6] +'" ';
		if(current_array[7] != '') str = str + 'width="'+ current_array[7] +'" ';
		str = str + 'border="0" ';
		str = str + 'class="banner" ';
		str = str + '/>';

		// Add the closing href
		if(current_array[4] != "") str = str + '</a></div>';

		// Call the viewed function if you are tracking that 
		if(current_array[5].indexOf('internal_views')) ajax_update_advert_stats(current_array[0], 'viewed');
		break;

	case "flash":

		// prepare filename
		var flashFilename = current_array[2];
		flashFilename = flashFilename.replace(".swf", "");
		flashFilename = '/creo_files' + flashFilename;
		flashVarCode = 'advert_id=' + current_array[0];
		if(current_array[4] != "") flashVarCode = flashVarCode + '&amp;clickTAG=' + current_array[4];
		if(current_array[8] == 'false') {wmode_value = 'opaque'} else {wmode_value = 'transparent'};

		str = AC_FL_RunContent(
			'codebase','http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab##version=7,0,19,0',
			'width',current_array[7],
			'height',current_array[6],
			'title','Banner',
			'src',flashFilename,
			'quality','high',
			'FlashVars',flashVarCode ,
			'pluginspage','http://www.macromedia.com/go/getflashplayer',
			'movie',flashFilename,
			'wmode',wmode_value
		);
		return;
		break; 
		
	case "html":		
		str = '<div class="advert_top">' + current_array[3] + '</div>';
		 str = str + '<div class="advert_bottom"></div>';
		 if(current_array[9] != '') {
			str = str + '<div class="inner_advert">' + current_array[9] + '</div>';	 
		 }
		 
		 document.getElementById(div_id).innerHTML = str; 
		 
		 return;
	default:
		return str;
	}		
	// Insert html content	
	
	if(str != '') {
		document.getElementById(div_id).innerHTML = str; 
	}

};

function display_advert2(advert_array,div_id ) {

	//Get the banner to display
	var str = "";
	if(advert_array == undefined || advert_array.length == 0){
		return str;
	}

	var current_array = advert_array.pop();
	switch(current_array[1]){
	case "image":

		// Add the opening href
		if(current_array[4] != ""){
			str = '<div class="advert_element">';
			str = str + '<a href="/creo_admin/includes_scripts/' +current_array[4]+ '" ';
			// Insert our tracking code
			str = str + 'onclick="javascript:';
						
			if(current_array[5].indexOf('internal_click')){	
				str = str + 'ajax_update_advert_stats('+current_array[0]+' , \'clicked\');';	
			}
			// Is there a google key?
			if(current_array[5].indexOf('google_click')){
				str = str + 'urchinTracker(\'advert\',' + current_array[0] + ');';	
			}

			str = str + 'return true;\">';	
		}
		
		// Add image tag
		str = str + '<img class="banner" src="/creo_files' +current_array[2] +'" ';
		if(current_array[6] != ''){
			str = str + 'height="'+ current_array[6] +'" ';		
		}
		if(current_array[7] != ''){
			str = str + 'weight="'+ current_array[7] +'" ';		
		}		
		str = str + 'border="0"';
		str = str + '>';
		
		// Add the closing href
		if(current_array[4] != ""){
			str = str + '</a>';
		}
		str = str + '</div>';
		// Call the viewed function if you are tracking that 
		if(current_array[5].indexOf('internal_views')){
			ajax_update_advert_stats(current_array[0], 'viewed');
		}
		break; 

	case "flash":

		// prepare filename
		var flashFilename = current_array[2];
		flashFilename = flashFilename.replace(".swf", "");
		flashFilename = '/creo_files' + flashFilename;
		flashVarCode = 'advert_id=' + current_array[0];
		
		str = AC_FL_RunContent(
			'codebase','http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab##version=7,0,19,0',
			'width',current_array[7],
			'height',current_array[6],
			'title','Banner',
			'src',flashFilename,
			'quality','high',
			'FlashVars',flashVarCode ,
			'pluginspage','http://www.macromedia.com/go/getflashplayer',
			'movie',flashFilename,
			'wmode','transparent'
		);
		return;
		break; 
		
	case "html":		
		return current_array[3];
		
	default:
		return str;
	}		
	// Insert html content	
	if(str != '') { 
		document.getElementById(div_id).innerHTML = str; 
	}

};

function ajax_update_advert_stats(advert_id, action) {
	switch(action){
		case "clicked":
			//alert("hi");
			var pars = '';
			apiurl = "/creo_admin/run_task.cfm?task_name=advert_click&advert_id=" + advert_id + "&rand="+Math.random();
			//alert(apiurl);
			ajaxRequest = new Ajax.Request(apiurl, {method: 'get', parameters: pars, onComplete: passThrough});
			//alert('hi');
		break; 
		case "viewed":
			var pars = '';	
			apiurl = "/creo_admin/run_task.cfm?task_name=advert_viewed&advert_id=" + advert_id + "&rand="+Math.random();
			ajaxRequest = new Ajax.Request(apiurl, {method: 'get', parameters: pars, onComplete: passThrough});
		break; 
	}
	return false;
}

function passThrough( originalRequest )
{
	// Helps debug api errors
	 //alert( originalRequest.responseText );
}


// -- Array based functions
// Array.concat() - Join two arrays
if( typeof Array.prototype.concat==='undefined' ) {
 Array.prototype.concat = function( a ) {
  for( var i = 0, b = this.copy(); i<a.length; i++ ) {
   b[b.length] = a[i];
  }
  return b;
  };
}

// Array.copy() - Copy an array
if( typeof Array.prototype.copy==='undefined' ) {
 Array.prototype.copy = function() {
  var a = [], i = this.length;
  while( i-- ) {
   a[i] = typeof this[i].copy!=='undefined' ? this[i].copy() : this[i];
  }
  return a;
 };
}

// Array.pop() - Remove and return the last element of an array
if( typeof Array.prototype.pop==='undefined' ) {
 Array.prototype.pop = function() {
  var b = this[this.length-1];
  this.length--;
  return b;
 };
}

// Array.push() - Add an element to the end of an array, return the new length
if( typeof Array.prototype.push==='undefined' ) {
 Array.prototype.push = function() {
  for( var i = 0, b = this.length, a = arguments, l = a.length; i<l; i++ ) {
   this[b+i] = a[i];
  }
  return this.length;
 };
}

// Array.shift() - Remove and return the first element
if( typeof Array.prototype.shift==='undefined' ) {
 Array.prototype.shift = function() {
  for( var i = 0, b = this[0], l = this.length-1; i<l; i++ ) {
   this[i] = this[i+1];
  }
  this.length--;
  return b;
 };
}

// Array.slice() - Copy and return several elements
if( typeof Array.prototype.slice==='undefined' ) {
 Array.prototype.slice = function( a, c ) {
  var i, l = this.length, r = [];
  if( !c ) { c = l; }
  if( c<0 ) { c = l + c; }
  if( a<0 ) { a = l - a; }
  if( c<a ) { i = a; a = c; c = i; }
  for( i = 0; i < c - a; i++ ) { r[i] = this[a+i]; }
  return r;
 };
}

// Array.splice() - Remove or replace several elements and return any deleted elements
if( typeof Array.prototype.splice==='undefined' ) {
 Array.prototype.splice = function( a, c ) {
  var i = 0, e = arguments, d = this.copy(), f = a, l = this.length;
  if( !c ) { c = l - a; }
  for( i; i < e.length - 2; i++ ) { this[a + i] = e[i + 2]; }
  for( a; a < l - c; a++ ) { this[a + e.length - 2] = d[a - c]; }
  this.length -= c - e.length + 2;
  return d.slice( f, f + c );
 };
}

// Array.unshift() - Add an element to the beginning of an array
if( typeof Array.prototype.unshift==='undefined' ) {
 Array.prototype.unshift = function() {
  this.reverse();
  var a = arguments, i = a.length;
  while(i--) { this.push(a[i]); }
  this.reverse();
  return this.length;
 };
}

// -- 4umi additional functions

// Array.forEach( function ) - Apply a function to each element
Array.prototype.forEach = function( f ) {
 var i = this.length, j, l = this.length;
 for( i=0; i<l; i++ ) { if( ( j = this[i] ) ) { f( j ); } }
};

// Array.indexOf( value, begin, strict ) - Return index of the first element that matches value
Array.prototype.indexOf = function( v, b, s ) {
 for( var i = +b || 0, l = this.length; i < l; i++ ) {
  if( this[i]===v || s && this[i]==v ) { return i; }
 }
 return -1;
};

// Array.insert( index, value ) - Insert value at index, without overwriting existing keys
Array.prototype.insert = function( i, v ) {
 if( i>=0 ) {
  var a = this.slice(), b = a.splice( i );
  a[i] = v;
  return a.concat( b );
 }
};

// Array.lastIndexOf( value, begin, strict ) - Return index of the last element that matches value
Array.prototype.lastIndexOf = function( v, b, s ) {
 b = +b || 0;
 var i = this.length; while(i-->b) {
  if( this[i]===v || s && this[i]==v ) { return i; }
 }
 return -1;
};

// Array.random( range ) - Return a random element, optionally up to or from range
Array.prototype.random = function( r ) {
 var i = 0, l = this.length;
 if( !r ) { r = this.length; }
 else if( r > 0 ) { r = r % l; }
 else { i = r; r = l + r % l; }
 return this[ Math.floor( r * Math.random() - i ) ];
};

// Array.shuffle( deep ) - Randomly interchange elements
Array.prototype.shuffle = function( b ) {
 var i = this.length, j, t;
 while( i ) {
  j = Math.floor( ( i-- ) * Math.random() );
  t = b && typeof this[i].shuffle!=='undefined' ? this[i].shuffle() : this[i];
  this[i] = this[j];
  this[j] = t;
 }
 return this;
};

// Array.unique( strict ) - Remove duplicate values
Array.prototype.unique = function( b ) {
 var a = [], i, l = this.length;
 for( i=0; i<l; i++ ) {
  if( a.indexOf( this[i], 0, b ) < 0 ) { a.push( this[i] ); }
 }
 return a;
};

// Array.walk() - Change each value according to a callback function
Array.prototype.walk = function( f ) {
 var a = [], i = this.length;
 while(i--) { a.push( f( this[i] ) ); }
 return a.reverse();
};