$(document).ready(function() {
	leftMenu();
    
	$(".image-replace").hide(); //needs to come before new release otherwise IE adjusts padding after loading
	
	//$("#logo h1 a").html("&nbsp;");
		
	$('.search-box').focus(function() { $(this).select(); });

	if (typeof( window['mycarousel_itemList'] ) != "undefined")
		preloadImages(mycarousel_itemList);
  });

function preloadImages(arguments){
  for(var i = 0; i<arguments.length; i++)
  {
    var imm = new Image();
    $(imm).attr('src', arguments.url);
  }
  
  setTimeout("cycleNewReleases()", 1000);
}

function leftMenu() {
	$("#categories li ul")
		.hide()
		.each(function() {
			$(this).parent().hover(
				function() {
					liPosition = $(this).position();
					aWidth = $(this).children("a")
								.addClass("parent")
								.width();
					$(this).children("ul")
					  	.addClass("hover")
						.show()
						.css("left",(liPosition.left + aWidth + 20) + "px")
						.css("top",(liPosition.top + 10) + "px");
					  },
				function() {
					  $(this).children("ul").removeClass("hover").hide();
					  $(this).children("a").removeClass("parent");
				}
			)
											 
	});;
}


function cycleNewReleases() {
    var col1 = $("#col1");
	if (col1.length > 0 && mycarousel_itemList.length > 0)
	{
	    //create new releases markup
        col1.before("<ul id='new-releases'></ul>");
        
        if ($("#new-releases").length > 0) {
            jQuery('#new-releases').jcarousel({
            wrap: 'last', //'circular',
            auto: 3,
            initCallback: mycarousel_initCallback,
            size: mycarousel_itemList.length,
            itemLoadCallback: {onBeforeAnimation: mycarousel_itemLoadCallback}

            //animation: 1100,
            //itemVisibleInCallback: {onBeforeAnimation: mycarousel_itemVisibleInCallback},
            //itemVisibleOutCallback: {onAfterAnimation: mycarousel_itemVisibleOutCallback}
            });
        }
	}
}

function mycarousel_initCallback(carousel)
{

    // Pause autoscrolling if the user moves with the cursor over the clip.
    carousel.clip.hover(function() {
        carousel.stopAuto();
    }, function() {
        carousel.startAuto();
    });
};

function mycarousel_itemLoadCallback(carousel, state)
{
    for (var i = carousel.first; i <= carousel.last; i++) {
        if (carousel.has(i)) {
            continue;
        }

        if (i > mycarousel_itemList.length) {
            break;
        }

        carousel.add(i, mycarousel_getItemHTML(mycarousel_itemList[i-1]));
    }
};




function mycarousel_itemVisibleInCallback(carousel, item, i, state, evt)
{
    // 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);
    if (mycarousel_itemList[idx - 1] == null)
        idx = 1; //ie8 fix

    carousel.add(i, mycarousel_getItemHTML(mycarousel_itemList[idx - 1]));
};

function mycarousel_itemVisibleOutCallback(carousel, item, i, state, evt)
{
    carousel.remove(i);
};
/**
 * Item html creation helper.
 */
function mycarousel_getItemHTML(item)
{
    return '<a href="' + item.link + '" title="' + item.title + '" onClick="tb_show(\'' + item.title 
            + '\',\'' + item.link + '\',false);return false;">'
            + '<img src="' + item.url + '" height="200" alt="' + item.title + '" /></a>';
};