/**
 * page variables
 */
var firstImage;
var lastImage;

/**
 * We use the initCallback callback
 * to assign functionality to the controls
 */
function mycarousel_initCallback(carousel, state) {
    jQuery('.jcarousel-control a').bind('click', function() {
        carousel.scroll(jQuery.jcarousel.intval(jQuery(this).text()));
        return false;
    });

    jQuery('.jcarousel-scroll select').bind('change', function() {
        carousel.options.scroll = jQuery.jcarousel.intval(this.options[this.selectedIndex].value);
        return false;
    });

    jQuery('#head_next, #foot_next').bind('click', function(event) {
        if (getSelectedImageIndex($("#photo_carousel li")) >= $("#photo_carousel li").size())
        {
          return false;
        }
        
        try
        {
            if (typeof(_hbLink) == 'function' )
            {
                if (event.target.id == 'head_next')
                {
                  _hbLink('Photo_Feat_Main', 'Photo_Feat_Main_NextButton');
                }
                else if (event.target.id == 'a_foot_next' || event.target.id == 'foot_next')
                {
                  _hbLink('Photo_Feat_Main', 'Photo_Feat_Main_Next');
                }
            }
        }
        catch(e)
        {
        }
        
        //
        // do a page view
        //
        DoWSSPageView();

    	setSelectedItem(getSelectedImageIndex($("#photo_carousel li")));

    	if ((firstImage+1) == getSelectedImageIndex($("#photo_carousel li"))) {		
        	carousel.next();
    	}

		externalControlsCheck($("#photo_carousel li"));

        if (typeof RefreshAds == 'function')
        {
            RefreshAds();
        }    	

        return false;
    });

    jQuery('#head_prev, #foot_prev').bind('click', function(event) {    
        if (getSelectedImageIndex($("#photo_carousel li")) == 1)
        {
          return false;
        }
    

        try
        {
            if (typeof(_hbLink) == 'function' )
            {
                //
                // notify the wss if _hbLink is defined
                //
                if (event.target.id == 'head_prev')
                {
                  _hbLink('Photo_Feat_Main', 'Photo_Feat_Main_PrevButton');
                }
                else if (event.target.id == 'a_foot_prev' || event.target.id == 'foot_prev')
                {
                  _hbLink('Photo_Feat_Main', 'Photo_Feat_Main_Prev');
                }
            }
        }
        catch(e)
        {
        }

        //
        // Do a Page View
        //
        DoWSSPageView();
	

    	setSelectedItem(getSelectedImageIndex($("#photo_carousel li"))-2);
    	
    	if ((lastImage-1) == getSelectedImageIndex($("#photo_carousel li"))) {
    		carousel.prev();		    			
    	}
    	
    	externalControlsCheck($("#photo_carousel li"));
    	
        if (typeof RefreshAds == 'function')
        {
            RefreshAds();
        }    	

	    return false;
    });
};

/**
 * This is the callback function which receives notification
 * when an item becomes the first one in the visible range.
 */
function mycarousel_itemFirstInCallback(carousel, item, idx, state) {
	lastImage = idx; 
};

/**
 * This is the callback function which receives notification
 * when an item is no longer the first one in the visible range.
 */
function mycarousel_itemFirstOutCallback(carousel, item, idx, state) {
    //console.log('Item #' + idx + ' is no longer the first item');    
};

/**
 * This is the callback function which receives notification
 * when an item becomes the first one in the visible range.
 */
function mycarousel_itemLastInCallback(carousel, item, idx, state) {
  	firstImage = idx;
};

/**
 * This is the callback function which receives notification
 * when an item is no longer the first one in the visible range.
 */
function mycarousel_itemLastOutCallback(carousel, item, idx, state) {
	// console.log('Item #' + idx + ' is no longer the last item');
};


/**
 * This is the callback function which receives notification
 * about the state of the next button.
 */
