(function($){$.fn.selectChain=function(options){var defaults={key:"id",value:"label",preselect:":first"};var settings=$.extend({},defaults,options);if(!(settings.target instanceof $))settings.target=$(settings.target);var preselect_string=settings.preselect;if(typeof(settings.preselect)!="undefined"&&settings.preselect!=''&&settings.preselect!=':first'){preselect_string='[value='+settings.preselect+']';}
return this.each(function(){var $$=$(this);$$.change(function(){var data=null;if(typeof settings.data=='string'){data=settings.data+'&'+this.name+'='+$$.val();}else if(typeof settings.data=='object'){data=settings.data;data[this.name]=$$.val();}
settings.target.empty();$.ajax({url:settings.url,data:data,type:(settings.type||'get'),dataType:'json',success:function(j){var options=[],i=0,o=null;for(i=0;i<j.length;i++){o=document.createElement("OPTION");o.value=typeof j[i]=='object'?j[i][settings.key]:j[i];o.text=typeof j[i]=='object'?j[i][settings.value]:j[i];settings.target.get(0).options[i]=o;}
setTimeout(function(){if(settings.target.find(preselect_string).length==0){preselect_string=':first';}
settings.target
.find(preselect_string)
.attr('selected','selected')
.parent('select')
.trigger('change');},0);},error:function(xhr,desc,er){alert("An internal error occured. Please try again in a few seconds.");}});});});};})(jQuery);

$(document).ready(function(){var continent=$('#qa-continent');var country=$('#qa-country');var region=$('#qa-region');var spot=$('#qa-spot');if($.cookie('qa-continent')){var last_continent=$.cookie('qa-continent');$('#qa-continent option').each(function(intIndex){if($(this).val()==last_continent){$('#qa-continent option').removeAttr('selected');$(this).attr('selected',true);continent.trigger('change');}});}else{$('#qa-continent option').each(function(intIndex){if($(this).val()=='Europe'){$('#qa-continent option').removeAttr('selected');$(this).attr('selected',true);continent.trigger('change');}});}
var preselect_country='Spain';var preselect_region='es3';var preselect_spot='tarifa#1#1,2,10';if($.cookie('qa-spot')){preselect_spot=$.cookie('qa-spot');}
if($.cookie('qa-country')){preselect_country=$.cookie('qa-country');}
if($.cookie('qa-region')){preselect_region=$.cookie('qa-region');}
region.selectChain({target:spot,url:'/wind-cgi/qa-stations.pl',data:{mode:'region'},preselect:preselect_spot});country.selectChain({target:region,url:'/wind-cgi/qa-stations.pl',data:{mode:'country'},preselect:preselect_region});continent.selectChain({target:country,url:'/wind-cgi/qa-stations.pl',data:{mode:'continent'},preselect:preselect_country}).trigger('change');continent.change(function(){$.cookie('qa-continent',continent.val(),{expires:31});});country.change(function(){$.cookie('qa-country',country.val(),{expires:31});});region.change(function(){$.cookie('qa-region',region.val(),{expires:31});});spot.change(function(){$.cookie('qa-spot',spot.val(),{expires:31});var spotname=spot.val();var spotinfo=spotname.split("#");var has_report=spotinfo[1];if(has_report==1){$('#qa-windreport').attr('disabled',false);}else{$('#qa-windreport').attr('disabled',true);}
$('#qa-forecast').attr('disabled',false);});$('#qa-windreport').click(function(){location.href="/report/"+split_spotname(spot.val());});$('#qa-forecast').click(function(){location.href="/forecast/"+split_spotname(spot.val());});function split_spotname(spotname){var spotinfo=spotname.split("#");return spotinfo[0];}});

function spotfilter(spottag){var select=document.getElementById("qa-spot");var o=select.options;var spottag_id=getIdForTag(spottag);if(spottag=='all'){for(var i=0;i<o.length;i++){o[i].style.display="block";o[i].style.color="#000";}
var spottags=new Array("kitesurfing","windsurfing","sailing","surfing","paragliding");for(var i=0;i<spottags.length;i++){$("#spotfilter-"+spottags[i]).attr('checked','checked');}}else{$("#spotfilter-all").removeAttr("checked");for(var i=0;i<o.length;i++){var found=false;var parts=o[i].value.split("#");var tags=parts[2].split(",");for(var j=0;j<tags.length;j++){if(tags[j]==spottag_id){found=true;}}
if(found){if($("#spotfilter-"+spottag).is(":checked")){o[i].style.display="block";o[i].style.color="#000";}else{o[i].style.display="none";o[i].style.color="#ccc";}}}}}
function getIdForTag(spottag){switch(spottag){case"kitesurfing":return 1;case"windsurfing":return 2;case"paragliding":return 8;case"sailing":return 3;case"surfing":return 4;}}

