//-------------------------------------------------------------------------------------------

// Authors : Guillaume Quimper & David Boudreau, Influenza marketing http://www.influenza.ca
// Version 1.0
// Created: October 2009

//-------------------------------------------------------------------------------------------

// ----------------------------
// Preload CSS Images
// ----------------------------
jQuery.preloadCssImages = function(){
	  var allImgs = [];//new array for all the image urls 
	  var k = 0; //iterator for adding images
	  var sheets = document.styleSheets;//array of stylesheets
	 
	  for(var i = 0; i<sheets .length; i++){//loop through each stylesheet
			  var cssPile = '';//create large string of all css rules in sheet
			  var csshref = (sheets[i].href) ? sheets[i].href : 'window.location.href';
			  var baseURLarr = csshref.split('/');//split href at / to make array
			  baseURLarr.pop();//remove file path from baseURL array
			  var baseURL = baseURLarr.join('/');//create base url for the images in this sheet (css file's dir)
			  if(baseURL!="") baseURL+='/'; //tack on a / if needed
			  if(document.styleSheets[i].cssRules){//w3
					  var thisSheetRules = document.styleSheets[i].cssRules; //w3
					  for(var j = 0; j<thisSheetRules.length; j++){
							  cssPile+= thisSheetRules[j].cssText;
					  }
			  }
			  else {
					  cssPile+= document.styleSheets[i].cssText;
			  }
			 
			  //parse cssPile for image urls and load them into the DOM
			  var imgUrls = cssPile.match(/[^\(]+\.(gif|jpg|jpeg|png)/g);//reg ex to get a string of between a "(" and a ".filename"
			  if(imgUrls != null && imgUrls.length>0 && imgUrls != ''){//loop array
					  var arr = jQuery.makeArray(imgUrls);//create array from regex obj       
					  jQuery(arr).each(function(){
							  allImgs[k] = new Image(); //new img obj
							  allImgs[k].src = (this[0] == '/' || this.match('http://')) ? this : baseURL + this;     //set src either absolute or rel to css dir
							  k++;
					  });
			  }
	  }//loop
	  return allImgs;
}

//------------
// Top Drawers
//------------
$(function() {
	
	var tabContainers = $("div#panel > div");
    /*
    $("#panelButtons ul li a").click(function () {
        tabContainers.animate({height: "0"}, "fast")
			.filter(this.hash).animate({height: "300"}, "normal");
        
        $("#panelButtons ul li a").removeClass("active");
		$("#panelWrapper").addClass("active");
	    $(this).addClass("active");
		
		$("a.closePanel").fadeIn("slow");
        
        return false;
    });*/
	
    $("#panelButtons ul li a#panelButtonNews, #panelButtons ul li a#panelButtonMortgage, #panelButtons ul li a#panelButtonVentes").click(function () {
        tabContainers.animate({height: "0"}, "fast")
			.filter(this.hash).animate({height: "302"}, "normal");
        
        $("#panelButtons ul li a").removeClass("active");
		$("#panelWrapper").addClass("active");
	    $(this).addClass("active");
		
		$("a.closePanel").fadeIn("slow");
        
        return false;
    });
	
	$("#panelButtons ul li a#panelButtonContact").click(function () {
        tabContainers.animate({height: "0"}, "fast")
			.filter(this.hash).animate({height: "302"}, "normal");
        
        $("#panelButtons ul li a").removeClass("active");
		$("#panelWrapper").addClass("active");
	    $(this).addClass("active");
		
		$("a.closePanel").fadeIn("slow");
        
        return false;
    });
	
	// Close button
	$("a.closePanel").click(function () {
		$("div#panel > div").animate({height: "0"}, "fast");
		$("#panelButtons ul li a").removeClass("active");
		$("#panelWrapper").removeClass("active");
		$("label.error").remove();
		$(this).fadeOut("fast");
		return false;
	});
	
});

//------------
// Latest News
//------------

$(function() {
	
	var articleContainer = $(".articleContainer > div");
    
    $(".articleNav ul li a").click(function () {
        articleContainer.hide()
			.filter(this.hash).fadeIn("slow");
        
        $(".articleNav ul li a").removeClass("active");
	    $(this).addClass("active");
        
        return false;
    }).filter(':first').click();
	
	// Sub menu 1
	var tabContainers2 = jQuery('.menuContainer > div');
    
    jQuery('#panels ul.level2 a').click(function () {
        tabContainers2.hide()
			.filter(this.hash).fadeIn("slow");
        
        jQuery('#panels ul.level2 a').removeClass('active');
        jQuery(this).addClass('active');
		 //alert('yo!');
        
        return false;
    }).filter(':first').click();
	
});

//----------------
// Main Navigation
//----------------

$(function() {
	
	//Add "color" spans
	$('#mainNav ul li:not(".lang") a').append('<span class="color"></span>');
	
	$("#mainNav ul li a:not('.active')").hover(
      function () {
        $("span.color",this).stop().animate({width: "199"}, "fast");
      }, 
      function () {
        $("span.color",this).stop().animate({width: "0"}, "normal");
      }
    );
   
});



//---------------
// Sub Navigation
//---------------
$(function() {	
	$("ul.navFx li:last").addClass("last");
	
});
$(function() {
	$('.navFx').lavaLamp({fx: 'swing', speed: 333});
});

//---------------
// LAVALAMP
//---------------
$(function() {			   
	$("ul.navFx li:first").addClass("first");
	
	// Projet nav
	$("ul.navFx li a").click(function(){
		$("ul.navFx li").removeClass("current"); 
		$(this).parent("li").addClass("current"); 
	});
	
});

//---------------
// Building Floor
//---------------
$(function() {
	$("#Map .floor11:not('.active')").hover(
      function () {
        $("li.floorTip11").fadeIn(200);
		$("li.floorHover11").fadeIn(200);
      }, 
      function () {
        $("li.floorTip11").hide();
		$("li.floorHover11").hide();
      }
    );
	$(".floor12:not('.active')").hover(
      function () {
        $("li.floorTip12").fadeIn(200);
		$("li.floorHover12").fadeIn(200);
      }, 
      function () {
        $("li.floorTip12").hide();
		$("li.floorHover12").hide();
      }
    );
	$(".floor13:not('.active')").hover(
      function () {
        $("li.floorTip13").fadeIn(200);
		$("li.floorHover13").fadeIn(200);
      }, 
      function () {
        $("li.floorTip13").hide();
		$("li.floorHover13").hide();
      }
    );
	$(".floor14:not('.active')").hover(
      function () {
        $("li.floorTip14").fadeIn(200);
		$("li.floorHover14").fadeIn(200);
      }, 
      function () {
        $("li.floorTip14").hide();
		$("li.floorHover14").hide();
      }
    );
	$(".floor15:not('.active')").hover(
      function () {
        $("li.floorTip15").fadeIn(200);
		$("li.floorHover15").fadeIn(200);
      }, 
      function () {
        $("li.floorTip15").hide();
		$("li.floorHover15").hide();
      }
    );
	$(".floor16:not('.active')").hover(
      function () {
        $("li.floorTip16").fadeIn(200);
		$("li.floorHover16").fadeIn(200);
      }, 
      function () {
        $("li.floorTip16").hide();
		$("li.floorHover16").hide();
      }
    );
	$(".floor17:not('.active')").hover(
      function () {
        $("li.floorTip17").fadeIn(200);
		$("li.floorHover17").fadeIn(200);
      }, 
      function () {
        $("li.floorTip17").hide();
		$("li.floorHover17").hide();
      }
    );
	$(".floor18:not('.active')").hover(
      function () {
        $("li.floorTip18").fadeIn(200);
		$("li.floorHover18").fadeIn(200);
      }, 
      function () {
        $("li.floorTip18").hide();
		$("li.floorHover18").hide();
      }
    );
	$(".floor19:not('.active')").hover(
      function () {
        $("li.floorTip19").fadeIn(200);
		$("li.floorHover19").fadeIn(200);
      }, 
      function () {
        $("li.floorTip19").hide();
		$("li.floorHover19").hide();
      }
    );
	$(".floor20:not('.active')").hover(
      function () {
        $("li.floorTip20").fadeIn(200);
		$("li.floorHover20").fadeIn(200);
      }, 
      function () {
        $("li.floorTip20").hide();
		$("li.floorHover20").hide();
      }
    );
	$(".floor21:not('.active')").hover(
      function () {
        $("li.floorTip21").fadeIn(200);
		$("li.floorHover21").fadeIn(200);
      }, 
      function () {
        $("li.floorTip21").hide();
		$("li.floorHover21").hide();
      }
    );
	$(".floor22:not('.active')").hover(
      function () {
        $("li.floorTip22").fadeIn(200);
		$("li.floorHover22").fadeIn(200);
      }, 
      function () {
        $("li.floorTip22").hide();
		$("li.floorHover22").hide();
      }
    );
	$(".floor23:not('.active')").hover(
      function () {
        $("li.floorTip23").fadeIn(200);
		$("li.floorHover23").fadeIn(200);
      }, 
      function () {
        $("li.floorTip23").hide();
		$("li.floorHover23").hide();
      }
    );
	$(".floor24:not('.active')").hover(
      function () {
        $("li.floorTip24").fadeIn(200);
		$("li.floorHover24").fadeIn(200);
      }, 
      function () {
        $("li.floorTip24").hide();
		$("li.floorHover24").hide();
      }
    );
	$(".floor25:not('.active')").hover(
      function () {
        $("li.floorTip25").fadeIn(200);
		$("li.floorHover25").fadeIn(200);
      }, 
      function () {
        $("li.floorTip25").hide();
		$("li.floorHover25").hide();
      }
    );
	$(".floor26:not('.active')").hover(
      function () {
        $("li.floorTip26").fadeIn(200);
		$("li.floorHover26").fadeIn(200);
      }, 
      function () {
        $("li.floorTip26").hide();
		$("li.floorHover26").hide();
      }
    );
	$(".floor27:not('.active')").hover(
      function () {
        $("li.floorTip27").fadeIn(200);
		$("li.floorHover27").fadeIn(200);
      }, 
      function () {
        $("li.floorTip27").hide();
		$("li.floorHover27").hide();
      }
    );
	$(".floor28:not('.active')").hover(
      function () {
        $("li.floorTip28").fadeIn(200);
		$("li.floorHover28").fadeIn(200);
      }, 
      function () {
        $("li.floorTip28").hide();
		$("li.floorHover28").hide();
      }
    );
	$(".floor29:not('.active')").hover(
      function () {
        $("li.floorTip29").fadeIn(200);
		$("li.floorHover29").fadeIn(200);
      }, 
      function () {
        $("li.floorTip29").hide();
		$("li.floorHover29").hide();
      }
    );
	$(".floor30:not('.active')").hover(
      function () {
        $("li.floorTip30").fadeIn(200);
		$("li.floorHover30").fadeIn(200);
      }, 
      function () {
        $("li.floorTip30").hide();
		$("li.floorHover30").hide();
      }
    );
	$(".floor31:not('.active')").hover(
      function () {
        $("li.floorTip31").fadeIn(200);
		$("li.floorHover31").fadeIn(200);
      }, 
      function () {
        $("li.floorTip31").hide();
		$("li.floorHover31").hide();
      }
    );
    $(".floor32:not('.active')").hover(
      function () {
        $("li.floorTip32").fadeIn(200);
		$("li.floorHover32").fadeIn(200);
      }, 
      function () {
        $("li.floorTip32").hide();
		$("li.floorHover32").hide();
      }
    );
    $(".floor33:not('.active')").hover(
      function () {
        $("li.floorTip33").fadeIn(200);
		$("li.floorHover33").fadeIn(200);
      }, 
      function () {
        $("li.floorTip33").hide();
		$("li.floorHover33").hide();
      }
    );
    $(".floor34:not('.active')").hover(
      function () {
        $("li.floorTip34").fadeIn(200);
		$("li.floorHover34").fadeIn(200);
      }, 
      function () {
        $("li.floorTip34").hide();
		$("li.floorHover34").hide();
      }
    );
    $(".floor35:not('.active')").hover(
      function () {
        $("li.floorTip35").fadeIn(200);
		$("li.floorHover35").fadeIn(200);
      }, 
      function () {
        $("li.floorTip35").hide();
		$("li.floorHover35").hide();
      }
    );
});

//---------------
// UNITES
//---------------
$(function() {
	
	 $("#unitMap ul li").hover(
      function () {
        $("span.imageUnit", this).fadeIn("fast");
      }, 
      function () {
        $("span.imageUnit", this).fadeOut("fast");
      }
    );

});

$(function() {	
	$('#unitMap ul li').tooltip({
		track: true,
		delay: 0,
		showURL: false,
		showBody: " - ",
		fixPNG: true,
		left: 15,
		top: -5//,
		//fade: 350
	});
});


$(function() {
	
	 $("#floorDisplay ul li.residential.available").hover(
														  
      function () {
        $("span.floorTip", this).show();
      }, 
      function () {
        $("span.floorTip", this).hide();
      }
    );
	 
	 
	 $("#floorDisplay ul li.residential.available").click(function () {
		$("a", this).addClass("selected");
      });
	 
});

//---------------
// Zoom
//---------------
$(function() {
	
	$("a.zoom").click(function () {
		// Open Zoom Pan
		//$("#zoomPan").animate({height: "500"}, "normal");
		
		var maskHeight = $(document).height();  
        var maskWidth = $(window).width(); 
		
		 //Set height and width to mask to fill up the whole screen  
         $("#zoomPan").css({"width":maskWidth,"height":maskHeight});  
           
         //transition effect       
         $("#zoomPan").fadeIn(1000);
		
		// Empty Zoom Pan
		$("#zoomPan img").remove();
		
		//New image attributes
		var newImg = $(this).attr("href");
		//var newAlt = $(this).attr("title");
               
		//Create new image
		var newPic = new Image();
		$(newPic).attr({ src: newImg });
		
		//Load new image
		$("#zoomPan").append(newPic);
		
		//Call Pan Function
		
		return false;
		
	});
	
	$("#zoomPan div.zoomInstruction a").click(function () {
		$("#zoomPan").fadeOut("fast");
		//$(".viewport").remove();
	});
	
	//if mask is clicked  
     $("#zoomPan").click(function () {  
         $(this).fadeOut();
     });  
	
});


//---------------
// Gallery
//---------------
$(function() {
	
	$("#photos li a").click(function () {
		// Open Zoom Pan
		$("#galleryZoom").animate({height: "540"}, "normal");
		
		// Empty Zoom Pan
		//$("#galleryZoom img").remove();
		
		//New image attributes
		//var newImg = $(this).attr("href");
		//var newAlt = $(this).attr("title");
               
		//Create new image
		//var newPic = new Image();
		//$(newPic).attr({ src: newImg });
		
		//Load new image
		//$("#galleryZoom").append(newPic);
		
		return false;
		
	});
	
	$("#videoTrigger").click(function () {
		// Open Zoom Pan
		$("#galleryZoom").animate({height: "540"}, "normal");
		return false;
		
	});
	
	
	$("#galleryZoom div.zoomClose a").click(function () {
		$("#galleryZoom").animate({height: "0"}, "fast");
		//$(".viewport").remove();
	});
	
	
	$("#thumbs li a").click(function () {
		$("#gallery").animate({height: "540"}, "normal");
		return false;	
	});
	
	$("#gallery div.zoomClose a").click(function () {
		$("#gallery").animate({height: "0"}, "fast");
	});
	
});

//---------------
// Video
//---------------
$(function() {
	
	
	$("#videoTrigger, #videoTrigger2").click(function () {
		// Open Zoom Pan
		$("#videoZoom").animate({height: "540"}, "normal");
		$('#videoContainer').flash({swf:'../library/video/video-player.swf',height:421,width:750});
		$(this).fadeOut('fast');
		
		return false;
		
	});
	
	
	$("#videoZoom div.zoomClose a").click(function () {
		$("#videoZoom").animate({height: "0"}, "fast");
		//Remove Flash
 		$('#videoContainer').flash().remove();
		$("#videoTrigger, #videoTrigger2").fadeIn("");
	});

	
});

//-------------
// LOCALISATION
//-------------
$(function() {
	
	$("#localisation a.zoom").click(function () {
		$("#localisationImageZoom").animate({height: "540"}, "normal");
		return false;
	});
	
	$("#localisationImageZoom div.zoomClose a").click(function () {
		$("#localisationImageZoom").animate({height: "0"}, "fast");
	});
	
});

$(function() {	   
	
	$('#localisationImageZoomContent ul li.greenTip a').tooltip({
		track: true,
		delay: 0,
		showURL: false,
		showBody: " - ",
		fixPNG: true,
		extraClass: "greenTipBg",
		left: 15,
		top: -5//,
	});
	
	$('#localisationImageZoomContent ul li.blueTip a').tooltip({
		track: true,
		delay: 0,
		showURL: false,
		showBody: " - ",
		fixPNG: true,
		extraClass: "blueTipBg",
		left: 15,
		top: -5//,
	});
	
	$('#localisationImageZoomContent ul li.pinkTip a').tooltip({
		track: true,
		delay: 0,
		showURL: false,
		showBody: " - ",
		fixPNG: true,
		extraClass: "pinkTipBg",
		left: 15,
		top: -5//,
	});
	
	$("#localisationImageZoomContent ul li#altoria a").mouseover(function() {

	   
	});
	
});

//----------------------
// VALIDATION FORMULAIRE
//----------------------

/* Français */
$(function() {

	 $.validator.addMethod("phone", function (value, el, params) {
	return this.optional(el) || /^[01]?[- .]?\(?[2-9]\d{2}\)?[- .]?\d{3}[- .]?\d{4}$/.test(value);
	}, "Numéro invalide");

	var validator = $("#contactForm").bind("invalid-form.validate", function() {
			
		}).validate({
			
		rules: {
			phone: {
				phone: true
			}
		},
		
		messages: {
			name: "Requis",
			courriel: {
				required: "Requis",
				email: "Invalide"
			},
			phone: {
				required: "Requis"
			},
			adress: {
				required: "Requis"
			},
			city: {
				required: "Requis"
			},
			province: {
				required: "Requis"
			},
			cpostal: {
				required: "Requis"
			},
			country: {
				required: "Requis"
			},
			condoType: {
				required: "Requis"
			},
			agent: {
				required: "Requis"
			},
			message: "Requis"
   		},
						
			submitHandler: function(form) {
			jQuery(form).ajaxSubmit({
				target: "#contactForm",
				success: function() {
					$("#contactForm").html("<div id=\"message\"></div>");
					_gaq.push(['_trackEvent', 'Formulaire', 'Envoi-Formulaire-Contact', 'FR']);
					$("#message").html("<h5>Merci votre demande d'information nous a &eacute;t&eacute; transmise.</h5><p>Vous serez en contact avec un membre de notre équipe tr&egrave;s prochainement...</p>")
					.hide()
					.fadeIn(1500, function() {
					  $("#message");
					});
				  }
			});
		}					
								
								
	});
		
});

/* English */
$(function() {

	 $.validator.addMethod("phone", function (value, el, params) {
	return this.optional(el) || /^[01]?[- .]?\(?[2-9]\d{2}\)?[- .]?\d{3}[- .]?\d{4}$/.test(value);
	}, "Invalid phone number");

	var validator = $("#contactFormEn").bind("invalid-form.validate", function() {
			
		}).validate({
			
		rules: {
			phone: {
				phone: true
			}
		},
		
		messages: {
			name: "Required",
			courriel: {
				required: "Required",
				email: "Invalid"
			},
			phone: {
				required: "Required"
			},
			adress: {
				required: "Required"
			},
			city: {
				required: "Required"
			},
			province: {
				required: "Required"
			},
			cpostal: {
				required: "Required"
			},
			country: {
				required: "Required"
			},
			condoType: {
				required: "Required"
			},
			agent: {
				required: "Required"
			},
			message: "Required"
   		},
						
			submitHandler: function(form) {
			jQuery(form).ajaxSubmit({
				target: "#contactFormEn",
				success: function() {
					$("#contactFormEn").html("<div id=\"message\"></div>");
					_gaq.push(['_trackEvent', 'Formulaire', 'Envoi-Formulaire-Contact', 'EN']);
					$("#message").html("<h5>Thank you, your information request has been sent.</h5><p>A member of our sales team will contact soon...</p>")
					.hide()
					.fadeIn(1500, function() {
					  $("#message");
					});
				  }
			});
		}					
								
								
	});
		
});

//---------------
// SPECIFICATIONS
//---------------
$(function() {
		//$("#specsSubNav").animate({opacity:0.7});
		
		$("ul.navFx li a").click(
			function() {
				$("#specsSubNav div ul li:not(:first) a").removeClass("active"); 							  
		});
		
	 	$("ul.navFx li a#constructionLink").click(
			function () {
				$("#specsSubNav div").fadeOut("fast"); 
				$("div#specsSubNavConstruction").fadeIn("fast"); 
				$("#specsSubNavConstruction ul li:first a").addClass("active"); 
			});
		
		$("ul.navFx li a#securiteLink").click(
			function () {
				$("#specsSubNav div").fadeOut("fast"); 
				$("#specsSubNav div#specsSubNavSecurite").fadeIn("fast"); 
				$("#specsSubNavSecurite ul li:first a").addClass("active"); 
			});
		
		$("ul.navFx li a#finitionsLink").click(
			function () {
				$("#specsSubNav div").fadeOut("fast"); 
				$("#specsSubNav div#specsSubNavFinitions").fadeIn("fast"); 
				$("#specsSubNavFinitions ul li:first a").addClass("active"); 
			});
		$("ul.navFx li a#noticeLink").click(
			function () {
				$("#specsSubNav div").fadeOut("fast"); 
			});
		
});

$(function() {
	 $("#specsSubNav a").click(
	  function () {
		$("#specsSubNav a").removeClass("active");
		$(this).addClass("active");
	  });
});


//-----------------
// Mortage Results
//-----------------
$(function() {
	$("a#calculate").click(
		function () {
			$("fieldset#results div").fadeIn("fast"); 
		});
});

$(function() {
	$(".articleContainer a").attr({ target: "_blank" });
});


//--------------------
// Google Analytics
//--------------------

$(function() {
	$('#panelButtonContact').click(function () {
		_gaq.push(['_trackEvent', 'Acces-Page', 'Click', 'Page-Contact']);
		//alert("click");
	});
	$('#panelButtonVentes').click(function () {
		_gaq.push(['_trackEvent', 'Acces-Page', 'Click', 'Page-Bureau-Ventes']);
		//alert("click");
	});
	/*$('input.submitButton').click(function () {
		_gaq.push(['_trackEvent', 'Formulaire', 'Envoi-Formulaire-Contact', 'Envoi-Formulaire-Contact']);
	});*/
});




//--------------------
//Pagination News
//--------------------

$(function() {
	var countArticles = parseInt($('.articleNav ul').size());
	for(i = 5; i < countArticles ; i++) { 
		$('.articleNav ul').eq(i).hide(); 
	};
	var nbrPages = Math.floor(countArticles/5) + 1;
	$('.articleNav').append('<div id="pagination"></div>');
	$('#pagination').append('<a id="n1" class="nav" href="#1">1</a>');
	for(i = 2; i <= nbrPages ; i++) { 
		$('#pagination').append('<a id="n' + i + '" class="nav" href="#">' + i + '</a>')
	};
	$('.nav').click(function(){
		$(this).addClass('current');
		$('.nav').not(this).removeClass('current');
		var whereTo = $(this).attr('id');
		var where  = parseInt(whereTo.substr(1));
		var maxaffiche = where*5;
		var affiche = maxaffiche - 5;
		$('.articleNav ul').hide(); 
		for(i = affiche; i < maxaffiche ; i++) { 
		$('.articleNav ul').eq(i).show(); 
		};
		return false;
	})
});

