// REDBACK SOLUTIONS 2011

WebFont.load({
	typekit: {
		id: 'xcd8aqm'
    }
});

$(document).ready(function() {
	
	// FADES -------------------------------------------------------------------------------------
	$('#followUs img, #col4 img').fadeTo(1, .6);
	$('#followUs img, #col4 img').mouseenter(function(){$(this).stop().fadeTo(200, 1);});	
	$('#followUs img, #col4 img').mouseleave(function(){$(this).stop().fadeTo(200, .6);})
	$('.servicesHome img, #contact img, .vsdSubmit').mouseenter(function(){$(this).stop().fadeTo(200, .6);});	
	$('.servicesHome img, #contact img, .vsdSubmit').mouseleave(function(){$(this).stop().fadeTo(200, 1);});
	$('.dataForm_button').mouseenter(function(){$(this).stop().fadeTo(200, .75);});	
	$('.dataForm_button').mouseleave(function(){$(this).stop().fadeTo(200, 1);});
	$('.button').mouseenter(function(){$(this).animate({top:"-=5px"}, 100);});
	$('.button').mouseleave(function(){$(this).animate({top:"+=5px"}, 100);});
	$('#fact1 img, #fact2 img, #fact3 img').mouseenter(function(){$(this).stop().fadeTo(300, 0);});	
	$('#fact1 img, #fact2 img, #fact3 img').mouseleave(function(){$(this).stop().fadeTo(300, 1);});
	
	$(".viewMap").toggle(
		function () {
			$(this).html("View Newcastle Map");
			if (navigator.appName=="Microsoft Internet Explorer") {
				$("#map").css("z-index","3"); 
			} else {
				
				$("#map").stop().fadeTo(500, 0, function() {
					$("#map").css("z-index","3");
				}); 
			}
		},
		function () {
			$(this).html('View Sydney Map');
			if (navigator.appName=="Microsoft Internet Explorer") {
				$("#map").css("z-index","5"); 
			} else {
				$("#map").css("z-index","5");
				$("#map").stop().fadeTo(500, 1);
			}
		}
	);
	
	// MENU --------------------------------------------------------------------------------------
	$('.menuNavThirdLevelCurrent').parent().parent().addClass("menuNavSubLevelCurrent");
	$('.menuNavSubLevelCurrent').parent().parent().addClass("menuNavTopLevelCurrent");
	$("#menuNavItem19541 a:first, #menuNavItem19541 ul").addClass('expand');
	$(".expand").mouseenter(
		function () {
			$('.menuNavTopLevel ul').stop().animate({height:"75px"}, 300);
		}
	);
	$("#menu").mouseleave(
		function () {
			$('.menuNavTopLevel ul').stop().animate({height:"0"}, 300);
		}
	);
	
	// ENQUIRY FORM ------------------------------------------------------------------------------
	$('#softwareDev .button, #otherService .button').click(function() {
		toggleForm('show');
	});
	$('#enquiryClose img').click(function() {
		toggleForm('hide');
	});
	
	$('#_ctl1_lblThankyou').parent().prev().prev().prev().html("Message sent");
	
	// HOMEPAGE FEATURE SLIDER -------------------------------------------------------------------
	//Show the featureNav and activate its first link
	$(".featureNav").show();
	$(".featureNav a:first").addClass("active");
	
	//Get size of the image, how many images there are, then determine the size of the image reel.
	var imageWidth = $(".featureWindow").width();
	var imageSum = $(".featureContent img").size();
	var imageReelWidth = imageWidth * imageSum;
	
	//Adjust the image reel to its new size
	$(".featureContent").css({'width' : imageReelWidth});
	
	//featureNav and Slider Function
	rotate = function(){
		var triggerID = $active.attr("rel") - 1; //Get number of times to slide
		var featureContentPosition = triggerID * imageWidth; //Determines the distance the image reel needs to slide
	
		$(".featureNav a").removeClass('active'); //Remove all active class
		$active.addClass('active'); //Add active class (the $active is declared in the rotateSwitch function)
	
		//Slider Animation
		$(".featureContent").animate({
			left: -featureContentPosition
		}, 1000 );
	}; 
	
	//Rotation  and Timing Event
	rotateSwitch = function(){
		play = setInterval(function(){ //Set timer - this will repeat itself every 7 seconds
			$active = $('.featureNav a.active').next(); //Move to the next featureNav
			if ( $active.length === 0) { //If featureNav reaches the end...
				$active = $('.featureNav a:first'); //go back to first
			}
			rotate(); //Trigger the featureNav and slider function
		}, 8000); //Timer speed in milliseconds (5 seconds)
	};
	
	rotateSwitch(); //Run function on launch
	
	//On Hover
	$(".featureContent a").hover(function() {
		clearInterval(play); //Stop the rotation
	}, function() {
		rotateSwitch(); //Resume rotation timer
	});	
	
	//On Click
	$(".featureNav a").click(function() {
		$active = $(this); //Activate the clicked featureNav
		//Reset Timer
		clearInterval(play); //Stop the rotation
		rotate(); //Trigger rotation immediately
		rotateSwitch(); // Resume rotation timer
		return false; //Prevent browser jump to link anchor
	});
	
	// COMMUNITY SLIDER --------------------------------------------------------------------------
	
	//Calculate width
	var numOf = $("#content-holder > li").length;
	$("#content-holder").width(numOf * 188);
	
	// TOOLTIP PREVIEWS --------------------------------------------------------------------------
	$('.portfolioDetail h1').mouseenter(
		function(){
			$(this).parent().parent().prev(".tooltip").stop().fadeTo(200, 1);
			$(this).parent().parent().prev(".tooltip.hide").css("display", "none");
		}
	);	
	$('.portfolioDetail h1').mouseleave(
		function(){
			$(this).parent().parent().prev(".tooltip").stop().fadeTo(200, 0, function(){
				$(this).css("display","none");
			});
		}
	);	
	 
	// PORTFOLIO ACCORDIAN  ----------------------------------------------------------------------
	$(".portfolioDetail h1").click(function() {
		if ($(this).hasClass("expanded")) {
			hideProject(this);
		} else {
			showProject(this);
		}
	});
	$(".closeProject").click(function() {
		hideProject($(this).parent().prev().prev().prev().prev().prev().prev());
	});
	
	//Open to correct project from home page banner
	var anchorVal = window.location.hash.split("#")[1];
	featureProject(anchorVal);
});

