var cookies = false;

$(document).ready(function(){

	if(typeof $.cookie !== 'undefined'){
	cookies = true;	
	}

	$('.panel form').submit(function(){

	var id = $(this).parents('.panel').attr('id');
	
		var ajax_options = { 
			type: 			"POST",
			url: 			'ajax_callback.php',
			data: 			$(this).serialize(),
			success: 		function(data, textStatus){
							update_panel(data, textStatus, id);
							}	
		};
			
		$.ajax(ajax_options);

		return false;
	
	});

	attach_number_of_feeds_event();
	
	/* fix IE bug - add active class with js */
	
	$('.top-panel-links').find('a:first').addClass('active');
	
		
	$('.top-panel-links a').mouseover(function(){

		if($(this).prev('img:hidden').length){

			var panel_id = $(this).parents('div.panel').attr('id'); 
		
			$('#'+panel_id+' a.active').removeClass('active');
			$(this).addClass('active');
		
			$('#'+panel_id+' .top-panel-links img:visible').hide();
			$(this).prev('img').show();
		
		}

	});
		
	
	$('.edit-panel').click(function(){
	
		$(this).parents('.panel').find('div.panel-main').slideDown('fast');
		$(this).parents('.panel').find('form').slideToggle('fast');
		return false;
		
	});
	
	/* expand panel */
	
	$('.toggle-panel').click(function(){
	
		var img = $(this).children('img');
	
		$(this).parents('.panel').find('div.panel-main').slideToggle('fast', function(){
			
			if(cookies){
			
			//remember in a cookie
			var parent_id = $(this).parents('.panel').attr('id');
			
			var collapsed_cookie = read_cookie($.cookie('collapsed'));
			
				switch($('#'+parent_id+' div.panel-main').css('display')){
					
					case 'block':
			
					collapsed_cookie[parent_id] = 0;
					img.attr('src', 'images/open_panel.gif');
					img.attr('height', 9);
					img.attr('width', 13);
					
					break;
					
					case 'none':
			
					collapsed_cookie[parent_id] = 1;
					img.attr('src', 'images/closed_panel.gif')
					img.attr('height', 13);
					img.attr('width', 9);			
					break;
					
				}	

			write_cookie('collapsed', collapsed_cookie);
			
			}		
				
		});

		return false;
		
	});	

	$("div.column").sortable(

	    { 
			revert: true, 
			scroll: true,
			connectWith: ['.column'],
			placeholder: 'placeholder',
			handle: $('h2'),
			stop: update_positions,
			zIndex: 200,
			start: start_drag,
			appendTo: $('#container')
	    });

	$('#customize').click(function(){

		$('#top-form form').slideToggle('fast');
		return false;

	});

	$('#top-form form #cancel').click(function(){

		$('#top-form form').slideUp('fast');

	});
	

	$('#choose-colours label').click(function(){

		$('#choose-colours li.active').removeClass('active');
		$(this).parents('li').addClass('active');

	})
		
	$("#features > ul").tabs();
	
	if($.browser.msie){
	$('#LogoHeader #Logo img').ifixpng();
	$('#LogoHeader #Logo img').css('display', 'block');
	$('.feedpanel ul li img').ifixpng();
	$('.feedpanel ul li img').css('display', 'block');
	$('#footerboy img').ifixpng();
	$('#footerboy img').css('display');
	}

	if(typeof $.fn.jclock !== 'undefined'){
		$('#jclock').jclock({
			timeNotation: '12h',
			am_pm: true
		});
	}

});

function start_drag(e, ui){

if($.browser.msie){
	var margin = $(document).scrollTop()+20;
	ui.helper.css('margin-top', margin+'px');	
}
	
}

function update_positions(e, ui){
	
	if(!cookies){
		return true;
	}
	
	var panel_positions = read_cookie($.cookie('panel_positions'));
	
	var moved_panel_id = ui.item.attr('id');

	var moved_to_column_id = ui.item.parents('.column').attr('id');
	
	var order_in_column = 0;
	
	$('#'+moved_to_column_id+' .panel:not(.ui-sortable-helper)').each(function(){
		
		var panel_id = $(this).attr('id');
			
		panel_positions[panel_id] = moved_to_column_id+':'+order_in_column;
		
	order_in_column++;	
		
	})
	
	write_cookie('panel_positions', panel_positions);
	
}

function read_cookie(cookie){
	
	var cookie_array = new Array();
	
	if(cookie){
	
	var cookie_elements = cookie.split(';');
	
		for(i=0;i<=cookie_elements.length;i++){
	
			if(cookie_elements[i]){
		
				cookie_data = cookie_elements[i].split('=');
			
				var key = cookie_data[0];

				cookie_array[key] = cookie_data[1];
			
			}
		
		}
	
	}
	
	return cookie_array;
	
}

function write_cookie(name, cookie_array){
	
	var cookie_str = '';
	
	for (key in cookie_array){
		
		cookie_str += key+'='+cookie_array[key]+';';
		
	}
	
	$.cookie(name, cookie_str);

}

function attach_number_of_feeds_event(){
	
	$('div.feed-top a').unbind('click');
	$('div.feed-top a').click(function(){
	
	if(cookies){
	feed_limits_cookie = read_cookie($.cookie('feed_limits'));	
	}

	var id = $(this).parents('.feed').attr('id');
	
	var current_limit = $('#'+id+' li:visible').length;
	
	switch($(this).attr('class')){
		
		case 'add':
	
		$('#'+id+' li:hidden:first').show();
		current_limit ++;
		
		break;
		
		case 'remove':
		
		$('#'+id+' li:visible:last').hide();
		current_limit --;
		
		break;
			
	}
	
	feed_limits_cookie[id] = current_limit;
	
	if(cookies){
	write_cookie('feed_limits', feed_limits_cookie);	
	}

	return false;
		
	});
	
	
}

function update_panel(data, textStatus, id){
		
	$('#'+id+' .feed-output').html(data);

	$('#'+id+' form.edit-panel-form').slideUp();

	//reattach the events
	attach_number_of_feeds_event();
	
}
