var maandLengtes = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
var base = '';
function getPos(element_id){
	var positions = new Array();
	var element = document.getElementById(element_id);
	if (element != null){
		positions["x"] = element.style.left;
		positions["y"] = element.style.top;
	}
	return positions;
}

function buildDays(dag_id, maand_id, jaar_id){
	var dag_element = document.getElementById(dag_id);
	var maand_element = document.getElementById(maand_id);
	var jaar_element = document.getElementById(jaar_id);
	if (dag_element != null && maand_element != null && jaar_element != null){
		dag = 1;
		if (dag_element.options.length > 0){
			var dag = parseFloat(dag_element.options[dag_element.selectedIndex].value);
		}
		var maand = parseFloat(maand_element.options[maand_element.selectedIndex].value);
		var jaar = parseFloat(jaar_element.options[jaar_element.selectedIndex].value);
		var dagen = maandLengtes[(maand-1)];
		if(maand == 2 && isLeap(jaar)){
			dagen = 29;
		}
		dag_element.options.length = 0;
		var found = false;
		for(i = 1; i <= dagen; i++){
			var option = document.createElement("OPTION");
			var optionText = i;
			if (optionText < 10){
				optionText = "0"+optionText;
			}
			option.text = optionText;
			option.value = i;
			if (i == dag){
				option.selected = true;
				found = true;
			} else if(i == dagen && !found){
				option.selected = true;
			}
			dag_element.options.add(option);
		}
	}
}

function isLeap(jaar){
	if (jaar % 400 == 0){
		return true;
	} else if (jaar % 100 == 0){
		return false;
	} else if (jaar % 4 == 0){
		return true;
	} else {
		return false;
	}
}

function moveTo(elementId, x, y){
	var element = document.getElementById(elementId);
	if(element != null){
		element.style.position = "absolute";
		element.style.left = x;
		element.style.top = y;
	}
}

function isText(string){
	string = string.toLowerCase();
	var l = string.length;
	var allowed = "abcdefghijklmnopqrstuvwxyz";
	var valid = true;
	for(i = 0; i < string.length; i++){
		var letter = string.charAt(i);
		if(!allowed.indexOf(letter)){
			valid = false;
		}
	}
	return valid;
}

function isNumeric(number){
	var string = number+"";
	string = string.toLowerCase();
	var allowed = "0123456789";
	var valid = true;
	for(i = 0; i < string.length; i++){
		var nr = string.charAt(i);
		if(!allowed.indexOf(nr)){
			valid = false;
		}
	}
	return valid;
}

function isPhone(number){
	var string = number+"";
	string = string.toLowerCase();
	string.replace("-", "");
	string.replace(" ", "");
	if(isNumeric(string)){
		
	} else {
		return false;
	}
}

function isLength(string, length){
	if(string.length == length){
		return true;
	} else {
		return false;
	}
}

function isEmpty(string){
	if(string.length == 0){
		return true;
	} else {
		return false;
	}
}

function resizeWindow(w,h){
	window.resizeTo(w,h);
}

function clearText(e){
	if(e.value == 'Trefwoord...') { 
		e.value = '';
	}
}

function fillEmpty(e){
	if(e.value == ''){
		e.value = 'Trefwoord...';
	}
}


function hover(e) {
	c = e.className;
	e.className = c+'_hover';
}

function hoverout(e) {
	c = e.className;
	b = c.replace("_hover", "");
	e.className = b;
}

function hide(el){
	var e = document.getElementById(el);
	if(e){
		e.style.display='none';
	}
}

var time;

function switchColor(e) {
	if(e) {
		if(e.style.background == '#f3f4ff' || e.style.background == '') {
			e.style.background = '#ffffff';
		}else {
			e.style.background = '#f3f4ff';
		}
	}
}

function switchTextinput(e) {
	if(e) {
		if(e.style.border == '#afafaf 1px solid' || e.style.border == '') {
			e.style.border = '#cc0000 1px solid';
		}else {
			e.style.border = '#afafaf 1px solid';
		}
	}
}

function getExtraInfo(id, pid) {
	var extraButtons = ['1', '2', '3', '4'];
	for (x in extraButtons)
	{
		b = document.getElementById('extraButton'+extraButtons[x]+'');
		i = document.getElementById('info'+extraButtons[x]+'');
		if(b && i) {
			if(extraButtons[x] == id) {				
				b.className = 'extraButtonActive';
				i.style.display = 'block';
			}else {
				b.className = 'extraButton';
				i.style.display = 'none';
			}
		}
	}	
}

function showDoelgroep(id) {
	var doelgroepen = [
		'Mannen',
		'Vrouwen',
		'Kinderen',
		'Senioren'
	];
	for (x in doelgroepen){
		e = document.getElementById('doelgroeplink_'+doelgroepen[x]+'');
		d = document.getElementById('doelgroep_'+doelgroepen[x]+'');
		if(d && e) {
			if(doelgroepen[x] == id) {				
				d.style.display = 'block';
				e.style.color = '#f69100';
			}else {
				d.style.display = 'none';
				e.style.color = '#2a2625';
			}
		}
	}	
}

