// JavaScript Document
window.onload=init;


var browserVersion = checkVersion();
var alternativeJs = false;
var ie7 = false;
	if(browserVersion == 8 || browserVersion == 7|| browserVersion == 6 ){
		// alert('NOT IE 9');
		alternativeJs = true;
	}else if(browserVersion < 8){
		var ie7 = true;
		
	}else{
		// alert('NOT IE');
	}

/*******************************************************************************
 * VARIABLES
 * 
 ******************************************************************************/
var currentPage;
var currentOpen = 0;

/*******************************************************************************
 * Initializes action
 * 
 ******************************************************************************/
function init(){
	
	if(browserVersion == 7 ){
		document.getElementById('whiteOverlay').innerHTML='<img src="images/Home/new_logo.jpg"   style="margin-bottom:45px;"/><br/><span style="color: #770000; font-size: 20px;">Sorry, you are currently using an outdated version of Internet Explorer, to view this site please <a href="http://windows.microsoft.com/en-CA/internet-explorer/products/ie/home">update your browser</a>. Or view in another browser like <a href="http://www.mozilla.com/en-US/firefox/fx/">Firefox</a>, or <a href="http://www.google.com/chrome/">Google Chrome</a></span>';
		
	}else{
							if(document.getElementById('pageType') != null){
								 currentPage = document.getElementById('pageType').value;
								 if(currentPage != 'contact_investor'){
									gotoAjax('',currentPage);
								 }
								  fadeIn();
								
							}
							// For White Fade out Overlay (when changing pages)
							$('#whiteOverlay').animate({				
									opacity: '0'
								}, {
									duration: 1000,
									specialEasing: {
									width: 'linear',
									height: 'swing'
								},
									complete: function() {
									
									document.getElementById('whiteOverlay').style.display='none';
									}
							});	
							
							
							
							/*
							 * Create listener for Readmore hover text **
							 * IMPORTANT *** Must add an id to the object with
							 * the .readmore class or the listener with not work
							 * 
							 */
							$(".readmore").hover(function(e) {
														  
									var strBox =this.getAttribute('id').substring(9);
									if(document.getElementById('boxTitle_'+strBox) != null){
											$('#boxTitle_'+strBox).animate({borderTopColor:'#6d6d6d',borderLeftColor:'#6d6d6d',borderRightColor:'#6d6d6d',borderBottomColor:'#6d6d6d' }, 900);
									}
									$(this).animate({letterSpacing:'3px',color:'#8eb3dd'},700, function(){}); 
								
								}, function() {
									var strBox =this.getAttribute('id').substring(9);
									if(document.getElementById('boxTitle_'+strBox) != null){
											$('#boxTitle_'+strBox).animate({borderTopColor:'#e6e5e5',borderLeftColor:'#e6e5e5',borderRightColor:'#e6e5e5',borderBottomColor:'#e6e5e5' }, 900);
									}
									$(this).animate({letterSpacing:'0px', color:'#1268bb'}, 700, function(){}); 
								
							});
							
							
							$(".homeTitleBox").hover(function() {
														  
									var strBox =this.getAttribute('id').substring(9);
									if(document.getElementById('boxTitle_'+strBox) != null){
						$('#boxTitle_'+strBox).animate({borderTopColor:'#6d6d6d',borderLeftColor:'#6d6d6d',borderRightColor:'#6d6d6d',borderBottomColor:'#6d6d6d' }, 900);
									}
									
									if(document.getElementById('readmore_'+strBox) != null){
										$('#readmore_'+strBox).animate({letterSpacing:'3px',color:'#8eb3dd'},700, function(){}); 
									}
									
									
								
								
								}, function() {
									var strBox =this.getAttribute('id').substring(9);
									if(document.getElementById('boxTitle_'+strBox) != null){
								$('#boxTitle_'+strBox).animate({borderTopColor:'#e6e5e5',borderLeftColor:'#e6e5e5',borderRightColor:'#e6e5e5',borderBottomColor:'#e6e5e5' }, 900);
									}
									
									if(document.getElementById('readmore_'+strBox) != null){
										$('#readmore_'+strBox).animate({letterSpacing:'0px', color:'#1268bb'}, 700, function(){});
									}
									
								
							});
							
							
							
							
							
							/*
							 * Checks if Banner needs to be animated (if more
							 * than one banner)
							 * 
							 */
							 beginBannerAnimation();
							
	
	}
}




