$(document).ready(function(){
	$('#content img').css({boxShadow: '0px 0px 10px #000'});
	$('#content img').mouseover(function(){
		$(this).animate({opacity: 1, boxShadow: '0px 0px 7px #0cf'}, {duration: 500, easing: 'easeInQuad', queue: false});
	});
	$('#content img').mouseout(function(){
		$(this).animate({opacity: 0.75, boxShadow: '0px 0px 10px #000'}, {duration: 500, easing: 'easeOutQuad', queue: false});
	});
	$('#sidebar').css({boxShadow: '0px 0px 5px #080808'});
	function showSidebar(){
		$('#sidebar').addClass('sidebar_expand');
		$('#sidebar').animate({boxShadow: '0px 0px 7px #0cf', width: '750px', right: '200px'}, {duration: 500, easing: 'easeInQuad', queue: false});
		$('#sidebar input[type=text], #sidebar textarea').animate({width: '725px'}, {duration: 500, easing: 'easeInQuad', queue: false});
		$('#sidebar textarea').animate({height: '300px'}, {duration: 500, easing: 'easeInQuad', queue: false});
		$('#overlay').show();
	}
	function hideSidebar(){
		$('#sidebar').removeClass('sidebar_expand');
		$('#sidebar').animate({boxShadow: '0px 0px 5px #080808', width: '150px', right: '5px'}, {duration: 500, easing: 'easeOutQuad', queue: false});
		$('#sidebar input[type=text], #sidebar textarea').animate({width: '125px'}, {duration: 500, easing: 'easeOutQuad', queue: false});
		$('#sidebar textarea').animate({height: '100px'}, {duration: 500, easing: 'easeOutQuad', queue: false});
		$('#overlay').hide();
	}
	function addError(element){
		$(element).animate({boxShadow: '0px 0px 5px #FF0000', backgroundColor: '#FFF1F1'}, {duration: 250, easing: 'easeInQuad', queue: false});
	}
	function removeError(element){
		$(element).animate({boxShadow: 'none', backgroundColor: '#FFFFFF'}, {duration: 250, easing: 'easeOutQuad', queue: false});
	}
	function validateEmail(email){
    	var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
    	return emailPattern.test(email);
	}
	$("#sidebar, #contact_submit").mouseup(function() {
		return false
	});
	$('#sidebar').mouseup(showSidebar);
	$(document).mouseup(hideSidebar);
	function contactSuccess(){
		$('#contact_response').html('').removeClass('active');
		hideSidebar;
	}
	$('#contact_form').submit(function(){
		var correct = 0;
		if($('#contact_name').val() != ''){
			removeError('#contact_name');
			correct++;
		}
		else{
			addError('#contact_name');
		}
		if(validateEmail($('#contact_email').val()) && $('#contact_email').val() != ''){
			removeError('#contact_email');
			correct++;
		}
		else{
			addError('#contact_email');
		}
		if($('#contact_message').val() != ''){
			removeError('#contact_message');
			correct++;
		}
		else{
			addError('#contact_message');
		}
		if(correct == 3){
			$('#contact_response').addClass('active').html('<img src="images/load.gif" /> Connecting...');
			$.post('/php/email.php', {name: $('#contact_name').val(), email: $('#contact_email').val(), message: $('#contact_message').val()}, function(e){
				if(e == '1'){
					$('#contact_name').val('');
					$('#contact_email').val('');
					$('#contact_message').val('');
					$('#contact_response').addClass('active').html('Sent!');
					hideSidebar();
				}
				else if(e == '0'){
					$('#contact_response').addClass('active').html('You did something wrong...');
				}
				else{
					$('#contact_response').addClass('active').html('Server messed up, try again.');
				}
			});
		}
		else{
			$('#contact_response').addClass('active').html('Please correct highlighted fields.');
		}
		return false;
	});
	$(function () {
		var msie6 = $.browser == 'msie' && $.browser.version < 7;
		if (!msie6) {
			var top = $('#sidebar').offset().top;
			$(window).scroll(function (event) {
			var y = $(this).scrollTop();
			if (y+10 >= top) { $('#sidebar').addClass('fixed'); }
			else { $('#sidebar').removeClass('fixed'); }
		});
		}
	});
	$('input[tip], select[tip], textspace[tip]').qtip({
		preload: true,
		prerender: true,
		content:{
			text: function(api) {
        		return $(this).attr('tip');
      		}
		},
		show: {
			event: "focusin"
		},
		hide: {
			event: "focusout"
		},
		style: {
			classes: 'ui-tooltip-skimberk1'
		},
		position: {
			at: "right center",
			my: "left center"
		}
	});
	$('a[tip], div[tip], span[tip], img[tip]').qtip({
		preload: true,
		prerender: true,
		content:{
			text: function(api) {
        		return $(this).attr('tip');
      		}
		},
		show: {
			event: "mouseover"
		},
		hide: {
			event: "mouseout"
		},
		style: {
			classes: 'ui-tooltip-skimberk1'
		},
		position: {
			at: "right center",
			my: "left center"
		}
	});
});