function setActiveNews(id) {
	var berichten = new Array();
	var c = 1;
	var check = 0;

	while(check != 1) {
		var e = document.getElementById('newscontainer_'+c);
		if(e) {
			berichten.push(c);
		}else {
			check = 1;
		}
		c++;
	}
	for (x in berichten)
	{
		var e = document.getElementById('newscontainer_'+berichten[x]);
		var n = document.getElementById('newsImage_'+berichten[x]);
		if(e && n) {
			if(berichten[x] == id) {
				e.className = 'newsitemHomeActive';
				n.style.display = 'block';
			}else {
				e.className = 'newsitemHome';
				n.style.display = 'none';
			}
		}
	}	
}

function showExtraOptions(type){
	var m;
	switch(type){
		case 'category':
			m = new Ext.menu.Menu({
				minWidth:150,
				items:[{
					text:'Wijzigen',
					icon:'../../images/icons/edit.png',
					cls:'x-btn-text-icon',
					handler:function(){
						window.location = '../../../customer/plugins/webshop/index.php?tab=0';
						win.hide();
					}
				}]
			});
		break;
		case 'aanbiedingen':
			m = new Ext.menu.Menu({
				minWidth:150,
				items:[{
					text:'Wijzigen',
					icon:'../../images/icons/edit.png',
					cls:'x-btn-text-icon',
					handler:function(){
						window.location = '../../../customer/plugins/webshop/index.php?tab=1';
						win.hide();
					}
				}]
			});
		break;
	}
	if(m != null){
		var win = new Ext.Window({
			modal:true,
			title:'Opties',
			resizable:false,
			items:new Ext.ux.MenuPanel({
				menu:m,
				border:false
			})
		});
		win.setPosition(_x,_y);
		win.show();
	}
}

function switchRegister(bool){
	if(bool !== true){
		bool = false;
	}
	var t = document.getElementById('passwordTable');
	if(t){
		if(bool === true){
			t.style.display = 'block';
		} else {
			t.style.display = 'none';
		}
	} else {
		alert('Kan de wachtwoord tabel niet vinden.');		
	}
}

function fade(args){
	var data = {
		percentage:10,
		time:100
	};

	this.setPercentage = function(p){
		p = parseInt(p);
		if(p > 0 && p < 100){
			data.percentage = p;
		}
	}

	this.setTime = function(t){
		t = parseInt(t);
		if(t > 20){
			data.time = t;
		}
	}

	this.setOpacity = function(op){
		data.el.style.opacity = (op / 100);
		data.el.style.MozOpacity = (op / 100);
		data.el.style.KhtmlOpacity = (op / 100);
		data.el.style.filter = "alpha(opacity="+op+")";
	}

	for(i in args){
		switch(i){
			case 'percentage':
				this.setPercentage(args[i]);
			break;
			case 'time':
				this.setTime(args[i]);
			break;
			default:
				data[i] = args[i];
			break;
		}
	}
	if(typeof data.el != 'undefined'){
		if(data.direction == 'in' || data.direction == 'out'){
			if(typeof data.current == 'undefined'){
				if(data.direction == 'out'){
					data.current = 100;
				} else {
					data.current = 0;
				}
			}
			switch(data.direction){
				case 'out':
					data.current -= data.percentage;
					if(data.current < 0){
						data.current = 0;
					}
				break;
				case 'in':
					data.current += data.percentage;
					if(data.current > 100){
						data.current = 100;
					}
				break;
			}
			this.setOpacity(data.current);
			var finished = false;
			if(data.direction == 'out' && data.current == 0){
				finished = true;
			} else if (data.direction == 'in' && data.current == 100){
				finished = true;
			}
			if(!finished){
				setTimeout(function(){
					fade(data);
				},data.time);
			} else if(typeof data.finished == 'function'){
				data.finished(data);
			}
		} else {
			alert('Onbekend fade "in" of fade "out"');
		}
	} else {
		alert('Geen element mee gevonden.');
	}
}

$(function(){
	$('#errorBG').click(function(){
		$(this).remove();
	});

	var margin = 250;
	var moving = false;
	var mouseover = false;

	$("#containerTopAanbiedingen").mouseover(function() {
		mouseover = true;
	});

	$("#containerTopAanbiedingen").mouseout(function() {
		mouseover = false;
	});
	
	function next(elem){
		do{
			elem = elem.nextSibling;
		} while (elem && elem.nodeType != 1);
		return elem;
	}

	function prev(elem){
		do{
			elem = elem.previousSibling;
		} while (elem && elem.nodeType != 1);
		return elem;
	}

	function first(elem){
		elem = elem.firstChild;
		return elem && elem.nodeType != 1 ? next(elem) : elem;
	}

	function last(elem){
		elem = elem.lastChild;
		return elem && elem.nodeType != 1 ? prev(elem) : elem;
	}

	function moveLeft() {
		var s_items = document.getElementById('tableTopAanbiedingen');
		if(s_items && mouseover == false) {
			var s_el = first(first(s_items));
			var s_td = first(s_el).cloneNode(true);
			var s_f = last(s_el);
			s_el.appendChild(s_td);
			s_items.style.left = 0+'px';
			$("#tableTopAanbiedingen").animate({left:-238}, 1200, 'swing', function() {				
				var s_el = first(first(s_items));
				var s_td = first(s_el);
				s_td.parentNode.removeChild(s_td);
				s_items.style.left = 0+'px';
				setTimeout(function(){
					moveLeft();	
				}, 2500);
			});	
		}else {
			setTimeout(function(){
				moveLeft();	
			}, 1500);
		}
	}

	setTimeout(function(){
		moveLeft();
	}, 1500);
	
	
});