jQuery(document).ready(function($) {
	/*** Function and initial vars ***/
	var menu_width = 250;
	var photo_height = 548;
	var content_width = 730;
	///
	function windowResized() {
		var topval = $(window).height()/2 - $("#menu_logo").outerHeight()/2;
		var marginTop = $(window).height()/2 - photo_height/2;
		var marginBottom = $(window).height();
		var leftval = $(window).width()/2 - content_width/2;
		if (leftval < menu_width) {leftval = menu_width + 10;}

		if (layout == 'page' || layout == 'default') {
			$('#main_content').animate({"left": leftval, "top": marginTop}, {"duration" : 800, "easing" : "easeInOutCubic", queue:false});
		} else if (layout == 'portfolio') {
			$('#gallery .gallery_list li').css({"marginTop": marginTop, "marginBottom": marginBottom});
			$('#gallery').animate({"left": leftval}, {"duration" : 800, "easing" : "easeInOutCubic", queue:false});

			//var new_y = (- $('.gallery_list li').outerHeight(true) + marginTop) * cur_photo;
			//$('.gallery_list').animate({top: new_y}, {queue:false, duration: 0});
		}
		$("#menu_logo").animate({"top": topval}, {"duration" : 500, "easing" : "easeInOutCubic", queue: false});
		//}
	};

	function openSiteSection(menu_item) {
		var new_x = $(window).width();
		if (layout == 'page' || layout == 'default') {
			$("#main_content").animate({"left": new_x}, {"duration": 800, "easing" : "easeInOutCubic", queue: false, "complete" : function() {loadContent(menu_item)}});
		} else if (layout == 'portfolio') {
			$("#gallery").animate({"left": new_x}, {"duration": 800, "easing" : "easeInOutCubic", queue: false, "complete" : function() {loadContent(menu_item)}});
		}
	}

        photos = new Array();
	cur_photo = 0;
	function nextPhoto() {
                if (layout == 'portfolio') {
                    photos[cur_photo].fadeOut('slow');
                    cur_photo++;
                    var total_photos = $('.gallery_list li').size();
                    if (cur_photo == total_photos) {cur_photo = 0;}
                    photos[cur_photo].fadeIn('slow');
		} else {
                    cur_photo++;
                    var dur = 1000;
                    var total_photos = $('.gallery_list li').size();
                    if (cur_photo == total_photos) {cur_photo = 0;dur = 1500;}
                    var marginTop = $(window).height()/2 - photo_height/2;
                    var new_y = (- $('.gallery_list li').outerHeight(true) + marginTop) * cur_photo;
                    $('.gallery_list').animate({top: new_y}, {queue:false, duration: dur});
		}
	}
	function prevPhoto() {
                if (layout == 'portfolio') {
                    photos[cur_photo].fadeOut('slow');
                    cur_photo--;
                    var total_photos = $('.gallery_list li').size();
                    if (cur_photo == -1) {cur_photo = total_photos-1}
                    photos[cur_photo].fadeIn('slow');
		} else {
                    cur_photo--;
                    var dur = 1000;
                    var total_photos = $('.gallery_list li').size();
                    if (cur_photo == -1) {cur_photo = total_photos-1;dur = 1500;}
                    var marginTop = $(window).height()/2 - photo_height/2;
                    var new_y = (- $('.gallery_list li').outerHeight(true) + marginTop) * cur_photo;
                    $('.gallery_list').animate({top: new_y}, {queue:false, duration: dur});
                }
	}

	function show_logo() {
		$('#intro').css({"left": $(window).width()/2 - 125, "top" : $(window).height()/2 - $('#intro').outerHeight()/2 - 70});
		var intro = $('#intro').offset();

		$('#intro').fadeOut(0).fadeIn(2000, "easeInOutCubic");
		$('#intro').animate({"top": intro.top + 50}, {"duration" : 800, "easing" : "easeInOutCubic", queue:false});
	}
	function hide_logo() {
		var intro = $('#intro').offset();
		$('#intro').stop().animate({"top": intro.top + 30, "opacity": 0}, {"duration" : 800, "easing" : "easeInOutCubic", queue:false});
	}
	function show_menu() {
		var topval = $(window).height()/2 - $("#menu_logo").outerHeight()/2;
		$("#menu_logo").css({"top": topval, "left": -$("#menu_logo").outerWidth()});
		$("#menu_logo").animate({opacity: 1.0}, 500).animate({"left": 0}, {"duration" : 1000, "easing" : "easeInOutCubic", queue: true});
	}
	function show_twitter_btn() {
		$("#twitter_btn").css({"right": -100});
		$("#twitter_btn").animate({opacity: 1.0}, 2000).animate({"right": 0}, {"duration" : 1000, "easing" : "easeInOutCubic", queue: true});
	}
	function show_social_icons() {
		$("#social_icons").css({"right": -100});
		$("#social_icons").animate({opacity: 1.0}, 2100).animate({"right": 0}, {"duration" : 1000, "easing" : "easeInOutCubic", queue: true});
	}

	function count_photos() {
                photos = new Array();
                $('.gallery_list li').each(function() {
                    photos.push(jQuery(this));
                    jQuery(this).css('position', 'fixed');
                    jQuery(this).css('display', 'block');
                    jQuery(this).css('float', 'left');
                    jQuery(this).css('height', '548px');
                    jQuery(this).css('width', '730px');
                    jQuery(this).hide();
                });
		var i = 1;
		var total_photos = $('.gallery_list li').size();
                if (total_photos) {
                    photos[0].fadeIn('slow');
                }
		$(".ph_count").each(function (i) {
			i++;
			$(this).html(i + "/" + total_photos);
		});

	}
	function enable_prettyPhoto() {
		$("a[rel^='destaque']").prettyPhoto({
			animationSpeed: 'normal', /* fast/slow/normal */
			padding: 15, /* padding for each side of the picture */
			opacity: 0.7, /* Value betwee 0 and 1 */
			showTitle: false, /* true/false */
			allowresize: true, /* true/false */
			counter_separator_label: '/', /* The separator for the gallery counter 1 "of" 2 */
			theme: 'light_square', /* light_rounded / dark_rounded / light_square / dark_square */
			hideflash: false, /* Hides all the flash object on a page, set to TRUE if flash appears over prettyPhoto */
			modal: false, /* If set to true, only the close button will close the window */
			changepicturecallback: function(){}, /* Called everytime an item is shown/changed */
			callback: function(){} /* Called when prettyPhoto is closed */
		});
	}
	function enable_scroll() {
		$('.scroll-holder').jScrollPane({
			'reinitialiseOnImageLoad' : true,
			'scrollbarMargin' : 10
		});
	}
	function showLoader() {
		$("#loader").fadeIn();
	}
	function hideLoader() {
		$("#loader").fadeOut();
	}
	function open_section() {
		if (layout == 'page' || layout == 'default') {
			var leftval = $(window).width()/2 - content_width/2;

			if (leftval < menu_width) {leftval = menu_width + 10;}
			var marginTop = $(window).height()/2 - photo_height/2;

			$('#main_content').css({"top": marginTop});
			$('#main_content').css({"left": $(window).width()});
			$('#main_content').animate({opacity: 1.0}, 1000).animate({"left": leftval}, {"duration" : 1200, "easing" : "easeInOutCubic", queue:true});

			enable_scroll();
			enable_prettyPhoto();
		} else if (layout == 'portfolio') {
			var topval = $(window).height()/2 - $("#menu_logo").outerHeight()/2;
			var marginTop = $(window).height()/2 - photo_height/2;
			var marginBottom = $(window).height();
			cur_photo = 0;
			var leftval = $(window).width()/2 - content_width/2;
			if (leftval < menu_width) {leftval = menu_width + 10;}

			$('#gallery .gallery_list li').css({"marginTop": marginTop, "marginBottom": marginBottom});
			$('#gallery_control').animate({"opacity": 0}, {duration:0, queue: false});

			$('#gallery').css({"left": $(window).width()});
			$('#gallery').animate({opacity: 1.0}, 1000).animate({"left": leftval}, {"duration" : 1200, "easing" : "easeInOutCubic", queue:true});

			count_photos();
		}
	}
	function loadContent(menu_item) {
		showLoader();
		show_logo();
		$(menu_item).addClass("current");
		var urlToLoad = $(menu_item).attr("href");
		$.ajax({
			type: "POST",
			url: urlToLoad,
			data: "ajax=true",

			success: function(msg) {
				hide_logo();
				if (first_time) {
					show_menu();
					show_twitter_btn();
					show_social_icons();
					first_time = false;
				}
				if (typeof( window['layout'] ) != "undefined") {
					if (layout == 'page' || layout == 'default') {
						$("#main_content").remove();
					} else if (layout == 'portfolio') {
						$("#gallery").remove();
						$("#gallery_control").remove();
					}
				}
				$("body").append(msg);
				open_section();
				hideLoader();
			}
		});
	}

	function load_single_post(menu_item) {
		showLoader();
		var urlToLoad = $(menu_item).attr("href");
		$.ajax({
			type: "POST",
			url: urlToLoad,
			data: "ajax=true",

			success: function(msg) {
				$(menu_item).parent().html(msg);
				hideLoader();
				enable_scroll();
				enable_prettyPhoto();
			}
		});
	}
	/*********************************/

	$(".post_link, .thumbnail_a").live("click", function(){
		$(".site_menu .current").removeClass("current");
		load_single_post($(this));
		return false;
	});


	$("#tweets").tweet({
		join_text: "",
		username: twitterlogin,
		avatar_size: 0,
		count: 5,
		auto_join_text_default: "",
		auto_join_text_ed: "",
		auto_join_text_ing: "",
		auto_join_text_reply: "",
		auto_join_text_url: "",
		loading_text: "loading tweets..."
	});
	$("#twitter_btn").toggle(
		function () {
			$("#tweets_list").animate({"right" : 30});
		},
		function () {
			$("#tweets_list").animate({"right" : -200});
		}
    );

	$('.social_ico img').hover(function() {
		$(this).animate({right: '5px'}, {queue:false, duration: 150});
	},
	function(){
		$(this).animate({right: '0px'}, {queue:false, duration: 150});
	});

	$("#menu_logo").css({"width": menu_width});
	$('.site_menu li a').hover(function() {
		$(this).animate({paddingLeft: '50px'}, {queue:false, duration: 150}); /*, easing:'easeOutQuart'*/
	},
	function(){
		$(this).animate({paddingLeft: '30px'}, {queue:false, duration: 150});
	});

	$(".site_menu a").click(function () {
		if ($(this).hasClass("current") == false) {
			$(".site_menu .current").removeClass("current");
			openSiteSection($(this));
		}
		return false;
	});

	$(".post_meta a, .tags a").live("click", function () {
		$(".site_menu .current").removeClass("current");
		openSiteSection($(this));
		return false;
	});


	$('#gallery_next').live("click", function(){
		nextPhoto();
		return false;
	});

	$('#gallery_prev').live("click", function(){
		prevPhoto();
		return false;
	});


	$('.gallery_list img').live("mousemove", function(e){
		var total_photos = $('.gallery_list li').size();
		if (total_photos > 1) {
			var leftval = e.pageX - $('#gallery_control').outerWidth()/2;
			var topval = e.pageY - $('#gallery_control').outerHeight()/2;
			$('#gallery_control').animate({"left": leftval, "top": topval}, {"duration" : 500,  queue:false});
			$('#gallery_control').animate({"opacity": 1}, {queue: false});
		}
	})
	.live("mouseout", function(){
    	$('#gallery_control').animate({"opacity": 0}, {queue: false});
	});

	$('#gallery_control').live("mouseover", function(){
		var total_photos = $('.gallery_list li').size();
		if (total_photos > 1) {
			$(this).animate({"opacity": 1}, {queue: false});
		}
    })
	.live("mouseout", function(){
    	$('#gallery_control').animate({"opacity": 0}, {queue: false});
	});



	/********************************/
	if (first_time) {
		$(window).load(function () {
			loadContent($(".site_menu li.cat-item-"+photo_cat_id+" a:first"));
		});
	} else {
		open_section();
		hideLoader();
		show_menu();
		show_twitter_btn();
		show_social_icons();
		enable_scroll();
	}

	var resizeTimer = null;

	$(window).bind('resize', function() {
		if (resizeTimer) clearTimeout(resizeTimer);
		resizeTimer = setTimeout(windowResized, 10);
	});
});
