/* Expandable Drop Down Ticker
* Author: Dynamic Drive at http://www.dynamicdrive.com/
* Visit http://www.dynamicdrive.com/ for full source code
* Last Edited: Jan 25th, 2010
*/

var $eeeeee = jQuery.noConflict();

var expandticker={
	buttonhtml: '<img src="expand.jpg" style="width:142px; height:23px; cursor:pointer" />', //HTML of "expand" button
	buttonoffset: [5, -10], //offset of button from lower left edge of ticker
	//No need to edit beyond here

	dsetting: {snippetlength:30, manual:false, timers: {rotatepause:3000, fxduration:300}},
	effectfuncts: ['fadeIn', 'slideDown'],

fetchajaxcontent:function($eee, s){
window.status+='x'
	clearTimeout(s.playtimer) //clear timers and remove $eeebutton, $eeemenu if fetchajaxcontent() is being called more than once
	clearTimeout(s.pausetimer)
	clearTimeout(s.refreshtimer)
	if (s.$eeebutton){
		s.$eeebutton.remove()
		s.$eeemenu.remove()
	}
	s.$eeeticker.html("(Re)Fetching Ticker Contents...")
	$eee.ajax({
		url: s.remotecontent[0],
		error:function(ajaxrequest){
			s.$eeeticker.html('Error fetching content.<br />Server Response: '+ajaxrequest.responseText)
		},
		success:function(content){
			s.$eeeticker.html(content)
			expandticker.setup($eee, s)
			if (s.remotecontent[1]>5000) //5 secs minimum time allowed between updates
				s.refreshtimer=setTimeout(function(){expandticker.fetchajaxcontent($eee, s)}, s.remotecontent[1])
			else if (s.remotecontent[1]>0) //if value is NOT 0 (0=never refetch)
				alert("Please Enter a value larger than 5 (sec) for the time between Ajax updates")
		}
	})
},

getmsgtitles:function(s){
	var titlearray=[]
	for (var i=0; i<s.msglength; i++){
		var title=s.$eeecontents.eq(i).attr('title') || s.$eeecontents.eq(i).text().substring(0, s.snippetlength)+' ...' //extract snippet of message
		titlearray.push(title)
	}
	return titlearray
},

adddropmenu:function($eee, s){
	var titles=this.getmsgtitles(s)
	var $eeelis=$eee([])
	var $eeemenu=$eee('<ul class="dropdownlist"></ul>')
	for (var i=0; i<s.msglength; i++){ //construct a new LI element for each ticker message title
		$eeelis=$eeelis.add($eee('<li/>').html((i+1)+". "+titles[i]).wrapInner('<a href="#message'+(i+1)+'" data-pos="'+i+'"></a>'))
	}
	$eeemenu.append($eeelis).hide().unbind('click').click(function(e){ //go to particular message when menu title is clicked on
		if (e.target.tagName=="A"){
			clearTimeout(s.playtimer)
			s.curmsg=parseInt(e.target.getAttribute('data-pos'))
			expandticker.selectmsg($eee, s, s.curmsg)
			if (!s.manual)
				s.playtimer=setTimeout(function(){expandticker.rotatemsg($eee, s)}, s.timers.rotatepause)
			e.preventDefault()
		}
	})
	$eeemenu.appendTo(document.body)
	$eeemenu.data('state', 'closed') //indicate menu is currently closed
	s.$eeemenu=$eeemenu //remember $eeemenu
	s.$eeemenulis=$eeelis //remember $eeemenu LIs
},

positionbutton:function($eee, s){
	var toffset=s.$eeeticker.offset()
	var buttonpos=[toffset.left+this.buttonoffset[0], toffset.top+s.$eeeticker.outerHeight()+this.buttonoffset[1]]
	s.$eeebutton.css({left:buttonpos[0], top:buttonpos[1]})	
},

addexpandbutton:function($eee, s){
	s.$eeebutton=$eee(this.buttonhtml).css({position:'absolute'}).appendTo(document.body) //create expand button, add it to page, and remember it
	this.positionbutton($eee, s)
	this.adddropmenu($eee, s)
	s.$eeebutton.unbind('click').bind('click', function(e){ //show menu when expand button is clicked
		s.$eeemenu.css({left:s.$eeebutton.css('left'), top:parseInt(s.$eeebutton.css('top'))-expandticker.buttonoffset[1]}).show()
		s.$eeemenulis.removeClass('selected').eq(s.curmsg).addClass('selected') //highlight current message within menu
		s.$eeemenu.data('state', 'open') //indicate menu is open
		e.stopPropagation()
	})
},


selectmsg:function($eee, s, selected){
	s.$eeecontents.stop(true,true).hide().eq(selected)[s.effectfunct](s.timers.fxduration, function(){  //animate message into view
		if (this.style && this.style.removeAttribute)
			this.style.removeAttribute('filter') //fix IE clearType problem when animation is fade-in
	})
	s.curmsg=selected
	if (s.$eeemenu.data('state')=="open")
		s.$eeemenulis.removeClass('selected').eq(selected).addClass('selected') //highlight current message within menu
},

rotatemsg:function($eee, s){
	if (s.$eeeticker.data('state')=="over"){ //pause ticker onMouseover
		clearTimeout(s.pausetimer)
		s.pausetimer=setTimeout(function(){expandticker.rotatemsg($eee, s)}, 100)
		return
	}
	s.nextmsg=(s.curmsg<s.msglength-1)? s.curmsg+1 : 0 //go to next message
	this.selectmsg($eee, s, s.nextmsg)
	s.playtimer=setTimeout(function(){expandticker.rotatemsg($eee, s)}, s.timers.rotatepause)
},

setup:function($eee, s){
		s.$eeecontents=s.$eeeticker.find('.expandcontent').hide()
		s.msglength=s.$eeecontents.length
		s.curmsg=0
		expandticker.addexpandbutton($eee, s)
		expandticker.selectmsg($eee, s, s.curmsg)
		if (!s.manual){
			s.$eeeticker.unbind('mouseenter').bind('mouseenter', function(){$eee(this).data('state', 'over')})
			s.$eeeticker.unbind('mouseleave').bind('mouseleave', function(){$eee(this).data('state', 'out')})
			s.playtimer=setTimeout(function(){expandticker.rotatemsg($eee, s)}, s.timers.rotatepause)
		}	
},
	

init:function(setting){
	jQuery(document).ready(function($eee){ //fire on DOM ready
		var s=setting
		s=jQuery.extend({}, expandticker.dsetting, s)
		s.timers.rotatepause+=s.timers.fxduration //add slide duration to rotate timer
		s.$eeeticker=$eee('#'+s.id)
		if (s.$eeeticker.length==0)
			return
		s.effectfunct=s.fx=="fade"? expandticker.effectfuncts[0] : expandticker.effectfuncts[1]
		if (s.remotecontent && s.remotecontent[0].length>0){
			s.remotecontent[1]=s.remotecontent[1]*1000 //convert units into seconds
			expandticker.fetchajaxcontent($eee, s)
		}
		else
			expandticker.setup($eee, s)
		$eee(window).bind('load resize', function(e){ //reposition enlarge images when window has loaded or is resized
			if (s.$eeebutton)
				setTimeout(function(){expandticker.positionbutton($eee, s)}, (e.type=="load")? 200 : 0)
		})
		$eee(document).click(function(){
			if (s.$eeemenu){ //hide menu when user clicks anywhere on page
				s.$eeemenu.hide()
				s.$eeemenu.data('state', 'closed')
			}
		})
		
	})
}
}
