﻿var aProd = new Array( );
var index = new Array();
var time = new Array();
var timeOut = new Array();
var imgPath = '/public/tpl/zxo/images/';


/**
 *
 * @access public
 * @return void
 **/
function generate(urlpath){
	$.get(urlpath, {}, function(xml) {
		i = new Array();
		$('product', xml).each(function(iaa){
			type = $(this).find('section').text();
			if(!aProd[type]){
				aProd[type] = new Array( );
				i[type]=0;
				time[type]=1;
				index[type] = 0;
				timeOut[type] = false;
			}
			aProd[type][i[type]] = new Array( );
			aProd[type][i[type]]['title'] = $(this).find('title').text();
			aProd[type][i[type]]['subtitle'] = $(this).find('subtitle').text();
			aProd[type][i[type]]['url'] = $(this).find('url').text();
			aProd[type][i[type]]['thumb'] = $(this).find('thumb').text();
			aProd[type][i[type]]['desc'] = $(this).find('desc').text();
			aProd[type][i[type]]['price'] = $(this).find('price').text();
			aProd[type][i[type]]['pricecatalog'] = $(this).find('pricecatalog').text();
			i[type]++;
		});
	},"xml");
}


function rotator(type,milsec){
	if(time[type] == 1){
		navigate('next',type);
	}
	timeOut[type] = setTimeout(function(){ rotator(type,milsec);},milsec);
}
function rotatorSet(type,milsec){
	timeOut[type] = setTimeout(function(){ rotator(type,milsec);},milsec);
}
function navigate(act,type){
	if(aProd[type] && aProd[type].length > 0){
		switch(act){
			case 'nextClear':
				clearTimeout(timeOut[type]);
				rotatorSet(type,10000);
			case 'next':
				if(aProd[type].length-1 >= index[type]+1){
					index[type]++;
				} else{
					index[type] = 0;
				}
				generateBox(type);
				break;
			case 'prev':
				if(index[type] > 0){
					index[type]--;
				} else{
					index[type] = aProd[type].length - 1;
				}
				generateBox(type);
				break;
			case 'pause':
				if(time[type] == 1){
					time[type]=0;
					$('#buttonplay'+type+' img').attr({'src':imgPath+'push_play.gif','alt':'Uruchom'});

				} else {
					time[type]=1;
					$('#buttonplay'+type+' img').attr({'src':imgPath+'push_pause.gif','alt':'Zatrzymaj'});
				}
		}
	}
}

function generateBox(type){
	sHtml = '';
	sImg = '';
	if(aProd[type][index[type]]['thumb']){
		sImg = '<div class="coverBox"><a href="' + aProd[type][index[type]]['url'] + '" title="' + aProd[type][index[type]]['title'] + '" rel="nofollow"><img class="cover" src="' + aProd[type][index[type]]['thumb'] + '" alt="" /></a></div>';
	}
	if(type == 3 || type == 6){
		sHtml += sImg;
	}
	sHtml += '<h1 class="title"><a class="a3" href="'+aProd[type][index[type]]['url']+'" title="'+aProd[type][index[type]]['title']+'">'+aProd[type][index[type]]['title']+'</a></h1>';
	if(aProd[type][index[type]]['subtitle']){
		sHtml += '<h4 class="bSubTitle">'+aProd[type][index[type]]['subtitle']+'</h4>';
	}
	if(type != 3 && type != 6){
		sHtml += sImg;
	}
	sHtml += aProd[type][index[type]]['desc'] + '<br /><br />';
	if(aProd[type][index[type]]['pricecatalog'] > 0){
		sHtml += 'Cena katalogowa: <del>'+aProd[type][index[type]]['pricecatalog']+' zł</del><br />';
	}
	if(aProd[type][index[type]]['price'] > 0){
		sHtml += 'Nasza cena: <span class="price">'+aProd[type][index[type]]['price']+' zł</span>';
	}
	sHtml += '<div class="clear">&nbsp;</div>';
	$('#siteContent'+type).html(sHtml);
}