var stepWidth = 597;
var stepImageWidth = 597;
var topGap = 366;
var widthGap = 298;
var currentMargin = 0;
var currentProject = '';
var blockProjects = false;
var blockImages = false;
var prevImageLinkOver = false;
var nextImageLinkOver = false;
var animateTime = 350; /*miliseconds*/
var tipHeight = 25; 
var tipTop = 366; 
var tipTop2 = 339; 
var animateTipTime = 250; /*miliseconds*/
var showTip = false;
var stepTestimnonialWidth = 283;

$(document).ready(function(){
    $('.project_list_outer a.prev').click(function(){
        if (!blockProjects) {
            blockProjects = true
            currentMargin = parseInt($(this).siblings('.project_list_inner').css('marginLeft'),10);
            if (currentMargin + stepWidth >= 0) {
                $(this).css('visibility','hidden');
            }
            $('.testimonial_inner').animate({marginLeft: "+=" + stepTestimnonialWidth  + "px"}, animateTime);
            $('.project_list_inner').animate({marginLeft: "+=" + stepWidth + "px"}, animateTime, '', function(){
                blockProjects=false;
            });
            currentProject=$('#'+currentProject).prev().attr('id');
            $('#'+currentProject+' .project_images_inner').css('margin-left',0);
            $('#'+currentProject+' .before_image').hide();
            $('#'+currentProject+' .after_image').show();
            $('#'+currentProject+' .project_title a').removeClass('current');
            $('#'+currentProject+' .project_title a:first').addClass('current');

            loadProjectImages(currentProject);
            $(this).siblings('a.next').css('visibility','');
        }
        return false;
    });

    $('.project_list_outer a.next').click(function(){
        if (!blockProjects) {
            blockProjects = true
            currentMargin = parseInt($(this).siblings('.project_list_inner').css('marginLeft'),10);
            maxMargin = ($(this).siblings('.project_list_inner').children('.project_item').size() - 1)*stepWidth;
            if (currentMargin + maxMargin - stepWidth <= 0) {
                $(this).css('visibility','hidden');
            }
            $('.testimonial_inner').animate({marginLeft: "-=" + stepTestimnonialWidth  + "px"}, animateTime);
            $('.project_list_inner').animate({marginLeft: "-=" + stepWidth  + "px"}, animateTime, '', function(){
                blockProjects=false;
            });
            currentProject=$('#'+currentProject).next().attr('id');
            $('#'+currentProject+' .project_images_inner').css('margin-left',0);
            $('#'+currentProject+' .before_image').hide();
            $('#'+currentProject+' .after_image').show();
            $('#'+currentProject+' .project_title a').removeClass('current');
            $('#'+currentProject+' .project_title a:first').addClass('current');

            loadProjectImages(currentProject);
            $(this).siblings('a.prev').css('visibility','');
        }
        return false;
    });

    $('.after_image').click(function(){
    	$(this).fadeOut(500,function(){
    		$(this).siblings('.before_image').fadeIn(500);
    	});
    })

    $('.before_image').click(function(){
    	$(this).fadeOut(500,function(){
    		$(this).siblings('.after_image').fadeIn(500);
    	});
    })

    $('.project_image').hover(function(){
        showTip = true;
        $(this).find('div.after_before_image').css("visibility", "visible");
        $(this).find('div.after_before_image').stop().animate({
            top: tipTop2+"px",
            height: tipHeight+"px"
        }, animateTipTime);
    }, function(){
        showTip = false;
        $(this).find('div.after_before_image').stop().animate({
            top: tipTop+"px",
            height: "0px"
        }, animateTipTime, function(){
            if (!showTip) {
                $(this).css("visibility", "hidden");
            }
        });
    });

    $('.project_item').each(function(){
        $('#'+$(this).attr('id')+' .project_image:first a').each(function(){
            $(this).prev('img.hidden:last').attr('src',$(this).attr('href')).load(function(){
                $(this).removeClass('hidden').next('a').remove();
                $(this).prev('img.temp').remove();
            });
        });
    });

    currentProject=$('.project_item:first').attr('id');
    loadProjectImages(currentProject);
})

function loadProjectImages(projectDivId)
{
    $('#'+projectDivId+' .project_image a').each(function(){
        $(this).prev('img.hidden:last').attr('src',$(this).attr('href')).load(function(){
            $(this).removeClass('hidden').next('a').remove();
            $(this).prev('img.temp').remove();
            if ($(this).hasClass('before_image')) {
            	$(this).hide();
            }
        });
    });
}

function goTo(projectId)
{
    blockProjects = true;
    $('#project_'+projectId+' .project_images_inner').css('margin-left',0);
    $('#project_'+projectId+' .before_image').hide();
    $('#project_'+projectId+' .after_image').show();
	$('#project_'+projectId+' .project_title a').removeClass('current');
	$('#project_'+projectId+' .project_title a:first').addClass('current');

    newMargin = $('#project_'+projectId).prevAll('.project_item').size()*stepWidth;
    newTestimnonialMargin = $('#testimonial_'+projectId).prevAll('.testimonial_item').size()*stepTestimnonialWidth;
    maxMargin = ($('.project_list_inner').children('.project_item').size() - 1)*stepWidth;
    $('.project_list_outer a').css('visibility','');
    if (newMargin <= 0) {
        $('.project_list_outer a.prev').css('visibility','hidden');
    }
    if (newMargin == maxMargin) {
        $('.project_list_outer a.next').css('visibility','hidden');
    }
    $('.testimonial_inner').animate({marginLeft: "-" + newTestimnonialMargin + "px"}, animateTime);
    $('.project_list_inner').animate({marginLeft: "-" + newMargin + "px"}, animateTime, '', function(){
        blockProjects=false;
    });
    currentProject=$('#project_'+projectId).attr('id');
    loadProjectImages(currentProject);
//    return false;
}

function goToImage(projectId,imageId)
{
	$('.project_title a').removeClass('current');
	$('a#link'+imageId).addClass('current');
    newMargin = $('#image_'+imageId).prevAll('.project_image').size()*stepImageWidth;
    maxMargin = ($('#project_'+projectId).children('.project_images_outer').children('.project_images_inner')
    		.children('.project_item').size() - 1)*stepWidth;
    $('#image_'+imageId+' .before_image').hide();
    $('#image_'+imageId+' .after_image').show();
    $('#project_'+projectId).children('.project_images_outer').children('.project_images_inner').animate(
		{marginLeft: "-" + newMargin + "px"}, animateTime, '', function(){
			blockProjects=false;
		}
	);
}

function hidePrevLink(){
    if(!prevImageLinkOver) {
        $('div.prev_image').css('visibility','hidden');
    }
}

function hideNextLink(){
    if(!nextImageLinkOver) {
        $('div.next_image').css('visibility','hidden');
    }
}