//
// Utilities for caroleblouin.com
//

// Returns the version of Internet Explorer or a -1
// (indicating the use of another browser).
function getInternetExplorerVersion()
{
  var rv = -1; // Return value assumes failure.
  if (navigator.appName == 'Microsoft Internet Explorer')
  {
    var ua = navigator.userAgent;
    var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
    if (re.exec(ua) != null)
      rv = parseFloat( RegExp.$1 );
  }
  return rv;
}


function isIE() 
{
  var version = getInternetExplorerVersion();
  if (version < 0)
  	return false;
  else
  	return true;
}


function getFloatingWindowAnchor( link, maxWidth, maxHeight )
{
   var pageSize = getPageSize();
   winW = pageSize[2] - 100;
   winH = pageSize[3] - 100;
   
   if ( maxWidth != "undefined" && winW > maxWidth )
   	winW = maxWidth;
   if ( maxHeight != "undefined" && winH > maxHeight )
        winH = maxHeight;
   
   return " <a href='"+link+"?"+(typeof(parameters)!="undefined"?(parameters+"&"):"")+"placeValuesBeforeTB_=savedValues&TB_iframe=true&height="+winH+"&width="+winW+"&modal=false' title='' class='thickbox'>";  
}


function getFloatingWindowHref( token, maxWidth, maxHeight )
{
   var pageSize = getPageSize();
   winW = pageSize[2] - 100;
   winH = pageSize[3] - 100;

   
   if ( maxWidth != "undefined" && winW > maxWidth )
      winW = maxWidth;
   if ( maxHeight != "undefined" && winH > maxHeight )
      winH = maxHeight;
   
   var newHref = location.href;
   if ( newHref.indexOf('?') != -1 )
   {
      newHref = location.href.substring(0, location.href.indexOf('?') );
   }

   return newHref + "#TB_inline?height=" + winH + "&width="+ winW +"&inlineId="+ token + "&modal=false";
}


//
// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.org
// Edit for Firefox by pHaez
//
function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}

	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}

function attachOnloadHandler(handler)
{
    if (window.addEventListener) //DOM method for binding an event
        window.addEventListener("load", handler, false);
    else if (window.attachEvent) //IE exclusive method for binding an event
        window.attachEvent("onload", handler);
    else if (document.getElementById) //support older modern browsers
       window.onload=handler;
}

function extractNumber(obj, decimalPlaces, allowNegative)
{
	var temp = obj.value;
	
	// avoid changing things if already formatted correctly
	var reg0Str = '[0-9]*';
	if (decimalPlaces > 0) {
		reg0Str += '\\.?[0-9]{0,' + decimalPlaces + '}';
	} else if (decimalPlaces < 0) {
		reg0Str += '\\.?[0-9]*';
	}
	reg0Str = allowNegative ? '^-?' + reg0Str : '^' + reg0Str;
	reg0Str = reg0Str + '$';
	var reg0 = new RegExp(reg0Str);
	if (reg0.test(temp)) return true;

	// first replace all non numbers
	var reg1Str = '[^0-9' + (decimalPlaces != 0 ? '.' : '') + (allowNegative ? '-' : '') + ']';
	var reg1 = new RegExp(reg1Str, 'g');
	temp = temp.replace(reg1, '');

	if (allowNegative) {
		// replace extra negative
		var hasNegative = temp.length > 0 && temp.charAt(0) == '-';
		var reg2 = /-/g;
		temp = temp.replace(reg2, '');
		if (hasNegative) temp = '-' + temp;
	}
	
	if (decimalPlaces != 0) {
		var reg3 = /\./g;
		var reg3Array = reg3.exec(temp);
		if (reg3Array != null) {
			// keep only first occurrence of .
			//  and the number of places specified by decimalPlaces or the entire string if decimalPlaces < 0
			var reg3Right = temp.substring(reg3Array.index + reg3Array[0].length);
			reg3Right = reg3Right.replace(reg3, '');
			reg3Right = decimalPlaces > 0 ? reg3Right.substring(0, decimalPlaces) : reg3Right;
			temp = temp.substring(0,reg3Array.index) + '.' + reg3Right;
		}
	}
	
	obj.value = temp;
}

function blockNonNumbers(obj, e, allowDecimal, allowNegative)
{
	var key;
	var isCtrl = false;
	var keychar;
	var reg;
		
	if(window.event) {
		key = e.keyCode;
		isCtrl = window.event.ctrlKey
	}
	else if(e.which) {
		key = e.which;
		isCtrl = e.ctrlKey;
	}
	
	if (isNaN(key)) return true;
	
	keychar = String.fromCharCode(key);
	
	// check for backspace or delete, or if Ctrl was pressed
	if (key == 8 || isCtrl)
	{
		return true;
	}

	reg = /\d/;
	var isFirstN = allowNegative ? keychar == '-' && obj.value.indexOf('-') == -1 : false;
	var isFirstD = allowDecimal ? keychar == '.' && obj.value.indexOf('.') == -1 : false;
	
	return isFirstN || isFirstD || reg.test(keychar);
}


function showQuoteList()
{
    var $j = jQuery.noConflict();
	$j('#quoteList').show();	
	$j('#quoteList').innerfade({
		animationtype: 'fade',
		speed: 2000,
		timeout: 10000,
		type: 'random',
		containerheight: '110px'
	});
}


function enableQuotes()
{
    var $j = jQuery.noConflict();
    $j(document).ready(
	 function(){
	    showQuoteList();
    });
}


function getThumbnail(folderName, filePrefix, fileSuffix, title, text, thumbnailText, width, height)
{
    var thumbnailTextHtml = "";
    var imgStyle = "";
    
    if (thumbnailText == null || thumbnailText == "") {
       thumbnailTextHtml = "";
       imgStyle = "";
    }
    else {
       thumbnailTextHtml = "<div class='thumbnailText'>"+thumbnailText+"</div>";
       imgStyle = "style='display: block; margin-left: auto; margin-right: auto'";
    }
       
    var divStyle = "style='width: " + (width == null ? "auto;" : width + "px;") 
                   + "height: " + (height == null ? "auto;" : height + "px;") + "border: none; padding: 5px'";
       
    return "<div class='thumbnail' "+ divStyle +">"+
           "<a href='images/"+folderName+"/"+filePrefix+fileSuffix+".jpg' title='"+title+"' text='"+text+"' rel='lightbox["+filePrefix+"]'>" +
           "<img src='images/"+folderName+"/thumb"+filePrefix+fileSuffix+".jpg' border=0  alt='"+title+"' "+ imgStyle +"/></a>"+
           thumbnailTextHtml+"</div>";          
}

function getFooter()
{
    	footer = "<div class='footer'>Copyright " + getCurrentYear() + " - Tous droits réservés "
    		+ "<a href='mailto:sylvain.st-denis@666danse.com'>Carole Blouin et Sylvain St-Denis</a>"
    		+"<div class='realisation'><a href='mailto:webmaster@caroleblouin.com'>Réalisation: Philippe</a> </div></div>";
    	return footer;
}

function getCurrentYear()
{
  var d = new Date();
  return d.getFullYear();
}
