var cookies = false;

$(document).ready(function(){

	if(typeof $.cookie !== 'undefined'){
	cookies = true;	
	}

	

	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();
		
		}

	});
		
	/* 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 panel_colour = "default";
			
			// Discover the widget colour
			if ($(this).parents('.panel').hasClass("blue")) {
				panel_colour = "blue";
			} else if ($(this).parents('.panel').hasClass("turquoise")) {
				panel_colour = "turquoise";
			} else if ($(this).parents('.panel').hasClass("orange")) {
				panel_colour = "orange";
			}
			
			var collapsed_cookie = read_cookie($.cookie('panel_collapsed'));
			
				switch($('#'+parent_id+' div.panel-main').css('display')){
					
					case 'block':
			
					collapsed_cookie[parent_id] = 0;
					img.attr('src', 'minimise-up'  + (panel_colour == "default" ? "" : "-" + panel_colour) +  '.gif');
					img.attr('height', 33);
					img.attr('width', 35);
					
					break;
					
					case 'none':
			
					collapsed_cookie[parent_id] = 1;
					img.attr('src', 'minimise-down'  + (panel_colour == "default" ? "" : "-" + panel_colour) +  '.gif')
					img.attr('height', 33);
					img.attr('width', 35);			
					break;
					
				}	

			write_cookie('panel_collapsed', collapsed_cookie);
			
			}		
				
		});

		return false;
		
	});	

	$("div.column").sortable(

	    { 
			revert: true, 
			scroll: true,
			connectWith: ['.column'],
			placeholder: 'placeholder',
			handle: $('h2'),
			stop: update_positions,
			zIndex: 500,
			start: start_drag,
			appendTo: $('body')
	    });

	$('#customize').click(function(){

		$('#top-form').slideToggle('fast');
		
		return false;

	});

	$('#top-form form .no').click(function(){

		$('#top-form').slideUp('fast');

	});
	
	$('#addaddresslink').click(function(){

		$('#addaddress').slideToggle('fast');
		return false;

	});

	$('#addaddress form .no').click(function(){

		$('#addaddress').slideUp('fast');

	});


	if($.browser.msie){
	$('#header img').ifixpng(); 
	$('#header img').css('display', 'block');
	}

	restore_panels()

});

function start_drag(e, ui){

if($.browser.msie){
	var margin = $(document).scrollTop()+0;
	ui.helper.css('margin-top', margin+'px');	
}
	
}

function restore_panels() {
	if(!cookies){
		return true;
	}

	var panel_positions = read_cookie($.cookie('panel_positions'));

	var panelName = '';

	//alert('running')

	for (var column_index = 1; column_index <= 3; column_index ++) {
		$('#column-'+column_index+' > *:not(.ui-sortable-helper)').each(function(){
		
			panelName = $(this).attr('id');
			//alert(panelName)

			if (panel_positions[panelName] != undefined && $('#' + panelName).size() > 0){
					
				var destCol = panel_positions[panelName].split(':')[0];
				var destPos = parseInt(panel_positions[panelName].split(':')[1])+1;
			        
				var totalItemsInColumn = $('#' + destCol + ' > div:not(#' + panelName + ')').size() + 1
			
				if (destPos >= totalItemsInColumn) {
					$('#' + panelName).insertAfter($('#' + destCol + ' > div:last'))	
				} else if(destPos == 0) { 
					($('#' + panelName)).insertBefore($('#' + destCol + ' > div:first'))
				} else {
					for (var x = 1; x < totalItemsInColumn; x ++) {
						if (x == destPos) { 
							($('#' + panelName)).insertBefore($('#' + $('#' + destCol + ' > div:not(#' + panelName 
+ ')')[x-1].id))
						}
					}
				}
			}
	
		})
	}


	var panel_collapsed = read_cookie($.cookie('panel_collapsed'));

	$('.panel').each(function(){
	
		if (panel_collapsed[$(this).attr('id')] != undefined && $('#' + $(this).attr('id')).size() > 0) {
		
			var panel_colour = "default";
			
			// Discover the widget colour
			if ($(this).parents('.panel').hasClass("blue")) {
				panel_colour = "blue";
			} else if ($(this).parents('.panel').hasClass("turquoise")) {
				panel_colour = "turquoise";
			} else if ($(this).parents('.panel').hasClass("orange")) {
				panel_colour = "orange";
			}
	
			if (panel_collapsed[$(this).attr('id')] == '1') { 
			
				$('#' + $(this).attr('id') + ' div.panel-main').hide();
				var img = $('#' + $(this).attr("id") + ' .toggle-panel img')
				img.attr('src', 'minimise-down'  + (panel_colour == "default" ? "" : "-" + panel_colour) +  '.gif')
				img.attr('height', 33);
				img.attr('width', 35);
			} else {
				$('#' + $(this).attr('id') + ' div.panel-main').show();
				var img = $('#' + $(this).attr("id") + ' .toggle-panel img')
				img.attr('src', 'minimise-up'  + (panel_colour == "default" ? "" : "-" + panel_colour) +  '.gif')
				img.attr('height', 33);
				img.attr('width', 35);
			}

		}
	})

}

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+' > *: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, {expires: 365});

}

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 setSelectedPanelSideMenuDisplay(sideMenuID, panelName) {
	$("#" + panelName + "PanelSideMenuDisplay").hide().html($("#" + panelName + sideMenuID).html()).show();
	$("#" + panelName + "PanelSideMenuContainer ul li").removeClass("current");
	$("li#" + panelName + "-item" + sideMenuID).addClass("current");

	$("#" + panelName + " .content .t").removeAttr('style');
	$("#" + panelName + " .content .t").attr('style', 'height: 100%; _height: 1600px;');
}

