
/**
 * Image src URLs
 **/
var imageList = [
				 "#",
                 "/img/trash/spbcc.png",
                 "/img/trash/megafon.gif",
                 "/img/trash/valio.gif",
				 "/img/trash/gazprom.gif",
		 		 "/img/trash/rzhd2.gif",
				 "/img/trash/jacob_del.gif",
                 "/img/trash/event_petersburg.gif",
                 "/img/trash/ska.gif",
                 "/img/trash/ipm.gif",
                 "/img/trash/pulkovo.gif",
                 "/img/trash/gatchina.gif",
			 	 "/img/trash/tele2_.gif",
	 			 "/img/trash/comstar.gif",
                 "/img/trash/normann.gif",
				 "/img/trash/setlestate.png",
 				 "/img/trash/m_industry.gif",
 				 "/img/trash/eurosib.gif",
	 			 "/img/trash/spbopen.gif",
	 			 "/img/trash/talosto.gif",
	 			 "/img/trash/peterstar.png",
	 			 "/img/trash/gruzomob.gif",
				 "/img/trash/start_tel.gif",
				 "/img/trash/otsp.png",
				 "/img/trash/nge.gif",
				 "/img/trash/soda.gif",
				 "/img/trash/may.png",
				 "/img/trash/innova.png",
                 "/img/trash/srv.gif"
				 ];
				 
var urlList = [
				 "#",
				 "/clients/106.html", //adm SPb
				 "/clients/135.html", //megafon
				 "/clients/137.html", //valio
				 "/clients/78.html", //gazprom
				 "/clients/80.html ", //rzhd
				 "/clients/38.html", //jacob_del
				 "/clients/116.html", //event_petersburg
				 "/clients/118.html", //SKA
				 "/clients/131.html", //ipm
				 "/clients/139.html", //pulkovo
				 "/clients/132.html", //gatchina
				 "/clients/59.html", //tele2
				 "/clients/73.html ", //comstar
				 "/clients/138.html", //normann
				 "/clients/103.html", //setlestate
				 "/clients/49.html", //m_industry
				 "/clients/6.html", //eurosib
				 "/clients/84.html", //spbopen
				 "/clients/64.html", //talosto
				 "/clients/102.html", //peterstar
				 "/clients/23.html", //gruzomob
				 "/clients/46.html", //start_tel
				 "/clients/101.html", //otsp
				 "/clients/74.html", //nge
				 "/clients/82.html ", //soda
				 "/clients/97.html", //may
				 "/clients/98.html", //innova
				 "/clients/115.html" //SRV
				 ];
				 
var altList = [
				 "#",
				 "Администрация Санкт-Петербурга",
				 "Общероссийский оператор сотовой связи &#34;МегаФон&#34;",
				 "Лидер молочного рынка Финляндии &#34;Valio&#34;",
				 "Одна из крупнейших нефтегазовых компаний России &#34;Газпром нефть&#34;",
				 "ОАО &#34;Российские железные дороги&#34;",
				 "Французский производитель элитной сантехники Jacob Delafon",
				 "Санкт-Петербургский Международный центр фестивалей и праздников",
				 "Хоккейный клуб СКА",
				 "Ломоносовский фарфоровый завод",
				 "Санкт-Петербургский аэропорт &#34;Пулково&#34;",
				 "Государственный музей-заповедник &#34;Гатчина&#34;",
				 "Оператор мобильной связи TELE2 Россия",
				 "Подразделение ОАО &#34;Комстар - ОТС&#34; - ОАО &#34;Тюменнефтегазсвязь&#34;",
				 "Инвестиционно-строительная Группа &#34;НОРМАНН&#34;",
				 "Ведущая компания в составе международного холдинга SetlEstate",
				 "Группа строительных компаний &#34;М-Индустрия&#34;",
				 "Холдинг &#34;Евросиб&#34;",
				 "Теннисный турнир ATP &#34;St. Petersburg Open&#34;",
				 "Лидер по производству замороженных продуктов питания ГК &#34;Талосто&#34;",
				 "Оператор связи &#34;ПетерСтар&#34;",				 
				 "Поставщик техники &#34;Грузомобиль&#34;",
				 "Телекоммуникационная компания &#34;Старт Телеком&#34;",
				 "Крупнейшая полиграфическая компания Санкт-Петербурга &#34;Образцовая Типография&#34;",
				 "Группа компаний в области энергетики &#34;НГ-Энерго&#34;",
				 "Сеть магазинов модной одежды SODA",
				 "Сеть салонов красоты &#34;Май&#34;",
				 "Российский издатель онлайн-игр Innova Systems",
				 "Инновативный строительный концерн SRV"
				];

