    var stageCenter = 0;

$(document).ready(function() {
    Cufon.replace('#myInfo h2, #contactForm h2, #twitter em');
    var closedPos = 198;
    var height = 140;
    var itemTotal = $('#workNavWrapper ul li a').size();
    var hash = location.hash;
    var workID = hash.split('/');

    loadWork(workID[1], 'l');
    
    getStageCenter();
    
    $('#contentWrapper').css({'top': $(window).height()/2});
    
    $('#content .link').attr("target","_blank");

    var navHandler = function() {

	var item = $(this).attr('id').substring(4);
	var curitem = $('#content h2').attr('class').substring(4);

	if (item > curitem) {
	    return loadWork(item, 'r');
	} else if (item < curitem) {
	    return loadWork(item, 'l');
	} else {
	    return false;
	}

    };

    var arrowHandler = function() {

	var curitem = $('#content h2').attr('class').substring(4);
	if ($(this).attr('id') == 'contentPrev') {
	    //curitem--;
	    loadWork(--curitem, 'l');
	} else {
	    //curitem++;
	    loadWork(++curitem, 'r');
	}

    }

    //    var portfolioNav = $.cookie('protfolioNav');
    //    
    //    if (portfolioNav == 'open') {
    //    
    //	$('#workNav').toggleClass('closed').css({bottom: height - closedPos}).find('#workNavContent').css({top: '3px'});
    //       
    //	$('#content').css({top: ((($(window).height() - height) / 2 ) -17)});
    //    
    //    }
    //$('#content').animate({top: ($(window).height()/2)}, 1000, 'easeOutCirc');

    //    $('#toggleWorkNav').click(function() {
    //
    //	if ($('#workNav').attr('class') == 'closed') {
    //
    //	    $('#workNav').toggleClass('closed').stop().animate({
    //		bottom: height - closedPos
    //	    },
    //	    800, 'easeInOutCirc').find('#workNavContent').stop().animate({
    //		top: '2px'
    //	    },
    //	    1200, 'easeInOutCirc');
    //
    //	    $('#content').animate({
    //		top: ((($(window).height() - height) / 2) - 17)
    //	    },
    //	    1200, 'easeInOutCirc');
    //
    //	    $.cookie('protfolioNav', 'open', { path: '/', expires: 365 });
    //
    //	} else {
    //
    //	    $('#workNav').toggleClass('closed').stop().animate({
    //		bottom: '-' + closedPos
    //	    },
    //	    800, 'easeInOutCirc', function() {
    //		$(this).find('#workNavContent').stop().css({
    //		top: '30px'
    //	    });
    //	    });
    //
    //	    $('#content').animate({
    //		top: (($(window).height() / 2) - 17)
    //	    },
    //	    800, 'easeInOutCirc');
    //
    //	    $.cookie('protfolioNav', 'closed', { path: '/', expires: 365 });
    //
    //	}
    //
    //    });

    $('#workNav').hover(function() {

	$('#workNav').stop().animate({
	    bottom: height - closedPos
	},
	800, 'easeInOutCirc').find('#workNavContent').stop().animate({
	    top: '2px'
	},
	1200, 'easeInOutCirc');

	$('#contentWrapper').stop().animate({
	    top: ((($(window).height() - height) / 2))
	},
	1200, 'easeInOutCirc');

	$('#toggleWorkNav').stop().fadeTo(800, 1).animate({
	    bottom: '0px'
	},
	300, 'easeInCirc');
    },
    function() {

	$('#workNav').stop().animate({
	    bottom: '-' + closedPos
	},
	800, 'easeInOutCirc',
	function() {
	    $(this).find('#workNavContent').stop().css({
		top: '30px'
	    });
	});

	$('#contentWrapper').stop().animate({
	    top: (($(window).height() / 2) - 17)
	},
	800, 'easeInOutCirc');

	$('#toggleWorkNav').stop().fadeTo(600, 1).animate({
	    bottom: '100px'
	},
	800, 'easeOutElastic');

    });

    $('#workNavWrapper ul li a').hover(function() {

	$(this).stop().animate({
	    backgroundColor: "#ff2020"
	},
	200);

    },
    function() {

	$(this).stop().animate({
	    backgroundColor: "#161616"
	},
	200);

    });

    var menuWidth = Math.ceil($('#workNavWrapper ul li a').size() / 4);
    //var menuPos = $.cookie('protfolioPos');

    //if (menuPos == null) {
    var menuPos = 0;
    //}

    var amount = menuPos * -300;
    $('#workNavWrapper').css({
	'left': amount
    });

    calcArrows(menuPos);

    function calcArrows(pos) {

	if (menuWidth - pos > 3) {

	    $('#workNavRight').addClass('active');

	} else {

	    $('#workNavRight').removeClass('active');

	}

	if (menuWidth - pos < menuWidth) {

	    $('#workNavLeft').addClass('active');

	} else {

	    $('#workNavLeft').removeClass('active');

	}

    }

    function scrollMenu(pos) {

	var amount = pos * -300;
	$('#workNavWrapper').animate({
	    'left': amount
	},
	1200, 'easeInOutCirc');
	calcArrows(pos);
	//$.cookie('protfolioPos', pos);
    }

    $('#workNavRight').click(function() {

	if (menuWidth - menuPos > 3) {
	    //calcArrows(menuPos);
	    menuPos++;

	    scrollMenu(menuPos);
	}
    });

    $('#workNavLeft').click(function() {

	if (menuWidth - menuPos < menuWidth) {
	    //calcArrows(menuPos);
	    menuPos--;
	    scrollMenu(menuPos);
	}

    });

    $('#openTopBar').click(function() {

	$('h1').css({
	    'zIndex': 130
	}).animate({
	    'top': '64px'
	},
	1200, 'easeInOutCirc');
	$('#overlay').css({
	    'opacity': 0,
	    'display': 'block'
	}).fadeTo(500, 0.95);
	$('#topBar').animate({
	    'top': '100px'
	},
	1200, 'easeInOutCirc');

    });

    $('#openSettingsBar').click(function() {

	$('h1').css({
	    'zIndex': 130
	});
	$('#overlay').css({
	    'opacity': 0,
	    'display': 'block'
	}).fadeTo(500, 0.95);
	$('#settingsBar').animate({
	    'top': '100px'
	},
	1200, 'easeInOutCirc');

    });

    $('.closeBar').click(function() {
	
	$('h1').animate({
	    'top': '10px'
	},
	1200, 'easeInOutCirc');
	
	$('#overlay').fadeOut(500);
	$(this).parent().parent().animate({
	    'top': '-400px'
	},
	1200, 'easeInOutCirc',
	function() {
	    $('h1').css({
		'zIndex': 50
	    });
	});
    });

    $('#contactForm form').validate({
	rules: {

	    name: "required",
	    email: {
		required: true,
		email: true
	    },
	    message: "required"
	},
	messages: {
	    name: "Not valid",
	    email: {
		required: "Not valid",
		email: "Not valid"
	    },
	    message: "Not valid"
	},
	submitHandler: function() {

	    $.ajax({

		url: 'includes/contact.php',
		type: 'POST',
		datatype: 'html',
		data: {

		    name: $('input[name=name]').attr('value'),
		    email: $('input[name=email]').attr('value'),
		    message: $('textarea[name=message]').attr('value'),
		    blank: $('input[name=phone]').attr('value'),
		    sendEmail: 'send'

		},
		success: function(data) {

		    $('#sendEmail').fadeOut(200);

		    $('input[name=name]').attr('value', '');
		    $('input[name=email]').attr('value', '');
		    $('textarea[name=message]').attr('value', '');
		}

	    });

	}

    });

    /*var IE6 = (navigator.userAgent.indexOf("MSIE 6")>=0) ? true : false;

    if(IE6){

    	$(function(){
	
	    $('body').append('<div id="overlay"></div><div id="popup"><p>No IE6!</p></div>');
    		
	});		
    
    }*/
    $('#myInfo').jScrollPane({
	showArrows: false,
	scrollbarWidth: 15,
	dragMaxHeight: 30,
	dragMinHeight: 30,
	scrollbarMargin: 15
    });
//    $('#contentInfo').jScrollPane({
//	showArrows: false,
//	scrollbarWidth: 15,
//	dragMaxHeight: 30,
//	dragMinHeight: 30,
//	scrollbarMargin: 15
//    });

    function loadWork(id, direction) {
	$('#contentPrev').fadeOut(300);
	$('#contentNext').fadeOut(300);
	$('#workNavWrapper ul li a').unbind('click', navHandler);
	$('.contentArrow').unbind('click', arrowHandler);

	if (direction == 'r') {
	    var startPos = $(document).width() + 500;
	    var endPos = '-500px';
	} else {
	    var startPos = '-500px';
	    var endPos = $(document).width() + 500;
	}

	$('#content').animate({
	    'left': endPos
	},
	1000, 'easeInCirc',
	function() {

	    $('#loading').show();
	    $.ajax({

		url: 'includes/content.php',
		type: 'GET',
		datatype: 'json',
		data: {

		    id: id

		},
		success: function(item) {

		    $('#loading').hide();
		    
		    $('#content h2').attr('class', 'item'+item.id).html('<em>'+ item.idzeros +'</em> '+ item.title +' <span class="date">'+ item.date +'</span>');
		    $('#contentInfo').show().html('<ul class="technology"></ul><p class="description"></p>');
		    $('#content .description').html(item.description);
		    $('#content .link').attr('href', item.link);
		    
		    for(var i in item.tech) {
			$('#content .technology').append('<li>'+ item.tech[i] +'</li>');
		    }
		    
		    $('#slideshowNav').html('');
		    $('#contentLeft').html('');
		    for(var i = 1; i <= item.previews; i++) {
			$('#contentLeft').append('<img id="preview'+ i +'" width="480" height="330" class="previewImage" src="previews/'+ item.id +'-'+ i +'.jpg" alt="preview '+ i +'" />');
		    }
		    
		    $('#content').css({
			'left': startPos
		    }).show().animate({
			'left': stageCenter+'px'
		    },
		    1000, 'easeOutCirc',
		    function() {
			$('#workNavWrapper ul li a').bind('click', navHandler);
			$('.contentArrow').bind('click', arrowHandler);
		
			var curID = $('#content h2').attr('class').substring(4);
		
			if (id != 1 && itemTotal > 1) {
			    var prevID = parseFloat(curID) - 1;
		
			    $('#contentPrev').fadeIn(300).attr('href', $('#item' + prevID).attr('href'));
			}
			if (id != itemTotal && id != undefined) {
			    var nextID = parseFloat(curID) + 1;
		
			    $('#contentNext').fadeIn(300).attr('href', $('#item' + nextID).attr('href'));
			}
		    });
		    Cufon.replace('#content h2, #slideshowNav a');
		    $('#contentInfo').jScrollPane({
			showArrows: false,
			scrollbarWidth: 15,
			dragMaxHeight: 30,
			dragMinHeight: 30,
			scrollbarMargin: 15
		    });
		    $('.slideshow').cycle({
			fx: 'scrollDown',
			speed: 300,
			easing: 'easeInOutCirc',
			timeout: 0,
			pager: '#slideshowNav'
		    });

		}

	    });

	});
    }

});

$(window).resize(function() {
   getStageCenter();
});

function getStageCenter() {
    stageCenter = $(document).width()/2;
};
