// jQuery ignition

$(document).ready(function() {
	// init carousel controls
	$("#mycarousel").jcarousel({
        scroll: 1,
        auto: 0,
        wrap: 0,
        initCallback: mycarousel_initCallback
	});
	$("#simpleCarousel").jcarousel({
        scroll: 1,
        auto: 0,
        wrap: 'circular'
	});
	$("#circularCarousel").jcarousel({
        scroll: 1,
        auto: 0,
        wrap: 'circular'
	});
	$("#autoCarousel").jcarousel({
		scroll: 1,
		auto: 4,
		wrap: 'circular'
	});
	$("#navCarousel").jcarousel({
        scroll: 1,
        auto: 0,
        wrap: 'circular'
	});
	// initial hide of project sections
	$("div.Collapsed").next(".projectCont").css({display: 'none'});

	//Full Box Sliding (Hidden to Visible) on work index page
	$('.block,.block-right').hover(function(){
		$(".slider", this).stop().animate({top:'186px'},{queue:false,duration:160});
	}, function() {
		$(".slider", this).stop().animate({top:'360px'},{queue:false,duration:160});
	});	     

	// team menu click state
	$(".team-list li a").live('click', function() {
		$(".selected").removeClass("selected");
		$(this).addClass("selected");
		return true;
	}); //End click 

	// project section hover states
	var original_pj_img = '';
	$(".projectDD").hover(function() {
		if($(this).hasClass("Collapsed")) {
			original_pj_img = $(this).children("img").attr("src");
			var pj_img = $(this).children("img").attr("src").replace("Dropdown_Expand.png", "Dropdown_Expand_R.png");
			$(this).children("img").attr("src", pj_img);
		}
		if($(this).hasClass("Expanded")) {
			original_pj_img = $(this).children("img").attr("src");
			var pj_img = $(this).children("img").attr("src").replace("Dropdown_Collapse.png", "Dropdown_Collapse_R.png");
			$(this).children("img").attr("src", pj_img);
		}
	}, function() { 
		if($(this).hasClass("Collapsed")) {
			var pj_img = $(this).children("img").attr("src").replace("Dropdown_Expand_R.png", "Dropdown_Expand.png"); 
			if( original_pj_img === pj_img ) {
				$(this).children("img").attr("src", original_pj_img); 
			}
		}
		if($(this).hasClass("Expanded")) {
			var pj_img = $(this).children("img").attr("src").replace("Dropdown_Collapse_R.png", "Dropdown_Collapse.png"); 
			if( original_pj_img === pj_img ) {
				$(this).children("img").attr("src", original_pj_img); 
			}
		}
	});
	// project section click state
	$(".Collapsed").live('click', function() {
		$(this).removeClass("Collapsed");
		$(this).addClass("Expanded");
		var nj_img = $(this).children("img").attr("src").replace("Dropdown_Expand_R.png", "Dropdown_Collapse.png");
		var pj_img = nj_img.replace("Dropdown_Expand.png", "Dropdown_Collapse.png");
		$(this).children("img").attr("src", pj_img); 
		$(this).next(".projectCont").slideDown("slow");
		return false;
	});
	$(".Expanded").live('click', function() {
		$(this).removeClass("Expanded");
		$(this).addClass("Collapsed");
		var nj_img = $(this).children("img").attr("src").replace("Dropdown_Collapse_R.png", "Dropdown_Expand.png");
		var pj_img = nj_img.replace("Dropdown_Collapse.png", "Dropdown_Expand.png");
		$(this).children("img").attr("src", pj_img); 
		$(this).next(".projectCont").slideUp("fast");
		return false;
	}); //End click
	$(".InnerCollapse").hover(function() {
			original_pj_img = $(this).attr("src");
			var pj_img = $(this).attr("src").replace("Dropdown_Collapse.png", "Dropdown_Collapse_R.png");
			$(this).attr("src", pj_img);
	}, function() { 
			var pj_img = $(this).attr("src").replace("Dropdown_Collapse_R.png", "Dropdown_Collapse.png"); 
			if( original_pj_img === pj_img ) {
				$(this).attr("src", original_pj_img); 
			}
	});
	$(".InnerCollapse").live('click', function() {
		$(this).parent().prev(".Expanded").addClass("Collapsed");
		var pj_img = $(this).parent().prev(".Expanded").children("img").attr("src").replace("Dropdown_Collapse.png", "Dropdown_Expand.png"); 
		$(this).parent().prev(".Expanded").children("img").attr("src", pj_img); 
		$(this).parent().slideUp("fast");
		$(this).parent().prev(".Expanded").removeClass("Expanded");
		$('html, body').animate({ scrollTop: $(this).parent().prev(".projectDD").offset().top }, 'fast'); 
		return false;
	}); //End click

	// social media hover states
	$(".SocialLink").hover(function() {
		var replace_image = $(this).attr("src").replace(".gif", "_Rollover.gif");
		$(this).attr("src", replace_image);
	}, function() { 
		var replace_image = $(this).attr("src").replace("_Rollover.gif", ".gif");
		$(this).attr("src", replace_image);
	});

	// social media description hover states
	$(".linkText a").hover(function() {
		$(".socialDesc").html( ' / '+$(this).attr("alt") );
	}, function() { 
		$(".socialDesc").html( '&nbsp;' );
	});

	// home page artboard pop up div
	$("#popImage, #popQImage").live('click', function() {
		$("div#popOver").show();
		$('#popQImage').stop(true, true).fadeIn().css('opacity', 1);
		return false;
	}); //End click
	$("#popClose").live('click', function() {
		$("div#popOver").hide();
		return false;
	}); //End click

	// home page pulse on artboard question image
	$('#popQImage').effect("pulsate", { times:100 }, 3000);

	//Get our elements for faster access and set overlay width
	var div = $('.jcarousel-wrapper'),
	ul = $('.jcarousel-menu'),
// unordered list's left margin
ulPadding = 0;

//Get menu width
var divWidth = div.width();

//Remove scrollbars
div.css({overflow: 'hidden'});

//Find last image container
var lastLi = ul.find('li:last-child');

//When user move mouse over menu
div.mousemove(function(e){

//As images are loaded ul width increases,
//so we recalculate it each time
var ulWidth = lastLi[0].offsetLeft + lastLi.outerWidth() + ulPadding;

var left = (e.pageX - div.offset().left) * (ulWidth-divWidth) / divWidth;
div.scrollLeft(left);
});

function mycarousel_initCallback(carousel) {
    jQuery('.jcarousel-control a').bind('click', function() {
        carousel.scroll(jQuery.jcarousel.intval(jQuery(this).attr('rel')));
        return false;
    });
};
function simpleCarousel_initCallback(carousel)
{
    // Disable autoscrolling if the user clicks the prev or next button.
    carousel.buttonNext.bind('click', function() {
        carousel.startAuto(0);
    });

    carousel.buttonPrev.bind('click', function() {
        carousel.startAuto(0);
    });
	
	// Pause autoscrolling if the user moves with the cursor over the clip.
    carousel.clip.hover(function() {
        carousel.stopAuto();
    }, function() {
        carousel.startAuto();
    });
};
});
