var	position = new Array(3);
position[0] = 0;
position[1] = 0;
position[2] = 0;

var objects = new Array(3);
for( var i = 0; i < 3; i++ )
	objects[i] = new Array();
	
function markProduct(formName, productId, group, slot, msg){
	var sbMark=document.forms[formName].mark;
	var markValue=sbMark.options[sbMark.options.selectedIndex].text;
	sbMark.disabled="disabled";
	//productId=objects[group][position[group]+slot].polyProduct.id;
	
	var url = "markProduct.action?productId="+productId+"&value="+markValue;

	new Ajax.Request(url, {
		method: 'get',
		onSuccess: function(transport){
			var mark=transport.responseText.evalJSON(true);
				
			objects[group][position[group]+slot].mark.value=mark.value;
			objects[group][position[group]+slot].marked=true;
			fill(group, slot, position[group]+slot);
			alert(msg);
		}
	});
}

function loadObjects( group, startId ) {
	var url = 'ratingProducts.action?pageParams.currentPage=' + (startId/4) + '&ratingId=' + ratings[group];
	new Ajax.Request( url, {
		method: 'get',
		onSuccess: function(transport){
			//alert(transport.responseText);
		try{
			var resp = transport.responseText.evalJSON(true);

			for( var i = 0; i < 4; i++ ) {
				var data=resp[i];
				objects[group][startId+i]=data;
			}
			fill( group, 0, startId);
			fill( group, 1, startId+1);
			fill( group, 2, startId+2);
			fill( group, 3, startId+3);
		}
		catch(e){
			//alert(e);
		}
		}
	});
}



/*
function changeBrand(){
	var selectBox = document.getElementById("brandSelect");
	var brand = selectBox.options[selectBox.selectedIndex].value; 

	var url = "devices.action?brand="+brand;
	new Ajax.Request(url, {
		method: 'get',
		onSuccess: function(transport){
			var devices=transport.responseText.evalJSON();
			var size = devices.length;

			$('deviceSelect').options.length = size;
			for( var i = 0; i < size; i++ ) {
				var tmp = devices[i].name;
				$('deviceSelect').options[i].text = tmp;
			}	
		}
	});
}
*/

function left( group ) {
	
	switchElems(group, 0, 0);
	switchElems(group, 1, 0);
	switchElems(group, 2, 0);
	switchElems(group, 3, 0);
	
	position[group] -= 4;
	
	if( position[group] < 0 )
		position[group] = ProdNumber[group]-4;

	//if( objects[group][position[group]] == null )
		//loadObjects( group, position[group] );
	//else{
	
		fill( group, 0, position[group] );
		fill( group, 1, position[group]+1 );
		fill( group, 2, position[group]+2 );
		fill( group, 3, position[group]+3 );
	
	//}
}
function right( group ) {

	switchElems(group, 0, 0);
	switchElems(group, 1, 0);
	switchElems(group, 2, 0);
	switchElems(group, 3, 0);
	
	position[group] += 4;
	
	if( position[group] > ProdNumber[group]-4 )
		position[group] = 0;
	
	//if( objects[group][position[group]] == null )
		//loadObjects( group, position[group] );
	
	//else{
		fill( group, 0, position[group] );
		fill( group, 1, position[group]+1 );
		fill( group, 2, position[group]+2 );
		fill( group, 3, position[group]+3 );
	//}
}