/*******************************************************************************
 * Functions
 * 
 ******************************************************************************/



/*
 * For Smooth Page navigation : Fades page out before Navigating
 */
function goto(event,anchorObj){
	document.body.style.backgroundColor='#FFF';
	$('#mainContainer').animate({				
			opacity: '0'
		}, {
			duration: 400,
			specialEasing: {
			width: 'linear',
			height: 'swing'
		},
			complete: function() {
			
			window.open( anchorObj.href, '_self', '' );
			event.preventDefault();
			return false;
			}
	});			
}

function fadeOut(){
	document.body.style.backgroundColor='#FFF';
	
		$('#rightColContainer,#rightColContainer2').animate({				
				opacity: '0'
			}, {
				duration: 800,
				specialEasing: {
				width: 'linear',
				height: 'swing'
			},
				complete: function() {
					
				}
		});	
	
	
	return true;
}
function fadeIn(){
	
	document.body.style.backgroundColor='#FFF';
	
		$('#rightColContainer, #rightColContainer2').animate({				
				opacity: '1'
			}, {
				duration: 1200,
				specialEasing: {
				width: 'linear',
				height: 'swing'
			},
				complete: function() {
					
				}
		});	
	
	
		
	
	// return true;
}
/*
 * For Smooth Page navigation : Fades page out before Navigating
 */
function gotoAjax(url,page){
			var xhr = getXhr();
				xhr.onreadystatechange = function(){
				
					if(xhr.readyState == 4 && xhr.status == 200){
						var leselect = xhr.responseText;	
						
						// removes the last character (the number 1) that is
						// returned from the include statment.
						var htmlStr = leselect.substring(0,leselect.length-1);
						
						document.getElementById('ajaxContainer').innerHTML = htmlStr;
						
						document.getElementById('pageType').value = page;
						fadeIn();
						
						// add listener
						$(".readmore").hover(function(e) {
							  
						var strBox =this.getAttribute('id').substring(9);
						if(document.getElementById('boxTitle_'+strBox) != null){
						$('#boxTitle_'+strBox).animate({borderTopColor:'#6d6d6d',borderLeftColor:'#6d6d6d',borderRightColor:'#6d6d6d',borderBottomColor:'#6d6d6d' }, 900);
						}
						$(this).animate({letterSpacing:'3px',color:'#8eb3dd'},700, function(){}); 
						
						}, function() {
						var strBox =this.getAttribute('id').substring(9);
						if(document.getElementById('boxTitle_'+strBox) != null){
						$('#boxTitle_'+strBox).animate({borderTopColor:'#e6e5e5',borderLeftColor:'#e6e5e5',borderRightColor:'#e6e5e5',borderBottomColor:'#e6e5e5' }, 900);
						}
						$(this).animate({letterSpacing:'0px', color:'#1268bb'}, 700, function(){}); 
						
						});
						
						// add color box listener
						$("a[rel='box']").colorbox();
						if(window.location.href.indexOf("investor-relations") != -1){
							Investor.initialize();
						}
						
					}
				
				}
				xhr.open("POST","include/tabs_AJAX.php",true);
				xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
				xhr.send('url='+url+'&page='+page);
				
				
				
/*
 * window.open( anchorObj.href, '_self', '' ); event.preventDefault(); return
 * false;
 */			
}

function gotoAjax_and_fade(url,page){
	if(fadeOut()){
	gotoAjax(url,page);
	}
			 

		
	 
	
	
}


/*
 * Will Toggle an object's height between preset CSS height and 0 receives an
 * object ID
 * 
 */
 
function toggleBox(id){
	// showMe(id);
	var pix = $('#'+id).css('display');
	
	// if something is open
	if(currentOpen != 0){
		
		// if the open div is not the currently clicked div
		if(currentOpen != id){
			hideMe(currentOpen);
		}
		
			
	}
	
	
	if(pix =='none'){
		currentOpen = id;
		showMe(id);
	}else{
		currentOpen = 0;
		hideMe(id);
	}


	
}

