$(document).ready(function(){

	setAutoCompleteOff('__VIEWSTATE');
	setAutoCompleteOff('__EVENTTARGET');
	setAutoCompleteOff('__EVENTARGUMENT');
	setAutoCompleteOff('__EVENTVALIDATION');
	
	
	var numRand = Math.floor(Math.random()*4) + 1
	$("#featured .features .feature" + numRand).css("z-index",1);
	$("#featured .features .feature" + numRand).css("display","block");
	$("#slides").attr("class", "feature" + numRand);
	
	var currentFeature = "feature" + numRand;
	
	$("#slides ul li.slide a").click(function(){
		
		var newFeature = $(this).attr("class");
			
		if(newFeature!=currentFeature){
			
			$("#featured .features ." + currentFeature).css("z-index",1);
			$("#featured .features ." + newFeature).css("display","block");

			$("#featured .features ." + newFeature + " .image").removeAttr("style");
			$("#featured .features ." + newFeature + " .card").removeAttr("style");

			var newFeatureImage_BackgroundPosition = $("#featured .features ." + newFeature + " .image").css("backgroundPosition");
			var newFeatureCard_Left = $("#featured .features ." + newFeature + " .card").css("left");
	
			if((!$.browser.msie)&&(navigator.platform != "iPad")&&(navigator.platform != "iPhone")){
				
				$("#featured .features ." + currentFeature).fadeOut("slow", function() {
					$(this).css("z-index",0);
					$("#featured .features ." + newFeature).css("display","block")
				});
				
				$("#featured .features ." + newFeature + " .image").css("backgroundPosition", "70% 50%");
				$("#featured .features ." + newFeature + " .card").css("left", "500px");
				
				$("#featured .features ." + newFeature + " .image").animate({"backgroundPosition": newFeatureImage_BackgroundPosition}, 500, function() {
					$(this).removeAttr("style");
				});
				$("#featured .features ." + newFeature + " .card").animate({"left": newFeatureCard_Left}, 500, function() {
					$(this).removeAttr("style");
				});

				$("#featured .features ." + currentFeature + " .image").animate({"backgroundPosition": "70% 50%"}, 500, function() {
					$(this).removeAttr("style");
				});
				$("#featured .features ." + currentFeature + " .card").animate({"left": "500px"}, 500, function() {
					$(this).removeAttr("style");
				});
	
			} else {
				$("#featured .features ." + currentFeature).hide(0, function() {
					$(this).css("z-index",0);
					$("#featured .features ." + newFeature).css("display","block")
				});
			}			
			$("#slides").attr("class", newFeature);
			currentFeature = newFeature;
		}
	});
		
	$("a.show-map").fancybox({
		"overlayColor" : "#000",
		"overlayOpacity" : "0.5",
		"width" : "90%",
		"height" : "90%",
		"autoScale" : false,
		"transitionIn" : "none",
		"transitionOut" : "none",
		"type" : "iframe",
		"centerOnScroll" : true
	});

	$("a.archive-projects").fancybox({
		"overlayColor" : "#000",
		"overlayOpacity" : "0.5",
		"width" : 950,
		"height" : "90%",
		"autoScale" : false,
		"transitionIn" : "none",
		"transitionOut" : "none",
		"type" : "iframe",
		"centerOnScroll" : true
	});

	$(".sidebar .contact fieldset input").click(function(){
		$(".sidebar .contact form .more").slideDown(500);
	});

	if((navigator.platform != "iPad")&&(navigator.platform != "iPhone")){
		$(".preview img").imgslide({top:"-229px", left:"0px"});
	}
	
	var jobsText = $(".sidebar .contact a.jobs").text();
	var jobsAddress = jobsText.replace(" at ", "@");
	$(".sidebar .contact a.jobs").attr('href', 'mailto:' + jobsAddress);
	$(".sidebar .contact a.jobs").text(jobsAddress);

	var salesText = $(".sidebar .contact a.sales").text();
	var salesAddress = salesText.replace(" at ", "@");
	$(".sidebar .contact a.sales").attr('href', 'mailto:' + salesAddress);
	$(".sidebar .contact a.sales").text(salesAddress);

	setTimeout("$('#preloader').fadeOut('slow');",2500);
	//setTimeout("$('#slides ul li.slide a.feature2').click();",5000);
	//setTimeout("$('#slides ul li.slide a.feature3').click();",7500);
	//setTimeout("$('#slides ul li.slide a.feature4').click();",10000);
	//setTimeout("$('#slides ul li.slide a.feature1').click();",12500);
	
	$("#jCarousel1").jcarousel({
		scroll: 1,
		initCallback: mycarousel1_initCallback,
        // This tells jCarousel NOT to autobuild prev/next buttons
        buttonNextHTML: null,
        buttonPrevHTML: null
	});
	$("#jCarousel2").jcarousel({
		scroll: 1,
		initCallback: mycarousel2_initCallback,
        // This tells jCarousel NOT to autobuild prev/next buttons
        buttonNextHTML: null,
        buttonPrevHTML: null
	});

});