function mycarousel_buttonNextCallback(carousel, button, enabled) {
    //console.log('Next button is now ' + (enabled ? 'enabled' : 'disabled'));
};

/**
 * This is the callback function which receives notification
 * about the state of the prev button.
 */
function mycarousel_buttonPrevCallback(carousel, button, enabled) {
    //console.log('Prev button is now ' + (enabled ? 'enabled' : 'disabled'));   
};


jQuery(document).ready(function() {
	// initial variables
	var gallery_items = $("#photo_carousel li");	
	var imageId = getUrlParameter("gix");

	if (imageId == "") { imageId = 1; } 
		
	imageId--;	
	
	// easing equation
	jQuery.easing['InOutCubic'] = function(x, t, b, c, d) {
		if ((t/=d/2) < 1) return c/2*t*t*t + b;
		return c/2*((t-=2)*t*t + 2) + b;
	};
	
	// initialize the carousel	
    jQuery('#photo_carousel').jcarousel({
        // Configuration goes here
        scroll: 7,
        easing: "InOutCubic",
        initCallback: mycarousel_initCallback,
        
        buttonNextCallback:   mycarousel_buttonNextCallback,
        buttonPrevCallback:   mycarousel_buttonPrevCallback,
        
        itemFirstInCallback:  mycarousel_itemFirstInCallback,
        itemFirstOutCallback: mycarousel_itemFirstOutCallback,
        itemLastInCallback:   mycarousel_itemLastInCallback,
        itemLastOutCallback:  mycarousel_itemLastOutCallback
        
        // This tells jCarousel NOT to autobuild prev/next buttons
        //buttonNextHTML: null,
        //buttonPrevHTML: null
    });	    
    
    // enumerate number of items in gallery
    $("#gallery_total").text(gallery_items.size());
    
    // handle clicks on the carousel    
    $("#photo_carousel li a").bind('click', function() {
        //alert("hello world");
        		
		// clear selections on all li
		gallery_items.removeClass("selected");
		
		// add selection on currently selected
		$(this).parent().addClass("selected");
		
		// update image index    	    	
		$("#image_index").text(getSelectedImageIndex(gallery_items));
		
		// initialize external controls
		externalControlsCheck(gallery_items);
        
    	// negate behavior    	
    	return false;
    });
    
    // start on this item
    setSelectedItem(imageId);
    
    // initialize external controls
    //externalControlsCheck(gallery_items);   
});

function externalControlsCheck(galleryItems) {		
	// check next controls
	if (getSelectedImageIndex(galleryItems) == galleryItems.size()) {
		$("#head_next, #foot_next").addClass("disabled");
		
    } else {
    	$("#head_next, #foot_next").removeClass("disabled");
    }
    
    //check prev controls
    if (getSelectedImageIndex(galleryItems) == 1) {
    	$("#head_prev, #foot_prev").addClass("disabled");
    } else {
    	$("#head_prev, #foot_prev").removeClass("disabled");
    }
}

function getSelectedImageIndex(thisCol) {	
	var returnedIndex;
	
	$(thisCol).each(function(i) {			
    	if ($(this).hasClass("selected")) {       			
    		returnedIndex = i+1;	
    	}
    });
    return returnedIndex; 
}

function setSelectedItem(pId) {		
    $("#photo_carousel li:eq("+pId+")").addClass("selected").children("a").click();
    if (typeof showSelectedImage == 'function')
    {
        showSelectedImage(pId+1);
    }
}

// get url parameter
function getUrlParameter(name) {
  if (window.location.href.match("/moviephotos/") != null)
	{
	    var urlParts = window.location.href.split("_");
	    if (urlParts.length == 4)
	    {
	        return urlParts[3];
	    }
	    return 1;
	}
	else
	{
  		name 	= name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  		var regexS = "[\\?&]"+name+"=([^&#]*)";
  		var regex = new RegExp( regexS );
  		var results = regex.exec( window.location.href );
  		if( results == null )
    			return "";
  		else
    			return results[1];
	}
}
