/**
 * Made by: Ruben "Dr!fter" Gonzalez
 */
jQuery.noConflict();
 
 /**
 * Marquee Functions
 */
 var TickerTimerArray = new Object();
 var TickerTimerCount = new Object();
 
function CreateVerticalMarquee(list, containerid, tickerid, tickerclass, time, nonietime)
{
	jQuery("#"+containerid).append("<div id='"+tickerid+"' class='"+tickerclass+"'></div><div id='"+tickerid+"2' class='"+tickerclass+"'></div>");
	for(var i = 0; i < list.length; i++)
	{
		jQuery("#"+tickerid).append(list[i]);
		jQuery("#"+tickerid+"2").append(list[i]);
	}
	var height = jQuery("#"+tickerid).height();
	if (jQuery.browser.msie)
	{
		var distanceper = height/time;
		//StartMarquee(list, height, containerid, tickerid, tickerclass, time);
		TickerTimerCount[containerid] = 1;
	
		jQuery("#"+containerid).hover(function(){StopMarquee(list, height, containerid, tickerid, tickerclass, time, distanceper)},function(){ScrollMarquee(list, height, containerid, tickerid, tickerclass, time, distanceper);});
	
		TickerTimerArray[containerid] = setTimeout(function(){ScrollMarquee(list, height, containerid, tickerid, tickerclass, time, distanceper)}, 1);
	}
	else
	{
		//This method CAN be used for ie with hooking right click and stopping on right click
		//But since it works fine as is i just changed it for firefox and others.
		if(!nonietime)
			nonietime = time*3;
		jQuery("#"+containerid).hover(function(){jQuery("#"+tickerid).stop()},function(){StartMarquee(list, height, containerid, tickerid, tickerclass, nonietime);});
		StartMarquee(list, height, containerid, tickerid, tickerclass, nonietime);
	}
}
function StartMarquee(list, height, containerid, tickerid, tickerclass, time)
{
	//Figure out the animation time
	var thetime = time;
	var margintop = parseInt(jQuery("#"+tickerid).css("margin-top").replace("px", ""));
	if(margintop)
	{
		var per = time/height;
		thetime = Math.round((height+margintop)*per);
	}
	jQuery("#"+tickerid).animate({"margin-top" : (height*-1)+"px"}, thetime, 'linear', function() {
		jQuery("#"+tickerid).remove();
		jQuery("#"+tickerid+"2").attr("id", tickerid);
		jQuery("#"+containerid).append("<div id='"+tickerid+"2' class='"+tickerclass+"'></div>");
		for(var i = 0; i < list.length; i++)
		{
			jQuery("#"+tickerid+"2").append(list[i]);
		}
		StartMarquee(list, height, containerid, tickerid, tickerclass, time)
	})
}
function StopMarquee(list, height, containerid, tickerid, tickerclass, time, distanceper)
{
	clearTimeout(TickerTimerArray[containerid]);
}
function ScrollMarquee(list, height, containerid, tickerid, tickerclass, time, distanceper)
{
	jQuery("#"+tickerid).css({"margin-top": (distanceper*TickerTimerCount[containerid]*-1)+"px"});
	
	TickerTimerCount[containerid] ++;
	if(TickerTimerCount[containerid] > time)
		ResetMarquee(list, height, containerid, tickerid, tickerclass, time, distanceper)
	else
		TickerTimerArray[containerid] = setTimeout(function(){ScrollMarquee(list, height, containerid, tickerid, tickerclass, time, distanceper)}, 1)
}
function ResetMarquee(list, height, containerid, tickerid, tickerclass, time, distanceper)
{
	jQuery("#"+tickerid).remove();
	jQuery("#"+tickerid+"2").attr("id", tickerid);
	jQuery("#"+containerid).append("<div id='"+tickerid+"2' class='"+tickerclass+"'></div>");
	for(var i = 0; i < list.length; i++)
	{
		jQuery("#"+tickerid+"2").append(list[i]);
	}
	TickerTimerCount[containerid] = 1;
	TickerTimerArray[containerid] = setTimeout(function(){ScrollMarquee(list, height, containerid, tickerid, tickerclass, time, distanceper)}, 1);
}

