// create html5 elemnts for use in old browsers
(function() {
	if(!/*@cc_on!@*/0)
		return;
	var e = "abbr,article,aside,audio,bb,canvas,datagrid,datalist,details,dialog,eventsource,figure,footer,header,hgroup,mark,menu,meter,nav,output,progress,section,time,video".split(',');
	for(var i = 0; i < e.length; i++) {
		document.createElement(e[i])
	}
})()

$(document).ready(function() {
	// hide all content for loading
	$("div").css('opacity', '0');
	// display page loader
	$("#loader").css('opacity', '1');
	// hide the portfolio section
	$("#portfolio").css("display", "none");
	// ajax the portfolio
	$("#port").click(function() {
		// perform ajax request
		$.ajax({
			url : "content.html",
			dataType : "html",
			success : function(data) {
				$("#portfolio .content_loader").fadeOut("slow");
				// animation functions only work on initially hidden elements
				$("#portfolio .content").html(data);
				// inititate content_close
				content_close();
				//initiate black&hwite images
				bw_images();
				//initiate fancyzoom
				setupZoom();
			}
		});
	});
	// content sections slider
	$("#cont").click(function() {
		$("#contact").slideDown("slow");
	});
	$("#port").click(function() {
		$("#portfolio").slideDown("slow");
	});
	// form ajax proccesser
	$('#contactform').submit(function() {
		// display validating message
		$("#contact_errors").html('validating...');
		var name = $("#name").val();
		var email = $("#email").val();
		var comments = $("#comments").val();
		// send ajax request
		$.ajax({
			type : "POST",
			url : "scripts/mail.php",
			data : "name=" + name + "&email=" + email + "&comments=" + comments,
			success : function(data) {
				$("#contact_errors").html(data);
			},
			error : function() {
				$("#contact_errors").html("ajax error");
			}
		});
		return false;
	});
	// hide errors on form reset
	$("#freset").click(function() {
		$("#contact_errors").html("");
	});
});
// when page has finished loading
$(window).load(function() {
	// display content
	$("div").animate({
		opacity : '1',
		easing : 'slow'
	}, 1000);
	// hide loader
	$("#loader").animate({
		opacity : '0',
		easing : 'fast'
	}, 100);
	//initiate fancyzoom
	setupZoom();
	// initiate content closing
	content_close();
	// icons hover
	$(".social").each(function() {
		$(this).mouseover(function() {
			//fade img out
			$(this).fadeTo("fast", 0.5);
		});
		$(this).mouseout(function() {
			// fade img back in
			$(this).fadeTo("slow", 1);
		});
	});
});
// end window.load

// F U N C T I O N S

// x-button: closes content section
function content_close() {
	$(".content_close").click(function() {
		$(this).parent().slideUp("slow");
	});
	$(".content_close").mouseover(function() {
		$(this).css("opacity", "1");
		$(this).mouseout(function() {
			$(this).css("opacity", "0.5");
		});
	});
}

// black and white image hover effect
function bw_images() {
	$(".box img").each(function() {
		$(this).mouseover(function() {
			//fade img out
			$(this).fadeTo("fast", 0);
		});
		$(this).mouseout(function() {
			// fade img back in
			$(this).fadeTo("slow", 1);
		});
	});
}

// smooth anchor scrolling
$(document).ready(function() {
	$('a[href*=#]').click(function() {
		if(location.pathname.replace(/^\//, '') == this.pathname.replace(/^\//, '') && location.hostname == this.hostname) {
			var $target = $(this.hash);
			$target = $target.length && $target || $('[name=' + this.hash.slice(1) + ']');
			if($target.length) {
				var targetOffset = $target.offset().top;
				$('html,body').animate({
					scrollTop : targetOffset
				}, 1000);
				return false;
			}
		}
	});
});
// top-link plugin
jQuery.fn.topLink = function(settings) {
	settings = jQuery.extend({
		min : 1,
		fadeSpeed : 200
	}, settings);
	return this.each(function() {
		//listen for scroll
		var el = $(this);
		el.hide();
		//in case the user forgot
		$(window).scroll(function() {
			if($(window).scrollTop() >= settings.min) {
				el.fadeIn(settings.fadeSpeed);
			} else {
				el.fadeOut(settings.fadeSpeed);
			}
		});
	});
};
//usage w/ smoothscroll
$(document).ready(function() {
	//set the link
	$('#top-link').topLink({
		// min scroll position for it to show
		min : 100,
		fadeSpeed : 500
	});
	//smoothscroll
	$('#top-link').click(function(e) {
		e.preventDefault();
		$.scrollTo(0, 300);
	});
});
// activate google analytics
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-23661595-1']);
_gaq.push(['_trackPageview']);
(function() {
	var ga = document.createElement('script');
	ga.type = 'text/javascript';
	ga.async = true;
	ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
	var s = document.getElementsByTagName('script')[0];
	s.parentNode.insertBefore(ga, s);
})();