function mycarousel1_initCallback(carousel) {
	$("#jCarousel-among-clients-control a").bind("click", function() {
		carousel.scroll(jQuery.jcarousel.intval($(this).attr("page")));
		$("#jCarousel-among-clients-control li a").removeClass("selected");
		$(this).addClass("selected");
		return false;
	}); 
}
function mycarousel2_initCallback(carousel) {
	$("#jCarousel-testimonials-control a").bind("click", function() {
		carousel.scroll(jQuery.jcarousel.intval($(this).attr("page")));
		$("#jCarousel-testimonials-control li a").removeClass("selected");
		$(this).addClass("selected");
		return false;
	}); 
}

// Fix Firefox caching
function setAutoCompleteOff(id) {
	var elem = document.getElementById(id);
	if(elem) {
		elem.setAttribute('autocomplete', 'off');
	}           
}

function fncShowLightbox(projectName, orig){
	if(orig == "play")
		orig = $("." + projectName + " .tools .play")
	else
	if(orig == "image")
		orig = $("." + projectName + " .preview")
	else
	if(orig == "hotspot")
		orig = $("." + projectName + " .hotspot img")
	else
		orig = $("." + projectName + " .tools .play")
	
	if((navigator.platform != "iPad")&&(navigator.platform != "iPhone")){
		$("a.lightbox[rel='" + projectName + "']").fancybox({
			"orig" : orig,
			"overlayColor" : "#000",
			"overlayOpacity" : "0.5",
			"transitionIn" : "elastic",
			"transitionOut" : "elastic",
			"easingIn" : "easeOutBack",
			"easingOut" : "easeInBack",
			"speedIn" : 800,
			"speedOut" : 800,
			"titlePosition":"over",
			"centerOnScroll" : true,
			"onComplete":function(){
				$("#fancybox-wrap").hover(function() {
					$("#fancybox-title").show();
				}, function() {
					$("#fancybox-title").hide();
				});
			}
		});
	} else {
		$("a.lightbox[rel='" + projectName + "']").fancybox({
			"orig" : orig,
			"overlayColor" : "#000",
			"overlayOpacity" : "0.5",
			"transitionIn" : "none",
			"transitionOut" : "none",
			"titlePosition":"over",
			"centerOnScroll" : true,
			"onComplete":function(){
				$("#fancybox-wrap").hover(function() {
					$("#fancybox-title").show();
				}, function() {
					$("#fancybox-title").hide();
				});
			}
		});
	}

	$("a.lightbox[rel='" + projectName + "']:first").trigger("click");
}

function fncShowContactForm(type){
	$("#contactForm").slideDown("slow");
	switch(type){
		case 1:
			$(".sidebar .thanks").slideUp("slow");
			break;
		case 2:
			$(".sidebar .send-error").slideUp("slow");
			break;
	}
}

function fncShowMoreProjects(){
	$(".content .latest-projects .projects .project.last").removeClass("last");
	$("#more-projects").slideDown("slow", function(){
		$(".more-projects").slideUp("slow");
	});
}
function fncShowMoreVideos(){
	$(".content .press-releases-videos .videos .video.last").removeClass("last");
	$("#more-videos").slideDown("slow", function(){
		$(".more-videos").slideUp("slow");
	});
}

function fncHighlightEmail(){
	
	$(".sidebar .contact a.jobs").animate({
		backgroundColor: "#fff",
		color: "#090909",
		padding: "3px",
		borderBottom: "0px"
	}, 150)
		.animate( { backgroundColor: "#090909" }, 150)
			.animate( { backgroundColor: "#fff" }, 150);

}