function fill( group, slot, index ) {
	
try{
	var size=12;
	var plusSize=2;

	var divauthor = $("div" + group + slot + "author");
	var author=null;
		
	if(objects[group][index].polyProduct!=undefined && objects[group][index].polyProduct.available=="true")
		author=objects[group][index].polyProduct.author;
	else if(objects[group][index].trueProduct!=undefined && objects[group][index].trueProduct.available=="true")
		author=objects[group][index].trueProduct.author;
	
	//alert(objects[group][index].trueProduct!=undefined);
	divauthor.update(author);
	if(author.length>size+plusSize){
		//var shortAuthor=author.substring(0, size)+"...";
		var trunk = truncateToFit(unescapeQuotes(author), size);
		var shortAuthor = escapeQuotes(trunk);
		divauthor.update(shortAuthor);
		divauthor.onmouseover=function(){
			showTooltip(this, author, 200, 0, 18);
		};
		divauthor.onmouseout=function(){
			hideTooltip();
		};
		//divauthor.writeAttribute("onmouseover", "showTooltip(this, '"+author+"', 200, 0, 18)");
		//divauthor.writeAttribute("onmouseout", "hideTooltip()");
	}
	else{
		
		divauthor.onmouseover=function(){
			empty();
		}
		//divauthor.writeAttribute("onmouseover", "empty()");
		divauthor.onmouseout=function(){
			empty();
		}
		//divauthor.writeAttribute("onmouseout", "empty()");
	}
	
	var divauthorsearch = $("div" + group + slot + "authorSearch");
	
	var divtitle = $("div" + group + slot + "title");
	var title=null;
	if(objects[group][index].polyProduct!=undefined && objects[group][index].polyProduct.available=="true")
		title=objects[group][index].polyProduct.title;
	else if(objects[group][index].trueProduct!=undefined && objects[group][index].trueProduct.available=="true")
		title=objects[group][index].trueProduct.title;
	
	var prodId=null;
	if(objects[group][index].trueProduct!=undefined && objects[group][index].trueProduct.available=="true")
		prodId=objects[group][index].trueProduct.id;
	else if(objects[group][index].polyProduct!=undefined && objects[group][index].polyProduct.available=="true")
		prodId=objects[group][index].polyProduct.id;
		
	var divimg=$("div" + group + slot + "image");
	divimg.writeAttribute("src", "presenter/track/"+prodId+".gif?pid="+prodId+"&type=www");
	
	var url="searchByText.action?pid="+prodId+"&mode=2";
	divauthorsearch.setAttribute("href", url);

	//if(objects[group][index].trueProduct.available){
		var divssn=$("div" + group + slot + "ssn");
		//divssn.writeAttribute("onclick", "javascript:playSoundSmall('ssnd"+prodId+"');");
		divssn.onclick=function(){
			playSoundSmall('ssnd'+prodId);
		}
		divssn.select('img')[0].writeAttribute("id", "ssnd"+prodId);
	/*}
	else{
		var divssn=$("div" + group + slot + "ssn");
		divssn.writeAttribute("onclick", "javascript:playSoundSmall('ssnd"+objects[group][index].polyProduct.id+"');");
		divssn.select('img')[0].writeAttribute("id", "ssnd"+objects[group][index].polyProduct.id);
	}*/
	divtitle.update(title);
	if(title.length>size+plusSize){
//		var shortTitle=title.substring(0, size)+"...";
		
		var trunk = truncateToFit(unescapeQuotes(title), size);
		var shortTitle = escapeQuotes(trunk);
		
		divtitle.update(shortTitle);
		divtitle.onmouseover=function(){
			showTooltip(this, title, 200, 0, 18);
		}
		//divtitle.writeAttribute("onmouseover", "showTooltip(this, '"+title+"', 200, 0, 18)");
		divtitle.onmouseout=function(){
			hideTooltip();
		}
		//divtitle.writeAttribute("onmouseout", "hideTooltip()");
	}
	else{
		
		divtitle.onmouseover=function(){
			empty();
		}
		divtitle.onmouseout=function(){
			empty();
		}
		//divtitle.writeAttribute("onmouseover", "empty()");
		//divtitle.writeAttribute("onmouseout", "empty()");
	}
	
	if(objects[group][index].trueProduct!="null" && objects[group][index].trueProduct!=undefined && objects[group][index].trueProduct.available=="true"){
		$("trueAct"+group+slot).show();
		$("true"+group+slot).hide();
	}
	else{
		$("trueAct"+group+slot).hide();
		$("true"+group+slot).hide();
	}
	if(objects[group][index].polyProduct!="null" && objects[group][index].polyProduct!=undefined){
		if(objects[group][index].trueProduct!=null && objects[group][index].trueProduct!=undefined && objects[group][index].trueProduct.available=="true"){
			$("polyAct"+group+slot).hide();
			$("poly"+group+slot).show();
		}
		else{
			$("polyAct"+group+slot).show();
			$("poly"+group+slot).hide();
		}
	}
	else{
		$("polyAct"+group+slot).hide();
		$("poly"+group+slot).hide();
	}

	if(objects[group][index].polyProduct!=null && objects[group][index].polyProduct!=undefined && objects[group][index].polyProduct.available=="true"){
		var divpolycode=$("div"+group+slot+"infoPoliCode")
		divpolycode.update(objects[group][index].polyCode.code);
		
		var divpolycodenr=$("div"+group+slot+"infoPoliNo")
		divpolycodenr.update(objects[group][index].polyCode.number);
		
		var divpolycodeprice=$("div"+group+slot+"infoPoliPrice")
		divpolycodeprice.update(objects[group][index].polyCode.price);
	}
	
	if(objects[group][index].trueProduct!=null && objects[group][index].trueProduct!=undefined && objects[group][index].trueProduct.available=="true"){
		if($("div"+group+slot+"infoTrueCode")!=null && $("div"+group+slot+"infoTrueNo")!=null && $("div"+group+slot+"infoTruePrice")!=null){
			var divtruecode=$("div"+group+slot+"infoTrueCode")
			divtruecode.update(objects[group][index].trueCode.code);
		
			var divtruecodenr=$("div"+group+slot+"infoTrueNo")
			divtruecodenr.update(objects[group][index].trueCode.number);
		
			var divtruecodeprice=$("div"+group+slot+"infoTruePrice")
			divtruecodeprice.update(objects[group][index].trueCode.price);
		}
	}

	var divimage = document.getElementById("div" + group + slot + "image");
	
	sbMark=document.forms["markForm"+group+""+slot].mark;
	sbMark.selectedIndex=0;
	sbMark.disabled=false;
	
	for( var i = 0; i < 5; i++ ) {
		var rank = document.getElementById("div" + group + slot + "rank" + i);
		
		if( objects[group][index].mark.value > i ){
			rank.setAttribute('src', 'images/plus.gif');
		}
		else
			rank.setAttribute('src', 'images/minus.gif');
	}
	
	if(objects[group][index].marked==true){
		$("sMarkForm"+group+""+slot).disabled="disabled";
	}
	else{
		$("sMarkForm"+group+""+slot).disabled="";
	}
	
	var divmark=$('sMarkForm'+group+''+slot);
	divmark.onchange=function(){
	markProduct('markForm'+group+''+slot, objects[group][index].mark.productId, group, slot, 'Dzi\u0119kujemy za oddanie g\u0142osu.');
	}
	
	if($('div'+group+slot+'phones')!=null && $('div'+group+slot+'phones')!=undefined){
		$('div'+group+slot+'phones').onclick=function(){
			showPhones(prodId);
		};
	}
}
catch(err) {
	//alert("err "+err);
}
}

