// Author: Manish B. Agrawal

// Created on: 09 May 2009

// Last modified on: 04 July 2009

// Bottom Logo Slider - Videocon d2h



var logoSlider = function() {
	//var logos = ['15', 'ch_history', "ch_star_news", "ch_times_now", 'ch_v', 'ch_vh1', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14'];
	var logos = ['14', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13'];
	var pathLogos = "http://acdemo1.com/hsa/wp-content/themes/HSA/images/logo3/";
	var logoFileExt = 'jpg';
	var nxtLeftImgPos = 3;
	var nxtRightImgPos = logos.length - 1;
	var count, leftInterval, rightInterval, logoDivs, direction;
	var containerWidth = 2444, logoWidth = 188;
			
	return {

		slideLeft: function() {
			
			clearInterval(rightInterval);
										
			// Make the 'Slide Left' button non-clickable

			$("#img_slide_left").unbind('click');

			$("#img_slide_left").css('cursor', 'default');

					

			// make the 'Slide Right' button clickable

			$("#img_slide_right").bind("click", function(){logoSlider.slideRight();});					

			$("#img_slide_right").css('cursor', 'pointer');

					

			logoDivs = $("#btm_logos_container > .btm_logo");

			

			if (!count)

				count = logoWidth;

			else if (direction != 'l')

				count = logoWidth - count;	// get the no of pixels to slide left

					

			direction = 'l';

			leftInterval = window.setInterval(function() {

				var leftOffset, newOffset;			

				if (!count) {

					clearInterval(leftInterval);

							

					$(logoDivs[0]).remove();

							

					if ($(logoDivs[logoDivs.length - 1]).css('left') != '686px') {

						var newDiv = "<div class='btm_logo' style='left:" +containerWidth+ "px;'><img src='"+pathLogos+logos[nxtLeftImgPos]+"."+logoFileExt+"' /></div>";							

						$("#btm_logos_container").append(newDiv);

						$("#btm_logos_container .btm_logo:last-child").bind('mouseover', function(){logoSlider.pause();});

						$("#btm_logos_container .btm_logo:last-child").bind('mouseout', function(){logoSlider.resume();});

									

						// position of next image/logo in the array

						(nxtLeftImgPos == logos.length - 1) ? nxtLeftImgPos = 0 : ++nxtLeftImgPos;

						(nxtRightImgPos == logos.length - 1) ? nxtRightImgPos = 0 : ++nxtRightImgPos;

					}

							

					logoSlider.slideLeft();

					return;

				}

						

				// slide each logo to left by 1 pixel

				for (var i = 0; i < logoDivs.length; i++) {

					leftOffset = parseInt($(logoDivs[i]).css('left'));

					newOffset = (leftOffset - 1) + "px";

					$(logoDivs[i]).css("left", newOffset);	

				}

				--count;

			}, 30);

		},

				

		slideRight: function() {

			clearInterval(leftInterval);

					

			// Make the 'Slide right' button non-clickable

			$("#img_slide_right").unbind('click');

			$("#img_slide_right").css('cursor', 'default');

					

			// make the 'Slide Left' button clickable

			$("#img_slide_left").bind("click", function(){logoSlider.slideLeft();});					

			$("#img_slide_left").css('cursor', 'pointer');

					

			logoDivs = $("#btm_logos_container > .btm_logo");

			

			if (!count)

				count = logoWidth;

			else if (direction != 'r')

				count = logoWidth - count;	// get the no of pixels to slide right

					

			direction = 'r';

			rightInterval = window.setInterval(function() {

				var leftOffset, newOffset;			

				if (!count) {

					clearInterval(rightInterval);

							

					if ($(logoDivs[0]).css('left') != '-188px') {

						$(logoDivs[logoDivs.length - 1]).remove();

						var newDiv = "<div class='btm_logo' style='left: -" +logoWidth+ "px;'><img src='"+pathLogos+logos[nxtRightImgPos]+"."+logoFileExt+"' /></div>";

						$("#btm_logos_container").prepend(newDiv);

						$("#btm_logos_container .btm_logo:first-child").bind('mouseover', function(){logoSlider.pause();});

						$("#btm_logos_container .btm_logo:first-child").bind('mouseout', function(){logoSlider.resume();});

								

						// position of next image/logo in the array

						(nxtRightImgPos == 0) ? nxtRightImgPos = logos.length - 1 : --nxtRightImgPos;

						(nxtLeftImgPos == 0) ? nxtLeftImgPos = logos.length - 1 : --nxtLeftImgPos;

					}

					logoSlider.slideRight();

					return;

				}

						

				// slide each logo to left by 1 pixel

				for (var i = 0; i < logoDivs.length; i++) {

					leftOffset = parseInt($(logoDivs[i]).css('left'));

					newOffset = (leftOffset + 1) + "px";

					$(logoDivs[i]).css("left", newOffset);	

				}

				--count;

			}, 30);

		},

				

		pause: function() {

			if (direction == 'l')

				clearInterval(leftInterval);

						

			if (direction == 'r')

				clearInterval(rightInterval);	

		},

				

		resume: function() {

			if (direction == 'l')

				logoSlider.slideLeft();

						

			if (direction == 'r')

				logoSlider.slideRight();

		}

	};

}();