/**
 * All the slider stuff now
 */
 
var TimerObjects = new Object();
var AllowedToChange = new Object();
var CurrentPage = new Object();

/**
 * Creates an animated slider in place
 * list					Array of items you want to be in the slider
 * itemsper				Items per page/slide
 * containerid			Unique id to give the slider container
 * pageclass			Class to use for each page
 * pageid				Unique page id.
 * slidertime			How long an animation takes
 * time					Time between next slide
 * height				Height of the slider
 * width				Width of the slider
 * type					Type of slider. 1 = Door slider right, 2 = Door slider left, 3 = Door slider down, 4 = Door slider up, 5 = Fade out slider, 6 = Slide in from top, 7 = slide in from bottom
 * controls				If true creates a controls panel
 * cheight				Height of the control panel box
 * overlay				If true sets the controls to be over the slider else under the slider.
 * cid					Unique controls box id
 * cclass				Class to apply to controls box
 * cbuttonclass			Class to apply to page buttons
 *
 */
function CreateAnimatedSlider(list, itemsper, containerid, pageclass, pageid, slidertime, time, height, width, type, controls, cheight, overlay, cid, cclass, cbuttonclass)
{
	var addheight = 0;
	if(!overlay && overlay != null && cheight != null)
	{
		addheight = cheight;
	}
	document.write("<div id='"+containerid+"' style='width:"+width+"px;height:"+(height+addheight)+"px;position:relative;overflow:hidden;'></div>");
	var pages = Math.ceil(list.length/itemsper);
	var num = pages;
	for(var x = 0; x < pages; x++)
	{	
		jQuery("#"+containerid).append("<div id='"+pageid+""+x+"' class='"+pageclass+"' style='position:absolute; top:0px; left:0px; width:"+width+"px;height:"+height+"px;z-index:"+num+"'></div>");
		num--;
	}
	if(controls)
	{
		jQuery("#"+containerid).append("<div id='"+cid+"' class='"+cclass+"' style='bottom:0px;left:0px;height:"+cheight+"px;width:"+width+"px;position:absolute;z-index:"+(pages+5)+"'></div>")
		for(var x = pages-1; x >= 0; x--)
		{
			var distance = height;
			if(type == 1 || type == 2)
			{
				distance = width;
				if(type == 2)
					distance = width*-1;
			}
			else if(type == 3 || type == 6)
			{
				distance = height*-1;
			}
			jQuery("#"+cid).append("<div id='"+pageid+"button"+x+"' class='"+cbuttonclass+"' onclick='GoToPage("+x+", \""+pageid+"\", "+pages+", "+slidertime+", "+distance+", "+time+", "+type+")' style='cursor:pointer;float:right;'></div>")
			jQuery("#"+pageid+"button"+x).fadeTo(-1, 0.5)
		}
		jQuery("#"+pageid+"button0").fadeTo(-1, 1.0)
	}
	var page = 0;
	num = 0;
	for(var i = 0; i < list.length; i++)
	{
		if(num == itemsper)
		{
			num = 0;
			page++;
		}
		jQuery("#"+pageid+page).append(list[i]);
		num++;
	}
	var width = jQuery("#"+containerid).width();
	if(type == 1 || type == 2)
	{
		if(type == 2)
			width = width*-1;
			
		TimerObjects[pageid] = setTimeout(function (){StartDoorSlider(pageid, 0, pages, width, slidertime, time, type, controls);}, time);
	}
	else if(type == 3 || type == 4)
	{
		if(type == 3)
			height = height*-1;
			
		TimerObjects[pageid] = setTimeout(function (){StartDoorSlider(pageid, 0, pages, height, slidertime, time, type, controls);}, time);
	}
	else if(type == 5)
	{
		TimerObjects[pageid] = setTimeout(function (){StartFadingSlider(pageid, 0, pages, slidertime, time, controls);}, time);
	}
	else if(type == 6 || type == 7)
	{
		if(type == 6)
			height = height*-1;
		for(var x = 1; x < pages; x++)
		{	
			jQuery("#"+pageid+x).css({"left" : "0px", "top" : height+"px", "z-index" : pages});
		}
		jQuery("#"+pageid+0).css({"left" : "0px", "top" : "0px", "z-index" : "0"});
		TimerObjects[pageid] = setTimeout(function (){StartSlideInSlider(pageid, 0, pages, height, slidertime, time, type, controls);}, time);
	}
	AllowedToChange[pageid] = true;
	CurrentPage[pageid] = 0;
}
function StartDoorSlider(pageid, page, pages, distance, slidertime, time, type, controls)
{
	AllowedToChange[pageid] = false;
	if(controls)
	{
		AnimateControls(pageid, page, pages, slidertime)
	}
	
	if(type == 1 || type == 2)
	{
		jQuery("#"+pageid+page).animate({"left" : distance+"px"}, slidertime, "linear", function(){
		SortPages(pages, page, pageid, 0, 0);
		if(page < pages-1)
			page++
		else
			page = 0;
		CurrentPage[pageid] = page;
		AllowedToChange[pageid] = true;
		});
		TimerObjects[pageid] = setTimeout(function (){StartDoorSlider(pageid, page, pages, distance, slidertime, time, type, controls);}, time);
	}
	else if(type == 3 || type == 4)
	{
		jQuery("#"+pageid+page).animate({"top" : distance+"px"}, slidertime, "linear", function(){
		SortPages(pages, page, pageid, 0, 0);
		if(page < pages-1)
			page++
		else
			page = 0;
		CurrentPage[pageid] = page;
		AllowedToChange[pageid] = true;
		});
		TimerObjects[pageid] = setTimeout(function (){StartDoorSlider(pageid, page, pages, distance, slidertime, time, type, controls);}, time);
	}
}
function StartFadingSlider(pageid, page, pages, slidertime, time, controls)
{
	AllowedToChange[pageid] = false;
	if(controls)
	{
		AnimateControls(pageid, page, pages, slidertime)
	}
	jQuery("#"+pageid+page).fadeTo(slidertime, 0.0, function(){
		SortPages(pages, page, pageid, 0, 0);
		if(page < pages-1)
			page++
		else
			page = 0;
		
		CurrentPage[pageid] = page;
		AllowedToChange[pageid] = true;
	});
	TimerObjects[pageid] = setTimeout(function (){StartFadingSlider(pageid, page, pages, slidertime, time, controls);}, time);
}
function StartSlideInSlider(pageid, page, pages, distance, slidertime, time, type, controls)
{
	AllowedToChange[pageid] = false;
	if(controls)
	{
		AnimateControls(pageid, page, pages, slidertime)
	}
	jQuery("#"+pageid+page).css({"z-index" : "0"});
	if(page < pages-1)
		page++
	else
		page = 0;
	
	jQuery("#"+pageid+page).animate({"top" : "0px"}, slidertime, "linear", function(){
		SortPagesSlideIn(pages, page, pageid, distance, 0);
		CurrentPage[pageid] = page;
		AllowedToChange[pageid] = true;
	});
	
	TimerObjects[pageid] = setTimeout(function (){StartSlideInSlider(pageid, page, pages, distance, slidertime, time, type, controls);}, time);
}
function SortPagesSlideIn(pages, page, pageid, top, left)
{
	for(var i = 0; i < pages; i++)
	{
		if(i == page)
			continue;
		jQuery("#"+pageid+i).css({"left" : left+"px", "top" : top+"px", "z-index" : pages});
	}
}
function SortPages(pages, page, pageid, top, left)
{		
	var num = pages;
	for(var i = page+1; i < pages; i++)
	{
		//alert(i+" num is "+ num);
		jQuery("#"+pageid+i).css({"z-index": num, "top" : top+"px", "left" : left+"px"});
		jQuery("#"+pageid+i).fadeTo(-1, 1.0)
		num--;
	}
	num = 0;
	for(var i = page; i >= 0; i--)
	{
		jQuery("#"+pageid+i).css({"z-index": num, "top" : top+"px", "left" : left+"px"});
		jQuery("#"+pageid+i).fadeTo(-1, 1.0)
		num++;
	}
}
function AnimateControls(pageid, page, pages, slidertime)
{
	var nextpage = page;
	if(page < pages-1)
		nextpage++
	else
		nextpage = 0;
	for(var i = 0; i < pages; i++)
	{
		if(i == nextpage)
			continue;
		jQuery("#"+pageid+"button"+i).fadeTo(slidertime, 0.5)
	}
	jQuery("#"+pageid+"button"+nextpage).fadeTo(slidertime, 1.0)
}
function GoToPage(page, pageid, pages, slidertime, distance, time, type)
{
	if(!AllowedToChange[pageid] || CurrentPage[pageid] == page)
		return;
	clearTimeout(TimerObjects[pageid])
	var prevpage;
	
	if(page == 0)
		prevpage = pages;
	if(page > 0)
		prevpage = page-1;
		
	if(type == 6 || type == 7)
	{
		jQuery("#"+pageid+CurrentPage[pageid]).css({"z-index" : "0"});
		StartSlideInSlider(pageid, prevpage, pages, distance, slidertime, time, type, true);
	}
	else if(type == 1 || type == 2)
	{
		AllowedToChange[pageid] = false;
		AnimateControls(pageid, prevpage, pages, slidertime);
		SortPageForGoTo(pageid, pages, page);

		jQuery("#"+pageid+CurrentPage[pageid]).animate({"left" : distance+"px"}, slidertime, "linear", function(){
		SortPages(pages, prevpage, pageid, 0, 0);
		CurrentPage[pageid] = page;
		AllowedToChange[pageid] = true;
		});
		TimerObjects[pageid] = setTimeout(function (){StartDoorSlider(pageid, page, pages, distance, slidertime, time, type, true);}, time);
	}
	else if(type == 3 || type == 4)
	{
		AllowedToChange[pageid] = false;
		AnimateControls(pageid, prevpage, pages, slidertime);
		SortPageForGoTo(pageid, pages, page);
		
		jQuery("#"+pageid+CurrentPage[pageid]).animate({"top" : distance+"px"}, slidertime, "linear", function(){
		SortPages(pages, prevpage, pageid, 0, 0);
		CurrentPage[pageid] = page;
		AllowedToChange[pageid] = true;
		});
		TimerObjects[pageid] = setTimeout(function (){StartDoorSlider(pageid, page, pages, distance, slidertime, time, type, true);}, time);
	}
	else if(type == 5)
	{
		AllowedToChange[pageid] = false;
		AnimateControls(pageid, prevpage, pages, slidertime);
		SortPageForGoTo(pageid, pages, page);
		
		jQuery("#"+pageid+CurrentPage[pageid]).fadeTo(slidertime, 0.0, function(){
		SortPages(pages, prevpage, pageid, 0, 0);
		CurrentPage[pageid] = page;
		AllowedToChange[pageid] = true;
		});
		TimerObjects[pageid] = setTimeout(function (){StartFadingSlider(pageid, page, pages, slidertime, time, true);}, time);
	}
}
function SortPageForGoTo(pageid, pages, page)
{
	for(i = 0; i < pages; i++)
	{
		if(i == CurrentPage[pageid])
		{
			jQuery("#"+pageid+i).css({"z-index": pages, "top" : "0px", "left" : "0px"});
			jQuery("#"+pageid+i).fadeTo(-1, 1.0)
		}
		else
		{
			jQuery("#"+pageid+i).css({"z-index": "0", "top" : "0px", "left" : "0px"});
			jQuery("#"+pageid+i).fadeTo(-1, 1.0)	
		}
	}
	jQuery("#"+pageid+page).css({"z-index": pages-1, "top" : "0px", "left" : "0px"});
	jQuery("#"+pageid+page).fadeTo(-1, 1.0)
}
