//subcategorie selecteren d.m.v. checkbox
function popupSelectSubCat() {
	$$('.popupSelectSubCat').addEvent('click', function(event) {
		var req = new Request.JSON({method: 'get', url:'/ajax/checkSubCats.php',
			onSuccess: function(jsonResponse) {
            //Clear the text currently inside the results div.
            $('popupSelectedCategories').set('text', '');
            //Inject the new DOM elements into the results div.
            $('popupSelectedCategories').set('html', jsonResponse.html);
            $('searchcategorie').set('value', jsonResponse.subCatNamen);            
            popupUnselectSubCat();
	      },
	      //Our request will most likely succeed, but just in case, we'll add an
	      //onFailure method which will let the user know what happened.
	      onFailure: function() {
	            $('popupSelectedCategories').set('text', '');
	      }
	    });
	    req.send('subcat='+this.get('value')+'&checked='+this.get('checked'));
	});
	popupUnselectSubCat();
}

function popupUnselectSubCat(renew){
	$$('.popupUnselectSubCat').addEvent('click', function(event) {
		var req = new Request.JSON({method: 'get', url:'/ajax/checkSubCats.php',
			onSuccess: function(jsonResponse) {
            //Clear the text currently inside the results div.
            $('popupSelectedCategories').set('text', '');
            //Inject the new DOM elements into the results div.
            $('popupSelectedCategories').set('html', jsonResponse.html);
            $('searchcategorie').set('value', jsonResponse.subCatNamen);
            if (renew != true){
            	popupUnselectSubCat(true);
            }
	      },
	      //Our request will most likely succeed, but just in case, we'll add an
	      //onFailure method which will let the user know what happened.
	      onFailure: function() {
	            $('popupSelectedCategories').set('text', '');
	      }
	    });
	    req.send('subcat='+this.get('value')+'&checked='+this.get('checked'));
	    renew = false;
	});
}


var requester = null;

window.addEvent('domready', function() {
	//categorie popup zichtbaar zetten
	$('searchKiesCategorie').addEvent('click', function(event) {
		$('popupWrapper').setStyle('display','');
		event.stop();
	});
	
	//klik op hele document
	$(document.body).addEvent('click', function(event) {
		//categorie popup onzichtbaar zetten
		if(null==$(event.target).getParent('#popupWrapper')){
			$('popupWrapper').setStyle('display','none');
		}
	});
	
	//klik op categorie popup sluiten
	$('popupWrapperClose').addEvent('click', function(event) {
		//categorie popup onzichtbaar zetten
		$('popupWrapper').setStyle('display','none');
	});
	
	
	//subcategorieen weergeven in popupSubcategories
	$$('.popupHeadCategory').addEvent('click', function(event) {
		var req = new Request.HTML({method: 'get', url:'/ajax/getSubCats.php', 
	          onSuccess: function(html) {
	                //Clear the text currently inside the results div.
	                $('popupSubcategories').set('text', '');
	                //Inject the new DOM elements into the results div.
	                $('popupSubcategories').adopt(html);
	                popupSelectSubCat();
	          },
	          //Our request will most likely succeed, but just in case, we'll add an
	          //onFailure method which will let the user know what happened.
	          onFailure: function() {
	                $('popupSubcategories').set('text', '');
	          }
	    });
	    req.send('catId='+this.get('id'));
	});
	
	//Trefwoord leeg maken
	$('searchword').addEvent('click', function(event) {
		if (this.value == 'type hier uw trefwoord...') {
			this.set('value', '');
		}
	});
	
	//fotos
	$$('.advertfoto').addEvent('mouseover', function(event) {
		$('advertMainFoto').src=this.href;
	});
	
	//zoek form submitten
	$('zoekform').addEvent('submit', function(event) {
		if ($('searchword').value == 'type hier uw trefwoord...') {
			$('searchword').set('value', '');
		}
		$('zoekform').submit();
	});
	
	//advert details naast zoekoverzicht weergeven
	$$('.advertBoxWrapper').addEvent('mouseenter', function(event) {
		requester = $clear(requester);
		
		req = new Request.HTML({method: 'get', url:'/ajax/getAdInfoSidebar.php', 
	          onSuccess: function(html) {
	                //Clear the text currently inside the results div.
	                $('informatieholderAdinfo').set('text', '');
	                //Inject the new DOM elements into the results div.
	                $('informatieholderAdinfo').adopt(html);
	          },
	          //Our request will most likely succeed, but just in case, we'll add an
	          //onFailure method which will let the user know what happened.
	          onFailure: function() {
	                $('informatieholderAdinfo').set('text', '');
	          }
	    });
		requester = req.send.delay(200,req,'adId='+this.get('id'));
	    
	});
	//informatie details mee scrollen
	if ($('informatieholder')){
		toggelOverlay();
		window.addEvent('scroll', toggelOverlay);
	}
	
	//andere plaats gekozen op pagina2
	if($chk($('advertplaced'))){
		$('advertplaced').addEvent('change', function(event) {
			//alert(this.value);
			//alert(location.href);
			var get = $get(location.href);
			var newurl = '?';
			$each(get,function(item,key){
				if(key != 'searchplace'){
					newurl += key+'='+item+'&';
				}
			});
			newurl += 'searchplace='+this.value;
			window.location = newurl;
		});
	}
	popupSelectSubCat();
});

function toggelOverlay()
{
	if (window.getScroll().y > $('informatieholder').getPosition().y-70  && window.getScroll().y > 600){
		$('informatieholder').set('class','sidebarHolder215floating sidebarHolder215');
	}
	else {
		$('informatieholder').set('class','sidebarHolder215');
	}
}

function $get(url){  
    url = url.split("?");  
    var results = {};  
        if(url.length > 1){  
            url = url[1].split("&");      
            //if(url.length > 1) results["hash"] = url[1];
            url.each(function(item,index){
                item = item.split("=");
                results[item[0]] = item[1];  
            });  
        }
    return results;    
}
