


//  Notes:
// toString()

// We need the global variable for the fade out and go to clicked page to work.
var DebugGlobal = 0; //Set this to zero for no degugging information, when set to 1, only firefox with firebug will work
var delayLinkTarget = "?page=home";
var api = 0;
var accordianIsInLink = 0;

var FlashAccordian = 0;

var InbarCounter = 0;
var InbarSpeed = 0.0;
var InbarDirection = 1;
var Tick;
var ClientCatCurrent = "1";
var ClientCatLink = "1";
var ClientCatPrevious = "1";
var FlashLinkIsBeingHandled = 0;

var BrowserIsIE = 0;
var BrowserIsSafari = 0;


//Detect Browser Being Used
var browser=navigator.appName;
var b_version=navigator.appVersion;
var version=parseFloat(b_version);
if ( (browser=="Microsoft Internet Explorer") ) {
	BrowserIsIE = 1;
	DebugGlobal = 0;
}
if (navigator.userAgent.search(/Safari/i) > 0 ) {
	BrowserIsSafari = 1;
	DebugGlobal = 0;
}


function debugSafe( message ) {
	try {
		console.debug( message );
	}
	catch (err) {
		var nothing = 0;
	}
}


//Function to simulate link clicked when flash button is clicked.
function getTestFromFlash( linkIdClicked ){
	if (DebugGlobal == 1) debugSafe( "Flash clicked a link." );
	FlashLinkIsBeingHandled = 1;
	$(linkIdClicked).click();
}

function debugStringFromFlash( debugString ){
	if (DebugGlobal == 1) debugSafe( debugString );
}

function flashembedOnFail(version)  { 

	var map = '<map name="accordianMap"><area href="index.php?page=clients&cat=1&client=b" shape=rect coords="0,0,850,500"></map>';
	var img = '<img id="clientPictureStill" src="images/accordian.jpg" usemap="#accordianMap"/>';

	return map + " " + img;  
}					

function variousOneSheetsMouseOver(direction){
	
	if (direction == 1){
		InbarDirection = 1;
	}
	else {
		InbarDirection = -1;
	}
	
	// This makes the MouseX value change whenever the mouse moves
	//$("#scroller").mousemove(function(e){  		MouseX = e.pageX;});
	Tick = window.clearInterval( Tick );
	Tick = window.setInterval(function() {
		//if ( typeof(e.pageX) == "number" ) var MouseX = e.pageX; else var MouseX = 0;
		//if (DebugGlobal == 1) debugSafe(MouseX);	
		
		var x = $("#scroller").css( "background-position");
		if (DebugGlobal == 1) debugSafe("Background Position is:  ");
		if (DebugGlobal == 1) debugSafe(x);
		x = parseInt(x);
		if (DebugGlobal == 1) debugSafe("Background Position is:  ");
		if (DebugGlobal == 1) debugSafe(x);

		
		InbarSpeed = InbarSpeed+1.0;
		if (InbarSpeed > 20){
			InbarSpeed = 20;
		}
		x = x + InbarSpeed * InbarDirection;
		x = parseInt(x);
		if ( x < -1890 ){
			x = -1890;
		}
		if ( x > 0 ){
			x = 0;
		}
		
		$("#scroller").css( "background-position", x );

		$("#inbar_buttonr").mouseout(function(e){
			InbarSpeed = 0;
			Tick = window.clearInterval( Tick );
		});
		$("#inbar_buttonl").mouseout(function(e){
			InbarSpeed = 0;
			Tick = window.clearInterval( Tick );
		});
	}, 20 ); // 	<< Set Interval timer is the number here


}


//This function is used to to wait because going to the clicked page, so that the fades out has some time
function delayLink(link){
	window.location.href = delayLinkTarget;
}


