(function($) {
	$.fn.HomeMarquee = function(p_options) {
		
		m_totalSlides = ($("#carouselMarque .marqueSlide").length);
		
		$('#marquePrevious').click(function(){ previousSlide() });
		$('#marquePause').click(function(){ pauseSlide() });
		$('#marqueNext').click(function(){ nextSlide() });
		
		$("#totalSlides").text(m_totalSlides);	
		
		m_currentDisplay = 1;
		m_interval = 0;
		m_pausedState = false;
		m_activeSlide = null;
		
		initMarquee();
	}
	  
 	function initMarquee(){
		
		if(jQuery.browser.msie && jQuery.browser.version == 6){
			// Add Support For PNG Files and IE 6
			fixPNG();

			var marqueList = $("#cmOne ~ div");

			for(i = 0; i < marqueList.length; i++){
				$("#" + marqueList[i].id).css('display','none');
			}
		}
	
		var savedIndex = getCookie("homeMarque");
					
		m_activeSlide = $("#cmOne");//document.getElementById("cmOne");
		m_activeSlide.css("display", "block");

		setTimer();
	}
	
	function changeSlide(){
		m_currentDisplay = (m_currentDisplay >= m_totalSlides) ? 1 : (m_currentDisplay + 1);
		showSlide();
		
		setTimer();
	}

	function showSlide(){
	
		$("#marqueCounter").html(m_currentDisplay + " | " + m_totalSlides)
		
		m_activeSlide.css("display", "none");

		switch(this.m_currentDisplay){
			case 1:
				m_activeSlide = $("#cmOne");
				break;
				
			case 2:
				m_activeSlide = $("#cmTwo");
				break;
				
			case 3:
				m_activeSlide = $("#cmThree");
				break;
				
			case 4:
				m_activeSlide = $("#cmFour");
				break;
				
			case 5:
				m_activeSlide = $("#cmFive");	
				break;
				
			case 6:
				m_activeSlide = $("#cmSix");
				break;
				
			case 7:
				m_activeSlide = $("#cmSeven");
				break;
				
			case 8:
				m_activeSlide = $("#cmEight");
				break;
				
			case 9:
				m_activeSlide = $("#cmNine");
				break;
		}
		
		setCookie('homeMarque', 'slideIndex')
		
		m_activeSlide.css("display", "block");
	}
	
	function pauseSlide(){
		if(m_pausedState){
			setTimer();
			m_pausedState = false;
		}else{
			clearTimeout(m_interval);
			m_pausedState = true;
		}
	}
	
	function nextSlide(){
		m_currentDisplay = (this.m_currentDisplay >= m_totalSlides) ? 1 : (this.m_currentDisplay + 1);
		clearTimeout(m_interval);	
		showSlide();
		setTimer();
	}
	
	function previousSlide(){	
		m_currentDisplay = (m_currentDisplay == 1) ? m_totalSlides : (m_currentDisplay - 1);
		clearTimeout(m_interval);
		showSlide();		
		setTimer();
	}
	
	
	function setTimer(){
		m_interval = setTimeout(function () { changeSlide(); }, 10000);
	}
	
	function fixPNG(){
		var arVersion = navigator.appVersion.split("MSIE")
		var version = parseFloat(arVersion[1])

		if ((version >= 5.5) && (document.body.filters)) 
		{

		   for(var i=0; i<document.images.length; i++)
		   {
			  var img = document.images[i]
			  var imgName = img.src.toUpperCase()
			  
			  if (imgName.substring(imgName.length-3, imgName.length) == "PNG")
			  {
				 var imgID = (img.id) ? "id='" + img.id + "' " : ""
				 var imgClass = (img.className) ? "class='" + img.className + "' " : ""
				 var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' "
				 var imgStyle = "display:inline-block;" + img.style.cssText;

				 if (img.align == "left") imgStyle = "float:left;" + imgStyle
				 if (img.align == "right") imgStyle = "float:right;" + imgStyle
				 if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle

				 var strNewHTML = "<span " + imgID + imgClass + imgTitle
				 + " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"
				 + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
				 + "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>" 
				 img.outerHTML = strNewHTML
				 i = i-1
			  }
		   }
		}
	}
})(jQuery);