function rank( group, slot, value ) {
	//alert("position[group]+slot : "+(position[group]+slot))
	var id=objects[group][position[group]+slot].polyProduct.id;
	var url = "markProduct.action?productId="+id+"&value="+(value+1);
	new Ajax.Request(url, {
		method: 'get',
		onSuccess: function(transport){
			alert("marked");
		}
	});
}

function switchActive( toActive, toDeactive, toShow, toHide ) {
	toActive.className='current';	//swichActive';
	toDeactive.className='';	//swichDeactive';
	toShow.style.display='block';
	toHide.style.display='none';
}
function switchBorder( l, c, r, state ) {

	if( state == 0 ) {
		l.className='menu-dolne-current-L srodek';
		r.className='menu-dolne-P';
		c.className='menu-dolne-current-P';
	}
	else {
		l.className='menu-dolne-L';
		r.className='menu-dolne-current-P';
		c.className='menu-dolne-current-L srodek';
	}
}

function switchActiveId( toActive, toDeactive, toShow, toHide ) {
	var toAct = document.getElementById(toActive);
	var toDeact = document.getElementById(toDeactive);

	var show = document.getElementById(toShow);
	var hide = document.getElementById(toHide);

	switchActive( toAct, toDeact, show, hide );
}

function switchInfo( group, slot, state ) {
	var divname = 'div' + group + slot; 
	var toAct, toDeact, show, hide, 
	borderL, borderP, borderCenter;

	if( state == 0 ) {
		toAct = document.getElementById( divname + 'true');
		toDeact = document.getElementById( divname + 'poli' );

		show = document.getElementById( divname + 'infoTrue' );
		hide = document.getElementById( divname + 'infoPoli' );
	}
	else {
		toAct = document.getElementById( divname + 'poli' );
		toDeact = document.getElementById( divname + 'true');

		show = document.getElementById( divname + 'infoPoli' );
		hide = document.getElementById( divname + 'infoTrue' );
	}
	borderL = document.getElementById( divname + 'trueL' );
	borderCenter = document.getElementById( divname + 'center' );
	borderP = document.getElementById( divname + 'poliP' );

	switchActive( toAct, toDeact, show, hide );
	switchBorder( borderL, borderCenter, borderP, state );
}