//This function runs after new content is loaded
function fixHrefs(){

	// Add a delayed fade transition to any click.
	
	$("a").each(
		function(i){
			var noContentString = "noContent";
			var homeString = "portfolio.php";
			var jqhref = this.jqhref;
			var href = this.href;
			var isClientMenuLink = "   change this  ";
			if (this.target == "noContent" ) {
				var a = 0;
			}
			else {		

				//Check for reasons not to change the link, then in the else part, change it.	
				if ( this.href.match("mailto:info@massif.ca")  ) {  		
				}
				else if (0==1) {	//this.href.match("logo=clicked") ) {       //We can check for reasons not to change the link here.	
				}
				else if (0==1) {	//this.href.match("logo=clicked") ) {       //We can check for reasons not to change the link here.	
				}
				else {			//JavaLinkChange Else
		
					//we set up a temporary variable to store the original href
					this.jqhref = this.href;
					//set the href to something harmless, so that when we click it, we don't go anywhere
					//this.target = "_none";
					this.target = "noContent";
					//We used to have to change this.href here so that the link wouldn't get handled, but now the onclick handler returns false, which makes browsers not handle the href
						//this.href = "javascript:function noLinkFromHref(){noLinkFromHref=''}noLinkFromHref()";
					
					
					//We Have to be very careful that this only runs once, which is why the "one" method is used
					$(this).one( "click", function()
						{
							//alert("clicked");
							if (DebugGlobal == 1) debugSafe("Link was clicked");
							//this.jqhref = "#";
							//Unfortunately we have to cheat and use a global variable, because the setTimeout callback won't take any arguments
							var link = this.jqhref;
							var toLoadContent = this.jqhref + " div.content";
							var toLoadNavMenu = this.jqhref + " div.nav_menu";
							var toLoadNavBoxes = this.jqhref + " div.nav_boxes";
							var accordianIsAlreadyShowing = 0;

							//Determine If Catagory Is Changing
							var clientCatIsChanging = 0;
							var catPosInString = link.search(/cat=/);
							ClientCatLink = link.substring(catPosInString+4,catPosInString+5);
							if (DebugGlobal == 1) debugSafe("Client Catagory link is:");
							if (DebugGlobal == 1) debugSafe(ClientCatLink);
							
							if (ClientCatLink == ClientCatPrevious) {
								ClientCatPrevious = ClientCatLink;
							}
							else {
								if (DebugGlobal == 1) debugSafe("Client Catagory link is changing!");
								clientCatIsChanging = 1;
								ClientCatPrevious = ClientCatLink;
							}
							
							
							//Check to see if the accordian is already showing, by looking for non flash content identifiers, setting a variable to track the flash showing state.
							if (  ( $("#clientPictureStill").length > 0 || $("#inbar").length > 0)  ) {
								accordianIsAlreadyShowing = 0;
								if (DebugGlobal == 1) debugSafe("Accordian Not Already Showing");
								// /*tempIEAlert*/ alert("accordian is not already showing");
							}
							else if ( !document.getElementById("accordian") ) {
								accordianIsAlreadyShowing = 0;
								if (DebugGlobal == 1) debugSafe("Accordian Not Already Showing because getElement by Id fails on it");
								// /*tempIEAlert*/ alert("accordian is not already showing because prepend image is showing");							
							}
							else {
								accordianIsAlreadyShowing = 1;
								// /*tempIEAlert*/ alert("accordian is already showing");
								if (DebugGlobal == 1) debugSafe("Accordian Is Already Showing, or is being clicked and hasnn't finished appearing yet");
							}
	
							//Check to see if the link contains a reference to the accordian being active.  Set a variable to track its state.
							if (  link.match("accordian=yes")  ) { //{this.jqhref  ) {
								accordianIsInLink = 1;
								if (DebugGlobal == 1) debugSafe("Link destination contains accordian");
							}
							else {
								accordianIsInLink = 0;
								if (DebugGlobal == 1) debugSafe("Link destination does NOT contain accordian");
							}
													
							

							
							//if the accordian isn't already showing, but we want to show it...
							if ( accordianIsInLink == 1 && accordianIsAlreadyShowing==0 ) {
								if (DebugGlobal == 1) debugSafe("Fading out image and loading accordian.");
								$(".accordian:first").fadeOut('fast', function() {
									if (DebugGlobal == 1) debugSafe("Showing accordian...");
									$(".accordian:first").html(" ");
									$(".accordian:first").flashembed( {src: 'accordian.swf', version: [9,115], id: 'accordian',
										onFail: flashembedOnFail									
									});
									$(".accordian:first").fadeIn('fast');
								});
							}							

							
							//########################3
							//NOTE!  Use a unique identifier such as #accordian_transitions for all these events, otherwise you'll get duplicating animations etc!
							//##########################
							
							//If the link doesn't go to an accordian enabled page,  we load the content that should be logically linked to.							
							if (accordianIsInLink == 0) {
								//
								if (DebugGlobal == 1) debugSafe("Running -Accordian Isn't in link- condition ");
								
								//We tell it to load the link target, but only load a portion of that page into a specific div on this page.
								var toLoadTop = this.jqhref + " div.accordian";
								if  (accordianIsAlreadyShowing == 0) {
								
									//Transition Between Two Client Images, *WITH* change of catagory
									if (clientCatIsChanging == 1) {
										if (DebugGlobal == 1) debugSafe("About to wipe transition client image")
										$(".accordian:first").animate( {width: 0 }, 700, function(){
											if (DebugGlobal == 1) debugSafe("Wipe out transition complete");	
											$(".accordian:first").load(  toLoadTop, function(){
												$(".accordian:first").css({"opacity": 0});
												$(".accordian:first").animate({opacity: 1},1, function() {
													$(".accordian:first").animate( {width: 850 }, 700);
												});												
												if (DebugGlobal == 1) debugSafe("Trying to Wipe in accordian");
												
											});
										});
									}
									//Transition Between Two Client Images, *WITHOUT* change of catagory
									else {
										if (DebugGlobal == 1) debugSafe("About to fade out client image");
										$(".accordian:first").fadeOut('fast', function() {
											if (DebugGlobal == 1) debugSafe("Running code after fade out client image");
											$(".accordian:first").css({"opacity": 0});	
											$(".accordian:first").load(  toLoadTop, function(){
												$(".accordian:first").css({"opacity": 0});												
												$(".accordian:first").animate({opacity: 1},1, function() {
													$(".accordian:first").fadeIn('normal');
												});
											});
										});
									}		
								}
								//Goes from accordian showing
								else { 
									if (DebugGlobal == 1) debugSafe("Going from accordian showing - then fading out accordian and loading new content");

									//Handle Clicks From Flash
									if (FlashLinkIsBeingHandled == 1){
										if (DebugGlobal == 1) debugSafe("Flash Link Is Being Handled");		
										//This code has to figure out which image to prepent
										if (1==1){
											var catPosInString = this.jqhref.search(/cat=/);
											var catString = this.jqhref.substring(catPosInString + 4, catPosInString + 5);										
											var clientPosInString = this.jqhref.search(/client=/);
											var clientString = this.jqhref.substring(clientPosInString + 7 , clientPosInString + 8);
											var prependString = '<img id="preloadImage01" src="images/clients_' + catString + clientString + '_pic01.jpg" usemap="#prependImageMap" style:"margin-left: -10000; margin-right: -850;/>';
											
											//var prependString = "<p>test</p>";
											
											//if (DebugGlobal == 1) debugSafe("this.jqhref is: " + this.jqhref);
											//if (DebugGlobal == 1) debugSafe("clientPosInString is: ");
											//if (DebugGlobal == 1) debugSafe(  clientPosInString.toString()  );
											//if (DebugGlobal == 1) debugSafe("clientString is: " + clientString);
											
											//if (DebugGlobal == 1) debugSafe("Prepend String is: " + prependString);
										}
										else {
											var prependString = '<img id="prependImage" src="images/clients_1b_pic01.jpg" usemap="#prependImageMap" style:"margin-left: -10000; margin-right: -850;/>';
										}


										$(".accordian:first").prepend(prependString);



										if ( BrowserIsIE == 0) {
											if (BrowserIsSafari == 0) {
												$("embed").remove();
												$("object").remove();
											}
											
										}
										else {
											$(".accordian:first").animate( {marginRight: 30}, "slow", function (){
												$("object").remove();
												$("embed").remove();
											});
										}
									
										FlashIsBeingHandled = 0;
									}
									else {

										if (DebugGlobal == 1) debugSafe("Regular non flash Link Is Being Handled");
									
										//Fade Out Flash
										try {
											document.getElementById('accordian').javaCallsFadeOut();
										}
										catch(err) {
											if (DebugGlobal == 1) debugSafe("An error occurred attempting to fade out flash");
										}
										$(".accordian:first").fadeOut(1300, function() {
											//$(".accordian:first").css({"opacity": 0});
											$(".accordian:first").load(  toLoadTop, function(){
											//$(".accordian:first").animate({opacity: 1},'normal');
												$(".accordian:first").fadeIn('normal');
											});
										});	
									}
									FlashIsBeingHandled = 0;
								}
							}
							else { //Here is what we do if the accordian is in the target link
							//if the accordian is showing, but we want to reload it
								if (DebugGlobal == 1) debugSafe("Accordian is in target link");
								
								if (   ( this.jqhref.match("page=clients") || this.jqhref.match("page=contact") || this.jqhref.match("page=home") )  ) {
									try {
										document.getElementById('accordian').javaCallsDoesExist();
										if (DebugGlobal == 1) debugSafe("Tried to fade out accordian and didn't fail, so not showing accordian");										
									}
									catch(err){
										if (DebugGlobal == 1) debugSafe("Error Caught, so showing accordian");
										$(".accordian:first").flashembed( {src: 'accordian.swf', version: [9,115], id: 'accordian',
											onFail: flashembedOnFail									
										});
								
									}
										
								}
								else {
									if ( accordianIsAlreadyShowing==1 ) {
										try {
											document.getElementById('accordian').javaCallsFadeOut();
										}
										catch(err) {
											if (DebugGlobal == 1) debugSafe("An error occurred attempting to fade out flash");
										}
										$(".accordian:first").fadeOut('fast', function() {
											if (DebugGlobal == 1) debugSafe("Showing accordian...");
											$(".accordian:first").html(" ");
											$(".accordian:first").flashembed( {src: 'accordian.swf', version: [9,115], id: 'accordian',
												onFail: flashembedOnFail									
											});
											$(".accordian:first").fadeIn('fast');
										});
									}
								}
							}
							$("div.content").load(  toLoadContent, { big: 0} , function(){
								$("div.nav_menu").load(  toLoadNavMenu, { big: 0}, function(){
									$("div.nav_boxes").load(  toLoadNavBoxes, { big: 0}, fixHrefs  );
								});
							});
							fixHrefs();
							FlashLinkIsBeingHandled = 0;
							return false;
						}
					);
				}	//JavaLinkChange Else		
			}
		}
	);  // end of jquery to fix hrefs

} //end of fixHrefs function