/*
 * Will Toggle an object's height between preset CSS height and 0 receives an
 * object ID
 * 
 */
 
function toggleBox2(id){
	// showMe(id);
	var pix = $('#'+id).css('display');
	
	
	if(pix =='none'){
		showMe(id);
	}else{
		hideMe(id);
	}


	
}
/*
 * Will Toggle an object's height between preset CSS height and 0 receives an
 * object ID
 * 
 */
function toggleBox_back(id){

	$('#'+id).animate({
		  height: 'toggle',
		opacity: 'toggle'
		
	  },{
		  duration: 700, 
		  specialEasing: {
			 
			  height: 'jswing'
		  },
    complete: function() {
      // $(this).after('<div>Animation complete.</div>');
    }
  });
	
	
}

/*
 * Will Toggle divs between preset CSS height and 0 receives an small ID, large
 * ID, and 'more' or 'less'
 * 
 */
 var hideMe;
 var showMe;
function toggleText(smallID, fullID, moreLess ){

		if(moreLess == 'less'){
			hideMe = fullID;
			showMe = smallID;
			$('.readmore').animate({letterSpacing:'0px', color:'#1268bb'}, 700, function(){
				
				}); 
			
		}else{
			hideMe = smallID;
			showMe = fullID;
			$('.readmore').animate({letterSpacing:'0px', color:'#1268bb'}, 700, function(){
				
				}); 
		}
		
		
		$( '#'+this.hideMe ).hide( 'blind','',300, function(){  $( '#'+showMe ).show( 'blind','', 500 );});
			
			// callback function to bring a hidden box back
		function callback2(s) {
			alert('THIS: '+s);
			$( '#'+s).show( 'blind','', 1000 );
		};
	
}



		
		
function showMe(id){
	$('#'+id ).css({opacity:1});

	
$(  '#'+id  ).show( 'blind','',700, function(){});
			
	
}
function hideMe(id){
	$('#'+id ).animate({opacity:0.5}, 500, function(){
	$(  '#'+id  ).hide( 'blind','',700, function(){$('#'+id ).css({display:'none'})});
		});
	
}

function clearForm(){
		document.getElementById('name').value='';
		document.getElementById('name').removeAttribute('class','error');
		document.getElementById('name').setAttribute('class','inputfeild');
		document.getElementById('phone').value='';
		document.getElementById('phone').removeAttribute('class','error');
		document.getElementById('phone').setAttribute('class','inputfeild');
		document.getElementById('email').value='';
		document.getElementById('email').removeAttribute('class','error');
		document.getElementById('email').setAttribute('class','inputfeild');
		document.getElementById('message').value='';
		document.getElementById('message').removeAttribute('class','error');
		document.getElementById('message').setAttribute('class','inputfeild');
}


	/*
	 * Checks if Banner needs to be animated (if more than one banner)
	 * 
	 */
	 function beginBannerAnimation(){
		 if(document.getElementById('bannerNum').value > 1){
			var cycler = $('#bannerImgContainer').cycle({
			
				fx:'fade',
				startingSlide: 0,						
				speed:   2000,						
				timeout: 7000
				
				}); 
		 }
	 }









  
/*
 * description: takes care of creating the XMLHttpRequest object returns: the
 * XMLHttpRequest object if it was successfully created or returns: false if the
 * object was unsuccessfully created
 */
function getXhr(){
    var xhr = null;
    if(window.XMLHttpRequest){ // Firefox et autres
        xhr = new XMLHttpRequest();
    }
    else if(window.ActiveXObject){ // Internet Explorer
        try{
            xhr = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            xhr = new ActiveXObject("Microsoft.XMLHTTP");
        }
    }
    else { // XMLHttpRequest non support par le navigateur
        alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest...");
        xhr = false;
    }
    return xhr;
}

 
 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 checkVersion() {

    var msg = 2;

    var ver = getInternetExplorerVersion();

    if (ver > -1) {

        if (ver == 9.0){

            msg = 9;

		}else if(ver == 8.0){

            msg = 8;
		}else if(ver == 7.0){
			msg = 7;
		}else{
			msg = 6;
		}

    }

    // alert(msg);
	return msg;

}