function switchElems(group, slot, state){
	var divname = 'div' + group + slot;
	var toAct, toDeact, show, hide;
	
	try{
		if(playingSound)
			doStopSound();
	}
	catch(e){
	}
	
	var index=position[group]+slot;
	
	if( state == 0 && objects[group][index]!=undefined && 
		objects[group][index].trueProduct!=null && objects[group][index].trueProduct!=undefined && 
		objects[group][index].trueProduct.available=="true") {
		toTrueDeact = $('true'+group+slot);
		toPolyDeact = $('polyAct'+group+slot);
		toTrueAct = $('trueAct'+group+slot);
		toPolyAct = $('poly'+group+slot);

		show = $(divname+'infoPoli');
		hide = $(divname+'infoTrue');
		
		var divssn=$("div" + group + slot + "ssn");

		if(objects[group][index]!=undefined && objects[group][index].trueProduct!=undefined){
			divssn.onclick=function(){
				playSoundSmall('ssnd'+objects[group][index].trueProduct.id);
			};
			divssn.select('img')[0].writeAttribute("id", "ssnd"+objects[group][index].trueProduct.id);
			
			if($('div'+group+slot+'phones')!=null && $('div'+group+slot+'phones')!=undefined){
				$('div'+group+slot+'phones').onclick=function(){
					showPhones(objects[group][index].trueProduct.id);
				};
			}
		}
	}
	else {
		toTrueDeact = $('trueAct'+group+slot);
		toPolyDeact = $('poly'+group+slot);
		toTrueAct = $('true'+group+slot);
		toPolyAct = $('polyAct'+group+slot);

		show = $(divname+'infoTrue');
		hide = $(divname+'infoPoli');
		
		var index=position[group]+slot;
		
		var divssn=$("div" + group + slot + "ssn");

		if(objects[group][index]!=undefined && objects[group][index].polyProduct!=undefined){
			divssn.onclick=function(){
				playSoundSmall('ssnd'+objects[group][index].polyProduct.id);
			};
			divssn.select('img')[0].writeAttribute("id", "ssnd"+objects[group][index].polyProduct.id);
			
			if($('div'+group+slot+'phones')!=null && $('div'+group+slot+'phones')!=undefined){
				$('div'+group+slot+'phones').onclick=function(){
					showPhones(objects[group][index].polyProduct.id);
				};
			}
		}
	}
	
	if(toTrueAct)
		toTrueAct.show();
	if(toTrueDeact)
		toTrueDeact.hide();
	if(toPolyAct)
		toPolyAct.show();
	if(toPolyDeact)
		toPolyDeact.hide();
	if(show)
		show.hide();
	if(hide)
		hide.show();
	//switchActive( toAct, toDeact, show, hide );
} 


function changeBrand(){	
	var selectBox = document.forms['setDeviceForm'].brandName;
	
	var brand = selectBox.options[selectBox.selectedIndex].value; 
	
	if(brand==""){
		$('deviceSelect').options.length=0;
		$('deviceSelect').disabled=true;
		return;
	}
	
	selectBox.disabled = true;
	
	var device = document.getElementById("deviceSelect");
	device.disabled = true;
	
	var url = "devices.action?brand="+brand;
	new Ajax.Request(url, {
		method: 'get',
		onSuccess: function(transport){
		try{
			var devices=transport.responseText.evalJSON();
			var size = devices.length;

			$('deviceSelect').options.length = size+1;
			$('deviceSelect').options[0]=new Option();
			$('deviceSelect').options[0].text='Model';
			for( var i = 1; i <=size; i++ ) {
				var name = devices[i-1].name;
				var id=devices[i-1].id;
				$('deviceSelect').options[i]=new Option();
				//alert(name);
				$('deviceSelect').options[i].text = name;
				$('deviceSelect').options[i].value = id;
			}
			
			selectBox.disabled = false;
			device.disabled = false;	
		}
		catch(e){
			//alert(e);
		}
		}
	});
}