//	onReady Scripts
//
//		Scripts that run when the document loads
//
//		Generally, to dynamically replace/change content
//
//Use Jquery's document ready so that this stuff will run for sure.
$(document).ready(function() {
	
	// Detect flash if it exists and load Flash into accordian
	if (  1==1  ) {			//****Fix this!**** should really be flashembed.isSupported([9, 0])    Flash 9 is required for the accordian script to work, so we look for it.
		if (  DetectFlashVer(6,0,65)  ) {
			//Load flash into accordian since a verion is detected
			//If its an old version of flash that can be auto upgraded, it will be.
			$(".accordian:first").flashembed( {src: 'accordian.swf', version: [9,115], id: 'accordian', expressInstall:'expressinstall.swf'} );
		}
		else {
			//If there is no flash, or only a version of flash that is too old, show the no flash content.
			$(".accordian:first").flashembed( {src: 'accordian.swf', version: [9,115], id: 'accordian', expressInstall:'expressinstall.swf', onFail: flashembedOnFail } );
		}
	}
	
	
	fixHrefs();

	
	/*  Preload Images is commented out since they are now preloading another way
	
	//Preload Images
	var preload = [
							"images/clients_1b_pic01.jpg",
							"images/clients_4a_pic01.jpg",
							"images/clients_3a_pic01.jpg",
							"images/clients_3d_pic01.jpg",
							"images/clients_1d_pic01.jpg",
							"images/clients_2a_pic01.jpg",
							"images/clients_1c_pic01.jpg",
							"images/clients_1f_pic01.jpg",
							"images/clients_4b_pic01.jpg",
							"images/clients_3b_pic01.jpg",
							"images/clients_3c_pic01.jpg",
							"images/clients_2b_pic01.jpg"		
	];
	$(document.createElement('img')).bind('load', function(){
		if(preload[0]) {
			this.src = preload.shift();
		}
		if (DebugGlobal == 1) debugSafe("Preloading an image!");
	}).trigger('load');
	
	*/ //End of Preload Images 

	
}); // end of jquery "ready"


