// enable image caching
try { document.execCommand("BackgroundImageCache", false, true); } catch(e) {}

function cg_createiframe( id, sr, w, h, extra ) {
	var cgtxt = 'RAME id="' + id + '" cgrczz="' + sr + '" width=' + w + ' height=' + h + ' ' + extra + '></IF';
	cgtxt = cgtxt.replace('cgrczz','src');
	document.write( '<IF' + cgtxt + 'RAME>' );
}

// to fix realurl
function jbgoto(url) {
	var isHttp = new RegExp("^http(|s)://","");
	if ( ! isHttp.test(url)) {
		url = window.location.protocol + "//" + window.location.host + "/" + url;
	}
	
	if(window.opener)
		window.opener.location.href = url;
	else if(window.parent)
		window.parent.location.href = url;
	else
		self.location.href= url
} 


// clear input if text is default text
function clearifdefault(elem,defval) {
	if (elem.value == defval) elem.value ='';
}

/** functions for homepage switch between diaporama and calendar */
function hp_showdiaporama() {
	if (document.getElementById('cgdiaporama').style.display != 'none') return;
	switchto('cgcalendrier','cgdiaporama');
	document.getElementById('navfiche').className = 'actif';
	document.getElementById('navcalendrier').className = '';
}
function hp_showcalendar() {
	if (document.getElementById('cgcalendrier').style.display == 'block') return;
	switchto('cgdiaporama','cgcalendrier');
	document.getElementById('navfiche').className = '';
	document.getElementById('navcalendrier').className = 'actif';
}

function getDocumentSize(doc) {
	var docu = doc;
	return new Array((docu.documentElement && docu.documentElement.scrollWidth) ? docu.documentElement.scrollWidth : (docu.body.scrollWidth > docu.body.offsetWidth) ? docu.body.scrollWidth : docu.body.offsetWidth,(docu.documentElement && docu.documentElement.scrollHeight) ? docu.documentElement.scrollHeight : (docu.body.scrollHeight > docu.body.offsetHeight) ? docu.body.scrollHeight : docu.body.offsetHeight);
}

function getFrameHeight(iframe) {
//		var size = getDocumentSize(iframe.document);
	var size = getDocumentSize(iframe);
	return size[1];
}

function getFrameHeight2(iframe) {
	return document.body.clientHeight;
	var height = -1;
	
	if (iframe.documentElement) {
		height = iframe.documentElement.scrollHeight;
	} else if (iframe.document) {
		if (iframe.document.documentElement) {
			height = iframe.document.documentElement.scrollHeight;
		} else if (iframe.document.body) {
//			(docu.body.scrollHeight > docu.body.offsetHeight) ? docu.body.scrollHeight : docu.body.offsetHeight
			height = iframe.document.body.scrollHeight;
		}
	}
	return height;
}

function resizeIframe() {
	if(self==parent) return false; /* Checks that page is in iframe. */
	var iframe=window.frameElement;
	if (!iframe) return false;

	var height = getFrameHeight2(iframe);
	height = ( height < 500 ) ? 500 : height;

	iframe.style.overflow="hidden"; 
	iframe.style.height= height + "px";
}


function addLoadEvent(func) { 
  if (typeof window.onload != 'function') { 
	window.onload = func; 
  } else { 
       var oldonload = window.onload; 
	window.onload = function() { 
	  if (oldonload) { 
		oldonload(); 
	  } 
	  func(); 
	};
  } 
} 

function cg_getElementsByClass(node,searchClass,tag) {
	var classElements = new Array();
	var els = node.getElementsByTagName(tag); // use "*" for all elements
	var elsLen = els.length;
	var pattern = new RegExp("\\b"+searchClass+"\\b");
	for (ii = 0, j = 0; ii < elsLen; ii++) {
		 if ( pattern.test(els[ii].className) ) {
		 classElements[j] = els[ii];
		 j++;
		}
	}
//alert( "found " + classElements.length + " elems for " + tag + "." + searchClass);
	return classElements;
}

/** prototype.js makes Array.prototype unusable */
/*
// Array overload
Array.prototype.cg_insertAt = function(idx, val){
	this.splice( idx, 0, val);
};

Array.prototype.cg_addSortVal = function(k, val) {
//alert( "adding " + k );
	var el = { key: k, elem : val} ;
	if (this.length == 0)  {
		this.push(el)
		return;
	}	
	for (var i = 0; i < this.length; i++) {
		if (+ this[i].key > +k) {
			this.cg_insertAt(i, el);
			return;
		}
	}
	this.cg_insertAt(i, { key: k, elem : val} );
};

Array.prototype.cg_arrayFromSorted = function() {
	var res = new Array();
	for (var i = 0; i < this.length; i++) {
		res.push( this[i].elem );
	}
	return res;
};
*/

ArrayinsertAt = function(arr,idx, val){
	arr.splice( idx, 0, val);
//alert("after insert at " + idx + " : " + arr.length);
	return arr;	
};

ArrayaddSortVal = function(arr, k, val) {
//alert( "adding " + k );
	var el = { key: k, elem : val} ;
	if (arr.length == 0)  {
		arr.push(el)
		return arr;
	}	
	for (var i = 0; i < arr.length; i++) {
		if (+ arr[i].key > +k) {
			return ArrayinsertAt(arr,i, el);
		}
	}
	return ArrayinsertAt(arr,i, el);
};

ArrayarrayFromSorted = function( arr ) {
	var res = new Array();
	for (var i = 0; i < arr.length; i++) {
		res.push( arr[i].elem );
	}
	return res;
};



function cogitae_get_imageurl( IFF ) {
   var step = 5000;
   var rep = Math.round( (IFF / step) -0.5) * step;
   if (!rep) rep = '00000'; 
   if (rep == 5000) rep = '05000';
   return 'http://images-fr.traveltainment.de/images/content/booking_thumbs/' + rep + '/THB_999_H' + IFF + '.jpg';
}


function imagepush( IFF ) {
   document.write( '<img class="photoproduit" src="' + cogitae_get_imageurl( IFF ) + '">');
}
