var _showcase = null;
var _listitems = $("#carousel li");
var _moving = false;

function trace(arg) {
	$("#trace").html(arg);
}

function checkForDirectLink() {                 
    var args = window.location.href.split("#");
	if(args.length > 1) {
	   if(args[1].length > 0) {
	   		$.prettyPhoto.autoLink($(".overlay a[rel^='overlay["+args[1]+"]']")[0]);
	   }
	}
}

function startAnimation() {
    _moving = true;
    $("#carousel").stopTime("hide");
    $("#carousel").everyTime(20, "hide", function(i) {
        var cpos = parseInt($("#carousel").css("left"));
        var tpos = -$("#slider").slider("option", "value");
        if(Math.abs(tpos - cpos) < 1 && !_moving) {
            $(this).stopTime("hide");
        } else {
            $('#carousel').jcarousel('animate',  cpos + (tpos - cpos) / 10, false);
        }                                                                                         
    }, 0);
}

function stopAnimation() {
    _moving = false;
}

function onStartRollOver(scope, tpos) {
	$(scope).stopTime("rollover");
	$(scope).everyTime(20, "rollover", function(i) {
		var cpos = parseInt($('p', scope).css("bottom"));
		if(Math.abs(tpos - cpos) < 1) {
            $(scope).stopTime("rollover");
			$('p', scope).css("bottom", tpos);
			$('a span.title', scope).css("bottom", tpos + ($('p', scope).height() + $('span.title', scope).height() - parseInt($('p', scope).css("font-size"))));
        } else {
            $('p', scope).css("bottom", cpos + (tpos - cpos) / 4);
			$('a span.title', scope).css("bottom", parseInt($('p', scope).css("bottom")) + ($('p', scope).height() + $('span.title', scope).height() - parseInt($('p', scope).css("font-size"))));
        }
	//trace(cpos);
	},0);
} 

function initCarouselCallback(carousel) {
 	_showcase = carousel;
};

function adjustSize() {        
	
	 $("#carousel").stopTime("hide");
	 $('#slider').slider("option", "max", _listitems.length * (_listitems.width() + parseInt(_listitems.css("margin-right")) * 3) - $(window).width());
	 
	if($(window).width() < _listitems.length * (_listitems.width() + parseInt(_listitems.css("margin-right")) * 2)) {
	 	if($('.bottom').is(':visible')) {
			$('.bottom').hide();
			$('#slider').show();
		}
		$('#slider .ui-slider-handle').css("left", 0);
		$('#carousel').jcarousel('animate', 0, false);
	} else {
	 	if($('.bottom').is(':hidden')) {
			$('.bottom').show();
			$('#slider').hide();
			
		}
		$('#carousel').jcarousel('animate', - $('#carousel').width() / 2 + $(window).width() / 2, false);
		$('#slider .ui-slider-handle').css("left", $("#slider").width()/2);
		if(BrowserDetect.browser == "Explorer" && (BrowserDetect.version == "7" || BrowserDetect.version == "6")) {
		 	$('#carousel').css("left" , "0");
		}
	}            
}

$(window).resize(adjustSize);    

$(function() {
    /*
	$('#portfolio').fadeIn('slow');

	$('#nav a').click(function() {
	  	var url = $(this).attr('href');
		//_showcase.reset();
		$('#portfolio').fadeOut(function(){
	    	document.location.href = url;
	  	});
		
	  return false;
	});
    
    */
    $(".bottom").css("display","none");
	
	$('#carousel').jcarousel({initCallback:initCarouselCallback});
	
	
	var options = {start:startAnimation, stop:stopAnimation};
   	$("#slider").slider(options);
   	
    $("#slider").css("margin-top", parseInt($("#slider").css("margin-top")) - 92);
	             
    adjustSize();                          
	
	if(!DetectMobileQuick()) {
		$(".overlay a[rel^='overlay']").prettyPhoto({theme:'light_square'});
	}
	
	Cufon.replace('a .title');
	$("a .label").toggleClass("title");
	$("a .title").toggleClass("label");
	Cufon.now();
	
	$('#carousel li').bind({
		mouseenter: function() {
			onStartRollOver(this, 0);
		},
	  	mouseleave: function() {
			onStartRollOver(this, -180);
		}
	});
	
	checkForDirectLink();       
});
