jQuery.noConflict();

//Dom Ready
jQuery(function($){
			
	
	$('tr:even').addClass('even');
	
	//Weather
	if (document.getElementById('weather')) {
		var $weather = $('#weather tbody');		
		$.ajax({
				url: 'http://gjersjoengolf.no/fileadmin/yr.php',
				success: function(data){			
					JSON = xml2json.parser(data);
					today = JSON.weatherdata.forecast.tabular.time[0];
					$today = $('<tr><td class="hours">'+today.from.substr(11,5)+' - '+today.to.substr(11,5)+'</td><td><img src="fileadmin/weather/'+today.symbol.number+'.png" alt="'+today.symbol.name+'" title="'+today.symbol.name+'" /></td><td><strong>'+today.temperature.value+'℃</strong></td><td>'+today.windspeed.mps+' m/s</td></tr>')
						.appendTo($weather);										
				}
	        });
	 }
     
    //Weather page
    if (document.getElementById('weather-full')) {
		var $weatherFull = $('#weather-full tbody');
		var $loading = $('<tr><td class="loading" colspan="6"></td></tr>').appendTo($weatherFull);
		$.ajax({
				url: 'http://gjersjoengolf.no/fileadmin/yr.php',
				
				success: function(data){
					if ((!data) || ($.trim(data) == '')) {
						$item = $('<tr><td colspan="6">Det oppstod et problem henting av data</td></tr>')
							.appendTo($weatherFull);
						$loading.remove();
					} else {
						JSON = xml2json.parser(data);
						
						if (JSON.error) {
							$item = $('<tr><td colspan="6">Det oppstod et problem henting av data</td></tr>')
							.appendTo($weatherFull);
							$loading.remove();
						} else {										
							rows = JSON.weatherdata.forecast.tabular.time;
							var yesterday = 0;
							$.each(rows, function(index, row){
								var date = {
									hourStart: row.from.substr(11,5),
									hourEnd: row.to.substr(11,5),
									day: row.from.substr(8,2),
									month: row.from.substr(5,2)
								}
								
								var odd = (index%2==0)?'odd':'even';
								newDay = (date.day != yesterday)?'new-day':'';
								yesterday = date.day; //Setting yesterday to add a visual cue when days change
								
								$item = $('<tr class="'+newDay+' '+odd+'"><td class="hours">'+date.day+'. '+date.month+' - '+date.hourStart+' - '+date.hourEnd+'</td><td><img src="fileadmin/weather/'+row.symbol.number+'.png" alt="'+row.symbol.name+'" title="'+row.symbol.name+'" /></td><td>'+row.precipitation.value+'mm</td><td><strong>'+row.temperature.value+'℃</strong></td><td>'+row.windspeed.name+'</td><td>'+row.windspeed.mps+' m/s</td></tr>')
								.appendTo($weatherFull);
								$loading.remove();
							});
						}
					}									
				},
				
				error: function() {
					$item = $('<tr><td colspan="6">Det oppstod et problem henting av data</td></tr>')
							.appendTo($weatherFull);
					$loading.remove();
				}
				
	        });
	}
        
    //Rounded corners 
	$('#column img').each(function(){
		var $img = $(this);
		$img.wrap('<div class="round-corner-image" />');
		$img.parent().append('<div class="tl"></div><div class="tr"></div><div class="bl"></div><div class="br"></div>');		
	});
	
	
	//Fancybox
	if (!document.getElementById('home')) {
		$('#column .csc-textpic a')
			.attr('rel','gallery')
			.attr('title', function(){
				return $(this).parent('dt').next('.csc-textpic-caption').text();
			})
			.attr('onclick','')		
			.fancybox({
				'transitionIn'	: 'fade',
				'transitionOut'	: 'fade',
				'overlay'		: '#57811D',
				'type'			: 'ajax',
				'scrolling'		: 'no',
				'cyclic'		: true,
				'padding'		: 0
			});		
     }
     
    //Calendar
    
    //Event on multiple days
    $('[uid]').each(function(){
    	if (this.title == '') {
    		this.title = $('[uid='+$(this).attr('uid')+']').eq(0).attr('title');
    	}
    });
    
    $('.calendar-table .linked_day').parent().addClass('linked-day'); 
	$('.newscalendarTooltip').each(function(){		
		var el = this;
		var categories = '';
		$('.newsCalendar_calendarItem', el).each(function(){
			categories += this.title+' ';
		});
		
		var newId = el.id.substr(13);
		$('#idMenu'+newId).parent().addClass(categories);		
			
	});
	
	//Calendar Today
	if (document.getElementById('news-single')) {
		var today = $('.linked_today_nolink, .linked_today').text();
		var haveDateToday = false;		
		$('.newsCalendar_listBody').eq(0).find('.date').each(function(){
			var dateText = $(this).text();
			var day = dateText.substr(dateText.indexOf(' ')+1,2).replace(' ','0');						
			if (today != day) { 
				$(this).parent().hide();
			} else {
				haveDateToday = true;
			}
			
			if (!haveDateToday) $(this).parents('.csc-default').hide();
		});
		
	}
	
	//Custom validation
	$('#pmelding :radio:first').attr('checked',true);
});
