
var Browser = {
	Version: function() {
		var version = 999; // we assume a sane browser
		if (navigator.appVersion.indexOf("MSIE") != -1)
			// bah, IE again, lets downgrade version number
			version = parseFloat(navigator.appVersion.split("MSIE")[1]);
		return version;
	}
}

$(document).ready(function(){
	
	// **************************************************************************************************************************
	// GENERAL ACTIONS
	
	// external links
	$("a[rel='external']").click(function() {
		return !window.open($(this).attr("href"));
	});
	$('.hide-with-js').hide(); // use this class to hide the message "please open this link in new window"
	
	// fade in messages
	$('.successMsg').hide();
	$('.successMsg').fadeIn(1000);
	$('.errorMsg div').hide();
	$('.errorMsg div').fadeIn(1000);
	$('.warningMsg').hide();
	$('.warningMsg').fadeIn(1000);
	
	// non-clickable links
	$(".return-false").click( function() {
		return false;
	});
	
	
	// JUMP MENU
	$("select.jump-menu").change(function(x){
		var url = $("option:selected", this).attr("title");
		if (url.length) {
			window.location.href = url;
		}
	});
	
	
	// Fading links
	$(".fading-link img").fadeTo(1000, 0.50);
	$(".fading-link").hover(
		function(){
			$("img", this).fadeTo(50, 1);
		},
		function(){
			$("img", this).fadeTo(50, 0.50);
		}
	);
	

	// END GENERAL ACTIONS
	// **************************************************************************************************************************
	
	
	// Contact
	$("form a.submit").click(function(e){
		if ( !$(this).hasClass("inactive") ) {
			
			var self = $(this);
			e.preventDefault();
			e.stopPropagation();
			
			self.find("span").text("Please wait...");
			self.addClass('inactive');
			
			$(this).closest("form").submit();
		}
		return false;
	});
	
	
	// Index Featured Work
	$("#featured-work img:first").show();
	$("#featured-work ul li a:first").addClass("active");
	
	$("#featured-work ul li a").hover(
		function(){
			$("#featured-work img").hide();
			
			$("#featured-work ul li a").removeClass("active");
			$(this).addClass("active");
			
			var elem = $(this).attr("rel");
			$("#"+elem).show();
		},
		function(){
		}
	);
	
	
	// Portfolio
	var isAnimating = false;
	$(".portfolio-listing li a").click(function(){
		
		if (isAnimating==false) {
			
			var elem = $(this);
			isAnimating = true;
			
			$(".portfolio-listing li a").removeClass("active");
			elem.addClass("active");
			elem.addClass("preload");
			
			var id = $(this).attr("rel");
			
			$.ajax({
			    type: "POST",
			    url: path + "portfolio/show",
		   	    data: {
					id_project: id
		   	    },
			    success: function(data){
					
					elem.removeClass("preload");
					
					var delay = 0;
					if ( $('#portfolio-details').length != 0 ) {
						// if there already is details on screen, we slide it up, and set a delay for the next details slide down
						delay = 300;
						$("#portfolio-details").slideUp(300);
					}
					
					setTimeout(
						function() {
							$("#portfolio-details").remove(); // remove any existing details
							elem.closest("ul").before(data); // adding clicked details
							$("#portfolio-details").hide().addClass("margin").slideDown(300); // hide, adding margins, slide it down
							
							$.scrollTo( $('#portfolio-details'), 500 ); // scrolling to details, if not in viewport
							
							isAnimating = false;
						}
					,delay + 10);
				}
			});
		} // end IF (isAnimating==false)
		
		return false;
	});
	
	
});


function closeDetails() {
	$("#portfolio-details").slideUp(500);
	$(".portfolio-listing li a").removeClass("active");
}

function switchPortfolioImage(elem) {
	var img = $(elem).attr("rel");
	
	$("#portfolio-details .img-links ol a").removeClass("active");
	$(elem).addClass("active");
	
	$("#portfolio-details ul li").removeClass("active");
	$("#"+img).addClass("active");
}