function setDevice(){
	var selectBox = document.forms['setDeviceForm'].brandName;
	var brand = selectBox.options[selectBox.selectedIndex].value;
	var deviceIdBox=document.forms['setDeviceForm'].deviceId;
	var model = deviceIdBox.options[deviceIdBox.selectedIndex].value;
	
	if(brand=='' || model==''){
		return;
	}
	
	document.forms['setDeviceForm'].submit();
}

function showTooltip(el, text, eWidth, left, top){
	var offset=$(el).cumulativeOffset();
	var width=$(el).getWidth();
	var height=$(el).getHeight();
	
	left=0;
	
	$('tooltip').setStyle({'position': 'absolute'});
	
	$('tooltip').setStyle({'left': eval(offset["left"]+left)+"px"});
	$('tooltip').setStyle({'top': eval(offset["top"]+top)+"px"});
	
	$('tooltip').update(text);
	
	if(eWidth!=0 && $('tooltip').getWidth()>eWidth)
		$('tooltip').setStyle({'width': eWidth+"px"});
	
	$('tooltip').show();
}
function hideTooltip(){
	$('tooltip').setStyle({'width': ''});
	$('tooltip').update('');
	$('tooltip').hide();
}
function empty(){
}

function loadBrands() {
	var selectBox = document.getElementById('brandSelect');

	var url = 'brands.action';
	new Ajax.Request( url, {
		method: 'get',
		onSuccess: function(transport){
			var brands=transport.responseText.evalJSON();
		
			var size = brands.length;
			selectBox.options.length = size+1;
			
			selectBox.options[0].text="Producent";
			
			for( var i = 1; i <= size; i++ ) {
				var tmp = brands[i];
				selectBox.options[i].text = tmp;
				selectBox.options[i].value = tmp;
			}
		}
   });
}

function changeModel(){
	//pobierz producentow
	loadBrands();
	
	$('deviceSelect').disabled="disabled";
	$('deviceSelect').options.length=1;
		$('deviceSelect').options[0].text="Model";
	
	$('modelSelected').hide();
	$('modelSelectedForm').show();
	
	var model=$('selectedModel');
	
	if(model!=null)
		model.setAttribute("src", "images/un_fon.jpg");
}

function showPhones(prodId){
	window.open('productPhones.action?productId='+prodId,'mywindow','width=600,height=600,scrollbars=yes');
}

function changeCategory(){
	var sbCategory=document.forms['categoryForm'].categoryId;
	if(sbCategory.selectedIndex!=0){
		document.forms['categoryForm'].submit();
	}
}

function search(){
	var text=document.forms["search_form"].text.value;
	if(text!=null && text.length > 1){
		document.forms["search_form"].submit();
		return true;
	}
	else{
		alert("Wpisz przynajmniej dwie litery");
		return false;
	}
}

function submit(){
}

function checkHintKey(e){
	var keynum;
	var keychar;
	var numcheck;
	
	if(window.event) // IE
	{
	  keynum = e.keyCode;
	}
	else if(e.which) // Netscape/Firefox/Opera
	{
	  	keynum = e.which;
	}
	
	if(keynum==27 || keynum==13){// esc lub enter
		hideHints();
	}
	
	var select=false;
	if(keynum==38){
		prevHint();
		select=true;
	}
	if(keynum==40){
		nextHint();
		select=true;
	}
	if(!select)
		hint_pos=-1;
	
	if(keynum==38 || keynum==40)
		return false;
	
	return true;
} 

function escapeQuotes(text){
    text=text.replace(/&/g, "&amp;");
    text=text.replace(/\'/g, "&#39;");
    text=text.replace(/\"/g, "&#34;");
    
    return text;
}

function unescapeQuotes(text){
    text=text.replace(/&amp;/g, "&");
    text=text.replace(/&#39;/g, "\'");
    text=text.replace(/&#34;/g, "\"");
    
    return text;
}


function truncateToFit(text, size){
    if(text == undefined ) return text;
    
    if( text.length<=size+2) {
        return text;
    }   else {
        return text.substring(0, size)+"...";
    }
}