/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \
|		
|		Copyright (c) 2009
|		Design + HTML/CSS/DOM JavaScript : Smart Agence
|		http://www.smartagence.com/
|		
\ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */


var mycarousel_itemList = [];
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);
    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 target="_blank" href="' + item.href + '" class="' + item.style + '">' + item.title + '</a><img src="' + item.url + '" width="113" height="57" alt="' + item.title + '" />';
};


function mycarousel_initCallback(carousel)
{
    // Disable autoscrolling if the user clicks the prev or next button.
    carousel.buttonNext.bind('click', function() {
        carousel.startAuto(0);
    });
    carousel.buttonNext.bind('mouseover', function() {
        carousel.stopAuto();
    });
    carousel.buttonNext.bind('mouseout', function() {
        carousel.startAuto(2);
    });

    carousel.buttonPrev.bind('click', function() {
        carousel.startAuto(0);
    });
    carousel.buttonPrev.bind('mouseover', function() {
        carousel.stopAuto();
    });
    carousel.buttonPrev.bind('mouseout', function() {
        carousel.startAuto(2);
    });

    // Pause autoscrolling if the user moves with the cursor over the clip.
    carousel.clip.hover(function() {
        carousel.stopAuto();
    }, function() {
        carousel.startAuto();
    });
};


jQuery(document).ready(function($jQ) { 
	if($jQ("#carousel").length != 0){
		var lg = $jQ('ul#carousel li img').length ;
		for(var i = 0; i < lg; i += 1) {
			var url = $jQ('ul#carousel li img').eq(i).attr('src');
			var title = $jQ('ul#carousel li a').eq(i).text();
			var style = $jQ('ul#carousel li a').eq(i).attr('class');
			var href = $jQ('ul#carousel li a').eq(i).attr('href');
			if(/'/.test(title)) {
				title =title.replace("'","&rsquo;");
			}
			var str = "{url : '" + url + "', title : '" + title+ "',style : '" +style+ "',href : '" +href+ "'}";
			mycarousel_itemList[i] = eval('(' + str + ')');
		}
		$jQ('ul#carousel').empty();		
		$jQ('#carousel').jcarousel({
			scroll:3,
			auto: 5,
			wrap: 'circular',
			initCallback: mycarousel_initCallback,
			itemVisibleInCallback: {onBeforeAnimation: mycarousel_itemVisibleInCallback},
			itemVisibleOutCallback: {onAfterAnimation: mycarousel_itemVisibleOutCallback}
		});
	}	
});
