function optionFilter(src, dest, setEl){
	src.change(function(e){
	
		var filter = src.find('option:selected').attr('value');
		if (filter == "-1"){
			dest.empty().hide().prev().hide();
			return false;
		}
		
		$.get('/getDevicesByVendor?vid='+filter, function(elems){
			dest.empty();
			//console.log(elems);
			$.each(elems.models, function(v,k){
				var url = '?dev='+k+'&game=-1',
					option = $('<option value='+k+'>'+v+'</option>');
				dest.append(option);
			});
			dest.show().prev().show();
		});
	});
	
	setEl.click(function(){
		var k = dest.find(":selected").attr('value');
		if(typeof k == 'undefined') return false;
		document.location = '?scId='+scId+'&dev='+k+'&game='+gameId;
	});
}

$(function(){
	optionFilter($('#brand'),$('#model'),$('#phoneSet'));
});



sfHover = function() {
	if (document.getElementById("telefony")) {
		var sfEls = document.getElementById("telefony").getElementsByTagName("LI");
		for (var i=0; i<sfEls.length; i++) {
			sfEls[i].onmouseover=function() {
				this.className+=" sfhover";
			}
			sfEls[i].onmouseout=function() {
				this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
			}
		}
	}
}

buttons = function() {
	if (document.getElementById("buttonContact")) {
		var el = document.getElementById("buttonContact");
		el.onclick = function() {
			openWindow('/html/kontakt.html');
		}
	} 
	if (document.getElementById("buttonClose")) {
		var el = document.getElementById("buttonClose");
		el.onclick = function(){
			self.close()
		}
	}
	if (document.getElementById("buttonFormularz")) {
		var el = document.getElementById("buttonFormularz");	
		el.onclick = function(){
			openWindow('/html/wpisz_reklamacje.html', 540)
		}
	}
	if (document.getElementById("buttonSafety")) {
		var el = document.getElementById("buttonSafety");
		el.onclick = function(){
			openWindow('/html/bezpieczenstwo.html');
		}
	}
	if (document.getElementsByTagName("span")) {
		var els = document.getElementsByTagName("span");
		for (var i=0; i <els.length; i++) {
			if (els[i].className && els[i].className.indexOf("buttonRules") >= 0) {
				els[i].onclick = function(){
					openWindow('/html/regulamin.html')
				}
			}
		}
	}
	if (document.getElementById("buttonAbout")) {
		var el = document.getElementById("buttonAbout");
		el.onclick = function(){
			openWindow('/html/informacje.html')
		}
	}
	if (document.getElementById("buttonAdd")) {
		var el = document.getElementById("buttonAdd");
		el.onclick = function(){
			openWindow('/html/reklama.html')
		}
	}
	if (document.getElementById("buttonDownload")) {
		var el = document.getElementById("buttonDownload");
		el.onclick = function(){
			openWindow('/html/jaksciagnac.html')
		}
	}
	if (document.getElementById("buttonHelp")) {
		var el = document.getElementById("buttonHelp");
		el.onclick = function(){
			openWindow('/html/reklamacje.html', 538)
		}
	}
	if (document.getElementById("buttonFirst")) {
		var el = document.getElementById("buttonFirst");
		el.onclick = function(){
			openWindow('/html/pierwszy_raz.html')
		}
	}
	if (document.getElementById("buttonRegister")) {
		var el = document.getElementById("buttonRegister");
		el.onclick = function(){
			openWindow('/register')
		}
	}
	if (document.getElementById("buttonFaq")) {
		var el = document.getElementById("buttonFaq");
		el.onclick = function(){
			openWindow('/html/faq.html')
		}
	}
	if (document.getElementById("buttonCeny")) {
		var el = document.getElementById("buttonCeny");
		el.onclick = function(){
			openWindow('/html/ceny.html', 535)
		}
	}
	if (document.getElementById("buttonBack")) {
		var el = document.getElementById("buttonBack");
		el.onclick = function(){
			history.back()
		}
	}
	if (document.getElementById("buttonSms")) {
		var el = document.getElementById("buttonSms");
		el.onclick = function(){
			openWindow('/html/sms.html')
		}
	}

}
function openWindow(url, width) {
	var height = 368;
	var scroll = 1;
	var resize = 1;
	//width = 360;
	if (!width) width = 360;
	
	var str = "width="+width+",height="+height+",toolbar=0,location=0,directories=0,status=0,menuBar=0"
	
	
	if(scroll == 1) str += ",scrollbars=1"
	else if (scroll == 0) str += ",scrollbars=0"
               
	if(resize == 1) str += ",resizable=1"
	else if (resize == 0) str += ",resizable=0"
     
	if (window.screen){
		var ah = screen.availHeight - 30;
		var aw = screen.availWidth - 10;
		var xc = (aw - width) / 2;
		var yc = (ah - height) / 2;
		xc = xc >= 0 ? xc : 0;
		yc = yc >= 0 ? yc : 0;
		str += ",left=" + xc + ",screenX=" + xc;
		str += ",top=" + yc + ",screenY=" + yc;
	}    

   
	newWindow = window.open(url, '',  str);
	newWindow.focus();
}          


function xpAddEvent(obj, type, listener, useCapture) { 
        if (window.addEventListener) { 
                obj.addEventListener(type, listener, useCapture) 
        } else if (window.attachEvent) { 
                obj.attachEvent("on"+type, listener) 
        } 
} 

if (window.attachEvent) { window.attachEvent("onload", sfHover) } 
xpAddEvent(window, "load", buttons, false); 

function next() {         
	var el = document.getElementById('number');
	var number = parseInt(el.className);
	var count = parseInt(document.getElementById('count').className);
		
	var element1 = document.getElementById('s'+number);
	if (number+1 <= count) {
		var element2 = document.getElementById('s'+(number+1));
  		element1.style.display = "none";
  		element2.style.display = "block";   
 		el.className = number + 1;
	}
}
  	
function prev() {                                  
	var el = document.getElementById('number');  	
	var number = parseInt(el.className);
	var count = parseInt(document.getElementById('count').className);
	var element1 = document.getElementById('s'+number);
	if (number > 1) {
		var element2 = document.getElementById('s'+(number-1));
		element1.style.display = "none";
		element2.style.display = "block";   
  		el.className = number - 1;
	}
}

function show(num) {
	var el = document.getElementById('number');  	
	var number = parseInt(el.className);
	var count = parseInt(document.getElementById('count').className);
	var element1 = document.getElementById('s'+number);
	if (num > 0) {
		var element2 = document.getElementById('s'+num);
		element1.style.display = "none";
		element2.style.display = "block";   
  		el.className = num;
	}
}
