
if(window.YUI) {
Y = YUI({
base: '/share/js/yui/3.3.0/build/',
loadOptional: true
}).use("anim","cookie","event","node","json-parse","sortable", function(Y) { 
Y.namespace('W');
var cookiecfg = {path:'/',domain:'wfimc.org'}

dateFormat = function(d) {
	var m = {}
	if(wlanguage == 'fr') {
		m['01'] = 'jan.';
		m['02'] = 'f&eacute;v.';
		m['03'] = 'mars';
		m['04'] = 'avr.';
		m['05'] = 'mai';
		m['06'] = 'juin';
		m['07'] = 'juil.';
		m['08'] = 'ao&ucirc;t.';
		m['09'] = 'sep.';
		m['10'] = 'oct.';
		m['11'] = 'nov.';
		m['12'] = 'd&eacute;c.';
	} else {
		m['01'] = 'Jan.';
		m['02'] = 'Feb.';
		m['03'] = 'Mar.';
		m['04'] = 'Apr.';
		m['05'] = 'May.';
		m['06'] = 'Jun.';
		m['07'] = 'Jul.';
		m['08'] = 'Aug.';
		m['09'] = 'Sep.';
		m['10'] = 'Oct.';
		m['11'] = 'Nov.';
		m['12'] = 'Dec.';		
	}
	return d[2]+'. '+m[d[1]]+' '+d[0];
}

compLink = function(o) {
	var d = o.date.split('-');
	var e = o.ends.split('-');
	var c = '';
	for(i in o.discipline) {
		if(o.discipline[i]) c += '<b class="c'+o.discipline[i]+'"></b>';
	}
	return '<a href="'+Y.one('#compinfo').get('innerHTML')+'?cmp='+o.id+'">'+c+'<span class="date">'+dateFormat(d)+' - '+dateFormat(e)+'</span><span class="title">'+o.city+', '+o.country+'</span><span class="subtitle">'+o.title+'</span></a>';
	//<img src="/share/gfx/flags/16x11/'+o.code+'.png" />
}

Y.W.compUpcoming = function() {
	Y.Cookie.remove('startyear');			
	Y.Cookie.remove('endyear');			
	Y.Cookie.remove('discipline');			
	Y.Cookie.remove('country');			
	Y.Cookie.remove('city');			
	if(n = Y.one('#right-1-top')) {
		if(o = Y.JSON.parse(Y.one('#competitions').get('innerHTML'))) {
			var out = '';
			var count = 0;
			var today = new Date();
			var m = today.getMonth()+1;
			if(m<10) m = String('0'+m); 
			var d = today.getDate();
			if(d<10) d = String('0'+d); 
			var now = Number(String(today.getFullYear())+String(m)+String(d));
			for(i in o) {
				var d = o[i].date.split('-');
				d = String(d[0])+String(d[1])+String(d[2]);
				d = Number(d);
				if(d>now) {
					out += compLink(o[i]);
					count += 1; if(count>4) break;
				}
			}
			var h2 = 'Upcoming competitions';
			var dl = 'UPCOMING DEADLINES';
			if(wlanguage == 'fr') {
				h2 = 'Prochains concours';
				dl = 'Prochains dates limite';
			}
			n.set('innerHTML','<h2 class="header">'+h2+'</h2><hr /><div class="list">'+out+'<a href="/Webnodes/deadlines/10475" class="botlink">'+dl+'</a></div>');
		}
	}
}

Y.W.compSearch = function() {
	// start year
	var startyear = '0000';
	if(n = Y.one('#search #startyear')) {
		Y.Cookie.set('startyear',n.get('value'));
	}
	if(v = Y.Cookie.get('startyear')) {
		startyear = v;
	}
	// start month
	var startmonth = '00';
	if(n = Y.one('#search #startmonth')) {
		Y.Cookie.set('startmonth',n.get('value'));
	}
	if(v = Y.Cookie.get('startmonth')) {
		startmonth = v;
	}
	// end year
	var endyear = '0000';
	if(n = Y.one('#search #endyear')) {
		Y.Cookie.set('endyear',n.get('value'));
	}
	if(v = Y.Cookie.get('endyear')) {
		endyear = v;
	}
	// end month
	var endmonth = '00';
	if(n = Y.one('#search #endmonth')) {
		Y.Cookie.set('endmonth',n.get('value'));
	}
	if(v = Y.Cookie.get('endmonth')) {
		endmonth = v;
	}
	// discipline
	var discipline = 0;
	if(n = Y.one('#search #discipline')) {
		Y.Cookie.set('discipline',n.get('value'));
	}
	if(v = Y.Cookie.get('discipline')) {
		discipline = v;
	}
	discipline = String(discipline);
	// city & country
	var city = '';
	if(n = Y.one('#search #city')) {
		Y.Cookie.set('city',n.get('value'));
	}
	if(v = Y.Cookie.get('city')) {
		city = v;
	}	
	var country = '';
	if(n = Y.one('#search #country')) {
		if(n.get('value') && n.get('value') != Y.Cookie.get('country')) {
			Y.Cookie.set('city','');
			city = '';
		}
		Y.Cookie.set('country',n.get('value'));
	}
	if(v = Y.Cookie.get('country')) {
		country = v;
	}
	if(o = Y.JSON.parse(Y.one('#cities').get('innerHTML'))) {
		var out = '<option value="">-</option>';
		if(country == '') {
			o = o.all;
			for(i in o) {
				if(o[i] != '') {
					out += '<option value="'+o[i]+'">'+o[i]+'</option>';
				}
			}
		} else {
			o = o[country];
			for(i=0;i<o.length;i++) {
				out += '<option value="'+o[i]+'">'+o[i]+'</option>';
			}
			o = o[country];
		}
		Y.one('#city').setContent(out).set('value',city);
	}
	
	if(startyear == '0000' && endyear == '0000' && discipline == 0 && country == '' && city == '') {
		Y.W.compUpcoming();
	} else if(o = Y.JSON.parse(Y.one('#competitions').get('innerHTML'))) {
		Y.one('#right-1-top').set('innerHTML','<h2 class="header">No matches found</h2><hr />');			
		var out = '', count = 0;
		var start = Number(String(startyear)+String(startmonth));
		var end = Number(String(endyear)+String(endmonth));
		var date;
		for(i in o) {
			if(o[i].date) {
				date = o[i].date.split('-');
				date = Number(String(date[0])+String(date[1]));
				if(startyear == '0000' || date >= start) {
					if(endyear == '0000' || date <= end) {
						if(discipline == '0' || inArray(o[i].discipline,discipline)) {
							if(city == '' || city == o[i].city) {
								if(country == '' || country == o[i].code) {
									out += compLink(o[i]);
									count += 1;
								}
							}
						}
					}
				}
			}
		}
		if(count>0) {
			var h2 = count == 1 ? count+' match found':count+' matches found';
			Y.one('#right-1-top').set('innerHTML','<h2 class="header">'+h2+'</h2><hr /><div class="list">'+out+'</div>');
		}
	}
}

Y.W.pie = function(v) {
	if(n = Y.one('.search input.pie')) {
		if(v == n.get('value')) {
			n.set('value',0);
			if(n = Y.one('.search img.pie')) {
				var i = '11915';
				if(wlanguage == 'fr') i = '16477';
				n.setAttribute('src','/Webnodes/binary/w86/image_'+i+'.png');
			}
		} else {
			n.set('value',v);
			if(n = Y.one('.search img.pie')) {
				var i = {1:11916,2:11919,3:11917,4:11920,5:11921,6:11918}
				if(wlanguage == 'fr') i = {1:16473,2:16474,3:16472,4:16476,5:16475,6:16471}
				n.setAttribute('src','/Webnodes/binary/w86/image_'+i[v]+'.png');
			}
		}
		Y.W.compSearch();
	}
}

// php'ish function
inArray = function(a,q) {
	for(j in a) {
		if(a[j] == q) {
			return true;
		}
	}
	return false;
}

Y.on("domready", function(){
	var n = Y.one('body');
	if(n.hasClass('webpage21')) {
		Y.W.compSearch();

		var v,now = new Date();
		if(n = Y.one('#search #startyear')) {
			v = String(now.getFullYear());
			n.set('value',v);
		}	
		if(n = Y.one('#search #startmonth')) {
			v = String(now.getMonth()+1);
			n.set('value',v);
		}	

		// googlemap
		var map,flag;
		var locations = []
		var markers = {}	
		
		var latlng = new google.maps.LatLng(26.2,-20.501785);
		var myOptions = {
			zoom: 2,
			center: latlng,
			mapTypeId: google.maps.MapTypeId.ROADMAP,
    		scrollwheel: false
		};
		map = new google.maps.Map(document.getElementById("googlemap"),myOptions);	

		// setup markers
		if(o = Y.JSON.parse(Y.one('#locations').get('innerHTML'))) {
			locations = o;
			for(i in o) {
				markers[i] = '<h2>'+o[i].city+'</h2><a href="/Webnodes/'+wlanguage+'/Web/Public/Competitions/Competition+info?cmp='+o[i].id+'">'+o[i].name+'</a>';
				if(o[i].city == 'Dublin')Y.log(o[i].name); 
			}
		}

		// info window
		var infowin = new google.maps.InfoWindow({maxWidth:300,maxHeight:200,autoScroll:true});
		
		// handle marker click
		markerClick = function(marker, i) {
			return function() {
				infowin.setContent('<div class="wfimc">'+markers[i]+'</div>');
				infowin.open(map,marker);
			};
		}

		// add markers
		var err = '';
		for(i in markers) {
			if(locations[i].location) {
				var loc = locations[i].location.split(',');
				var pos = new google.maps.LatLng(loc[0],loc[1]);
				var marker = new google.maps.Marker({map:map,position:pos,title:locations[i].name});        
				google.maps.event.addListener(marker,'click',markerClick(marker,i)); 
			}
		}
		
	} else {
		Y.W.compUpcoming();
	}

	if(n = Y.one('body.webpage21') && n.get('winWidth') <= 1000 && !Y.Cookie.get('sizealert')) {		
		var msg = "This website is designed for a screen resolution of 1024x768 or higher. If you can not see the whole site, chances are you are running in minimum resolution 800x600. In order to change screen resolution do the following.\n\nIf you are using Windows XP right-click the desktop and choose Properties. Click the Settings tab and here you can change the Screen resolution.\n\nIf you are using Windows 7 (or Vista) right click the desktop and choose Screen resolution and change the resolution in the menu that appears.";
		if(wlanguage == 'fr') {
			msg = "Ce site est conçu pour une résolution d'écran de 1024x768 ou plus. Si vous ne pouvez pas voir l'ensemble du site, les chances sont que vous utilisez la résolution minimale de 800x600. Afin de changer la résolution d'écran,veuillez procéder comme suit:\n\nSi vous utilisez Windows XP, cliquez le bouton droit sur le bureau et choisissez \"Propriétés\". Cliquez sur l'onglet \"Paramètres\" et vous pouvez y changer la résolution de l'écran.\n\nSi vous utilisez Windows 7 (ou Vista),  cliquez le bouton droit sur le bureau, choisissez \"Résolution de l'écran\" et changez la résolution dans le menu qui s'affiche.";
		}
		alert(msg);
		Y.Cookie.set('sizealert',1);
	}

});

});
}
