var poolsForm;
var submitTimeout;

jQuery(document).ready(function($){
	//$('.sidebar-overlay').css('display','none');
	$('a[rel*=external]').attr('target','_blank');
	
	//BEGIN: Add pool provinces ajax
	var provincesSelectbox = jQuery('#poolprovince');
	var countrySelectbox = jQuery('#poolcountry');
	if ($(countrySelectbox).length && $(provincesSelectbox).length) {
		$('#poolcountry').change(function(){
			$(provincesSelectbox).html('<option value="" selected="selected">Laden. Een moment geduld aub...</option>');
			var url = ROOT_URL;
			$.post(url,{poolcountry: $(this).val(), isajax: true},
				function(data){
					data = jQuery.trim(data);
					dataArray = data.split(',');
					$(provincesSelectbox).html('');
					if (data != '' && dataArray.length) {
						$(provincesSelectbox).html('<option value="" selected="selected">Kies de provincie...</option>');
						for (var i = 0;i < dataArray.length; i++) {
							$(provincesSelectbox).append('<option value="'+dataArray[i]+'">'+dataArray[i]+'</option>');
						}
					}else {
						$(provincesSelectbox).html('<option value="" selected="selected">Om te beginnen, kies een land...</option>');
					}
				}
			);
			return false;
		});
	}
	//END: Add pool provinces ajax
	
	//BEGIN: Pool form
	poolsForm = jQuery('#poolsform');
	$('input:checkbox',poolsForm).change(function(){
		clearTimeout(submitTimeout);
		submitTimeout = setTimeout(function(){$(poolsForm).submit()},800);
	});
	//END: Pool form
	
	//BEGIN: Clear filters link
	$('.clear-all-filters').click(function(){
		var boxId = $(this).attr('href');
		var checkboxes = $('input:checkbox',boxId);
		var checked = $('input:checked',boxId);
		if ($(checkboxes).length == $(checked).length)
			$(checkboxes).removeAttr('checked');
		else $(checkboxes).attr('checked','checked');
		clearTimeout(submitTimeout);
		submitTimeout = setTimeout(function(){$(poolsForm).submit()},800);
		return false;
	});
	//BEGIN: Clear filters link
	
	$('INPUT.auto-hint').each(function(i, el){
        if($(this).val() == ''){
            $(this).val($(this).attr('title'));
        }
        $(el).focus(function(){
            if ($(this).val() == $(this).attr('title')) {
                $(this).val('');
            }       
        });
        $(el).blur(function(){
            if ($(this).val() == '') {
                $(this).val($(this).attr('title'));
            }
        });
    });
	
	/* Expand Panels*/
	$(".expander").click(function(){
		var box = $(this).parents('.box:first');
		var boxId = $(box).attr('id');
		
		var boxContent = $('.box-content',box);
		if ( $(box).hasClass('actived') ) {
			//var date = new Date();
			//date.setTime(date.getTime() - 1000);
			//$.cookie(boxId, 'closed', { expires: date, path: '/' });//Remove cookie
			$.cookie(boxId, 'closed', {path: '/'});//Set cookie
			$(box).removeClass('actived');
			$(boxContent).stop(true,true);
			$(boxContent).css({'overflow': 'hidden'});
			var maxHeight = parseInt($(boxContent).css('max-height'));
			if (maxHeight && maxHeight > 0) $(boxContent).height(maxHeight);
			$(boxContent).slideUp(300, function(){
				$(this).css({"height": "auto", "overflow-y": "auto", "overflow-x": "hidden"});
			});
		} else {
			$.cookie(boxId, 'opened', {path: '/'});//Set cookie
			$(box).addClass('actived');
			$(boxContent).stop(true,true);
			$(boxContent).slideDown(300);
		}	
		return false;
	});
	/* Expand Google Map*/
	$(".close-button").click(function(){
		if ( $(this).parent('.lbc').hasClass('pasived') ) {
			$(this).parent('.lbc').removeClass('pasived');
			$(this).parent().children('.map-box').stop();
			$(this).parent().children('.map-box').slideDown(300);
		} else {
			$(this).parent('.lbc').addClass('pasived');
			$(this).parent().children('.map-box').stop();
			$(this).parent('.lbc').children('.map-box').slideUp(300, function(){
				$(this).css("height", "auto");
			});
		}	
		return false;
	});
	
	$('.thumbs-list a:first').addClass('current');
	$('.thumbs-list a').click(function(){
		var img = $('#slider-image img');
		changeImage(img,this);
		return false;
	});
	$('#next-thumb').click(function(){
		var img = $('#slider-image img');
		var next = $('.thumbs-list a.current').next();
		if (!$(next).length) next = $('.thumbs-list a:first');
		changeImage(img,next);
		return false;
	});
	$('#prev-thumb').click(function(){
		var img = $('#slider-image img');
		var prev = $('.thumbs-list a.current').prev();
		if (!$(prev).length) prev = $('.thumbs-list a:last');
		changeImage(img,prev);
		return false;
	});
	
	$('#filter SELECT,#filter2 SELECT').change(function(){
		$('.overlay').show();
		var form = $(this).parents('form:first');
		$(form).trigger('submit');
	});
	
$('#menu-left-side-1 LI A.toggle-pool,#menu-left-side-2 LI A.toggle-pool').click(function(){
		if ($(this).parents('LI').hasClass('open') && !$(this).parents('UL').hasClass('sub-menu'))
			return false;
		else if (!$(this).parents('UL').hasClass('sub-menu')) {
			$('#menu-left-side-1 LI,#menu-left-side-2 LI').removeClass('open');
			$('#menu-left-side-1 LI UL,#menu-left-side-2 LI UL').slideUp();
			$(this).parents('LI').addClass('open');
			$(this).parents('LI').find('UL').slideDown();
			return false;
		}
	});	
});


function changeImage(img,currentThumb){
	var src = jQuery(currentThumb).attr('href');
	if (jQuery(img).attr('src') == src) return false;
	jQuery(img).stop(true,true);
	jQuery(img).fadeTo(150,0.3,function(){
		jQuery(this).attr('src',src);
	});
	jQuery('.thumbs-list a').removeClass('current');
	jQuery(currentThumb).addClass('current');
	jQuery(img).fadeTo(150,1);
}

//Sorting pools
function changesort(sortby,chnageTo){
	/*jQuery('input[name*=[sort]]').val('');
	var input = jQuery('#'+inputId);
	jQuery(input).val(chnageTo);*/
	jQuery('#sortdirection').val(chnageTo);
	jQuery('#sortby').val(sortby);
	
	//Find form and submit
	jQuery('#sortby').parents('form:first').submit();
}

function ratingPopups(starIds,noteIds){
	if (!starIds.length || !noteIds.length || starIds.length != noteIds.length) return false;
	
	for(var i = 0;i<starIds.length;i++) {
		if (!jQuery(starIds[i]).length || !jQuery(noteIds[i]).length) return false;
		//Find place for inserting question icon
		var tr = jQuery(starIds[i]).parents('.mtrow:first');
		var td = jQuery('td:first',tr); 
		jQuery(td).append('<a rel="'+noteIds[i]+'" class="rating-question">&nbsp;</a>');
	}
	
	jQuery('.rating-question').hover(function(){
		var offset = jQuery(this).offset();
		var x = offset.left - 16;
		var y = offset.top - 80;
		var note = jQuery(jQuery(this).attr('rel'));
		jQuery(note).css('left', x);
		jQuery(note).css('top', y);
		jQuery(note).css('display', 'block');
	},function(){
		var note = jQuery(jQuery(this).attr('rel'));
		jQuery(note).css('display', 'none');
	});
}