function featureProject(anchorVal) {
	switch(anchorVal) {
	case 'slingshot':
		showProject("#slingshot h1");
		$('html,body').delay(1000).animate({scrollTop: $("#slingshot").offset().top},'1000');
	  	break;
	case 'bunnamagoo':
		showProject("#bunnamagoo h1");
		$('html,body').delay(1000).animate({scrollTop: $("#bunnamagoo").offset().top},'1000');
	 	break;
	case 'mensshed':
		showProject("#mensshed h1");
		$('html,body').delay(1000).animate({scrollTop: $("#mensshed").offset().top},'1000');
	 	break;
	case 'oakwood':
		showProject("#oakwood h1");
		$('html,body').delay(1000).animate({scrollTop: $("#oakwood").offset().top},'1000');
	 	break;
	}
}

function hideProject(project) {
	$(project).removeClass("expanded");
	$(project).css("background-image", "url(/SiteFiles/rb2011comau/images/plus.png)"); 
	$(project).parent().parent().stop().animate({"height":"32px"}, 1000);
	$(project).next("div").stop().fadeTo(800, 0, function(){
		$(this).css("display","none");
	});		
	$(project).parent().parent().prev("div").removeClass("hide");
}

function showProject(project) {
	$(project).addClass("expanded");
	$(project).parent().parent().prev("div").addClass("hide");
	$(".tooltip.hide").css("display", "none");
	h = $(project).parent().height();											  
	$(project).parent().parent().stop().animate({"height":h}, 1000);
	$(project).next("div").stop().fadeTo(800, 1); 
	$(project).css("background-image", "url(/SiteFiles/rb2011comau/images/minus.png)"); 	
}

function toggleForm(text) {
	var openCookie = $.cookie('enquiry');
	if(openCookie) {
		if(text == 'hide') {
			$.cookie('enquiry', 'hidden');
			$('#enquiryForm').css('display','none');
			$('#lightbox').css('display','none');
		} else {
			$.cookie('enquiry', 'showing');
			$('#lightbox').css('display','block');
			$('#enquiryForm').css('display','block');
		}
	} else {
        $.cookie('enquiry', 'closed', { expires: 30 });
		toggleForm(text);
	}
}
