var Jticker={
    nTimeout:0,
    nLength:0,
    nCurrentIndex:0,
    nPause:100,
    nTransition:5000,
    oTickerItems:null,
    sNextImageURL:(location.protocol.indexOf('file:')!=-1 ? 'images/next.gif' : '/_layouts/jqueryBundle/Jscript/Jticker/images/next.gif'),
    sPreviousImageURL:(location.protocol.indexOf('file:')!=-1 ? 'images/prev.gif' : '/_layouts/jqueryBundle/Jscript/Jticker/images/prev.gif'),
    nImageWidth:17,
    nImageHeight:18,
    bDebug:false,
    Jinit:function(options) {
    	//table height
		$("#JtickerTable").attr("height",this.nImageHeight);
		//set td height (parent of JtickerPlaceHolder div)
		$("#JtickerPlaceHolder").parent().attr("height",this.nImageHeight);		
		//get reference to ticker items
		this.oTickerItems=$('#JtickerPlaceHolder div');
		if(options) { //change default settings		
			if(options.pause) this.nPause=options.pause;
			if(options.transition) this.nTransition=options.transition;
			if(options.nextImageURL) this.sNextImageURL=options.nextImageURL;
			if(options.previousImageURL) this.sPreviousImageURL=options.previousImageURL;
			if(options.imageWidth) this.nImageWidth=options.imageWidth;
			if(options.imageHeight) this.nImageHeight=options.imageHeight;
			if(options.debug || !options.debug) this.bDebug=options.debug;
		}
		//stop animation when mouseover ticketitem
        $('#JtickerPlaceHolder a').hover(function(){
        	Jticker.Jpause();
        },//resume animation when mouseout
        function(){
        	Jticker.JresumeNext();
        });
        //dynamically add next and previous arrow
        $('#JnextPrevPanel').html(
        	'<a id="Jprev" href="javascript:void(0)"><img title="Previous" alt="Previous" border="0" width="' + this.nImageWidth + '" height="' + this.nImageHeight + '" src="' + this.sPreviousImageURL + '"/></a>'+
        	'<a id="Jnext" href="javascript:void(0)"><img title="Next" alt="Next" border="0" width="' + this.nImageWidth + '" height="' + this.nImageHeight + '" src="' + this.sNextImageURL + '"/></a>'
        );
        //register next click event
        $('#Jnext').click(function() {
        	Jticker.Jnext();
            Jticker.oTickerItems.hide();
            Jticker.oTickerItems.eq(Jticker.nCurrentIndex).show();
            Jticker.Jlog();
        });
        //register prev click event
        $('#Jprev').click(function() {
            if(Jticker.nCurrentIndex-- < 0) Jticker.nCurrentIndex=Jticker.oTickerItems.length-1;
            Jticker.oTickerItems.hide();
            Jticker.oTickerItems.eq(Jticker.nCurrentIndex).show();
            Jticker.Jlog();                             
        });
        //register hover for next prev panel and stop animation when mouseover
        $('#JnextPrevPanel').hover(function() {
            Jticker.Jpause();
        },
        function(){//resume animation when mouseout
            Jticker.JresumeCurrent();
        }); Jticker.nLength=this.oTickerItems.length; //set length of tickeritems
        if(this.bDebug) this.JsetupDebug();
    },
    Jnext:function() {
    	//move to the next ticker item
    	this.nCurrentIndex++;
        this.nCurrentIndex= Jticker.nCurrentIndex % Jticker.nLength;
    },
    JresumeCurrent:function() {
    	//pickup where it left off
    	this.Jtick();
    	this.Jlog();
    },
    JresumeNext:function() {
    	//advance to the next item
    	this.oTickerItems.hide();
		this.Jnext();
    	this.Jtick();
    	this.Jlog();
    },
    Jpause:function() {
    	//stop the current animation, but still show the ticker item
    	Jticker.oTickerItems.eq(Jticker.nCurrentIndex).stop(true, true).fadeIn();
    	clearTimeout(Jticker.nTimeout);
    },
    Jtick:function() { //show, delay and hide then move to next item
        this.oTickerItems.eq(Jticker.nCurrentIndex).fadeIn('slow').fadeTo(Jticker.nTransition,1).fadeOut('slow',function() {
        	Jticker.Jlog();
        	Jticker.Jnext();
        	Jticker.nTimeout=setTimeout('Jticker.Jtick()', Jticker.nPause);
        });
    },
	Jlog:function() {
		if(Jticker.bDebug) {
    		if($('#JtickerDebug').length > 0) {
        		if(this.nCurrentIndex==0) $('#JtickerDebug').html('');
        		$('#JtickerDebug').append(
            		'<div>' +
            			'Current index : ' + Jticker.nCurrentIndex + '<br/>' +
            			'Item name : ' + Jticker.oTickerItems.eq(Jticker.nCurrentIndex).find('a').text() + '<br/>'+
            			'Item Url : ' + Jticker.oTickerItems.eq(Jticker.nCurrentIndex).find('a').attr('href') + '<br/>' +
            			'<hr/>' +
            			'</div>'
            	);
            }
    	}
    },
    JsetupDebug:function() {
    	if($('#JtickerDebug').length == 0) {
    		$('<div id="JtickerDebug">&nbsp;</div>').appendTo('body');
    	}
    }
}