var lastRan = -1;


/**
 * Since carousel.addItem uses an HTML string to create the interface
 * for each carousel item, this method formats the HTML for an LI.
 **/

var fmtItem = function(imgUrl, url, title, alttext) {

  	var innerHTML = 
  		'<a href="' + 
  		url + 
  		'" title="' + alttext + '" ><img src="' + 
  		imgUrl +
		'" width="' +
		132 +
		'" height="' +
		81+
		'"/>' + 
  		title + 
  		'</a>';
  
	return innerHTML;
	
};

/**
 * Custom inital load handler. Called when the carousel loads the initial
 * set of data items. Specified to the carousel as the configuration
 * parameter: loadInitHandler
 **/
var loadInitialItems = function(type, args) {

	var start = args[0];
	var last = args[1]; 

	load(this, start, last);	
};

/**
 * Custom load next handler. Called when the carousel loads the next
 * set of data items. Specified to the carousel as the configuration
 * parameter: loadNextHandler
 **/
var loadNextItems = function(type, args) {	

	var start = args[0];
	var last = args[1]; 
	var alreadyCached = args[2];
	
	if(!alreadyCached) {
		load(this, start, last);
	}
};

/**
 * Custom load previous handler. Called when the carousel loads the previous
 * set of data items. Specified to the carousel as the configuration
 * parameter: loadPrevHandler
 **/
var loadPrevItems = function(type, args) {
	var start = args[0];
	var last = args[1]; 
	var alreadyCached = args[2];
	
	if(!alreadyCached) {
		load(this, start, last);
	}
};

var load = function(carousel, start, last) {
	for(var i=start;i<=last;i++) {
		//var randomIndex = getRandom(13, lastRan);
		//lastRan = randomIndex;
	    lastRan = i;
		carousel.addItem(i, fmtItem(imageList[lastRan], urlList[lastRan], "", altList[lastRan]));
/*
		// Example of an alternate way to add an item (passing an element instead of html string)
		var p = document.createElement("P");
		var t = document.createTextNode("Item"+i);
		p.appendChild(t);
		carousel.addItem(i, p );
*/
	}
};

var getRandom = function(max, last) {
	var randomIndex;
	do {
		randomIndex = Math.floor(Math.random()*max);
	} while(randomIndex == last);
	
	return randomIndex;
};

/**
 * Custom button state handler for enabling/disabling button state. 
 * Called when the carousel has determined that the previous button
 * state should be changed.
 * Specified to the carousel as the configuration
 * parameter: prevButtonStateHandler
 **/
var handlePrevButtonState = function(type, args) {

	var enabling = args[0];
	var lnk = args[1];
	
	if(enabling) {
		lnk.style.display = 'block';
	} else {
		lnk.style.display = 'none';
	}
	
};
var handleNextButtonState = function(type, args) {

	var enabling = args[0];
	var lnk = args[1];
	
	if(enabling) {
		lnk.style.display = 'block';
	} else {
		lnk.style.display = 'none';
	}
	
};
/**
 * You must create the carousel after the page is loaded since it is
 * dependent on an HTML element (in this case 'dhtml-carousel'.) See the
 * HTML code below.
 **/

var carousel;
var pageLoad = function() 
{
	carousel = new YAHOO.extension.Carousel("dhtml-carousel", 
		{
			numVisible:        4,
			animationSpeed:   .25,
			scrollInc:         3,
			navMargin:         40,
			size: 22,
			//scrollAfterAmount:  2,
			//wrap: false,
			prevElement:       "prev-arrow",
			nextElement:       "next-arrow",
			loadInitHandler:   loadInitialItems,
			loadNextHandler:   loadNextItems,
			loadPrevHandler:   loadPrevItems,
			prevButtonStateHandler:   handlePrevButtonState,
			nextButtonStateHandler:   handleNextButtonState
		}
	);
};

YAHOO.util.Event.addListener(window, 'load', pageLoad);
