/*control hover interaction*/

var bindTimeout;
var hoverTimeout = "";
var scrollHoverTimeout = "";
var hideFunc;

function bindHover() { //this function controls the mouse over state on top of individual project
    $('.project_item_hvr').unbind();
    $(".project_item_hvr").mouseenter(
        function() {
            clearTimeout($(this).hoverTimeout)
            $(this).children(".project_hover").stop().css({display:'block',opacity:0}).animate({opacity:1},500)
        }
    ).mouseleave(
        function() {
        hideFunc = hideHover($(this))
        $(this).hoverTimeout = setTimeout("hideFunc",50);
        }
    );
};
function hideHover(obj) {obj.children(".project_hover").stop().animate({opacity:0},500, function(){$(this).css({display:'none'}) })
};

function bindCategory(carousel) { //this function controls the behavior of category items
	$(".project_category").unbind()
	
	$(".project_category").click(function() {

		var catName = $(this).text();
		for (i=0;i<mycarousel_itemList.length;i++) {
			if (mycarousel_itemList[i].first == catName) {
				if (i==0) {
					carousel.scroll(mycarousel_itemList.length -1);
				}
				else {
					if (i==1) {carousel.scroll(mycarousel_itemList.length);}
					else {carousel.scroll(i-1);}
				}
			}
		}

	});
	
};



function bindSecondaryNavigation(carousel) {

	$('.section_title').mousedown(function() {
	var catName = $(this).text();
	for (i=0;i<mycarousel_itemList.length;i++) {
		if (mycarousel_itemList[i].first == catName) {
			if (i==0) {
				carousel.scroll(mycarousel_itemList.length -1);
			}
			else {
				if (i==1) {carousel.scroll(mycarousel_itemList.length);}
				else {carousel.scroll(i-1);}
			}
		}
	}
});
    /*end secondary navigation binds*/
}

//determine if next item in list is a category box, if it is, trigger the event

carbg = '';
precat = '';

function mycarousel_itemFirstInCallback(carousel, widget, i, state, evt) {
	var idx = carousel.index(i, mycarousel_itemList.length);
	var nextCarousel;
	
	 
	 if(idx == mycarousel_itemList.length) {
	 	 nextCarousel = 0;
	 }
	 else {
	 	 if(idx == mycarousel_itemList.length - 1) {
	 	 	 nextCarousel = -1;
	 	 }
	 	 else {
	 	 	nextCarousel = idx;	 
	 	 }
	 }
	 var widget = mycarousel_itemList[nextCarousel + 1];

	// if(widget.first != "") {
	 	 $(".project_category").removeClass("project_category_active");
	 	$("." + widget.cat.replace(" ","")).addClass("project_category_active");
	 	
	 	// CALL SECONDARY NAV HEADER/SCROLL - PIXELDAY //
	 	scrollToSecNav(widget.cat.toLowerCase().replace(' ','_'));
	 	
	 	// CALL PAGE CONENT UPDATE - PIXELDAY //
		
		if (carbg != widget.bg) {
			setCarouselBackground(widget.bg);
			carbg = widget.bg;
		}
		
		if (precat != widget.cat) {
			scrollToCat = widget.cat.replace('c_','');
//			alert(scrollToCat);
			scrollToSecNav(scrollToCat);
			precat = widget.cat;
		 	setCarouselSectionText(widget.alt);
		}
	 	
	 	// NO LONGER USED IN PRODUCTION - PIXELDAY //
	 	// $("#currentCarousel").html("Current Carousel Category: " + widget.alt);
	 	
	 	// change secondary navigation
	 	// $("#navi option").attr("selected","");
	 	// $("#navi option[value='" + widget.alt + "']").attr("selected","selected");

	 //}
};

function mycarousel_itemVisibleInCallback(carousel, widget, i, state, evt)
{
	clearTimeout(bindTimeout);

    // The index() method calculates the index from a
    // given index who is out of the actual item range.	    

    
    var idx = carousel.index(i, mycarousel_itemList.length);
    carousel.add(i, mycarousel_getItemHTML(mycarousel_itemList[idx - 1]));
    //we rebind the necessary event triggers here after each re-rendering of the carousel
    
    bindTimeout = setTimeout("bindHover()",450);
    bindCategory(carousel);
    bindSecondaryNavigation(carousel);
    
};
 
function mycarousel_itemVisibleOutCallback(carousel, widget, i, state, evt)
{carousel.remove(i);};
 
/**
 * Item html creation helper.
 */
function mycarousel_getItemHTML(widget)
{
	if(widget.type == "project") {
		if(widget.first != '') {
		return '<div class="project_item"><div class="project_item_hvr"><img src="' + widget.url + '" width="184" height="141" alt="' + widget.alt + '" label="' + widget.href + '" /><div class="project_hover"><a href="'+widget.href+'"><span class="alphaback"></span><span class="project_name">' + widget.title  + '</span><span class="plus"></span></a></div></div><div class="project_category ' + widget.cat.replace(" ","") + '">' + widget.first + '</div></div>';
		}
		else {
			return '<div class="project_item"><div class="project_item_hvr"><img src="' + widget.url + '" width="184" height="141" alt="' + widget.alt + '" label="' + widget.href + '" /><div class="project_hover"><a href="'+widget.href+'"><span class="alphaback"></span><span class="project_name">' + widget.title  + '</span><span class="plus"></span></a></div></div><div class="project_category  ' + widget.cat.replace(" ","") + '">&nbsp;</div></div>';
		}
    	}
    	else if(widget.type == "category") {
    		return '<div class="project_category">' + widget.title + '</div>';
    	}
    	
    
};
 
/*begin secondary navigation functions*/


/*end secondary navigation functions*/

